package com.lunhan.xxx.service;
|
|
import com.lunhan.xxx.common.ExecutedResult;
|
import com.lunhan.xxx.common.PagerResult;
|
import com.lunhan.xxx.common.enums.EYesOrNo;
|
import com.lunhan.xxx.common.model.Tuple;
|
import com.lunhan.xxx.common.util.*;
|
import com.lunhan.xxx.entity.dto.search.SearchTestInfo;
|
import com.lunhan.xxx.entity.enums.EState;
|
import com.lunhan.xxx.entity.request.ReqListSetSort;
|
import com.lunhan.xxx.entity.request.ReqSetSort;
|
import com.lunhan.xxx.entity.request.test.ReqCreateTestInfo;
|
import com.lunhan.xxx.entity.request.test.ReqModifyTestInfo;
|
import com.lunhan.xxx.repository.impl.TestInfoMapperImpl;
|
import com.lunhan.xxx.repository.po.TestInfoPO;
|
import com.lunhan.xxx.repository.vo.TestInfoVO;
|
import com.lunhan.xxx.service.convert.TestInfoConvert;
|
import org.apache.commons.lang3.BooleanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 测试信息 相关服务
|
*/
|
@Service
|
public class TestInfoService extends BaseService {
|
@Autowired
|
private TestInfoMapperImpl mapper;
|
|
public ExecutedResult<Long> create(ReqCreateTestInfo request) {
|
// 转换po
|
TestInfoPO item = TestInfoConvert.INSTANCE.toCreate(request);
|
// 设置状态
|
item.setStatus(EState.NORMAL.getValue());
|
// 设置记录创建时间
|
item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
|
// 非逻辑删除
|
item.setIsDelete(EYesOrNo.NO.getValue());
|
|
int rowCount = mapper.insert(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("创建[测试信息]失败。");
|
}
|
return ExecutedResult.success(item.getId());
|
}
|
|
public ExecutedResult<String> modify(ReqModifyTestInfo request) {
|
// 验证记录是否存在
|
ExecutedResult<TestInfoPO> checkExists = this.check4Id(request.getId());
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
// 转换po
|
TestInfoPO item = TestInfoConvert.INSTANCE.toModify(request);
|
|
int rowCount = mapper.updateById(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("编辑[测试信息]失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<TestInfoVO> get(Long id) {
|
TestInfoVO result = new TestInfoVO();
|
|
TestInfoPO find = mapper.selectById(id);
|
if (null != find) {
|
result = TestInfoConvert.INSTANCE.toVo(find);
|
}
|
return ExecutedResult.success(result);
|
}
|
|
public ExecutedResult<String> stop(Long id) {
|
// 验证记录是否存在
|
ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
TestInfoPO item = new TestInfoPO();
|
item.setId(id);
|
item.setStatus(EState.DISABLED.getValue());
|
|
int rowCount = mapper.updateById(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("停用[测试信息]失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<String> enable(Long id) {
|
// 验证记录是否存在
|
ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
TestInfoPO item = new TestInfoPO();
|
item.setId(id);
|
item.setStatus(EState.NORMAL.getValue());
|
|
int rowCount = mapper.updateById(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("启用[测试信息]失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<String> setSort(ReqSetSort request) {
|
// 验证记录是否存在
|
ExecutedResult<TestInfoPO> checkExists = this.check4Id(request.getId());
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
TestInfoPO item = new TestInfoPO();
|
item.setId(request.getId());
|
item.setSort(request.getSort());
|
|
int rowCount = mapper.updateById(item);
|
if (rowCount != 1) {
|
return ExecutedResult.failed("[测试信息]设置排序值失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<String> listSetSort(ReqListSetSort request) {
|
// 类别id列表
|
List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList());
|
// 验证记录是否存在
|
ExecutedResult<List<TestInfoPO>> checkExists = this.check4Id(listId);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
List<TestInfoPO> listUpdate = request.getList().stream()
|
.map(c -> {
|
TestInfoPO item = new TestInfoPO();
|
item.setId(c.getId());
|
item.setSort(c.getSort());
|
return item;
|
})
|
.collect(Collectors.toList());
|
Boolean result = mapper.modifyList(listUpdate);
|
if (result) {
|
return ExecutedResult.success();
|
}
|
return ExecutedResult.failed("[测试信息]设置排序值失败");
|
}
|
|
public ExecutedResult<String> remove(Long id) {
|
// 验证记录是否存在
|
ExecutedResult<TestInfoPO> checkExists = this.check4Id(id);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
Boolean result = mapper.deleteLogic(id);
|
if (BooleanUtils.isFalse(result)) {
|
return ExecutedResult.failed("删除[测试信息]失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<String> removeList(List<Long> listId) {
|
// 验证记录是否存在
|
ExecutedResult<List<TestInfoPO>> checkExists = this.check4Id(listId);
|
if (checkExists.isFailed()) {
|
return ExecutedResult.failed(checkExists.getMsg());
|
}
|
|
Boolean result = mapper.deleteLogic(listId);
|
if (BooleanUtils.isFalse(result)) {
|
return ExecutedResult.failed("删除[测试信息]失败。");
|
}
|
return ExecutedResult.success();
|
}
|
|
public ExecutedResult<List<TestInfoVO>> getList(List<Long> listId) {
|
List<TestInfoVO> result = new ArrayList<>();
|
|
List<TestInfoPO> list = mapper.getList(listId);
|
if (ListUtil.isNotNullOrEmpty(list)) {
|
result = CopierUtil.mapTo(list, TestInfoVO.class);
|
}
|
return ExecutedResult.success(result);
|
}
|
|
public ExecutedResult<PagerResult<TestInfoVO>> search(SearchTestInfo search) {
|
// 处理创建时间范围-查询参数
|
Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());
|
if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {
|
search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());
|
}
|
if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {
|
search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());
|
}
|
|
PagerResult<TestInfoPO> pageList = mapper.search(search);
|
List<TestInfoVO> listVo = new ArrayList<>();
|
List<TestInfoPO> list = pageList.getList();
|
if (ListUtil.isNotNullOrEmpty(list)) {
|
pageList.setLastId(list.get(list.size() - 1).getId());
|
// 转换vo
|
listVo = TestInfoConvert.INSTANCE.toVo(list);
|
}
|
PagerResult<TestInfoVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
|
result.setLastId(pageList.getLastId());
|
return ExecutedResult.success(result);
|
}
|
|
protected ExecutedResult<TestInfoPO> check4Id(Long id) {
|
TestInfoPO exists = mapper.get(id);
|
if (Objects.isNull(exists)) {
|
return ExecutedResult.failed("[测试信息]不存在:" + id);
|
}
|
return ExecutedResult.success(exists);
|
}
|
protected ExecutedResult<List<TestInfoPO>> check4Id(List<Long> listId) {
|
// 从数据库查找类别
|
List<TestInfoPO> list = mapper.getList(listId);
|
if (ListUtil.isNullOrEmpty(list)) {
|
return ExecutedResult.failed("[测试信息]不存在." + listId);
|
}
|
// 数据库找到的类别id列表
|
List<Long> listIdFind = list.stream().map(TestInfoPO::getId).collect(Collectors.toList());
|
// 数量不一致
|
if (listId.size() != listIdFind.size()) {
|
// 筛选数据库不存在的类别
|
List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList());
|
if (ListUtil.isNullOrEmpty(list)) {
|
return ExecutedResult.failed("[测试信息]不存在." + listIdNotFound);
|
}
|
}
|
return ExecutedResult.success(list);
|
}
|
|
protected Map<Long, TestInfoPO> getMap4Id(List<Long> listId) {
|
List<TestInfoPO> list = mapper.getList(listId);
|
if (ListUtil.isNullOrEmpty(list)) {
|
return new HashMap<>(0);
|
}
|
return list.stream().collect(Collectors.toMap(TestInfoPO::getId, c -> c));
|
}
|
|
}
|