package com.battery.business.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.battery.business.domain.TTestType;
|
import com.battery.business.mapper.TTestTypeMapper;
|
import com.battery.business.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);
|
}
|
}
|