|
|
@@ -0,0 +1,218 @@
|
|
|
+package shop.alien.store.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.LifeSysDept;
|
|
|
+import shop.alien.entity.store.dto.LifeSysDeptAddDto;
|
|
|
+import shop.alien.entity.store.dto.LifeSysDeptSortDto;
|
|
|
+import shop.alien.mapper.LifeSysDeptMapper;
|
|
|
+import shop.alien.store.service.LifeSysDeptService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 部门表 服务实现类
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Transactional
|
|
|
+public class LifeSysDeptServiceImpl implements LifeSysDeptService {
|
|
|
+
|
|
|
+ private final LifeSysDeptMapper lifeSysDeptMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<LifeSysDept> addDept(LifeSysDeptAddDto addDto) {
|
|
|
+ log.info("LifeSysDeptServiceImpl.addDept?addDto={}", addDto);
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (addDto == null) {
|
|
|
+ log.warn("新增部门失败:参数为空");
|
|
|
+ return R.fail("参数不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验必填字段:部门名称
|
|
|
+ if (!StringUtils.hasText(addDto.getDeptName())) {
|
|
|
+ return R.fail("部门名称不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验部门名称是否已存在(同一父部门下不能有重名)
|
|
|
+ QueryWrapper<LifeSysDept> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("dept_name", addDto.getDeptName());
|
|
|
+ queryWrapper.eq("del_flag", "0"); // 未删除的部门
|
|
|
+ if (addDto.getParentId() != null && addDto.getParentId() != 0) {
|
|
|
+ queryWrapper.eq("parent_id", addDto.getParentId());
|
|
|
+ } else {
|
|
|
+ queryWrapper.eq("parent_id", 0);
|
|
|
+ }
|
|
|
+ LifeSysDept existingDept = lifeSysDeptMapper.selectOne(queryWrapper);
|
|
|
+ if (existingDept != null) {
|
|
|
+ return R.fail("该部门名称已存在,请更换其他名称");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果指定了父部门,校验父部门是否存在
|
|
|
+ if (addDto.getParentId() != null && addDto.getParentId() != 0) {
|
|
|
+ LifeSysDept parentDept = lifeSysDeptMapper.selectById(addDto.getParentId());
|
|
|
+ if (parentDept == null || !"0".equals(parentDept.getDelFlag())) {
|
|
|
+ return R.fail("父部门不存在或已被删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确定父部门ID
|
|
|
+ Long parentId = addDto.getParentId() != null ? addDto.getParentId() : 0L;
|
|
|
+
|
|
|
+ // 查询同级别部门的最大排序值,实现排序累加
|
|
|
+ QueryWrapper<LifeSysDept> sortQueryWrapper = new QueryWrapper<>();
|
|
|
+ sortQueryWrapper.eq("parent_id", parentId);
|
|
|
+ sortQueryWrapper.eq("del_flag", "0"); // 只统计未删除的部门
|
|
|
+ sortQueryWrapper.orderByDesc("dept_sort");
|
|
|
+ sortQueryWrapper.last("LIMIT 1");
|
|
|
+ LifeSysDept maxSortDept = lifeSysDeptMapper.selectOne(sortQueryWrapper);
|
|
|
+
|
|
|
+ // 计算新的排序值:如果存在同级别部门,则取最大排序值+1,否则从1开始
|
|
|
+ Integer newDeptSort = 1;
|
|
|
+ if (maxSortDept != null && maxSortDept.getDeptSort() != null) {
|
|
|
+ newDeptSort = maxSortDept.getDeptSort() + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建新部门
|
|
|
+ LifeSysDept lifeSysDept = new LifeSysDept();
|
|
|
+ lifeSysDept.setDeptName(addDto.getDeptName());
|
|
|
+ lifeSysDept.setParentId(parentId);
|
|
|
+ lifeSysDept.setDeptSort(newDeptSort); // 使用自动累加的排序值
|
|
|
+ lifeSysDept.setLeader(addDto.getLeader());
|
|
|
+ lifeSysDept.setPhone(addDto.getPhone());
|
|
|
+ lifeSysDept.setEmail(addDto.getEmail());
|
|
|
+ lifeSysDept.setStatus(StringUtils.hasText(addDto.getStatus()) ? addDto.getStatus() : "0");
|
|
|
+ lifeSysDept.setDelFlag("0"); // 未删除
|
|
|
+ lifeSysDept.setDescription(addDto.getDescription());
|
|
|
+
|
|
|
+ // 设置祖级列表
|
|
|
+ if (lifeSysDept.getParentId() != null && lifeSysDept.getParentId() != 0) {
|
|
|
+ LifeSysDept parentDept = lifeSysDeptMapper.selectById(lifeSysDept.getParentId());
|
|
|
+ if (parentDept != null && StringUtils.hasText(parentDept.getAncestors())) {
|
|
|
+ lifeSysDept.setAncestors(parentDept.getAncestors() + "," + parentDept.getDeptId());
|
|
|
+ } else if (parentDept != null) {
|
|
|
+ lifeSysDept.setAncestors(String.valueOf(parentDept.getDeptId()));
|
|
|
+ } else {
|
|
|
+ lifeSysDept.setAncestors("0");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ lifeSysDept.setAncestors("0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存部门
|
|
|
+ int result = lifeSysDeptMapper.insert(lifeSysDept);
|
|
|
+ if (result > 0) {
|
|
|
+ log.info("新增部门成功,部门ID={}", lifeSysDept.getDeptId());
|
|
|
+ return R.data(lifeSysDept, "新增部门成功");
|
|
|
+ }
|
|
|
+ return R.fail("新增部门失败,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<Boolean> updateFirstLevelDeptSort(LifeSysDeptSortDto sortDto) {
|
|
|
+ log.info("LifeSysDeptServiceImpl.updateFirstLevelDeptSort?sortDto={}", sortDto);
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (sortDto == null || sortDto.getDeptId() == null) {
|
|
|
+ log.warn("更新一级部门排序失败:参数为空或部门ID为空");
|
|
|
+ return R.fail("参数不能为空,且部门ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sortDto.getNewSort() == null || sortDto.getNewSort() < 1) {
|
|
|
+ log.warn("更新一级部门排序失败:新排序值无效");
|
|
|
+ return R.fail("新排序值不能为空且必须大于1");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long deptId = sortDto.getDeptId();
|
|
|
+ Integer newSort = sortDto.getNewSort();
|
|
|
+
|
|
|
+ // 查询当前部门信息
|
|
|
+ LifeSysDept currentDept = lifeSysDeptMapper.selectById(deptId);
|
|
|
+ if (currentDept == null) {
|
|
|
+ return R.fail("部门不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验是否是一级部门且未删除
|
|
|
+ if (currentDept.getParentId() == null || currentDept.getParentId() != 0) {
|
|
|
+ return R.fail("该部门不是一级部门");
|
|
|
+ }
|
|
|
+ if (!"0".equals(currentDept.getDelFlag())) {
|
|
|
+ return R.fail("该部门已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取旧排序值
|
|
|
+ Integer oldSort = currentDept.getDeptSort();
|
|
|
+ if (oldSort == null) {
|
|
|
+ oldSort = 0; // 如果旧排序值为空,默认为0
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果新旧排序值相同,无需更新
|
|
|
+ if (oldSort.equals(newSort)) {
|
|
|
+ log.info("部门排序值未变化,部门ID={}, 排序值={}", deptId, oldSort);
|
|
|
+ return R.success("排序值未变化");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 查询所有一级部门(排除当前部门),按排序值排序
|
|
|
+ QueryWrapper<LifeSysDept> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("parent_id", 0);
|
|
|
+ queryWrapper.eq("del_flag", "0");
|
|
|
+ queryWrapper.ne("dept_id", deptId); // 排除当前部门
|
|
|
+ queryWrapper.orderByAsc("dept_sort");
|
|
|
+ List<LifeSysDept> otherDepts = lifeSysDeptMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ // 根据新旧位置差异,调整其他部门的排序 TODO 有优化空间,批量更新
|
|
|
+ if (newSort < oldSort) {
|
|
|
+ // 向上移动:旧位置到新位置之间的部门排序值 +1
|
|
|
+ for (LifeSysDept dept : otherDepts) {
|
|
|
+ if (dept.getDeptSort() != null && dept.getDeptSort() >= newSort && dept.getDeptSort() < oldSort) {
|
|
|
+ LambdaUpdateWrapper<LifeSysDept> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(LifeSysDept::getDeptId, dept.getDeptId())
|
|
|
+ .set(LifeSysDept::getDeptSort, dept.getDeptSort() + 1);
|
|
|
+ lifeSysDeptMapper.update(null, updateWrapper);
|
|
|
+ log.debug("向上移动:部门ID={}的排序值从{}调整为{}", dept.getDeptId(), dept.getDeptSort(), dept.getDeptSort() + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 向下移动:旧位置到新位置之间的部门排序值 -1
|
|
|
+ for (LifeSysDept dept : otherDepts) {
|
|
|
+ if (dept.getDeptSort() != null && dept.getDeptSort() > oldSort && dept.getDeptSort() <= newSort) {
|
|
|
+ LambdaUpdateWrapper<LifeSysDept> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(LifeSysDept::getDeptId, dept.getDeptId())
|
|
|
+ .set(LifeSysDept::getDeptSort, dept.getDeptSort() - 1);
|
|
|
+ lifeSysDeptMapper.update(null, updateWrapper);
|
|
|
+ log.debug("向下移动:部门ID={}的排序值从{}调整为{}", dept.getDeptId(), dept.getDeptSort(), dept.getDeptSort() - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新当前部门的排序值
|
|
|
+ LambdaUpdateWrapper<LifeSysDept> currentUpdateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ currentUpdateWrapper.eq(LifeSysDept::getDeptId, deptId)
|
|
|
+ .set(LifeSysDept::getDeptSort, newSort);
|
|
|
+ int result = lifeSysDeptMapper.update(null, currentUpdateWrapper);
|
|
|
+
|
|
|
+ if (result > 0) {
|
|
|
+ log.info("更新一级部门排序成功,部门ID={}, 从位置{}移动到位置{}", deptId, oldSort, newSort);
|
|
|
+ return R.success("更新排序成功");
|
|
|
+ } else {
|
|
|
+ return R.fail("更新排序失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("更新一级部门排序失败", e);
|
|
|
+ return R.fail("更新排序失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|