|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.model.PageData; | 
|---|
|  |  |  | import com.doumee.core.model.PageWrap; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.Utils; | 
|---|
|  |  |  | import com.doumee.dao.business.CompanyDepartmentMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Company; | 
|---|
|  |  |  | import com.doumee.dao.business.model.CompanyDepartment; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Member; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemUserMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.model.SystemUser; | 
|---|
|  |  |  | import com.doumee.dao.system.vo.DepartmentTree; | 
|---|
|  |  |  | import com.doumee.service.business.CompanyDepartmentService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 企业信息表Service实现 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CompanyDepartmentMapper companyDepartmentMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemUserMapper systemUserMapper; | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer create(CompanyDepartment companyDepartment) { | 
|---|
|  |  |  | companyDepartmentMapper.insert(companyDepartment); | 
|---|
|  |  |  | return companyDepartment.getId(); | 
|---|
|  |  |  | public  List<CompanyDepartment> departTree(Integer type){ | 
|---|
|  |  |  | MPJLambdaWrapper<CompanyDepartment> queryWrapper = new MPJLambdaWrapper<>(); | 
|---|
|  |  |  | queryWrapper.selectAll(Company.class); | 
|---|
|  |  |  | queryWrapper.select("t2.realname",CompanyDepartment::getEditorName); | 
|---|
|  |  |  | queryWrapper.select("t2.realname",CompanyDepartment::getHeadName); | 
|---|
|  |  |  | queryWrapper.select("t2.mobile",CompanyDepartment::getHeadPhone); | 
|---|
|  |  |  | queryWrapper.select("t1.name",CompanyDepartment::getParentName); | 
|---|
|  |  |  | queryWrapper.select("t1.name_path",CompanyDepartment::getParentCompanyPath); | 
|---|
|  |  |  | queryWrapper.select("t1.type",CompanyDepartment::getParentType); | 
|---|
|  |  |  | queryWrapper.leftJoin(CompanyDepartment.class,CompanyDepartment::getId,CompanyDepartment::getParentId); | 
|---|
|  |  |  | queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,CompanyDepartment::getHeadId); | 
|---|
|  |  |  | queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,CompanyDepartment::getEditor); | 
|---|
|  |  |  | queryWrapper.eq(CompanyDepartment::getIsdeleted,Constants.ZERO); | 
|---|
|  |  |  | queryWrapper.eq(type!=null,CompanyDepartment::getType,type); | 
|---|
|  |  |  | queryWrapper.orderByAsc(true,"t.sortnum"); | 
|---|
|  |  |  | List<CompanyDepartment> companyList = companyDepartmentMapper.selectJoinList(CompanyDepartment.class,queryWrapper); | 
|---|
|  |  |  | DepartmentTree treeBuild = new DepartmentTree(companyList); | 
|---|
|  |  |  | companyList = treeBuild.buildTree(); | 
|---|
|  |  |  | return companyList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer create(CompanyDepartment company) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | if(StringUtils.isBlank(company.getName())){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.BAD_REQUEST); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(company.getType()==null || !(company.getType() ==0 || company.getType() ==1)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,组织类型不正确,请按要求填写~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int type = Constants.ZERO;//默认平台组织 | 
|---|
|  |  |  | if(Constants.equalsInteger(user.getType(),Constants.UserType.COMPANY.getKey())){ | 
|---|
|  |  |  | //如果企业用户 | 
|---|
|  |  |  | type  = Constants.ONE; | 
|---|
|  |  |  | }else if(Constants.equalsInteger(user.getType(),Constants.UserType.ZHUBO.getKey())){ | 
|---|
|  |  |  | //如果是商户 | 
|---|
|  |  |  | type  = Constants.TWO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //查询名称不能重复 | 
|---|
|  |  |  | if(companyDepartmentMapper.selectCount(new QueryWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getName,company.getName()) | 
|---|
|  |  |  | .eq(CompanyDepartment::getType,type) | 
|---|
|  |  |  | .eq(CompanyDepartment::getIsdeleted, Constants.ZERO)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,组织名称不能重复~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | company.setCompanyId(user.getCompanyId());//所属企业编码 | 
|---|
|  |  |  | company.setIdPath(company.getId()+"/");//名称路径 | 
|---|
|  |  |  | company.setNamePath(company.getName());//名称路径 | 
|---|
|  |  |  | String idPath = ""; | 
|---|
|  |  |  | if(company.getParentId() !=null){ | 
|---|
|  |  |  | CompanyDepartment parent = findById(company.getParentId()); | 
|---|
|  |  |  | if(parent == null || Constants.equalsInteger(parent.getIsdeleted(),Constants.ONE) | 
|---|
|  |  |  | || !Constants.equalsInteger(parent.getType(),type)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,父级组织信息不存在~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | idPath = parent.getIdPath(); | 
|---|
|  |  |  | company.setIdPath(parent.getIdPath()+company.getId()+"/"); | 
|---|
|  |  |  | company.setNamePath(parent.getNamePath()+"/"+company.getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | company.setCreateDate(new Date()); | 
|---|
|  |  |  | company.setCreator(user.getId()); | 
|---|
|  |  |  | company.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | company.setStatus(Constants.ZERO); | 
|---|
|  |  |  | company.setSortnum(0);//默认最上面 | 
|---|
|  |  |  | company.setEditDate(company.getCreateDate()); | 
|---|
|  |  |  | company.setEditor(company.getCreator()); | 
|---|
|  |  |  | companyDepartmentMapper.insert(company); | 
|---|
|  |  |  | //下发海康安防平台 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CompanyDepartment com = new CompanyDepartment(); | 
|---|
|  |  |  | com.setId(company.getId()); | 
|---|
|  |  |  | com.setIdPath(idPath+company.getId()+"/"); | 
|---|
|  |  |  | companyDepartmentMapper.updateById(com); | 
|---|
|  |  |  | return company.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void deleteById(Integer id) { | 
|---|
|  |  |  | companyDepartmentMapper.deleteById(id); | 
|---|
|  |  |  | LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int type = Constants.ZERO;//默认平台组织 | 
|---|
|  |  |  | if(Constants.equalsInteger(user.getType(),Constants.UserType.COMPANY.getKey())){ | 
|---|
|  |  |  | //如果企业用户 | 
|---|
|  |  |  | type  = Constants.ONE; | 
|---|
|  |  |  | }else if(Constants.equalsInteger(user.getType(),Constants.UserType.ZHUBO.getKey())){ | 
|---|
|  |  |  | //如果是商户 | 
|---|
|  |  |  | type  = Constants.TWO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CompanyDepartment model = findById(id); | 
|---|
|  |  |  | if(model == null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getType(),type)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,该组织信息不存在!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(companyDepartmentMapper.selectCount(new QueryWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getParentId,id) | 
|---|
|  |  |  | .eq(CompanyDepartment::getType,type) | 
|---|
|  |  |  | .eq(CompanyDepartment::getIsdeleted, Constants.ZERO)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,该组织存在下级组织,无法进行该操作!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(systemUserMapper.selectCount(new QueryWrapper<SystemUser>().lambda() | 
|---|
|  |  |  | .eq(SystemUser::getDepartmentId,id) | 
|---|
|  |  |  | .eq(SystemUser::getType,type) | 
|---|
|  |  |  | .eq(SystemUser::getDeleted, Boolean.FALSE)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,该组织下存在用户信息,无法进行该操作!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | companyDepartmentMapper.update(null, new UpdateWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getId,id) | 
|---|
|  |  |  | .set(CompanyDepartment::getIsdeleted,Constants.ONE) | 
|---|
|  |  |  | .set(CompanyDepartment::getEditor,user.getId()) | 
|---|
|  |  |  | .set(CompanyDepartment::getEditDate,new Date()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void delete(CompanyDepartment companyDepartment) { | 
|---|
|  |  |  | UpdateWrapper<CompanyDepartment> deleteWrapper = new UpdateWrapper<>(companyDepartment); | 
|---|
|  |  |  | companyDepartmentMapper.delete(deleteWrapper); | 
|---|
|  |  |  | LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int type = Constants.ZERO;//默认平台组织 | 
|---|
|  |  |  | if(Constants.equalsInteger(user.getType(),Constants.UserType.COMPANY.getKey())){ | 
|---|
|  |  |  | //如果企业用户 | 
|---|
|  |  |  | type  = Constants.ONE; | 
|---|
|  |  |  | }else if(Constants.equalsInteger(user.getType(),Constants.UserType.ZHUBO.getKey())){ | 
|---|
|  |  |  | //如果是商户 | 
|---|
|  |  |  | type  = Constants.TWO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CompanyDepartment model = findById(companyDepartment.getId()); | 
|---|
|  |  |  | if(model == null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getType(),type)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,该组织信息不存在!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(companyDepartmentMapper.selectCount(new QueryWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getParentId,companyDepartment.getId()) | 
|---|
|  |  |  | .eq(CompanyDepartment::getType,type) | 
|---|
|  |  |  | .eq(CompanyDepartment::getIsdeleted, Constants.ZERO)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,该组织存在下级组织,无法进行该操作!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(systemUserMapper.selectCount(new QueryWrapper<SystemUser>().lambda() | 
|---|
|  |  |  | .eq(SystemUser::getDepartmentId,companyDepartment.getId()) | 
|---|
|  |  |  | .eq(SystemUser::getType,type) | 
|---|
|  |  |  | .eq(SystemUser::getDeleted, Boolean.FALSE)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,该组织下存在用户信息,无法进行该操作!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | companyDepartmentMapper.update(null, new UpdateWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getId,companyDepartment.getId()) | 
|---|
|  |  |  | .set(CompanyDepartment::getIsdeleted,Constants.ONE) | 
|---|
|  |  |  | .set(CompanyDepartment::getEditor,user.getId()) | 
|---|
|  |  |  | .set(CompanyDepartment::getEditDate,new Date()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(ids)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | companyDepartmentMapper.deleteBatchIds(ids); | 
|---|
|  |  |  | for(Integer id :ids){ | 
|---|
|  |  |  | deleteById(id); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateById(CompanyDepartment companyDepartment) { | 
|---|
|  |  |  | companyDepartmentMapper.updateById(companyDepartment); | 
|---|
|  |  |  | public void updateById(CompanyDepartment company) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(company.getId() == null|| StringUtils.isBlank(company.getName())){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.BAD_REQUEST); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(company.getType()!=null && !(company.getType() ==0 || company.getType() ==1)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,组织类型不正确,请按要求填写~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LoginUserInfo user = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int type = Constants.ZERO;//默认平台组织 | 
|---|
|  |  |  | if(Constants.equalsInteger(user.getType(),Constants.UserType.COMPANY.getKey())){ | 
|---|
|  |  |  | //如果企业用户 | 
|---|
|  |  |  | type  = Constants.ONE; | 
|---|
|  |  |  | }else if(Constants.equalsInteger(user.getType(),Constants.UserType.ZHUBO.getKey())){ | 
|---|
|  |  |  | //如果是商户 | 
|---|
|  |  |  | type  = Constants.TWO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CompanyDepartment  model = findById(company.getId()); | 
|---|
|  |  |  | if(model == null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE) | 
|---|
|  |  |  | || !Constants.equalsInteger(model.getType(),type)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,该组织信息不存在!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //查询名称不能重复 | 
|---|
|  |  |  | if(companyDepartmentMapper.selectCount(new QueryWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .eq(CompanyDepartment::getName,company.getName()) | 
|---|
|  |  |  | .eq(CompanyDepartment::getType,type) | 
|---|
|  |  |  | .ne(CompanyDepartment::getId,company.getId()) | 
|---|
|  |  |  | .eq(CompanyDepartment::getIsdeleted,Constants.ZERO)) >0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "对不起,组织名称不能重复~"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | company.setEditDate(new Date()); | 
|---|
|  |  |  | company.setEditor(user.getId()); | 
|---|
|  |  |  | company.setParentId(null);//不支持修改父级 | 
|---|
|  |  |  | companyDepartmentMapper.updateById(company); | 
|---|
|  |  |  | //更新的companyPath | 
|---|
|  |  |  | String newName = model.getNamePath().replace(model.getName(),company.getName()); | 
|---|
|  |  |  | companyDepartmentMapper.update(null,new UpdateWrapper<CompanyDepartment>().lambda() | 
|---|
|  |  |  | .setSql("company_name_path=REPLACE(name_path,'"+ model.getNamePath()+"','"+newName+"')") | 
|---|
|  |  |  | .likeRight(CompanyDepartment::getIdPath,model.getIdPath())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|