forked from kidgrow-microservices-platform

bingbing
2021-02-26 81065b8fa819ee2653ee23a3574868784a9be412
kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java
@@ -6,9 +6,11 @@
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.model.SysOrganization;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.common.utils.StringUtils;
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;
@@ -18,10 +20,7 @@
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;
/**
@@ -38,6 +37,8 @@
public class SysOrganizationServiceImpl extends SuperServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    SysUserOrgMapper sysUserOrgMapper;
    /**
     * 列表
@@ -138,8 +139,21 @@
    @Override
    public ResultBody getTree(Map<String, Object> params) {
        List<SysOrganization> sysOrganizations = baseMapper.selectByMap(params);
        //查询 ,父类为-1的;即顶级父类
//        params.put("org_parent_id",Long.valueOf("-1"));
//        List<SysOrganization> sysOrgs=baseMapper.selectByMap(params);
//        if (sysOrgs.isEmpty()) {
//            return ResultBody.ok().data(null);
//        }
        List<Map<String, Object>> treeData = getTreeData(Long.valueOf("-1"), sysOrganizations);
        return ResultBody.ok().data(treeData);
    }
    @Override
    public int deleteByUserId(Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("user_id", userId);
        return sysUserOrgMapper.deleteByMap(params);
    }
    /**
@@ -152,15 +166,17 @@
    public List<Map<String, Object>> getTreeData(Long MyId, List<SysOrganization> sysOrganizations) {
        List<Map<String, Object>> listMap = new ArrayList<>();
        Map<Long, SysOrganization> collect = sysOrganizations.stream().collect(Collectors.toMap(SysOrganization::getId, SysOrganization -> SysOrganization));
        List<Long> idList = sysOrganizations.stream().filter(e -> e.getOrgParentId() == MyId).map(e -> e.getId()).collect(Collectors.toList());
        List<Long> idList = sysOrganizations.stream().filter(e -> MyId.equals(e.getOrgParentId())).map(e -> e.getId()).collect(Collectors.toList());
        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());
            List<Long> childs = sysOrganizations.stream().filter(e ->
                    id.equals(e.getOrgParentId())
            ).map(e -> e.getId()).collect(Collectors.toList());
            if (childs.size() > 0) {
                List<Map<String, Object>> treeData = getTreeData(id, sysOrganizations);
                map.put("children", treeData);
@@ -177,7 +193,7 @@
     * @return
     */
    @Override
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateSer(SysOrganization sysOrganization) {
        //获取code
@@ -189,7 +205,7 @@
        } else {
            //更新
            SysOrganization sysOrg = baseMapper.selectById(sysOrganization.getId());
            if(sysOrg.getOrgParentId()!=sysOrganization.getOrgParentId()){
            if (sysOrg.getOrgParentId() != sysOrganization.getOrgParentId()) {
                String code = this.getCode(sysOrganization);
                sysOrganization.setOrgCode(code);
            }
@@ -203,40 +219,58 @@
    /**
     * 获取自己组织的code
     *
     * @param sysOrganization
     * @return
     */
    public String getCode(SysOrganization sysOrganization){
    public String getCode(SysOrganization sysOrganization) {
        //获取父级
        Long orgParentId = sysOrganization.getOrgParentId();
        if (orgParentId == null) {
            return null;
        }
        SysOrganization sysOrg = baseMapper.selectById(orgParentId);
        if(sysOrg!=null){
        if (sysOrg != null) {
            String orgCode = sysOrg.getOrgCode();
            //查询
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("org_parent_id",sysOrg.getId());
            queryWrapper.orderByAsc("id");
            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) {
            if (list.size() > 0) {
                SysOrganization sysOrganizationLast = list.get(list.size() - 1);
                String orgCodeLast = sysOrganizationLast.getOrgCode();
                String orgCodeLastQianZhui=orgCodeLast.substring(0,orgCodeLast.length() - 4);
                Integer codenum = Integer.valueOf(orgCodeLast.substring(orgCodeLast.length() - 4));
                String str="0000";
                str+=(codenum+1);
                String substring = str.substring(str.length() - 4);
                return orgCodeLastQianZhui+substring;
            }else {
                if (sysOrganizationLast.getOrgLevel() >= 2) {
                    String orgCodeLast = sysOrganizationLast.getOrgCode();
                    if (StringUtils.isNotBlank(orgCodeLast)) {
                        //取前部分
                        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();
                    if (StringUtils.isNotBlank(orgCodeLast)) {
                    //取前部分
                    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="0001";
                return fucode+str;
                String str = "01";
                return fucode + str;
            }
        }
        return null;
    }
}