package com.cloud.user.service.impl; import com.alibaba.fastjson.JSONObject; import com.cloud.common.constants.GlobalDict; //import com.cloud.common.utils.AppUserUtil; import com.cloud.common.utils.OrgTypeUtil; import com.cloud.common.utils.PageUtil; import com.cloud.model.common.Page; import com.cloud.model.sys.AppUser; import com.cloud.model.sys.BbEmployee; import com.cloud.model.sys.SysOrganization; import com.cloud.user.constants.ORGConstant; import com.cloud.user.dao.*; import com.cloud.user.service.SysOrganizationService; import com.cloud.user.service.TokenService; import com.cloud.user.vo.SysOrganizationVO; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; @Slf4j @Service public class SysOrganizationServiceImpl implements SysOrganizationService { @Autowired private SysOrganizationDao orgDao; @Autowired private UserCredentialsDao userCredentialsDao; @Autowired private AppUserDao appUserDao; @Autowired private BbEmployeeDao bbEmployeeDao; /*@Autowired private BCryptPasswordEncoder passwordEncoder;*/ @Autowired private UserRoleDao userRoleDao; @Autowired private TokenService tokenService; @Transactional @Override public Map save(SysOrganizationVO org) { Integer repeatNum = orgDao.isRepeatOrgName(org.getParentId(),org.getName()); if (repeatNum>0) { throw new IllegalArgumentException("此机构下已有相同组织机构名,请更正!"); } Map result = new HashMap<>(); JSONObject jsonAddress = null; //初始化字段赋值 org.preInsert(tokenService.currentUser()); //判断orgId是什么 Long schoolValue = Long.parseLong(OrgTypeUtil.getByKey(GlobalDict.SCHOOL).getValue()); Long parentValue = Long.parseLong(org.getParentValue()); Long rootSchoolValue = Long.parseLong(OrgTypeUtil.getByKey(GlobalDict.ROOT_SCHOOL).getValue()); if(schoolValue < parentValue){ org.setOrgId(org.getOrgId()); }else{ org.setOrgId(org.getParentId()); } //总校ID /** * 2) 判断是否是学校,是学校添加总校id. */ if(schoolValue == Long.parseLong(org.getType())){ if(rootSchoolValue == parentValue){ org.setHeadquartersId(org.getParentId()); } } //经纬度 if(org.getLat() != null && org.getLng() != null){ jsonAddress = new JSONObject(); JSONObject jsonLocation = new JSONObject(); jsonLocation.put(ORGConstant.PARENT_JSON_LAT,org.getLat()); jsonLocation.put(ORGConstant.PARENT_JSON_LNG,org.getLng()); jsonAddress.put("address",jsonLocation); org.setRevJson(jsonAddress.toJSONString()); } //排序sort org.setSort(Integer.parseInt(org.getType().toString().substring(0,1))); Integer status = orgDao.save(org); if(status > 0){ //更新parentIds 、parentJson Map map = getParentsInfo(org); if(map!=null){ String parentIds = map.get("parentIds").toString(); String parentJson = map.get("parentJson").toString(); Boolean updataFlag= false; if(!StringUtils.isBlank(parentIds)){ org.setParentIds(parentIds); updataFlag =true; } if(!StringUtils.isBlank(parentJson)){ org.setParentJson(parentJson); updataFlag=true; } if(updataFlag){ orgDao.update(org); } } //逻辑处理 //判断字段 username password 都不为空 /** * 最后一步) 处理添加的管理员 */ try{ if(StringUtils.isNotEmpty(org.getUsername())&&StringUtils.isNotEmpty(org.getPassword())){ AppUserServiceImpl appUserService = new AppUserServiceImpl(); Map params = new HashMap<>(); AppUser appUser = new AppUser(); appUser.setUsername(org.getUsername()); appUser.setPassword(org.getPassword()); appUser.setNickname(org.getName()); appUser.setOrgId(org.getId()); appUser.setOfficeId(org.getId().intValue()); params.put("appUser",appUser); params.put("appDao",appUserDao); params.put("roleDao",userRoleDao); params.put("userDao",userCredentialsDao); params.put("passCoder",null); Long id = appUserService.registerAdmin(params); JSONObject jsonUser = new JSONObject(); jsonUser.put("userId",id); jsonUser.put("userName",org.getUsername()); jsonAddress.put("admin",jsonUser); SysOrganizationVO orgNew = new SysOrganizationVO(); orgNew.setId(org.getId()); orgNew.setRevJson(jsonAddress.toJSONString()); orgDao.update(orgNew); //获取CHECK的角色id // Integer roleId = 2; // params.put("id",id); //params.put("roleIds",2+","); //appUserService.setRoleToAdmin(params); }}catch (Exception e){ result.put("code",1); result.put("message","用户名已存在!"); orgDao.delete(org.getId(),org.getOrgId()); return result; } result.put("code",0); result.put("message","保存成功!"); }else{ result.put("code",1); result.put("message","保存失败!"); } return result; } @Transactional @Override public int update(SysOrganization org) { org.preUpdate(tokenService.currentUser()); return orgDao.update(org); } @Override public SysOrganization findById(Long id, Long orgId) { //逻辑处理 SysOrganization sysOrganization = orgDao.findById(id, orgId); if(sysOrganization != null) { sysOrganization.setCreateTime(null); sysOrganization.setUpdateTime(null); } return sysOrganization; } @Transactional @Override public int delete(Long id, Long orgId) { //1. 删除逻辑判断 //2. 相关数据处理 return orgDao.delete(id, orgId); } @Override public Page findPageByParams(Map params) { int total = orgDao.count(params); List list = Collections.emptyList(); if (total > 0) { PageUtil.pageParamConver(params, false); list = orgDao.findData(params); } return new Page<>(total, list); } /** * 查找上级第一个指定的机构类型 * 例如:查找学校 * * @param type 要查找的机构类型 * @param currentId 指定的机构id,查它之上的机构 * @return */ @Override public SysOrganization findUpByType(Integer type, Long currentId,Long orgId) { //当前组织的父集节点结合 String parentIds = orgDao.findCurentParentIds(currentId,orgId); //String parentStr = getParentIds(parentIds,currentId); //orgId分库时,当前机构和上级指定的机构类型不在同一个分库中 return orgDao.findUpByType(type,null,parentIds); } /** * 查找上级第一个机构(类型为机构) * 例如:查找学校 * * @param currentId 指定的机构id,查它之上的机构 * @return */ @Override public SysOrganization findUpById(Long currentId,Long orgId) { //当前组织的父集节点结合 String parentIds = orgDao.findCurentParentIds(currentId,orgId); //String parentStr = getParentIds(parentIds,currentId); //orgId分库时,当前机构和上级指定的机构类型不在同一个分库中,如果currentId是机构排除本身 return orgDao.findUpById(currentId,parentIds,OrgTypeUtil.BUSINESS_TYPE); } /** * 查找下级的指定类型的所有机构 * 例如;查找查找学校所有年级,年级所有的班级 * * @param type 要查找的机构类型 * @param currentId 指定的机构id,查它之下的机构 * @return */ @Override public List findDownByType(Integer type, Long currentId,Long orgId) { //当前组织的父集节点结合 String parentIds = orgDao.findCurentParentIds(currentId,orgId); return orgDao.findDownByType(type,orgId,parentIds); } @Override public Map findCheckSchool(Map params) { Map map = new HashMap<>(); //当前组织的父集节点结合 String parentIds = orgDao.findCurentParentIds(Long.parseLong(params.get("currentId").toString()),null); //从字典查出学校value List list = new ArrayList<>(); Integer school = Integer.parseInt(OrgTypeUtil.getByKey(GlobalDict.SCHOOL).getValue()); Integer xuequ = Integer.parseInt(OrgTypeUtil.getByKey(GlobalDict.ROOT_SCHOOL).getValue()); list.add(school); list.add(xuequ); params.put("type",list); params.put("parentIds",parentIds); List> data = orgDao.findCheckSchool(params); if(data != null){ for(Map org:data){ JSONObject jsonObject = new JSONObject(); if(org.containsKey("revJson")){ jsonObject = JSONObject.parseObject(org.get("revJson").toString()); JSONObject location = JSONObject.parseObject(jsonObject.get("address").toString()); org.put("lat",location.get("lat")); org.put("lng",location.get("lng")); }else { org.put("lat",""); org.put("lng",""); } if(!org.containsKey("address")){ org.put("address",""); } org.remove("revJson"); } } if(data.size()>0){ map.put("data",data); map.put("code",0); map.put("message","查询成功"); }else { map.put("data",""); map.put("code",1); map.put("message","暂无数据"); } return map; } @Override public List findDownByOffIds(Map param) { return orgDao.findDownByOffIds(param); } @Override public boolean isImportStudentClass(Map param) { return orgDao.isImportStudentClass(param); } @Override public Map isisImportTeacherOffice(Map param) { return orgDao.isisImportTeacherOffice(param); } @Override public String findCompactName(Integer officeId) { return orgDao.findCompactName(officeId); } @Override public List findOrgIds(Integer id) { String parentIds = orgDao.findCurentParentIds(id.longValue(),null); List types = new ArrayList<>(); int destrictType = Integer.parseInt(OrgTypeUtil.getByKey(GlobalDict.SCHOOL_DISTRICT).getValue()); int rootSchoolType = Integer.parseInt(OrgTypeUtil.getByKey(GlobalDict.ROOT_SCHOOL).getValue()); int schoolType = Integer.parseInt(OrgTypeUtil.getByKey(GlobalDict.SCHOOL).getValue()); types.add(destrictType); types.add(rootSchoolType); types.add(schoolType); Map params = new HashMap<>(); params.put("types",types); params.put("parentIds",parentIds); List list = orgDao.findOrgIds(params); return list; } @Override public String findOrgName(Integer id) { return orgDao.findOrgName(id); } /** * 查找所有的组织树 * @param params 组织机构ID * @return */ @Override public List findAll(Map params) { return orgDao.findAll(params); } @Override public List findHeadquarters(Long currentId, String type) { return orgDao.findHeadquarters(currentId,type); } @Override public List findSubOrg(Long headId, boolean isSelf, String type) { return orgDao.findSubOrg(headId,isSelf,type); } @Override @Transactional public Map deleteOrg(Map params) { //查询该机构下是否存在人员,如果存在人员就不允许删除 Map map = new HashMap<>(); params.put("delFlag", GlobalDict.IS_NOT_DEL); List employeeList = bbEmployeeDao.findDataByOfficeId(params); List appUserList = appUserDao.findDataByOfficeId(params); if((employeeList != null && employeeList.size()>0)||(appUserList != null && appUserList.size()>0)){ map.put("code",1); map.put("message","删除失败,该机构(包含下级机构)有人员存在"); }else{ Integer status = orgDao.deleteOrg(params); //删除组织下的所有登录用户 // appUserDao.deleteAppUserByOrgId(params.get("id").toString()); if(status > 0){ map.put("code",0); map.put("message","删除成功!!!"); }else { map.put("code",1); map.put("message","删除失败!!!"); } } return map; } /** * 递归遍历上级得到信息 */ public Map getParentsInfo(SysOrganization org){ Map result = new HashMap(); if(org.getParentId() != null && org.getParentId() != 0){ result = getParentInfoByData(org); }else{ result = getParentInfoByRecursion(org); } return result; } /** * 得到父节点所有信息,根据parent进行组装 * @param org * @return */ private Map getParentInfoByData(SysOrganization org){ Map result = new HashMap(); String parentIds = ""; String parentJson = ""; SysOrganization parent = this.findById(org.getParentId(),null); if(parent != null) { parentIds = parent.getParentIds() + org.getId() + ","; JSONObject jsonObject = new JSONObject(); JSONObject value = new JSONObject(); value.put(ORGConstant.PARENT_JSON_NAME, parent.getName()); value.put(ORGConstant.PARENT_JSON_TYPE, parent.getType()); //parentjson不为空,则拼接 if (!StringUtils.isBlank(parent.getParentJson())) { jsonObject = JSONObject.parseObject(parent.getParentJson()); } jsonObject.put(org.getId().toString(), value); parentJson = JSONObject.toJSONString(jsonObject==null ? value : jsonObject); } result.put("parentIds",parentIds); result.put("parentJson",parentJson); return result; } /** * 得到父节点所有信息(根据递归,无parent时) * @param org * @return */ private Map getParentInfoByRecursion(SysOrganization org){ Map result = new HashMap(); String parentIds = ","+org.getId()+","; String parentJson = ""; OrgTypeUtil.getByKey(GlobalDict.SCHOOL).getValue(); result.put("parentIds",parentIds); result.put("parentJson",parentJson); return result; } }