|
@@ -0,0 +1,206 @@
|
|
|
+/*
|
|
|
+ * Copyright [2022] [https://www.xiaonuo.vip]
|
|
|
+ *
|
|
|
+ * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
|
|
+ *
|
|
|
+ * 1.请不要删除和修改根目录下的LICENSE文件。
|
|
|
+ * 2.请不要删除和修改Snowy源码头部的版权声明。
|
|
|
+ * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
|
|
|
+ * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
|
|
|
+ * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
|
|
|
+ * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
|
|
|
+ */
|
|
|
+package vip.xiaonuo.modular.pigpen.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollStreamUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.lang.tree.Tree;
|
|
|
+import cn.hutool.core.lang.tree.TreeNode;
|
|
|
+import cn.hutool.core.lang.tree.TreeUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
|
|
|
+import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
|
|
|
+import vip.xiaonuo.common.enums.CommonSortOrderEnum;
|
|
|
+import vip.xiaonuo.common.exception.CommonException;
|
|
|
+import vip.xiaonuo.common.listener.CommonDataChangeEventCenter;
|
|
|
+import vip.xiaonuo.common.page.CommonPageRequest;
|
|
|
+import vip.xiaonuo.core.enums.BaseDataTypeEnum;
|
|
|
+import vip.xiaonuo.modular.pigpen.entity.BasePigpen;
|
|
|
+import vip.xiaonuo.modular.pigpen.mapper.BasePigpenMapper;
|
|
|
+import vip.xiaonuo.modular.pigpen.param.BasePigpenAddParam;
|
|
|
+import vip.xiaonuo.modular.pigpen.param.BasePigpenEditParam;
|
|
|
+import vip.xiaonuo.modular.pigpen.param.BasePigpenIdParam;
|
|
|
+import vip.xiaonuo.modular.pigpen.param.BasePigpenPageParam;
|
|
|
+import vip.xiaonuo.modular.pigpen.service.BasePigpenService;
|
|
|
+import vip.xiaonuo.sys.core.enums.SysDataTypeEnum;
|
|
|
+
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * base_pigpenService接口实现类
|
|
|
+ *
|
|
|
+ * @author newspaper
|
|
|
+ * @date 2023/12/05 08:55
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+public class BasePigpenServiceImpl extends ServiceImpl<BasePigpenMapper, BasePigpen> implements BasePigpenService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<BasePigpen> page(BasePigpenPageParam basePigpenPageParam) {
|
|
|
+ String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
|
|
|
+ QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().select(BasePigpen::getId, BasePigpen::getParentId, BasePigpen::getBuildName,
|
|
|
+ BasePigpen::getBuildName, BasePigpen::getSort);
|
|
|
+ queryWrapper.lambda().eq(BasePigpen::getOrgId,orgId);
|
|
|
+ if(ObjectUtil.isNotEmpty(basePigpenPageParam.getParentId())) {
|
|
|
+ queryWrapper.lambda().eq(BasePigpen::getParentId, basePigpenPageParam.getParentId());
|
|
|
+ }
|
|
|
+ if(ObjectUtil.isNotEmpty(basePigpenPageParam.getSearchKey())) {
|
|
|
+ queryWrapper.lambda().like(BasePigpen::getBuildName, basePigpenPageParam.getSearchKey());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(ObjectUtil.isAllNotEmpty(basePigpenPageParam.getSortField(), basePigpenPageParam.getSortOrder())) {
|
|
|
+ CommonSortOrderEnum.validate(basePigpenPageParam.getSortOrder());
|
|
|
+ queryWrapper.orderBy(true, basePigpenPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
|
|
|
+ StrUtil.toUnderlineCase(basePigpenPageParam.getSortField()));
|
|
|
+ } else {
|
|
|
+ queryWrapper.lambda().orderByAsc(BasePigpen::getId);
|
|
|
+ }
|
|
|
+ return this.page(CommonPageRequest.defaultPage(), queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void add(BasePigpenAddParam basePigpenAddParam) {
|
|
|
+ SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
|
|
|
+ String orgId = user.getOrgId();
|
|
|
+ BasePigpen basePigpen = BeanUtil.toBean(basePigpenAddParam, BasePigpen.class);
|
|
|
+ if (basePigpen.getParentId().equals("0")){
|
|
|
+ basePigpen.setType(0);
|
|
|
+ }else {
|
|
|
+ basePigpen.setType(this.queryEntity(basePigpen.getParentId()).getType()+1);
|
|
|
+ }
|
|
|
+ basePigpen.setOrgId(orgId);
|
|
|
+ boolean repeatName = this.count(new LambdaQueryWrapper<BasePigpen>().eq(BasePigpen::getParentId, basePigpen.getParentId())
|
|
|
+ .eq(BasePigpen::getBuildName, basePigpen.getBuildName())) > 0;
|
|
|
+ if(repeatName) {
|
|
|
+ throw new CommonException("存在重复的同级栋舍,名称为:{}", basePigpen.getBuildName());
|
|
|
+ }
|
|
|
+ basePigpen.setCreateBy(user.getId());
|
|
|
+ this.save(basePigpen);
|
|
|
+
|
|
|
+ // 发布增加事件
|
|
|
+ CommonDataChangeEventCenter.doAddWithData(BaseDataTypeEnum.PIGPEN.getValue(), JSONUtil.createArray().put(basePigpen));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void edit(BasePigpenEditParam basePigpenEditParam) {
|
|
|
+ SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
|
|
|
+ BasePigpen basePigpen = this.queryEntity(basePigpenEditParam.getId());
|
|
|
+ BeanUtil.copyProperties(basePigpenEditParam, basePigpen);
|
|
|
+ if (basePigpen.getParentId().equals("0")){
|
|
|
+ basePigpen.setType(0);
|
|
|
+ }else {
|
|
|
+ basePigpen.setType(this.queryEntity(basePigpen.getParentId()).getType()+1);
|
|
|
+ }
|
|
|
+ boolean repeatName = this.count(new LambdaQueryWrapper<BasePigpen>().eq(BasePigpen::getParentId, basePigpen.getParentId())
|
|
|
+ .eq(BasePigpen::getBuildName, basePigpen.getBuildName()).ne(BasePigpen::getId, basePigpen.getId())) > 0;
|
|
|
+ if(repeatName) {
|
|
|
+ throw new CommonException("存在重复的同级栋舍,名称为:{}", basePigpen.getBuildName());
|
|
|
+ }
|
|
|
+ List<BasePigpen> originDataList = this.getAllPigpenList(basePigpen.getOrgId());
|
|
|
+ boolean errorLevel = this.getChildListById(originDataList, basePigpen.getId(), true).stream()
|
|
|
+ .map(BasePigpen::getId).collect(Collectors.toList()).contains(basePigpen.getParentId());
|
|
|
+ if(errorLevel) {
|
|
|
+ throw new CommonException("不可选择上级栋舍:{}", this.getById(originDataList, basePigpen.getParentId()).getBuildName());
|
|
|
+ }
|
|
|
+ basePigpen.setUpdateBy(user.getId());
|
|
|
+ this.updateById(basePigpen);
|
|
|
+
|
|
|
+ // 发布更新事件
|
|
|
+ CommonDataChangeEventCenter.doUpdateWithData(BaseDataTypeEnum.PIGPEN.getValue(), JSONUtil.createArray().put(basePigpen));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void delete(List<BasePigpenIdParam> basePigpenIdParamList) {
|
|
|
+ List<String> toDeletePigpenIdList = CollStreamUtil.toList(basePigpenIdParamList, BasePigpenIdParam::getId);
|
|
|
+ // 执行删除
|
|
|
+ this.removeByIds(toDeletePigpenIdList);
|
|
|
+
|
|
|
+ // 发布删除事件
|
|
|
+ CommonDataChangeEventCenter.doDeleteWithDataId(SysDataTypeEnum.ORG.getValue(), toDeletePigpenIdList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BasePigpen detail(BasePigpenIdParam basePigpenIdParam) {
|
|
|
+ return this.queryEntity(basePigpenIdParam.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BasePigpen queryEntity(String id) {
|
|
|
+ BasePigpen basePigpen = this.getById(id);
|
|
|
+ if(ObjectUtil.isEmpty(basePigpen)) {
|
|
|
+ throw new CommonException("栋舍不存在,id值为:{}", id);
|
|
|
+ }
|
|
|
+ return basePigpen;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<BasePigpen> getAllPigpenList(String orgId) {
|
|
|
+ List<BasePigpen> orgList = this.list(new LambdaQueryWrapper<BasePigpen>().eq(BasePigpen::getOrgId,orgId).orderByAsc(BasePigpen::getId));
|
|
|
+ return orgList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<BasePigpen> getChildListById(List<BasePigpen> originDataList, String id, boolean includeSelf) {
|
|
|
+ List<BasePigpen> resultList = CollectionUtil.newArrayList();
|
|
|
+ execRecursionFindChild(originDataList, id, resultList);
|
|
|
+ if (includeSelf) {
|
|
|
+ BasePigpen self = this.getById(originDataList, id);
|
|
|
+ if (ObjectUtil.isNotEmpty(self)) {
|
|
|
+ resultList.add(self);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void execRecursionFindChild(List<BasePigpen> originDataList, String id, List<BasePigpen> resultList) {
|
|
|
+ originDataList.forEach(item -> {
|
|
|
+ if(item.getParentId().equals(id)) {
|
|
|
+ resultList.add(item);
|
|
|
+ execRecursionFindChild(originDataList, item.getId(), resultList);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BasePigpen getById(List<BasePigpen> originDataList, String id) {
|
|
|
+ int index = CollStreamUtil.toList(originDataList, BasePigpen::getId).indexOf(id);
|
|
|
+ return index == -1?null:originDataList.get(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Tree<String>> tree() {
|
|
|
+ String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
|
|
|
+ List<BasePigpen> allPigpenList = this.getAllPigpenList(orgId);
|
|
|
+ List<TreeNode<String>> treeNodeList = allPigpenList.stream().map(basePigpen ->
|
|
|
+ new TreeNode<>(basePigpen.getId(), basePigpen.getParentId(),
|
|
|
+ basePigpen.getBuildName(), basePigpen.getSort()).setExtra(JSONUtil.parseObj(basePigpen)))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ return TreeUtil.build(treeNodeList, "0");
|
|
|
+ }
|
|
|
+}
|