package com.battery.business.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.battery.business.domain.TBatteryGroupField; import com.battery.business.domain.TBatteryGroupTemplate; import com.battery.business.domain.TBatteryGroupTemplateField; import com.battery.business.mapper.TBatteryGroupTemplateMapper; import com.battery.business.service.ITBatteryGroupTemplateService; /** * 电池组模板 Service 实现。 * * @author ruoyi */ @Service public class TBatteryGroupTemplateServiceImpl implements ITBatteryGroupTemplateService { private static final String NORMAL = "0"; @Autowired private TBatteryGroupTemplateMapper batteryGroupTemplateMapper; /** * 按主键读取电池组模板,并装配模板的字段关系与显示顺序。 * * @param templateId 模板主键 * @return 模板及其字段关系 * @throws ServiceException 模板ID为空或模板不存在 */ @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; } /** * 返回正常状态的模板选项,供电池组选择字段模板。 * * @return 模板选项列表;没有可用模板时为空列表 */ @Override public List selectBatteryGroupTemplateOptions() { return batteryGroupTemplateMapper.selectBatteryGroupTemplateOptions(); } /** * 新增电池组模板,在同一事务内写入模板主记录与字段关系。 * *

前置条件:模板名称在当前数据中唯一,字段不能为空且不能重复,字段须启用, * 排序数量与字段数量一致。字段关系整体按提交的字段与顺序写入。

* * @param template 模板配置,含字段主键与可选的显示顺序 * @return 写入的模板主记录行数 * @throws ServiceException 模板信息为空、名称重复、字段为空或重复、字段不存在或已禁用、排序不合法 */ @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()); // STEP 1: 写入模板主记录,字段关系依赖本次生成的主键。 int rows = batteryGroupTemplateMapper.insertBatteryGroupTemplate(template); // STEP 2: 在同一事务内整体写入字段关系与显示顺序。 replaceFields(template); return rows; } /** * 修改电池组模板,字段关系按提交内容整体替换。 * *

前置条件:模板存在,模板名称不与其它模板重复(允许与自身同名), * 字段与排序的校验要求同新增。已有电池组引用的只是字段定义,模板变更不改写电池组当前字段值。

* * @param template 模板配置,须带模板主键 * @return 更新的模板主记录行数 * @throws ServiceException 模板ID为空、模板不存在、名称重复或字段与排序校验不通过 */ @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()); // STEP 1: 更新模板主记录。 int rows = batteryGroupTemplateMapper.updateBatteryGroupTemplate(template); // STEP 2: 字段关系整体替换:先清理旧关系再按提交顺序写入。 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 fieldIds = template.getFieldIds(); if (fieldIds == null || fieldIds.isEmpty()) { throw new ServiceException("模板字段不能为空"); } Set uniqueIds = new HashSet<>(); for (Long fieldId : fieldIds) { if (fieldId == null || !uniqueIds.add(fieldId)) { throw new ServiceException("模板字段不能重复或为空"); } } List 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 relations = new ArrayList<>(); List fieldIds = template.getFieldIds(); List 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; } }