|
@@ -0,0 +1,119 @@
|
|
|
+package com.your.packages.pigpen.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
+import cn.hutool.core.util.ArrayUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
|
|
|
+import com.hccake.ballcat.common.core.constant.GlobalConstants;
|
|
|
+import com.hccake.ballcat.common.model.result.BaseResultCode;
|
|
|
+import com.hccake.ballcat.common.model.result.R;
|
|
|
+import com.your.packages.pigpen.entity.BasePigpen;
|
|
|
+import com.your.packages.pigpen.entity.Vo.BasePigpenMoveChildParam;
|
|
|
+import com.your.packages.pigpen.mapper.BasePigpenMapper;
|
|
|
+import com.your.packages.pigpen.service.IBasePigpenService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author newspaper
|
|
|
+ * @since 2023-11-23
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class BasePigpenServiceImpl extends ServiceImpl<BasePigpenMapper, BasePigpen> implements IBasePigpenService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BasePigpenMapper basePigpenMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R create(BasePigpen basePigpen) {
|
|
|
+ Long count = basePigpenMapper
|
|
|
+ .selectCount(new QueryWrapper<BasePigpen>().eq("build_name", basePigpen.getBuildName())
|
|
|
+ .eq("organization_id", basePigpen.getOrganizationId()));
|
|
|
+ if (count > 0) {
|
|
|
+ return R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "该栋舍名称已存在");
|
|
|
+ }
|
|
|
+ fillDepthAndHierarchy(basePigpen, basePigpen.getParentId());
|
|
|
+ return SqlHelper.retBool(basePigpenMapper.insert(basePigpen)) ? R.ok()
|
|
|
+ : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增栋舍失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R delete(Map<String, String> paramsMap) {
|
|
|
+ String id = paramsMap.get("id");
|
|
|
+ List<BasePigpen> basePigpenList = basePigpenMapper
|
|
|
+ .selectList(new QueryWrapper<BasePigpen>().eq("parent_id", id));
|
|
|
+ if (CollUtil.isNotEmpty(basePigpenList)) {
|
|
|
+ return R.failed(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), "该栋舍拥有下级栋舍,不能删除!");
|
|
|
+ }
|
|
|
+ return SqlHelper.retBool(basePigpenMapper.deleteById(id)) ? R.ok()
|
|
|
+ : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "删除栋舍失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R updatePigpen(BasePigpen newBasePigpen) {
|
|
|
+ Long id = newBasePigpen.getId();
|
|
|
+ BasePigpen originBasePigpen = basePigpenMapper.selectById(id);
|
|
|
+ Long targetParentId = originBasePigpen.getParentId();
|
|
|
+ if (originBasePigpen.getParentId().equals(targetParentId)) {
|
|
|
+ return SqlHelper.retBool(basePigpenMapper.updateById(newBasePigpen)) ? R.ok()
|
|
|
+ : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "更新栋舍失败");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (targetParentId.equals(id)) {
|
|
|
+ return R.failed(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), "父节点不能是自己!");
|
|
|
+ }
|
|
|
+ if (!GlobalConstants.TREE_ROOT_ID_LONG.equals(targetParentId)) {
|
|
|
+ BasePigpen targetBasePigpen = basePigpenMapper.selectById(targetParentId);
|
|
|
+ String[] targetParentHierarchy = targetBasePigpen.getHierarchy().split("-");
|
|
|
+ if (ArrayUtil.contains(targetParentHierarchy, String.valueOf(id))) {
|
|
|
+ return R.failed(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), "父节点不能是自己的子节点!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fillDepthAndHierarchy(newBasePigpen, targetParentId);
|
|
|
+ BasePigpenMoveChildParam param = getBasePigpenMoveChildParam(newBasePigpen, originBasePigpen);
|
|
|
+ basePigpenMapper.followMoveChildNode(param);
|
|
|
+ return SqlHelper.retBool(basePigpenMapper.updateById(newBasePigpen)) ? R.ok()
|
|
|
+ : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "更新栋舍失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private BasePigpenMoveChildParam getBasePigpenMoveChildParam(BasePigpen newBasePigpen,
|
|
|
+ BasePigpen originBasePigpen) {
|
|
|
+ Long parentId = newBasePigpen.getId();
|
|
|
+ String originParentHierarchy = originBasePigpen.getHierarchy();
|
|
|
+ String targetParentHierarchy = newBasePigpen.getHierarchy();
|
|
|
+ int depthDiff = originBasePigpen.getDepth() - newBasePigpen.getDepth();
|
|
|
+ BasePigpenMoveChildParam param = new BasePigpenMoveChildParam();
|
|
|
+ param.setParentId(parentId);
|
|
|
+ param.setOriginParentHierarchy(originParentHierarchy);
|
|
|
+ param.setOriginParentHierarchyLengthPlusOne(originParentHierarchy.length() + 1);
|
|
|
+ param.setTargetParentHierarchy(targetParentHierarchy);
|
|
|
+ param.setDepthDiff(depthDiff);
|
|
|
+ param.setGrandsonConditionalStatement(originParentHierarchy + "-" + parentId + "-%");
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillDepthAndHierarchy(BasePigpen basePigpen, Long parentId) {
|
|
|
+ if (GlobalConstants.TREE_ROOT_ID_LONG.equals(parentId)) {
|
|
|
+ basePigpen.setDepth(1);
|
|
|
+ basePigpen.setHierarchy(GlobalConstants.TREE_ROOT_ID_LONG.toString());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ BasePigpen parentPigpen = basePigpenMapper.selectById(parentId);
|
|
|
+ Assert.notNull(parentPigpen, "不存在的父级栋舍!", new Object[0]);
|
|
|
+ basePigpen.setDepth(parentPigpen.getDepth() + 1);
|
|
|
+ basePigpen.setHierarchy(parentPigpen.getHierarchy() + "-" + parentPigpen.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|