123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- package com.fdkankan.fusion.service.impl;
- import cn.dev33.satoken.context.SaHolder;
- import cn.dev33.satoken.stp.StpUtil;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.core.util.RandomUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.fusion.common.PageInfo;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.common.enums.IdPreEnum;
- import com.fdkankan.fusion.common.enums.RoleKeyEnum;
- import com.fdkankan.fusion.common.util.IdUtils;
- import com.fdkankan.fusion.common.util.PatternEnum;
- import com.fdkankan.fusion.common.util.RedisKeyUtil;
- import com.fdkankan.fusion.entity.*;
- import com.fdkankan.fusion.exception.BusinessException;
- import com.fdkankan.fusion.mapper.ITmProjectMapper;
- import com.fdkankan.fusion.request.*;
- import com.fdkankan.fusion.response.DataGroupVo;
- import com.fdkankan.fusion.response.ProjectRandCodeVo;
- import com.fdkankan.fusion.service.*;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.redis.util.RedisUtil;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.net.URLEncoder;
- import java.time.LocalDateTime;
- import java.time.Period;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 火调项目信息表 服务实现类
- * </p>
- *
- * @author
- * @since 2023-07-28
- */
- @Service
- public class TmProjectServiceImpl extends ServiceImpl<ITmProjectMapper, TmProject> implements ITmProjectService {
- @Autowired
- ITmDepartmentService tmDepartmentService;
- @Autowired
- ITmUserService tmUserService;
- @Autowired
- ICaseService caseService;
- @Autowired
- ICaseNumService caseNumService;
- @Autowired
- ITmAttachmentService tmAttachmentService;
- @Autowired
- RedisUtil redisUtil;
- @Autowired
- ITmRoleService tmRoleService;
- @Autowired
- ITmUserRoleService tmUserRoleService;
- @Override
- public Object pageList(ProjectRequestDto tmProject) {
- LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
- //教学场景
- List<String> deptIds;
- if(tmProject.getQueryType() != null && tmProject.getQueryType() == 2){
- wrapper.eq(TmProject::getIsTeached,1);
- deptIds = tmDepartmentService.getByZdDeptIds();
- }else {
- String share = SaHolder.getRequest().getHeader("share");
- if(StringUtils.isNotBlank(share) && "1".equals(share)){ //分享请求头
- deptIds = tmDepartmentService.list().stream().map(TmDepartment::getId).collect(Collectors.toList());
- wrapper.eq(TmProject::getMapShow,false);
- wrapper.isNotNull(TmProject::getLatlng);
- }else {
- deptIds = tmDepartmentService.getDeptIds();
- }
- }
- if(deptIds.size() <=0){
- throw new BusinessException(ResultCode.NOT_DEPT);
- }
- //承办单位
- if(StringUtils.isNotBlank(tmProject.getOrganizerDeptName())){
- List<TmDepartment> list = tmDepartmentService.getLikeName(tmProject.getOrganizerDeptName());
- if(list.size() <=0){
- return PageInfo.PageInfo(new Page<>(tmProject.getPageNum(),tmProject.getPageSize()));
- }
- List<String> collect = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
- deptIds = deptIds.stream().filter(collect::contains).collect(Collectors.toList());
- }
- List<String> deptIds2 = tmDepartmentService.getSonByDeptIdAndDeptIds(deptIds, tmProject.getDeptId());
- wrapper.in(TmProject::getDeptId,deptIds2);
- //起火项目对象
- if(StringUtils.isNotBlank(tmProject.getProjectSn())){
- wrapper.like(TmProject::getProjectSn , tmProject.getProjectSn());
- }
- //起火对象
- if(StringUtils.isNotBlank(tmProject.getProjectName())){
- wrapper.like(TmProject::getProjectName , tmProject.getProjectName());
- }
- //起火地址
- if(StringUtils.isNotBlank(tmProject.getProjectAddress())){
- wrapper.like(TmProject::getProjectAddress , tmProject.getProjectAddress());
- }
- //起火场所
- if(StringUtils.isNotBlank(tmProject.getProjectSiteCode())){
- if(!StringUtils.equals("0" , tmProject.getProjectSiteCode())){
- wrapper.likeRight(TmProject::getProjectSiteCode , tmProject.getProjectSiteCode());
- }
- }
- //起火对象
- if(StringUtils.isNotBlank(tmProject.getOrganizerUsers())){
- wrapper.like(TmProject::getOrganizerUsers , tmProject.getOrganizerUsers());
- }
- //事故日期
- if(StringUtils.isNotBlank(tmProject.getAccidentDate())){
- wrapper.ge(TmProject::getAccidentDate,tmProject.getAccidentDateStart());
- wrapper.le(TmProject::getAccidentDate,tmProject.getAccidentDateEnd());
- }
- //起火原因
- if(StringUtils.isNotBlank(tmProject.getFireReason())){
- wrapper.like(TmProject::getFireReason , tmProject.getFireReason());
- }
- //是否是教学项目列表
- if(null != tmProject.getIsTeached()){
- wrapper.eq(TmProject::getIsTeached , tmProject.getIsTeached());
- }
- //项目状态
- if(StringUtils.isNotBlank(tmProject.getStatus())){
- wrapper.eq(TmProject::getStatus , tmProject.getStatus());
- }
- wrapper.orderByDesc(TmProject::getCreateTime);
- Page<TmProject> page = this.page(new Page<>(tmProject.getPageNum(), tmProject.getPageSize()), wrapper);
- List<String> ids = page.getRecords().stream().map(TmProject::getId).collect(Collectors.toList());
- Set<String> deptDbIds = page.getRecords().stream().map(TmProject::getDeptId).collect(Collectors.toSet());
- HashMap<String, TmDepartment> mapByDeptIds = tmDepartmentService.getMapByDeptIds(deptDbIds);
- HashMap<String ,CaseEntity> map = this.getCaseMap(ids);
- for (TmProject record : page.getRecords()) {
- CaseEntity caseEntity = map.get(record.getId());
- if(caseEntity != null){
- record.setCaseId(caseEntity.getCaseId());
- }
- TmDepartment tmDepartment = mapByDeptIds.get(record.getDeptId());
- if(tmDepartment != null){
- record.setOrganizerDeptName(tmDepartment.getName());
- }
- }
- return PageInfo.PageInfo(page);
- }
- @Override
- public Object getDetailWithoutAuth(ProjectRequest param) {
- if (ObjectUtil.isNotNull(param.getCaseId()) && StringUtils.isBlank(param.getRandCode())) {
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,param.getCaseId());
- if(!redisUtil.hasKey(redisKey)){
- throw new BusinessException(ResultCode.PROJECT_KEY_NOT_EXITS);
- }
- if (StringUtils.isBlank(param.getRandCode())) {
- throw new BusinessException(ResultCode.PROJECT_PASSWORD_NOT_EXITS);
- }
- String redisRandCode = redisUtil.get(redisKey);
- if(!StringUtils.equals(redisRandCode , param.getRandCode())){
- throw new BusinessException(ResultCode.PROJECT_PASSWORD_ERROR);
- }
- return true;
- }
- @Override
- public Object getRandCode(String caseId) {
- if(StringUtils.isBlank(caseId) ){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,caseId);
- if(redisUtil.hasKey(redisKey)){
- redisUtil.expire(redisKey,RedisKeyUtil.projectRandCodeKeyTime);
- return redisUtil.get(redisKey);
- }
- CaseEntity caseEntity = caseService.getById(caseId);
- if(caseEntity ==null){
- throw new BusinessException(ResultCode.CASE_NOT_EXITS);
- }
- String code = RandomUtil.randomString(4);
- redisUtil.set(redisKey, code,RedisKeyUtil.projectRandCodeKeyTime);
- return code;
- }
- @Override
- public void updateRandomCode(ProjectRandCodeDto projectRandCodeDto) {
- if(StringUtils.isBlank(projectRandCodeDto.getCaseId()) || StringUtils.isBlank(projectRandCodeDto.getRandCode())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if(!projectRandCodeDto.getRandCode().matches(PatternEnum.RAND_CODE_PATTERN)){
- throw new BusinessException(ResultCode.RAND_ERROR);
- }
- String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,projectRandCodeDto.getCaseId());
- if(!redisUtil.hasKey(redisKey)){
- throw new BusinessException(ResultCode.RAND_NOT_EXIST);
- }
- redisUtil.set(redisKey,projectRandCodeDto.getRandCode(),RedisKeyUtil.projectRandCodeKeyTime);
- }
- @Override
- public void addNewProject(TmProject tmProject) {
- if(StringUtils.isBlank(tmProject.getProjectSn())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- TmProject project = this.getByProjectSn(tmProject.getProjectSn());
- if(project != null){
- throw new BusinessException(ResultCode.PROJECT_EXITS);
- }
- TmUser tmUser = tmUserService.getLoginUser();
- if(tmUser == null || StringUtils.isBlank(tmUser.getDeptId())){
- throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
- }
- TmDepartment tmDepartment = tmDepartmentService.getById(tmUser.getDeptId());
- if(tmDepartment == null){
- throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
- }
- tmProject.setId(IdUtils.genId(IdPreEnum.PROJECT_PRE.getPre()));
- tmProject.setDeptId(tmUser.getDeptId());
- tmProject.setCreatorId(tmUser.getId());
- tmProject.setCreatorName(tmUser.getNickName());
- tmProject.setCreatorDeptId(tmUser.getDeptId());
- tmProject.setEditorId(tmUser.getId());
- tmProject.setEditorName(tmUser.getNickName());
- tmProject.setOrganizerDeptName(tmDepartment.getName());
- tmProject.setCaseNewName(setCaseNewName(tmProject));
- this.save(tmProject);
- CaseEntity caseEntity = new CaseEntity();
- caseEntity.setCaseTitle(tmProject.getProjectName());
- caseEntity.setUserName(tmUser.getUserName());
- caseEntity.setType(1);
- caseEntity.setTmProjectId(tmProject.getId());
- caseEntity.setMapUrl(tmProject.getMapUrl());
- caseEntity.setLatAndLong(tmProject.getLatAndLong());
- caseService.save(caseEntity);
- }
- private String setCaseNewName(TmProject tmProject) {
- String name = "";
- if(StringUtils.isNotBlank(tmProject.getProjectAddress())){
- name += tmProject.getProjectAddress();
- }
- if(tmProject.getAccidentDate() != null){
- name += "("+DateUtil.format(tmProject.getAccidentDate(),"yyyy-MM-dd") +")";
- }
- if(StringUtils.isNotBlank(tmProject.getProjectName())){
- name += tmProject.getProjectName();
- }
- return name;
- }
- @Override
- public void updateProject(TmProject tmProject) {
- if(StringUtils.isBlank(tmProject.getId())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if(tmProject.getIsDelete() == 1){
- CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
- if(caseEntity == null){
- throw new BusinessException(ResultCode.PROJECT_CASE_NOT_EXITS);
- }
- caseService.delete(caseEntity.getCaseId());
- tmAttachmentService.deleteByProjectId(tmProject.getId());
- this.removeById(tmProject.getId());
- return;
- }
- TmProject byProjectSn = this.getByProjectSn(tmProject.getProjectSn());
- if(byProjectSn != null && !byProjectSn.getId().equals(tmProject.getId())){
- throw new BusinessException(ResultCode.PROJECT_EXITS);
- }
- TmUser tmUser = tmUserService.getLoginUser();
- tmProject.setEditorId(tmUser.getId());
- tmProject.setEditorName(tmUser.getNickName());
- tmProject.setUpdateTime(null);
- tmProject.setCaseNewName(setCaseNewName(tmProject));
- this.updateById(tmProject);
- CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
- caseEntity.setCaseTitle(tmProject.getProjectName());
- caseEntity.setMapUrl(tmProject.getMapUrl());
- caseEntity.setLatAndLong(tmProject.getLatAndLong());
- caseService.updateById(caseEntity);
- }
- @Override
- public void setOrUnTeach(CommonDto commonDto) {
- if(null == commonDto || StringUtils.isBlank(commonDto.getIds())){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- List<String> idList = Arrays.asList(commonDto.getIds().split(","));
- if(CollectionUtils.isEmpty(idList)){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- if(commonDto.getStatus() != 1){ //撤销教学项目
- List<TmProject> tmProjects = this.listByIds(idList);
- Set<String> projectDeptIds = tmProjects.stream().map(TmProject::getDeptId).collect(Collectors.toSet());
- TmUser loginUser = tmUserService.getLoginUser();
- TmRole tmRole = tmRoleService.getByUserId(loginUser.getId());
- if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_DEPT.getKey())){
- List<TmDepartment> sonByDeptId = tmDepartmentService.getSonByDeptId(loginUser.getDeptId());
- List<String> deptIds = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
- deptIds.add(loginUser.getDeptId());
- for (String projectDeptId : projectDeptIds) {
- if(!deptIds.contains(projectDeptId)){
- throw new BusinessException(ResultCode.NOT_PER);
- }
- }
- }
- if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_ORDINARY.getKey())){
- for (String projectDeptId : projectDeptIds) {
- if(!projectDeptId.equals(loginUser.getDeptId())){
- throw new BusinessException(ResultCode.NOT_PER);
- }
- }
- }
- }
- LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
- wrapper.in(TmProject::getId,idList);
- wrapper.set(TmProject::getIsTeached,commonDto.getStatus());
- this.update(wrapper);
- }
- @Override
- public TmProject getByProjectSn(String projectSn) {
- LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TmProject::getProjectSn,projectSn);
- return this.getOne(wrapper);
- }
- @Override
- public HashMap<String, CaseEntity> getCaseMap(List<String> ids) {
- HashMap<String, CaseEntity> map = new HashMap<>();
- if(ids.size() >0){
- LambdaQueryWrapper<CaseEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(CaseEntity::getTmProjectId,ids);
- List<CaseEntity> list = caseService.list(wrapper);
- if(list !=null && list.size() >0){
- list.forEach(entity -> map.put(entity.getTmProjectId(),entity));
- }
- }
- return map;
- }
- @Override
- public void updateIdenTityStatus(String tmProjectId, Integer status) {
- if(StringUtils.isNotBlank(tmProjectId)){
- LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(TmProject::getId,tmProjectId);
- wrapper.set(TmProject::getStatus,status);
- this.update(wrapper);
- }
- }
- @Override
- public HashMap<String, TmProject> getMapByIds(Set<String> tmProIds) {
- HashMap<String, TmProject> map = new HashMap<>();
- if(tmProIds.size() >0){
- List<TmProject> tmProjects = this.listByIds(tmProIds);
- for (TmProject tmProject : tmProjects) {
- map.put(tmProject.getId(),tmProject);
- }
- }
- return map;
- }
- @Override
- public List<DataGroupVo> groupByDeptId(DataParam param) {
- return getBaseMapper().groupByDeptId(param.getStartTime(),param.getEndTime());
- }
- @Override
- public List<DataGroupVo> groupByMonth(DataParam param,List<String> deptIds) {
- return getBaseMapper().groupByMonth(param.getStartTime(),param.getEndTime(),deptIds);
- }
- @Override
- public List<DataGroupVo> groupByPlace(DataParam param,List<String> deptIds) {
- return getBaseMapper().groupByPlace(param.getStartTime(),param.getEndTime(),deptIds);
- }
- @Override
- public List<DataGroupVo> groupByReason(DataParam param,List<String> deptIds) {
- return getBaseMapper().groupByReason(param.getStartTime(),param.getEndTime(),deptIds);
- }
- }
|