| | |
| | | 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; |
| | | 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> |
| | | * |
| | | * @version 1.0 |
| | | * @Description: 组织架构表<br> |
| | | * @Project: 用户中心<br> |
| | | * @CreateDate: Created in 2020-03-31 11:01:35 <br> |
| | | * @Author: <a href="4345453@kidgrow.com">liuke</a> |
| | | * @version 1.0 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class SysOrganizationServiceImpl extends SuperServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService { |
| | | @Autowired |
| | | RedisUtils redisUtils; |
| | | @Autowired |
| | | SysUserOrgMapper sysUserOrgMapper; |
| | | |
| | | /** |
| | | * 列表 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public PageResult<SysOrganization> findList(Map<String, Object> params){ |
| | | public PageResult<SysOrganization> findList(Map<String, Object> params) { |
| | | Page<SysOrganization> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit")); |
| | | List<SysOrganization> list = baseMapper.findList(page, params); |
| | | List<SysOrganization> list = baseMapper.findList(page, params); |
| | | return PageResult.<SysOrganization>builder().data(list).code(0).count(page.getTotal()).build(); |
| | | } |
| | | |
| | | /** |
| | | * 根据SysOrganization对象当做查询条件进行查询 |
| | | * @param sysOrganization |
| | | * @return SysOrganization |
| | | */ |
| | | * 根据SysOrganization对象当做查询条件进行查询 |
| | | * |
| | | * @param sysOrganization |
| | | * @return SysOrganization |
| | | */ |
| | | @Override |
| | | public SysOrganization findByObject(SysOrganization sysOrganization){ |
| | | public SysOrganization findByObject(SysOrganization sysOrganization) { |
| | | return baseMapper.findByObject(sysOrganization); |
| | | } |
| | | |
| | | /** |
| | | * 根据用户 获取组织 |
| | | * |
| | | * @param userId |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 根据map 查询, 将参数中的 access_token 去掉 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | |
| | | //将access_token 参数去掉 |
| | | params.remove("access_token"); |
| | | List<SysOrganization> sysOrganizations = baseMapper.selectByMap(params); |
| | | List<SysOrganizationVo> sysOrganizationVos=new ArrayList<>(); |
| | | sysOrganizations.forEach(e ->{ |
| | | if(e.getOrgParentId()!=null){ |
| | | SysOrganizationVo sysOrganizationVo=new SysOrganizationVo(); |
| | | List<SysOrganizationVo> sysOrganizationVos = new ArrayList<>(); |
| | | sysOrganizations.forEach(e -> { |
| | | if (e.getOrgParentId() != null) { |
| | | SysOrganizationVo sysOrganizationVo = new SysOrganizationVo(); |
| | | BeanCopier beanCopier = BeanCopier.create(SysOrganization.class, SysOrganizationVo.class, false); |
| | | beanCopier.copy(e,sysOrganizationVo,null); |
| | | beanCopier.copy(e, sysOrganizationVo, null); |
| | | SysOrganization sysOrganization = baseMapper.selectById(e.getOrgParentId()); |
| | | if(sysOrganization!=null){ |
| | | if (sysOrganization != null) { |
| | | sysOrganizationVo.setOrgParentName(sysOrganization.getOrgName()); |
| | | } |
| | | sysOrganizationVos.add(sysOrganizationVo); |
| | |
| | | |
| | | /** |
| | | * 更新状态 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultBody updateEnabled(Map<String, Object> params) { |
| | | Long id = MapUtils.getLong(params, "id"); |
| | | if(id==null){ |
| | | if (id == null) { |
| | | return ResultBody.failed("请选取一条数据"); |
| | | } |
| | | SysOrganization sysOrganization = baseMapper.selectById(id); |
| | | if (sysOrganization != null) { |
| | | sysOrganization.setEnabled(MapUtils.getBoolean(params,"enabled")); |
| | | sysOrganization.setEnabled(MapUtils.getBoolean(params, "enabled")); |
| | | int i = baseMapper.updateById(sysOrganization); |
| | | if(i>0){ |
| | | return ResultBody.ok(); |
| | | }else { |
| | | redisUtils.hdel(RedisConstant.ORGANIZATION, sysOrganization.getId().toString()); |
| | | redisUtils.hset(RedisConstant.ORGANIZATION, sysOrganization.getId().toString(), sysOrganization); |
| | | if (i > 0) { |
| | | return ResultBody.ok(); |
| | | } else { |
| | | return ResultBody.failed("更新失败"); |
| | | } |
| | | }else { |
| | | } else { |
| | | return ResultBody.failed("更新失败"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取树状图 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | @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); |
| | | } |
| | | |
| | | /** |
| | | * 将数据 封装成 tree (递归方式) |
| | | * |
| | | * @param MyId |
| | | * @param sysOrganizations |
| | | * @return |
| | | */ |
| | | public List<Map<String,Object>> getTreeData(Long MyId,List<SysOrganization> sysOrganizations){ |
| | | List<Map<String,Object>> listMap=new ArrayList<>(); |
| | | 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()); |
| | | for (Long id:idList |
| | | ) { |
| | | Map<String,Object> map=new HashMap<>(); |
| | | 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()); |
| | | if(childs.size()>0){ |
| | | 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("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 -> |
| | | 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); |
| | | map.put("children", treeData); |
| | | } |
| | | listMap.add(map); |
| | | } |
| | | return listMap; |
| | | } |
| | | |
| | | /** |
| | | * 保存或者更新 |
| | | * |
| | | * @param sysOrganization |
| | | * @return |
| | | */ |
| | | @Override |
| | | @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(); |
| | | 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 = "01"; |
| | | return fucode + str; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |