From 98fb4b2802f949a730b87f55a8d100272817ac3e Mon Sep 17 00:00:00 2001
From: houruijun <411269194@kidgrow.com>
Date: Fri, 07 Aug 2020 18:46:12 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev

---
 kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java |   86 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java b/kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java
index d815af8..d3795a3 100644
--- a/kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java
+++ b/kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java
@@ -9,22 +9,18 @@
 import com.kidgrow.redis.util.RedisConstant;
 import com.kidgrow.redis.util.RedisUtils;
 import com.kidgrow.usercenter.mapper.SysOrganizationMapper;
+import com.kidgrow.usercenter.mapper.SysUserOrgMapper;
 import com.kidgrow.usercenter.service.ISysOrganizationService;
 import com.kidgrow.usercenter.vo.SysOrganizationVo;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.MapUtils;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cglib.beans.BeanCopier;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
 /**
  * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
@@ -40,7 +36,8 @@
 public class SysOrganizationServiceImpl extends SuperServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {
     @Autowired
     RedisUtils redisUtils;
-
+    @Autowired
+    SysUserOrgMapper sysUserOrgMapper;
     /**
      * 列表
      *
@@ -144,6 +141,13 @@
         return ResultBody.ok().data(treeData);
     }
 
+    @Override
+    public int deleteByUserId(Long userId) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("userId", userId);
+        return sysUserOrgMapper.deleteByMap(params);
+    }
+
     /**
      * 将数据  封装成  tree   (递归方式)
      *
@@ -158,9 +162,10 @@
         for (Long id : idList
         ) {
             Map<String, Object> map = new HashMap<>();
-            map.put("id", id);
+            map.put("id", "" + id);
             map.put("name", collect.get(id).getOrgName());
             map.put("level", collect.get(id).getOrgLevel());
+            map.put("parentId", collect.get(id).getOrgParentId());
             List<Long> childs = sysOrganizations.stream().filter(e -> e.getOrgParentId() == id).map(e -> e.getId()).collect(Collectors.toList());
             if (childs.size() > 0) {
                 List<Map<String, Object>> treeData = getTreeData(id, sysOrganizations);
@@ -178,17 +183,80 @@
      * @return
      */
     @Override
-    @Transactional
+    @Transactional(rollbackFor = Exception.class)
     public boolean saveOrUpdateSer(SysOrganization sysOrganization) {
+        //获取code
+
         if (sysOrganization.getId() == null) {
             //保存
+            String code = this.getCode(sysOrganization);
+            sysOrganization.setOrgCode(code);
             baseMapper.insert(sysOrganization);
         } else {
             //更新
+            SysOrganization sysOrg = baseMapper.selectById(sysOrganization.getId());
+            if (sysOrg.getOrgParentId() != sysOrganization.getOrgParentId()) {
+                String code = this.getCode(sysOrganization);
+                sysOrganization.setOrgCode(code);
+            }
             int i = baseMapper.updateById(sysOrganization);
         }
         redisUtils.hdel(RedisConstant.ORGANIZATION, sysOrganization.getId().toString());
         redisUtils.hset(RedisConstant.ORGANIZATION, sysOrganization.getId().toString(), sysOrganization);
         return true;
     }
+
+
+    /**
+     * 获取自己组织的code
+     *
+     * @param sysOrganization
+     * @return
+     */
+    public String getCode(SysOrganization sysOrganization) {
+        //获取父级
+        Long orgParentId = sysOrganization.getOrgParentId();
+        if (orgParentId == null) {
+            return null;
+        }
+        SysOrganization sysOrg = baseMapper.selectById(orgParentId);
+        if (sysOrg != null) {
+            String orgCode = sysOrg.getOrgCode();
+            //查询
+            QueryWrapper queryWrapper = new QueryWrapper();
+            queryWrapper.eq("org_parent_id", sysOrg.getId());
+            queryWrapper.orderByDesc("org_code");
+            queryWrapper.last("limit 1");
+            List<SysOrganization> list = baseMapper.selectList(queryWrapper);
+            if (list.size() > 0) {
+                SysOrganization sysOrganizationLast = list.get(list.size() - 1);
+                if(sysOrganizationLast.getOrgLevel()>=2){
+                    String orgCodeLast = sysOrganizationLast.getOrgCode();
+                    //取前部分
+                    String orgCodeLastQianZhui = orgCodeLast.substring(0, orgCodeLast.length() - 3);
+                    //取后部分
+                    Integer codenum = Integer.valueOf(orgCodeLast.substring(orgCodeLast.length() - 3));
+                    String str = "00";
+                    str += (codenum + 1);
+                    String substring = str.substring(str.length() - 3);
+                    return orgCodeLastQianZhui + substring;
+                }else {
+                    String orgCodeLast = sysOrganizationLast.getOrgCode();
+                    //取前部分
+                    String orgCodeLastQianZhui = orgCodeLast.substring(0, orgCodeLast.length() - 7);
+                    //取后部分
+                    Integer codenum = Integer.valueOf(orgCodeLast.substring(orgCodeLast.length() - 7));
+                    String str = "0000000";
+                    str += (codenum + 1);
+                    String substring = str.substring(str.length() - 7);
+                    return orgCodeLastQianZhui + substring;
+                }
+            } else {
+                String fucode = sysOrg.getOrgCode();
+                String str = "01";
+                return fucode + str;
+            }
+        }
+        return null;
+    }
 }

--
Gitblit v1.8.0