forked from kidgrow-microservices-platform

zhaoxiaohao
2020-04-24 8d384b6f5e510fd23f30061b0678abceb624c2a3
kidgrow-business/kidgrow-usercenter/kidgrow-usercenter-biz/src/main/java/com/kidgrow/usercenter/service/impl/SysOrganizationServiceImpl.java
@@ -158,7 +158,7 @@
        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());
            List<Long> childs = sysOrganizations.stream().filter(e -> e.getOrgParentId() == id).map(e -> e.getId()).collect(Collectors.toList());
@@ -180,15 +180,64 @@
    @Override
    @Transactional
    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.orderByAsc("id");
            List<SysOrganization> list = baseMapper.selectList(queryWrapper);
            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 {
                String fucode = sysOrg.getOrgCode();
                String str="0001";
                return fucode+str;
            }
        }
        return null;
    }
}