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; /** * @author Sun Lejun * @version 1.0 * @date 2024/1/25 */ @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class SysDictService extends BaseOrderDao { /** * 根据ID查询字典 * @param id 字典ID * @return 字典 */ public SysDictB queryById(Long id) { return this.getBaseMapper().selectById(id); } /** * 根据名称或者编码查询字典 * @param search 查询条件 * @return 字典tree */ public Page queryBySearch(SysDictSearch search) { //构建分页对象 Page page = new Page<>(search.getPage(), search.getPageSize()); //按PID和ID进行分页查询 Page 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 records = sysDictVoPage.getRecords(); List list = records.stream().map(SysDictB::getId).toList(); List sysDictVos = this.getBaseMapper().queryByPidOrIds(list); //转换成tree List tree = TreeUtil.list2ListTree(sysDictVos, 0L, SysDictVo::getId, SysDictVo::getPid, SysDictVo::setChildren, null); //构建返回对象 Page 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 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 queryWrapper2 = new QueryWrapper<>(); queryWrapper2.eq(SysDictB.COL_PID, pid); super.updateOrder(queryWrapper2, SysDictB::getSortOn, SysDictB::setSortOn, sysDictB, SysDictB::getId, t -> t.setTm(new Date())); } }