package com.ruoyi.system.service.impl;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
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.TBatteryGroup;
|
import com.ruoyi.system.domain.TBatteryGroupBattery;
|
import com.ruoyi.system.domain.TBatteryGroupRelationField;
|
import com.ruoyi.system.mapper.TBatteryGroupMapper;
|
import com.ruoyi.system.service.ITBatteryGroupService;
|
|
/** 电池组管理服务实现。 */
|
@Service
|
public class TBatteryGroupServiceImpl implements ITBatteryGroupService
|
{
|
private static final String ENABLED = "0";
|
private static final String TESTING = "1";
|
|
@Autowired
|
private TBatteryGroupMapper batteryGroupMapper;
|
|
@Override
|
public List<TBatteryGroup> selectBatteryGroupList(TBatteryGroup group)
|
{
|
return batteryGroupMapper.selectBatteryGroupList(group);
|
}
|
|
@Override
|
public List<Map<String, Object>> selectBatteryGroupCreatorOptions()
|
{
|
return batteryGroupMapper.selectBatteryGroupCreatorOptions();
|
}
|
|
@Override
|
public TBatteryGroup selectBatteryGroupById(Long groupId)
|
{
|
TBatteryGroup group = batteryGroupMapper.selectBatteryGroupById(groupId);
|
if (group == null)
|
{
|
throw new ServiceException("电池组不存在");
|
}
|
group.setBatteries(batteryGroupMapper.selectBatteries(groupId));
|
group.setTemplateFields(batteryGroupMapper.selectRelationFields(groupId));
|
return group;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int insertBatteryGroup(TBatteryGroup group)
|
{
|
validate(group, false);
|
group.setGroupName(StringUtils.trim(group.getGroupName()));
|
group.setCreateTime(new Date());
|
if (StringUtils.isEmpty(group.getStatus()))
|
{
|
group.setStatus(ENABLED);
|
}
|
int rows = batteryGroupMapper.insertBatteryGroup(group);
|
replaceRelations(group);
|
return rows;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int updateBatteryGroup(TBatteryGroup group)
|
{
|
if (group == null || group.getGroupId() == null)
|
{
|
throw new ServiceException("电池组ID不能为空");
|
}
|
if (batteryGroupMapper.selectBatteryGroupById(group.getGroupId()) == null)
|
{
|
throw new ServiceException("电池组不存在");
|
}
|
validate(group, true);
|
group.setGroupName(StringUtils.trim(group.getGroupName()));
|
group.setUpdateTime(new Date());
|
int rows = batteryGroupMapper.updateBatteryGroup(group);
|
if (rows == 0)
|
{
|
throw new ServiceException("电池组不存在或已删除");
|
}
|
replaceRelations(group);
|
return rows;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int updateBatteryGroupStatus(TBatteryGroup group)
|
{
|
if (group == null || group.getGroupId() == null || StringUtils.isEmpty(group.getStatus()))
|
{
|
throw new ServiceException("电池组ID和目标状态不能为空");
|
}
|
if (TESTING.equals(group.getStatus()) || !ENABLED.equals(group.getStatus()) && !"1".equals(group.getStatus()))
|
{
|
throw new ServiceException("电池组状态不正确");
|
}
|
if ("1".equals(group.getStatus()) && batteryGroupMapper.countTestingByGroupId(group.getGroupId()) > 0)
|
{
|
throw new ServiceException("检测中的电池组不能禁用");
|
}
|
return batteryGroupMapper.updateBatteryGroupStatus(group);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int deleteBatteryGroupByIds(Long[] groupIds, String updateBy)
|
{
|
if (groupIds == null || groupIds.length == 0)
|
{
|
throw new ServiceException("电池组ID不能为空");
|
}
|
int rows = 0;
|
for (Long groupId : groupIds)
|
{
|
if (groupId == null)
|
{
|
continue;
|
}
|
if (batteryGroupMapper.countTestingByGroupId(groupId) > 0)
|
{
|
throw new ServiceException("检测中的电池组不能删除");
|
}
|
rows += batteryGroupMapper.deleteBatteryGroup(groupId, updateBy);
|
batteryGroupMapper.deleteBatteries(groupId);
|
batteryGroupMapper.deleteRelationFields(groupId);
|
}
|
return rows;
|
}
|
|
private void validate(TBatteryGroup group, boolean update)
|
{
|
if (group == null)
|
{
|
throw new ServiceException("电池组信息不能为空");
|
}
|
if (update && group.getGroupId() == null)
|
{
|
throw new ServiceException("电池组ID不能为空");
|
}
|
if (StringUtils.isEmpty(StringUtils.trim(group.getGroupName())))
|
{
|
throw new ServiceException("电池组名称不能为空");
|
}
|
if (StringUtils.isEmpty(StringUtils.trim(group.getModel())))
|
{
|
throw new ServiceException("电池组型号不能为空");
|
}
|
TBatteryGroup existing = batteryGroupMapper.selectBatteryGroupByName(StringUtils.trim(group.getGroupName()));
|
if (existing != null && (group.getGroupId() == null || !group.getGroupId().equals(existing.getGroupId())))
|
{
|
throw new ServiceException("电池组名称已存在");
|
}
|
if (group.getBatteryNos() == null || group.getBatteryNos().isEmpty())
|
{
|
throw new ServiceException("电池号不能为空");
|
}
|
Set<String> uniqueNos = new HashSet<>();
|
for (String batteryNo : group.getBatteryNos())
|
{
|
String normalized = StringUtils.trim(batteryNo);
|
if (StringUtils.isEmpty(normalized) || !uniqueNos.add(normalized))
|
{
|
throw new ServiceException("电池号不能重复或为空");
|
}
|
TBatteryGroupBattery existingBattery = batteryGroupMapper.selectBatteryByNo(normalized, group.getGroupId());
|
if (existingBattery != null && (group.getGroupId() == null || !group.getGroupId().equals(existingBattery.getGroupId())))
|
{
|
throw new ServiceException("电池号已被其他电池组使用");
|
}
|
}
|
if (group.getTemplateId() == null)
|
{
|
throw new ServiceException("模板不能为空");
|
}
|
if (batteryGroupMapper.selectTemplate(group.getTemplateId()) == null)
|
{
|
throw new ServiceException("模板不存在");
|
}
|
List<Long> fieldIds = new ArrayList<>();
|
if (group.getTemplateFieldRelations() != null)
|
{
|
for (TBatteryGroupRelationField relation : group.getTemplateFieldRelations())
|
{
|
if (relation == null || relation.getBatteryGroupFieldId() == null || !fieldIds.add(relation.getBatteryGroupFieldId()))
|
{
|
throw new ServiceException("模板字段关系不正确");
|
}
|
}
|
}
|
List<Long> templateFieldIds = batteryGroupMapper.selectTemplateFieldIds(group.getTemplateId());
|
if (templateFieldIds == null || templateFieldIds.isEmpty())
|
{
|
throw new ServiceException("模板未配置有效字段");
|
}
|
if (fieldIds.isEmpty())
|
{
|
fieldIds.addAll(templateFieldIds);
|
}
|
if (!new HashSet<>(templateFieldIds).equals(new HashSet<>(fieldIds)))
|
{
|
throw new ServiceException("电池组字段必须与模板字段一致");
|
}
|
if (batteryGroupMapper.selectValidTemplateFields(group.getTemplateId(), fieldIds).size() != fieldIds.size())
|
{
|
throw new ServiceException("电池组字段不属于指定模板");
|
}
|
}
|
|
private void replaceRelations(TBatteryGroup group)
|
{
|
batteryGroupMapper.deleteBatteries(group.getGroupId());
|
batteryGroupMapper.deleteRelationFields(group.getGroupId());
|
List<TBatteryGroupBattery> batteries = new ArrayList<>();
|
for (int i = 0; i < group.getBatteryNos().size(); i++)
|
{
|
TBatteryGroupBattery item = new TBatteryGroupBattery();
|
item.setGroupId(group.getGroupId());
|
item.setBatteryNo(StringUtils.trim(group.getBatteryNos().get(i)));
|
item.setSortOrder(i);
|
item.setDelFlag("0");
|
batteries.add(item);
|
}
|
batteryGroupMapper.batchInsertBatteries(batteries);
|
List<TBatteryGroupRelationField> relations = group.getTemplateFieldRelations();
|
if (relations == null || relations.isEmpty())
|
{
|
relations = new ArrayList<>();
|
for (Long fieldId : batteryGroupMapper.selectTemplateFieldIds(group.getTemplateId()))
|
{
|
TBatteryGroupRelationField relation = new TBatteryGroupRelationField();
|
relation.setBatteryGroupFieldId(fieldId);
|
relations.add(relation);
|
}
|
}
|
for (TBatteryGroupRelationField relation : relations)
|
{
|
relation.setGroupId(group.getGroupId());
|
relation.setTemplateId(group.getTemplateId());
|
relation.setDelFlag("0");
|
}
|
batteryGroupMapper.batchInsertRelationFields(relations);
|
}
|
}
|