yeg
2026-09-01 0fcd0642efdff8d4e0584fa991aaea2178bc932b
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
198
199
200
201
202
203
204
205
206
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.TTestType;
import com.ruoyi.system.mapper.TTestTypeMapper;
import com.ruoyi.system.service.ITTestTypeService;
 
/**
 * 测试类型 Service 实现。
 *
 * @author ruoyi
 */
@Service
public class TTestTypeServiceImpl implements ITTestTypeService
{
    private static final String NORMAL = "0";
    private static final String FROZEN = "1";
 
    @Autowired
    private TTestTypeMapper testTypeMapper;
 
    /**
     * 查询未删除测试类型,不存在时抛出业务异常。
     */
    @Override
    public TTestType selectTestTypeById(Long testTypeId)
    {
        if (testTypeId == null)
        {
            throw new ServiceException("测试类型ID不能为空");
        }
        TTestType testType = testTypeMapper.selectTestTypeById(testTypeId);
        if (testType == null)
        {
            throw new ServiceException("测试类型不存在");
        }
        return testType;
    }
 
    /**
     * 按类型 ID、名称、创建时间区间和状态查询测试类型列表。
     */
    @Override
    public List<TTestType> selectTestTypeList(TTestType testType)
    {
        if (testType == null)
        {
            testType = new TTestType();
        }
        testType.setTestTypeName(trimToNull(testType.getTestTypeName()));
        testType.setBeginCreateTime(trimToNull(testType.getBeginCreateTime()));
        testType.setEndCreateTime(trimToNull(testType.getEndCreateTime()));
        validateOptionalStatus(testType.getStatus());
        return testTypeMapper.selectTestTypeList(testType);
    }
 
    /**
     * 新增测试类型并初始化正常状态。
     */
    @Override
    public int insertTestType(TTestType testType)
    {
        validateTestType(testType, false);
        testType.setStatus(NORMAL);
        testType.setDelFlag("0");
        testType.setCreateTime(new Date());
        return testTypeMapper.insertTestType(testType);
    }
 
    /**
     * 编辑测试类型名称和备注,不覆盖状态。
     */
    @Override
    public int updateTestType(TTestType testType)
    {
        validateTestType(testType, true);
        testType.setUpdateTime(new Date());
        return testTypeMapper.updateTestType(testType);
    }
 
    /**
     * 冻结或解冻测试类型,相同状态重复提交按幂等成功处理。
     */
    @Override
    public int updateTestTypeStatus(TTestType testType)
    {
        if (testType == null || testType.getTestTypeId() == null)
        {
            throw new ServiceException("测试类型ID不能为空");
        }
        validateStatus(testType.getStatus());
        TTestType current = selectTestTypeById(testType.getTestTypeId());
        if (testType.getStatus().equals(current.getStatus()))
        {
            return 1;
        }
        testType.setUpdateTime(new Date());
        return testTypeMapper.updateTestTypeStatus(testType);
    }
 
    /**
     * 先校验全部记录及引用关系,再在同一事务中执行批量逻辑删除。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTestTypeByIds(Long[] testTypeIds, String updateBy)
    {
        if (testTypeIds == null || testTypeIds.length == 0)
        {
            throw new ServiceException("请选择要删除的测试类型");
        }
        for (Long testTypeId : testTypeIds)
        {
            TTestType testType = selectTestTypeById(testTypeId);
            if (countTestTypeReferences(testTypeId) > 0)
            {
                throw new ServiceException("测试类型“" + testType.getTestTypeName() + "”已被使用,不能删除");
            }
        }
        int rows = 0;
        for (Long testTypeId : testTypeIds)
        {
            rows += testTypeMapper.deleteTestTypeById(testTypeId, updateBy);
        }
        return rows;
    }
 
    /**
     * 校验并规范化测试类型基础资料,同时检查名称唯一性。
     */
    private void validateTestType(TTestType testType, boolean update)
    {
        if (testType == null)
        {
            throw new ServiceException("测试类型信息不能为空");
        }
        if (update)
        {
            selectTestTypeById(testType.getTestTypeId());
        }
        testType.setTestTypeName(trimToNull(testType.getTestTypeName()));
        testType.setRemark(trimToNull(testType.getRemark()));
        if (StringUtils.isEmpty(testType.getTestTypeName()))
        {
            throw new ServiceException("测试类型名称不能为空");
        }
        if (testType.getTestTypeName().length() > 100)
        {
            throw new ServiceException("测试类型名称长度不能超过100个字符");
        }
        if (testType.getRemark() != null && testType.getRemark().length() > 500)
        {
            throw new ServiceException("备注长度不能超过500个字符");
        }
        TTestType existing = testTypeMapper.selectTestTypeByName(testType.getTestTypeName());
        if (existing != null && (testType.getTestTypeId() == null
                || !testType.getTestTypeId().equals(existing.getTestTypeId())))
        {
            throw new ServiceException("测试类型名称已存在");
        }
    }
 
    /**
     * 汇总测试类型的稳定 ID 引用数,新增关联表时在此扩展对应 Mapper 查询。
     */
    protected int countTestTypeReferences(Long testTypeId)
    {
        return testTypeMapper.countTestReferencesByTestTypeId(testTypeId);
    }
 
    /**
     * 校验可选状态查询条件。
     */
    private void validateOptionalStatus(String status)
    {
        if (StringUtils.isNotEmpty(status))
        {
            validateStatus(status);
        }
    }
 
    /**
     * 校验状态值只能为 0 或 1。
     */
    private void validateStatus(String status)
    {
        if (!NORMAL.equals(status) && !FROZEN.equals(status))
        {
            throw new ServiceException("状态只能为0或1");
        }
    }
 
    /**
     * 去除字符串首尾空白,并将空字符串转换为 null。
     */
    private String trimToNull(String value)
    {
        return StringUtils.isEmpty(StringUtils.trim(value)) ? null : StringUtils.trim(value);
    }
}