package com.ruoyi.system.service.impl;
|
|
import java.util.Date;
|
import java.util.List;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.system.domain.TBatteryGroupField;
|
import com.ruoyi.system.mapper.TBatteryGroupFieldMapper;
|
import com.ruoyi.system.service.ITBatteryGroupFieldService;
|
|
/**
|
* 电池组字段 Service 实现。
|
*
|
* @author ruoyi
|
*/
|
@Service
|
public class TBatteryGroupFieldServiceImpl implements ITBatteryGroupFieldService
|
{
|
private static final String NORMAL = "0";
|
private static final String DISABLED = "1";
|
private static final String TEXT = "text";
|
|
@Autowired
|
private TBatteryGroupFieldMapper batteryGroupFieldMapper;
|
|
/** 查询未删除字段,不存在时抛出业务异常。 */
|
@Override
|
public TBatteryGroupField selectBatteryGroupFieldById(Long fieldId)
|
{
|
if (fieldId == null)
|
{
|
throw new ServiceException("字段ID不能为空");
|
}
|
TBatteryGroupField field = batteryGroupFieldMapper.selectBatteryGroupFieldById(fieldId);
|
if (field == null)
|
{
|
throw new ServiceException("电池组字段不存在");
|
}
|
return field;
|
}
|
|
/** 按字段 ID、名称、创建时间和状态查询列表。 */
|
@Override
|
public List<TBatteryGroupField> selectBatteryGroupFieldList(TBatteryGroupField field)
|
{
|
if (field == null)
|
{
|
field = new TBatteryGroupField();
|
}
|
field.setFieldName(trimToNull(field.getFieldName()));
|
field.setBeginCreateTime(trimToNull(field.getBeginCreateTime()));
|
field.setEndCreateTime(trimToNull(field.getEndCreateTime()));
|
validateOptionalStatus(field.getStatus());
|
return batteryGroupFieldMapper.selectBatteryGroupFieldList(field);
|
}
|
|
/** 新增字段并初始化为启用状态。 */
|
@Override
|
public int insertBatteryGroupField(TBatteryGroupField field)
|
{
|
validateField(field, false);
|
field.setStatus(NORMAL);
|
field.setDelFlag(0L);
|
field.setCreateTime(new Date());
|
return batteryGroupFieldMapper.insertBatteryGroupField(field);
|
}
|
|
/** 编辑字段名称和类型,不覆盖启用状态。 */
|
@Override
|
public int updateBatteryGroupField(TBatteryGroupField field)
|
{
|
validateField(field, true);
|
field.setUpdateTime(new Date());
|
return batteryGroupFieldMapper.updateBatteryGroupField(field);
|
}
|
|
/** 启用或禁用字段,相同状态重复提交按幂等成功处理。 */
|
@Override
|
public int updateBatteryGroupFieldStatus(TBatteryGroupField field)
|
{
|
if (field == null || field.getFieldId() == null)
|
{
|
throw new ServiceException("字段ID不能为空");
|
}
|
validateStatus(field.getStatus());
|
TBatteryGroupField current = selectBatteryGroupFieldById(field.getFieldId());
|
if (field.getStatus().equals(current.getStatus()))
|
{
|
return 1;
|
}
|
field.setUpdateTime(new Date());
|
return batteryGroupFieldMapper.updateBatteryGroupFieldStatus(field);
|
}
|
|
/** 先校验全部记录,再在同一事务中执行批量逻辑删除。 */
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int deleteBatteryGroupFieldByIds(Long[] fieldIds, String updateBy)
|
{
|
if (fieldIds == null || fieldIds.length == 0)
|
{
|
throw new ServiceException("请选择要删除的电池组字段");
|
}
|
for (Long fieldId : fieldIds)
|
{
|
selectBatteryGroupFieldById(fieldId);
|
}
|
int rows = 0;
|
for (Long fieldId : fieldIds)
|
{
|
rows += batteryGroupFieldMapper.deleteBatteryGroupFieldById(fieldId, updateBy);
|
}
|
return rows;
|
}
|
|
/** 校验并规范化字段资料,同时检查名称唯一性。 */
|
private void validateField(TBatteryGroupField field, boolean update)
|
{
|
if (field == null)
|
{
|
throw new ServiceException("电池组字段信息不能为空");
|
}
|
if (update)
|
{
|
selectBatteryGroupFieldById(field.getFieldId());
|
}
|
field.setFieldName(trimToNull(field.getFieldName()));
|
field.setFieldType(trimToNull(field.getFieldType()));
|
field.setRemark(trimToNull(field.getRemark()));
|
if (StringUtils.isEmpty(field.getFieldName()))
|
{
|
throw new ServiceException("字段名称不能为空");
|
}
|
if (field.getFieldName().length() > 100)
|
{
|
throw new ServiceException("字段名称长度不能超过100个字符");
|
}
|
if (StringUtils.isEmpty(field.getFieldType()))
|
{
|
throw new ServiceException("字段类型不能为空");
|
}
|
if (!TEXT.equals(field.getFieldType()))
|
{
|
throw new ServiceException("字段类型仅支持text(文字)");
|
}
|
if (field.getRemark() != null && field.getRemark().length() > 500)
|
{
|
throw new ServiceException("备注长度不能超过500个字符");
|
}
|
TBatteryGroupField existing = batteryGroupFieldMapper.selectBatteryGroupFieldByName(field.getFieldName());
|
if (existing != null && (field.getFieldId() == null || !field.getFieldId().equals(existing.getFieldId())))
|
{
|
throw new ServiceException("字段名称已存在");
|
}
|
}
|
|
/** 校验可选状态查询条件。 */
|
private void validateOptionalStatus(String status)
|
{
|
if (StringUtils.isNotEmpty(status))
|
{
|
validateStatus(status);
|
}
|
}
|
|
/** 校验状态只能为启用或禁用。 */
|
private void validateStatus(String status)
|
{
|
if (!NORMAL.equals(status) && !DISABLED.equals(status))
|
{
|
throw new ServiceException("状态只能为0或1");
|
}
|
}
|
|
/** 去除字符串首尾空白,并将空字符串转换为 null。 */
|
private String trimToNull(String value)
|
{
|
return StringUtils.isEmpty(StringUtils.trim(value)) ? null : StringUtils.trim(value);
|
}
|
}
|