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.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.TBatteryGroupField;
|
import com.ruoyi.system.domain.TBatteryGroupTemplate;
|
import com.ruoyi.system.domain.TBatteryGroupTemplateField;
|
import com.ruoyi.system.mapper.TBatteryGroupTemplateMapper;
|
import com.ruoyi.system.service.ITBatteryGroupTemplateService;
|
|
/**
|
* 电池组模板 Service 实现。
|
*
|
* @author ruoyi
|
*/
|
@Service
|
public class TBatteryGroupTemplateServiceImpl implements ITBatteryGroupTemplateService
|
{
|
private static final String NORMAL = "0";
|
|
@Autowired
|
private TBatteryGroupTemplateMapper batteryGroupTemplateMapper;
|
|
@Override
|
public TBatteryGroupTemplate selectBatteryGroupTemplateById(Long templateId)
|
{
|
if (templateId == null)
|
{
|
throw new ServiceException("模板ID不能为空");
|
}
|
TBatteryGroupTemplate template = batteryGroupTemplateMapper.selectBatteryGroupTemplateById(templateId);
|
if (template == null)
|
{
|
throw new ServiceException("电池组模板不存在");
|
}
|
template.setFields(batteryGroupTemplateMapper.selectTemplateFields(templateId));
|
return template;
|
}
|
|
@Override
|
public List<TBatteryGroupTemplate> selectBatteryGroupTemplateOptions()
|
{
|
return batteryGroupTemplateMapper.selectBatteryGroupTemplateOptions();
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int insertBatteryGroupTemplate(TBatteryGroupTemplate template)
|
{
|
validateTemplate(template, false);
|
template.setTemplateName(trimToNull(template.getTemplateName()));
|
if (StringUtils.isEmpty(template.getUpdateBy()))
|
{
|
template.setUpdateBy(template.getCreateBy());
|
}
|
template.setCreateTime(new Date());
|
int rows = batteryGroupTemplateMapper.insertBatteryGroupTemplate(template);
|
replaceFields(template);
|
return rows;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int updateBatteryGroupTemplate(TBatteryGroupTemplate template)
|
{
|
if (template == null || template.getTemplateId() == null)
|
{
|
throw new ServiceException("模板ID不能为空");
|
}
|
if (batteryGroupTemplateMapper.selectBatteryGroupTemplateById(template.getTemplateId()) == null)
|
{
|
throw new ServiceException("电池组模板不存在");
|
}
|
validateTemplate(template, true);
|
template.setTemplateName(trimToNull(template.getTemplateName()));
|
template.setUpdateTime(new Date());
|
int rows = batteryGroupTemplateMapper.updateBatteryGroupTemplate(template);
|
replaceFields(template);
|
return rows;
|
}
|
|
private void validateTemplate(TBatteryGroupTemplate template, boolean update)
|
{
|
if (template == null)
|
{
|
throw new ServiceException("电池组模板信息不能为空");
|
}
|
if (update && template.getTemplateId() == null)
|
{
|
throw new ServiceException("模板ID不能为空");
|
}
|
template.setTemplateName(trimToNull(template.getTemplateName()));
|
if (StringUtils.isEmpty(template.getTemplateName()))
|
{
|
throw new ServiceException("模板名称不能为空");
|
}
|
TBatteryGroupTemplate existing = batteryGroupTemplateMapper
|
.selectBatteryGroupTemplateByName(template.getTemplateName());
|
if (existing != null && (template.getTemplateId() == null
|
|| !template.getTemplateId().equals(existing.getTemplateId())))
|
{
|
throw new ServiceException("模板名称已存在");
|
}
|
List<Long> fieldIds = template.getFieldIds();
|
if (fieldIds == null || fieldIds.isEmpty())
|
{
|
throw new ServiceException("模板字段不能为空");
|
}
|
Set<Long> uniqueIds = new HashSet<>();
|
for (Long fieldId : fieldIds)
|
{
|
if (fieldId == null || !uniqueIds.add(fieldId))
|
{
|
throw new ServiceException("模板字段不能重复或为空");
|
}
|
}
|
List<TBatteryGroupField> fields = batteryGroupTemplateMapper.selectEnabledFieldsByIds(fieldIds);
|
if (fields == null || fields.size() != fieldIds.size())
|
{
|
throw new ServiceException("模板包含不存在或已禁用的电池组字段");
|
}
|
if (template.getSortOrders() != null && template.getSortOrders().size() != fieldIds.size())
|
{
|
throw new ServiceException("字段排序数量必须与字段数量一致");
|
}
|
if (template.getSortOrders() != null)
|
{
|
for (Integer sortOrder : template.getSortOrders())
|
{
|
if (sortOrder == null || sortOrder < 0)
|
{
|
throw new ServiceException("字段排序必须为非负整数");
|
}
|
}
|
}
|
}
|
|
private void replaceFields(TBatteryGroupTemplate template)
|
{
|
if (template.getTemplateId() == null)
|
{
|
throw new ServiceException("模板ID不能为空");
|
}
|
batteryGroupTemplateMapper.deleteTemplateFields(template.getTemplateId());
|
List<TBatteryGroupTemplateField> relations = new ArrayList<>();
|
List<Long> fieldIds = template.getFieldIds();
|
List<Integer> sortOrders = template.getSortOrders();
|
for (int i = 0; i < fieldIds.size(); i++)
|
{
|
TBatteryGroupTemplateField relation = new TBatteryGroupTemplateField();
|
relation.setTemplateId(template.getTemplateId());
|
relation.setFieldId(fieldIds.get(i));
|
relation.setSortOrder(sortOrders == null ? i : sortOrders.get(i));
|
relation.setCreateBy(StringUtils.isEmpty(template.getUpdateBy()) ? template.getCreateBy() : template.getUpdateBy());
|
relation.setCreateTime(new Date());
|
relation.setDelFlag(0L);
|
relations.add(relation);
|
}
|
batteryGroupTemplateMapper.batchInsertTemplateFields(relations);
|
}
|
|
private String trimToNull(String value)
|
{
|
String trimmed = StringUtils.trim(value);
|
return StringUtils.isEmpty(trimmed) ? null : trimmed;
|
}
|
}
|