yeg
2026-09-02 29f1afa5fff5440ad5ce7dd40dee1053ea0ff7bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.ruoyi.system.service.impl;
 
import java.util.Date;
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.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 Set<String> SUPPORTED_FIELD_TYPES = Set.of("text", "number", "time", "remark");
 
    @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);
        if (StringUtils.isEmpty(field.getStatus()))
        {
            field.setStatus(NORMAL);
        }
        if (StringUtils.isEmpty(field.getUpdateBy()))
        {
            field.setUpdateBy(field.getCreateBy());
        }
        validateStatus(field.getStatus());
        field.setDelFlag(0L);
        field.setCreateTime(new Date());
        return batteryGroupFieldMapper.insertBatteryGroupField(field);
    }
 
    /** 编辑字段名称和类型,不覆盖启用状态。 */
    @Override
    public int updateBatteryGroupField(TBatteryGroupField field)
    {
        validateField(field, true);
        validateStatus(field.getStatus());
        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);
            if (batteryGroupFieldMapper.countFieldReferences(fieldId) > 0)
            {
                throw new ServiceException("字段已被模板或电池组引用,不能删除");
            }
        }
        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 (!SUPPORTED_FIELD_TYPES.contains(field.getFieldType()))
        {
            throw new ServiceException("字段类型仅支持text、number、time或remark");
        }
        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);
    }
}