gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/SysDictService.java

141 lines
4.5 KiB
Java
Raw Normal View History

2024-01-25 15:57:07 +08:00
package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gunshi.db.dao.BaseOrderDao;
import com.gunshi.project.xyt.entity.basedata.SysDictSearch;
import com.gunshi.project.xyt.entity.basedata.SysDictVo;
import com.gunshi.project.xyt.mapper.SysDictBMapper;
import com.gunshi.project.xyt.model.SysDictB;
import com.gunshi.util.common.tree.TreeUtil;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
2024-01-25 19:29:43 +08:00
import java.util.stream.Collectors;
2024-01-25 15:57:07 +08:00
/**
* @author Sun Lejun
* @version 1.0
* @date 2024/1/25
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SysDictService extends BaseOrderDao<SysDictBMapper, SysDictB> {
/**
* ID
* @param id ID
* @return
*/
public SysDictB queryById(Long id) {
return this.getBaseMapper().selectById(id);
}
/**
*
* @param search
* @return tree
*/
public Page<SysDictVo> queryBySearch(SysDictSearch search) {
//构建分页对象
Page<Long> page = new Page<>(search.getPage(), search.getPageSize());
//按PID和ID进行分页查询
Page<SysDictB> sysDictVoPage = this.getBaseMapper().queryPidBySearch(page, search);
long total = sysDictVoPage.getTotal();
long current = sysDictVoPage.getCurrent();
if(total == 0) {
return new Page<>(current, search.getPageSize());
}
//拿出查询出来的PID和ID获取完整的记录
List<SysDictB> records = sysDictVoPage.getRecords();
2024-01-25 19:29:43 +08:00
List<Long> list = records.stream().map(SysDictB::getId).collect(Collectors.toList());
2024-01-25 15:57:07 +08:00
List<SysDictVo> sysDictVos = this.getBaseMapper().queryByPidOrIds(list);
//转换成tree
List<SysDictVo> tree = TreeUtil.list2ListTree(sysDictVos, 0L, SysDictVo::getId, SysDictVo::getPid, SysDictVo::setChildren, null);
//构建返回对象
Page<SysDictVo> result = new Page<>(search.getPage(), search.getPageSize());
result.setCurrent(current);
result.setTotal(total);
result.setRecords(tree);
return result;
}
/**
*
* @param sysDictB
*/
public void insert(@Valid SysDictB sysDictB) {
checkExistCodeAndName(sysDictB);
sysDictB.setId(IdWorker.getId());
sysDictB.setStatus(1);
sysDictB.setCreateTm(new Date());
sysDictB.setTm(new Date());
this.getBaseMapper().insert(sysDictB);
resort(sysDictB);
}
/**
*
* @param sysDictB
*/
public void update(@Valid SysDictB sysDictB) {
checkExistById(sysDictB);
checkExistCodeAndName(sysDictB);
sysDictB.setTm(new Date());
if(sysDictB.getStatus() == 0L){
sysDictB.setSortOn(999);
}
this.getBaseMapper().updateById(sysDictB);
resort(sysDictB);
}
/**
*
* @param sysDictB
*/
public void checkExistCodeAndName(SysDictB sysDictB) {
if (sysDictB.getPid() != 0L) {
return;
}
boolean exist = this.getBaseMapper().checkExistCodeAndName(sysDictB);
if (exist) {
throw new IllegalArgumentException("顶层字典编码或名称已存在");
}
}
/**
*
* @param sysDictB
*/
public void checkExistById(SysDictB sysDictB) {
QueryWrapper<SysDictB> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SysDictB.COL_ID, sysDictB.getId());
boolean exists = this.exists(queryWrapper);
if (!exists) {
throw new IllegalArgumentException("字典不存在");
}
}
/**
*
* @param sysDictB
*/
private void resort(SysDictB sysDictB){
Long pid = sysDictB.getPid();
QueryWrapper<SysDictB> queryWrapper2 = new QueryWrapper<>();
queryWrapper2.eq(SysDictB.COL_PID, pid);
super.updateOrder(queryWrapper2, SysDictB::getSortOn, SysDictB::setSortOn, sysDictB, SysDictB::getId, t -> t.setTm(new Date()));
}
}