package com.doumee.service.business.impl; 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.Constants.DeclareStatus; import com.doumee.core.utils.Constants.ProjectRecord; import com.doumee.core.utils.Constants.UserType; import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.Utils; import com.doumee.dao.business.DeclareExpertMapper; import com.doumee.dao.business.DeclareHistoryMapper; import com.doumee.dao.business.DeclaresJoinMapper; import com.doumee.dao.business.DeclaresMapper; import com.doumee.dao.business.model.Company; import com.doumee.dao.business.model.DeclareExpert; import com.doumee.dao.business.model.DeclareHistory; import com.doumee.dao.business.model.Declares; import com.doumee.dao.business.model.Project; import com.doumee.dao.system.SystemUserMapper; import com.doumee.dao.system.model.SystemUser; import com.doumee.service.business.DeclareExpertService; 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.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.Date; import java.util.List; import java.util.Objects; /** * 项目申报分配专家关联表Service实现 * @author 江蹄蹄 * @date 2023/02/15 08:55 */ @Service public class DeclareExpertServiceImpl implements DeclareExpertService { @Autowired private DeclareExpertMapper declareExpertMapper; @Autowired private DeclaresMapper declaresMapper; @Autowired private DeclaresJoinMapper declaresJoinMapper; @Autowired private DeclareHistoryMapper declareHistoryMapper; @Autowired private SystemUserMapper systemUserMapper; @Override public Integer create(DeclareExpert declareExpert) { declareExpertMapper.insert(declareExpert); return declareExpert.getId(); } @Override public void deleteById(Integer id) { declareExpertMapper.deleteById(id); } @Override public void delete(DeclareExpert declareExpert) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(declareExpert); declareExpertMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } declareExpertMapper.deleteBatchIds(ids); } @Override public void updateById(DeclareExpert declareExpert) { declareExpertMapper.updateById(declareExpert); } @Override public void updateByIdInBatch(List declareExperts) { if (CollectionUtils.isEmpty(declareExperts)) { return; } for (DeclareExpert declareExpert: declareExperts) { this.updateById(declareExpert); } } @Override public DeclareExpert findById(Integer id) { return declareExpertMapper.selectById(id); } @Override public DeclareExpert findOne(DeclareExpert declareExpert) { QueryWrapper wrapper = new QueryWrapper<>(declareExpert); return declareExpertMapper.selectOne(wrapper); } @Override public List findList(DeclareExpert declareExpert) { QueryWrapper wrapper = new QueryWrapper<>(declareExpert); return declareExpertMapper.selectList(wrapper); } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); QueryWrapper queryWrapper = new QueryWrapper<>(); Utils.MP.blankToNull(pageWrap.getModel()); if (pageWrap.getModel().getId() != null) { queryWrapper.lambda().eq(DeclareExpert::getId, pageWrap.getModel().getId()); } if (pageWrap.getModel().getCreator() != null) { queryWrapper.lambda().eq(DeclareExpert::getCreator, pageWrap.getModel().getCreator()); } if (pageWrap.getModel().getCreateDate() != null) { queryWrapper.lambda().ge(DeclareExpert::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); queryWrapper.lambda().le(DeclareExpert::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); } if (pageWrap.getModel().getEditor() != null) { queryWrapper.lambda().eq(DeclareExpert::getEditor, pageWrap.getModel().getEditor()); } if (pageWrap.getModel().getEditDate() != null) { queryWrapper.lambda().ge(DeclareExpert::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); queryWrapper.lambda().le(DeclareExpert::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); } if (pageWrap.getModel().getIsdeleted() != null) { queryWrapper.lambda().eq(DeclareExpert::getIsdeleted, pageWrap.getModel().getIsdeleted()); } if (pageWrap.getModel().getRemark() != null) { queryWrapper.lambda().eq(DeclareExpert::getRemark, pageWrap.getModel().getRemark()); } if (pageWrap.getModel().getExpertId() != null) { queryWrapper.lambda().eq(DeclareExpert::getExpertId, pageWrap.getModel().getExpertId()); } if (pageWrap.getModel().getDeclareId() != null) { queryWrapper.lambda().eq(DeclareExpert::getDeclareId, pageWrap.getModel().getDeclareId()); } if (pageWrap.getModel().getScore() != null) { queryWrapper.lambda().eq(DeclareExpert::getScore, pageWrap.getModel().getScore()); } if (pageWrap.getModel().getStatus() != null) { queryWrapper.lambda().eq(DeclareExpert::getStatus, pageWrap.getModel().getStatus()); } if (pageWrap.getModel().getScoreDate() != null) { queryWrapper.lambda().ge(DeclareExpert::getScoreDate, Utils.Date.getStart(pageWrap.getModel().getScoreDate())); queryWrapper.lambda().le(DeclareExpert::getScoreDate, Utils.Date.getEnd(pageWrap.getModel().getScoreDate())); } for(PageWrap.SortData sortData: pageWrap.getSorts()) { if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { queryWrapper.orderByDesc(sortData.getProperty()); } else { queryWrapper.orderByAsc(sortData.getProperty()); } } return PageData.from(declareExpertMapper.selectPage(page, queryWrapper)); } @Override public long count(DeclareExpert declareExpert) { QueryWrapper wrapper = new QueryWrapper<>(declareExpert); return declareExpertMapper.selectCount(wrapper); } /** * 分配专家 * @param declareExpert * @throws BusinessException */ @Override @Transactional(rollbackFor = {BusinessException.class, Exception.class}) public void assignExperts(DeclareExpert declareExpert){ if(declareExpert.getDeclareId()==null||declareExpert.getExpertId()==null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),ResponseStatus.BAD_REQUEST.getMessage()); } LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); //查询申报记录并验证权限和状态合法 Declares model = getDeclaresAndValid(user,declareExpert); //查询已分配专家记录,只有一条专家分配记录 DeclareExpert result = getDeclareExpertRecord(declareExpert.getDeclareId()); //处理申报记录状态和录入历史数据 DeclareHistory history = initDeclareAndHistory( model,result,user); //插入数据 declareExpert.setCreator(user.getId()); declareExpert.setIsdeleted(Constants.ZERO); declareExpert.setCreateDate(history.getCreateDate()); declareExpert.setStatus(Constants.ZERO); /*if(Objects.nonNull(result)){ result.setExpertId(declareExpert.getExpertId()); declareExpertMapper.updateById(result); }else{*/ declareExpertMapper.insert(declareExpert); /* }*/ } private DeclareHistory initDeclareAndHistory( Declares model,DeclareExpert result, LoginUserInfo user) { Date date = new Date(); Declares declares=new Declares(); declares.setId(model.getId()); declares.setEditDate(date); declares.setEditor(user.getId()); declares.setStatus(Constants.DeclareStatus.SELECT_EXPERT.getKey()); DeclareHistory history = new DeclareHistory(); history.setCreateDate(date); history.setIsdeleted(Constants.ZERO); history.setCheckor(user.getId()); history.setStatus(declares.getStatus()); history.setDeclareId(model.getId()); history.setCheckor(user.getId()); history.setCheckDate(date); String checkInfo ; if(result!=null){ //重新分配专家 删除原有的分配记录 deleteOldRecord(result,user.getId()); if(Constants.equalsInteger(model.getEdType(),Constants.ONE)){ //如果是综合服务单位分配 history.setType(ProjectRecord.SD_RESELECT_EXPERT.getKey()); checkInfo = ProjectRecord.SD_RESELECT_EXPERT.getNoteinfo(); }else{ history.setType(ProjectRecord.SJ_RESELECT_EXPERT.getKey()); checkInfo = ProjectRecord.SJ_RESELECT_EXPERT.getNoteinfo(); } }else{ //首次分配 if(Constants.equalsInteger(model.getEdType(),Constants.ONE)){ //如果是综合服务单位分配 history.setType(ProjectRecord.SD_SELECT_EXPERT.getKey()); checkInfo = ProjectRecord.SD_SELECT_EXPERT.getNoteinfo(); }else{ checkInfo = ProjectRecord.SJ_SELECT_EXPERT.getNoteinfo(); history.setType(ProjectRecord.SJ_SELECT_EXPERT.getKey()); } } history.setCheckInfo(checkInfo.replace("${param2}", Constants.UserType.getName(user.getType())) .replace("${param3}", StringUtils.defaultString(user.getUsername(),"")) .replace("${param4}", DeclareStatus.SELECT_EXPERT.getNoteinfo())); declares.setCheckType(history.getType()); declares.setCheckor(history.getCheckor()); declares.setCheckInfo(history.getCheckInfo()); declaresMapper.updateById(declares); //审核记录表 declareHistoryMapper.insert(history); return history; } private void deleteOldRecord(DeclareExpert result,Integer userId) { result.setEditDate(new Date()); result.setEditor(userId); result.setIsdeleted(Constants.ONE); //逻辑删除原有的分配记录 declareExpertMapper.updateById(result); } private DeclareExpert getDeclareExpertRecord(Integer declareId) { DeclareExpert query=new DeclareExpert(); query.setIsdeleted(Constants.ZERO); query.setDeclareId(declareId); DeclareExpert result= declareExpertMapper.selectOne(new QueryWrapper<>(query).last("limit 1") ); return result; } /** * 查询申报记录并验证权限和状态合法 * @param user * @param declareExpert * @return */ private Declares getDeclaresAndValid(LoginUserInfo user, DeclareExpert declareExpert) { MPJLambdaWrapper queryWrapper =new MPJLambdaWrapper<>(); queryWrapper.selectAll(Declares.class); queryWrapper.leftJoin(Project.class,Project::getId,Declares::getProjectId); queryWrapper.leftJoin(Company.class,Company::getId,Declares::getCompanyId); queryWrapper.selectAs(Project::getEdType,Declares::getEdType); queryWrapper.selectAs(Company::getCityId,Declares::getCityId); queryWrapper.eq(Declares::getId,declareExpert.getDeclareId()); queryWrapper.last("limit 1"); Declares declares = declaresJoinMapper.selectJoinOne(Declares.class,queryWrapper); if(declares==null ||Constants.equalsInteger(declares.getIsdeleted(),Constants.ONE)){ throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,企业报名信息不存在!"); } if(!(Constants.equalsInteger(declares.getStatus(), DeclareStatus.SERVICE_DONE.getKey()) ||Constants.equalsInteger(declares.getStatus(), DeclareStatus.BACK_SERVER_DONE.getKey()) ||Constants.equalsInteger(declares.getStatus(), DeclareStatus.SELECT_EXPERT.getKey()))){ //只有服务完成或者已完成退回修改(待分配专家)和已分配专家两个状态可以操作 throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,企业报名状态已流转,不能进行当前操作!"); } /* if(Constants.equalsInteger(declares.getCityId(),user.getCityId())){ //如果项目不是本城市的,不能操作非本市的企业报名数据 throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该企业非管辖范围内数据,不能进行当前操作!"); }*/ if(Constants.equalsInteger(declares.getEdType(),Constants.ONE)){ //如果是综合服务单位分配 if(!(Constants.equalsInteger(user.getType(), UserType.SD_ADMIN.getKey())||Constants.equalsInteger(user.getType(), UserType.SD_CHILD.getKey()))){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!"); } if(Constants.equalsInteger(user.getType(), UserType.SD_CHILD.getKey())&& !Constants.equalsInteger(declares.getSdUserId(),user.getId())){ //如果是子账号,只能管理佩服到自己的数据 throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!"); } }else{ //如果是市局分配 if(!Constants.equalsInteger(user.getType(), UserType.SJ.getKey()) ){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!"); } } SystemUser expert = systemUserMapper.selectById(declareExpert.getExpertId()); if(expert == null || !Constants.equalsInteger(expert.getType(), UserType.EXPERT.getKey())){ //专家信息不存在 throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,您选择的专家信息不存在,请返回刷新页面重试!"); } return declares; } }