|  |  |  | 
|---|
|  |  |  | import com.doumee.core.utils.Utils; | 
|---|
|  |  |  | import com.doumee.dao.admin.request.UpdateCompanySortDTO; | 
|---|
|  |  |  | import com.doumee.dao.admin.response.CompanyDTO; | 
|---|
|  |  |  | import com.doumee.dao.business.CompanyMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.MemberMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.join.CompanyJoinMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Category; | 
|---|
|  |  |  | import com.doumee.dao.business.dao.CompanyMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.dao.join.CompanyJoinMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Company; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Member; | 
|---|
|  |  |  | import com.doumee.dao.business.vo.CompanyTree; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemUser; | 
|---|
|  |  |  | import com.doumee.service.business.CompanyService; | 
|---|
|  |  |  | import com.doumee.dao.business.vo.CompanyTree; | 
|---|
|  |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | user = company.getLoginUserInfo(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String rootOrgId = systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_ROOTORG_CODE).getCode(); | 
|---|
|  |  |  | company.setCompanyPath(company.getName());//名称路径 | 
|---|
|  |  |  | company.setCompanyPath(company.getId()+"/");//名称路径 | 
|---|
|  |  |  | company.setCompanyNamePath(company.getName());//名称路径 | 
|---|
|  |  |  | company.setHkParentId(rootOrgId); | 
|---|
|  |  |  | String idPath = ""; | 
|---|
|  |  |  | if(company.getParentId() !=null){ | 
|---|
|  |  |  | Company parent = findById(company.getParentId()); | 
|---|
|  |  |  | if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE)){ | 
|---|
|  |  |  | 
|---|
|  |  |  | if(StringUtils.isBlank(parent.getHkId())){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级组织信息尚未同步下发成功~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(Constants.equalsInteger(parent.getType(),Constants.ZERO)){ | 
|---|
|  |  |  | //如果父级是相关方组织,下级不能建立内部组织 | 
|---|
|  |  |  | if(Constants.equalsInteger(company.getType(),Constants.ONE)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,相关方组织,下级不能建立内部组织~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | idPath = parent.getCompanyPath(); | 
|---|
|  |  |  | company.setHkParentId(parent.getHkId()); | 
|---|
|  |  |  | company.setCompanyPath(parent.getCompanyPath()+"/"+company.getName()); | 
|---|
|  |  |  | company.setCompanyPath(parent.getCompanyPath()+company.getId()+"/"); | 
|---|
|  |  |  | company.setCompanyNamePath(parent.getCompanyNamePath()+"/"+company.getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | company.setHkCompanyPath(company.getCompanyPath()); | 
|---|
|  |  |  | company.setHkCompanyPath(company.getCompanyNamePath()); | 
|---|
|  |  |  | company.setCreateDate(new Date()); | 
|---|
|  |  |  | company.setCreator(user.getId()); | 
|---|
|  |  |  | company.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | 
|---|
|  |  |  | if(!addHkOrg(company)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,组织同步下发失败,请稍后重试"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Company com = new Company(); | 
|---|
|  |  |  | com.setId(company.getId()); | 
|---|
|  |  |  | com.setCompanyPath(idPath+company.getId()+"/"); | 
|---|
|  |  |  | companyMapper.updateById(com); | 
|---|
|  |  |  | return company.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | List<OrgAddRequest> addList = new ArrayList<>(); | 
|---|
|  |  |  | addList.add(getOrgAddModel(company)); | 
|---|
|  |  |  | BaseResponse<OrgOrUserAddResponse> result =  HKService.addBatchOrg(addList); | 
|---|
|  |  |  | if(result !=null && StringUtils.equals(result.getCode(),HKConstants.RESPONSE_SUCCEE)){ | 
|---|
|  |  |  | if(result !=null && StringUtils.equals(result.getCode(), HKConstants.RESPONSE_SUCCEE)){ | 
|---|
|  |  |  | OrgOrUserAddResponse data = result.getData(); | 
|---|
|  |  |  | if(data.getSuccesses()!=null && data.getSuccesses().size()>0){ | 
|---|
|  |  |  | //处理新增成功的数据,修改海康同步状态 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private boolean delHkOrg(Company company) { | 
|---|
|  |  |  | OrgDelRequest request =new OrgDelRequest(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | request.setIndexCodes(new String[]{company.getHkId()}); | 
|---|
|  |  |  | BaseResponse<List<OrgUpdateFailureResponse>> result =  HKService.delBatchOrg(request); | 
|---|
|  |  |  | if(result !=null && StringUtils.equals(result.getCode(),HKConstants.RESPONSE_SUCCEE)){ | 
|---|
|  |  |  | 
|---|
|  |  |  | .eq(Member::getIsdeleted,Constants.ZERO)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,该部门下存在人员信息,不能进行删除操作!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Company company = new Company(); | 
|---|
|  |  |  | company.setId(id); | 
|---|
|  |  |  | company.setEditDate(new Date()); | 
|---|
|  |  |  | 
|---|
|  |  |  | company.setHkDate(company.getHkDate()); | 
|---|
|  |  |  | companyMapper.updateById(company); | 
|---|
|  |  |  | //下发海康安防平台 | 
|---|
|  |  |  | if(StringUtils.isBlank(model.getHkId()) && Constants.equalsInteger(Constants.ONE,model.getHkStatus()) && !delHkOrg(model)){ | 
|---|
|  |  |  | if(StringUtils.isNotBlank(model.getHkId()) && Constants.equalsInteger(Constants.ONE,model.getHkStatus()) && !delHkOrg(model)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,组织删除下发失败,请确认该组织无下级组织或组织人员信息~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if(model == null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,组织信息不存在~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(StringUtils.isBlank(model.getHkId())){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,组织信息尚未同步下发成功,暂不支持修改,请尝试删除后重新添加~"); | 
|---|
|  |  |  | if( Constants.equalsInteger(Constants.ONE,model.getType())&& Constants.equalsInteger(Constants.ZERO,company.getType())){ | 
|---|
|  |  |  | //如果由内部组织切换成内部组织,排查下级组织是否有内部组织 | 
|---|
|  |  |  | if(companyJoinMapper.selectCount(new QueryWrapper<Company>().lambda() | 
|---|
|  |  |  | .eq(Company::getIsdeleted,Constants.ZERO ) | 
|---|
|  |  |  | .eq(Company::getType,Constants.ONE ) | 
|---|
|  |  |  | .ne(Company::getId,model.getId()) | 
|---|
|  |  |  | .likeRight(Company::getCompanyPath,model.getCompanyPath())) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,下级部门存在内部组织,该部门不支持切换为相关方组织!~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | company.setEditDate(new Date()); | 
|---|
|  |  |  | company.setEditor(user.getId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | company.setHkId(StringUtils.defaultString(model.getHkId(),UUID.randomUUID().toString().replace("-",""))); | 
|---|
|  |  |  | company.setParentId(null);//不支持修改父级 | 
|---|
|  |  |  | companyMapper.updateById(company); | 
|---|
|  |  |  | //更新的companyPath | 
|---|
|  |  |  | String newName = model.getCompanyNamePath().replace(model.getName(),company.getName()); | 
|---|
|  |  |  | companyMapper.update(null,new UpdateWrapper<Company>().lambda() | 
|---|
|  |  |  | .setSql("company_name_path=REPLACE(company_name_path,'"+ model.getCompanyNamePath()+"','"+newName+"')") | 
|---|
|  |  |  | .setSql("hk_company_path=REPLACE(hk_company_path,'"+ model.getCompanyNamePath()+"','"+newName+"')") | 
|---|
|  |  |  | .likeRight(Company::getCompanyPath,model.getCompanyPath())); | 
|---|
|  |  |  | //下发海康安防平台 | 
|---|
|  |  |  | if(StringUtils.isNotBlank(model.getHkId()) | 
|---|
|  |  |  | && Constants.equalsObject(Constants.ONE,model.getHkStatus())){ | 
|---|
|  |  |  | if(editHkOrg(company)){ | 
|---|
|  |  |  | if(StringUtils.isNotBlank(model.getHkId())){ | 
|---|
|  |  |  | if(!editHkOrg(company)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,组织同步下发失败,请稍后重试"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{//如果没有下发过,直接新建安防平台组织 | 
|---|
|  |  |  | if(addHkOrg(company)){ | 
|---|
|  |  |  | if(!addHkOrg(company)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,组织同步下发失败,请稍后重试"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | companyMapper.updateById(company); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public long count(Company company) { | 
|---|
|  |  |  | QueryWrapper<Company> wrapper = new QueryWrapper<>(company); | 
|---|
|  |  |  | return companyMapper.selectCount(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<CompanyDTO> findCompanyTreePage(Integer type) { | 
|---|
|  |  |  | 
|---|
|  |  |  | List<Company> companies = companyMapper.selectList(wrapper); | 
|---|
|  |  |  | return getCompanyTree( companies, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<CompanyDTO> getCompanyTree(List<Company> companies, String parentId){ | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(companies)){ | 
|---|
|  |  |  | 
|---|
|  |  |  | queryWrapper.selectAs(Member::getPhone,Company::getHeadPhone); | 
|---|
|  |  |  | queryWrapper.select("t1.name",Company::getParentName); | 
|---|
|  |  |  | queryWrapper.select("t1.company_path",Company::getParentCompanyPath); | 
|---|
|  |  |  | queryWrapper.select("t1.type",Company::getParentType); | 
|---|
|  |  |  | queryWrapper.leftJoin(Company.class,Company::getId,Company::getParentId); | 
|---|
|  |  |  | queryWrapper.leftJoin(Member.class,Member::getId,Company::getHeadId); | 
|---|
|  |  |  | queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Company::getEditor); | 
|---|
|  |  |  | queryWrapper.eq(Company::getIsdeleted,Constants.ZERO); | 
|---|
|  |  |  | queryWrapper.eq(type!=null,Company::getType,type); | 
|---|
|  |  |  | queryWrapper.orderByAsc(Company::getSortnum ); | 
|---|
|  |  |  | queryWrapper.orderByAsc(true,"t.sortnum"); | 
|---|
|  |  |  | List<Company> companyList = companyJoinMapper.selectJoinList(Company.class,queryWrapper); | 
|---|
|  |  |  | //配置组员人员类型来源方式 0自建 1ERP | 
|---|
|  |  |  | CompanyTree treeBuild = new CompanyTree(companyList, dataSyncConfig.getOrgUserDataOrigin()); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (currentMenuIndex - 1 < 0) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Company preMenu = companyPool.remove(currentMenuIndex - 1); | 
|---|
|  |  |  | Company preMenu = companyPool.get(currentMenuIndex - 1); | 
|---|
|  |  |  | companyPool.remove(currentMenuIndex - 1); | 
|---|
|  |  |  | companyPool.add(currentMenuIndex, preMenu); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 下移 | 
|---|