TmProjectServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.core.util.RandomUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.fdkankan.fusion.common.PageInfo;
  9. import com.fdkankan.fusion.common.ResultCode;
  10. import com.fdkankan.fusion.common.enums.IdPreEnum;
  11. import com.fdkankan.fusion.common.util.IdUtils;
  12. import com.fdkankan.fusion.common.util.PatternEnum;
  13. import com.fdkankan.fusion.common.util.RedisKeyUtil;
  14. import com.fdkankan.fusion.entity.*;
  15. import com.fdkankan.fusion.exception.BusinessException;
  16. import com.fdkankan.fusion.mapper.ITmProjectMapper;
  17. import com.fdkankan.fusion.request.CommonDto;
  18. import com.fdkankan.fusion.request.ProjectRandCodeDto;
  19. import com.fdkankan.fusion.request.ProjectRequest;
  20. import com.fdkankan.fusion.request.ProjectRequestDto;
  21. import com.fdkankan.fusion.response.ProjectRandCodeVo;
  22. import com.fdkankan.fusion.service.*;
  23. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  24. import com.fdkankan.redis.util.RedisUtil;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.util.CollectionUtils;
  29. import java.net.URLEncoder;
  30. import java.time.LocalDateTime;
  31. import java.time.Period;
  32. import java.util.*;
  33. import java.util.stream.Collectors;
  34. /**
  35. * <p>
  36. * 火调项目信息表 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2023-07-28
  41. */
  42. @Service
  43. public class TmProjectServiceImpl extends ServiceImpl<ITmProjectMapper, TmProject> implements ITmProjectService {
  44. @Autowired
  45. ITmDepartmentService tmDepartmentService;
  46. @Autowired
  47. ITmUserService tmUserService;
  48. @Autowired
  49. ICaseService caseService;
  50. @Autowired
  51. ICaseNumService caseNumService;
  52. @Autowired
  53. ITmAttachmentService tmAttachmentService;
  54. @Autowired
  55. RedisUtil redisUtil;
  56. @Override
  57. public Object pageList(ProjectRequestDto tmProject) {
  58. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  59. //教学场景
  60. List<String> deptIds;
  61. if(tmProject.getQueryType() == 2){
  62. wrapper.eq(TmProject::getIsTeached,1);
  63. deptIds = tmDepartmentService.getByZdDeptIds();
  64. }else {
  65. deptIds = tmDepartmentService.getDeptIds();
  66. }
  67. if(deptIds.size() <=0){
  68. throw new BusinessException(ResultCode.NOT_DEPT);
  69. }
  70. //承办单位
  71. if(StringUtils.isNotBlank(tmProject.getOrganizerDeptName())){
  72. List<TmDepartment> list = tmDepartmentService.getLikeName(tmProject.getOrganizerDeptName());
  73. if(list.size() <=0){
  74. return PageInfo.PageInfo(new Page<>(tmProject.getPageNum(),tmProject.getPageSize()));
  75. }
  76. List<String> collect = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
  77. deptIds = deptIds.stream().filter(collect::contains).collect(Collectors.toList());
  78. }
  79. wrapper.in(TmProject::getDeptId,deptIds);
  80. //起火项目对象
  81. if(StringUtils.isNotBlank(tmProject.getProjectSn())){
  82. wrapper.like(TmProject::getProjectSn , tmProject.getProjectSn());
  83. }
  84. //起火对象
  85. if(StringUtils.isNotBlank(tmProject.getProjectName())){
  86. wrapper.like(TmProject::getProjectName , tmProject.getProjectName());
  87. }
  88. //起火地址
  89. if(StringUtils.isNotBlank(tmProject.getProjectAdrress())){
  90. wrapper.like(TmProject::getProjectAddress , tmProject.getProjectAdrress());
  91. }
  92. //起火场所
  93. if(StringUtils.isNotBlank(tmProject.getProjectSiteCode())){
  94. if(!StringUtils.equals("0" , tmProject.getProjectSiteCode())){
  95. wrapper.likeRight(TmProject::getProjectSiteCode , tmProject.getProjectSiteCode());
  96. }
  97. }
  98. //起火对象
  99. if(StringUtils.isNotBlank(tmProject.getOrganizerUsers())){
  100. wrapper.like(TmProject::getOrganizerUsers , tmProject.getOrganizerUsers());
  101. }
  102. if(StringUtils.isNotBlank(tmProject.getDeptId())){
  103. wrapper.eq(TmProject::getDeptId , tmProject.getDeptId());
  104. }
  105. //事故日期
  106. if(null != tmProject.getAccidentDate()){
  107. wrapper.ge(TmProject::getAccidentDate,tmProject.getAccidentDateStart());
  108. wrapper.le(TmProject::getAccidentDate,tmProject.getAccidentDateEnd());
  109. }
  110. //起火原因
  111. if(StringUtils.isNotBlank(tmProject.getFireReason())){
  112. wrapper.like(TmProject::getFireReason , tmProject.getFireReason());
  113. }
  114. //是否是教学项目列表
  115. if(null != tmProject.getIsTeached()){
  116. wrapper.eq(TmProject::getIsTeached , tmProject.getIsTeached());
  117. }
  118. //项目状态
  119. if(StringUtils.isNotBlank(tmProject.getStatus())){
  120. wrapper.eq(TmProject::getStatus , tmProject.getStatus());
  121. }
  122. wrapper.orderByDesc(TmProject::getCreateTime);
  123. Page<TmProject> page = this.page(new Page<>(tmProject.getPageNum(), tmProject.getPageSize()), wrapper);
  124. List<String> ids = page.getRecords().stream().map(TmProject::getId).collect(Collectors.toList());
  125. Set<String> deptDbIds = page.getRecords().stream().map(TmProject::getDeptId).collect(Collectors.toSet());
  126. HashMap<String, TmDepartment> mapByDeptIds = tmDepartmentService.getMapByDeptIds(deptDbIds);
  127. HashMap<String ,CaseEntity> map = this.getCaseMap(ids);
  128. for (TmProject record : page.getRecords()) {
  129. CaseEntity caseEntity = map.get(record.getId());
  130. if(caseEntity != null){
  131. record.setCaseId(caseEntity.getCaseId());
  132. }
  133. TmDepartment tmDepartment = mapByDeptIds.get(record.getDeptId());
  134. if(tmDepartment != null){
  135. record.setOrganizerDeptName(tmDepartment.getName());
  136. }
  137. }
  138. return PageInfo.PageInfo(page);
  139. }
  140. @Override
  141. public Object getDetailWithoutAuth(ProjectRequest param) {
  142. if (ObjectUtil.isNotNull(param.getCaseId()) && StringUtils.isBlank(param.getRandCode())) {
  143. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  144. }
  145. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,param.getCaseId());
  146. if(!redisUtil.hasKey(redisKey)){
  147. throw new BusinessException(ResultCode.PROJECT_KEY_NOT_EXITS);
  148. }
  149. if (StringUtils.isBlank(param.getRandCode())) {
  150. throw new BusinessException(ResultCode.PROJECT_PASSWORD_NOT_EXITS);
  151. }
  152. String redisRandCode = redisUtil.get(redisKey);
  153. if(!StringUtils.equals(redisRandCode , param.getRandCode())){
  154. throw new BusinessException(ResultCode.PROJECT_PASSWORD_ERROR);
  155. }
  156. return true;
  157. }
  158. @Override
  159. public Object getRandCode(String caseId) {
  160. if(StringUtils.isBlank(caseId) ){
  161. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  162. }
  163. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,caseId);
  164. if(redisUtil.hasKey(redisKey)){
  165. redisUtil.expire(redisKey,RedisKeyUtil.projectRandCodeKeyTime);
  166. return redisUtil.get(redisKey);
  167. }
  168. CaseEntity caseEntity = caseService.getById(caseId);
  169. if(caseEntity ==null){
  170. throw new BusinessException(ResultCode.CASE_NOT_EXITS);
  171. }
  172. redisUtil.set(redisKey, RandomUtil.randomString(4));
  173. return RandomUtil.randomString(4);
  174. }
  175. @Override
  176. public void updateRandomCode(ProjectRandCodeDto projectRandCodeDto) {
  177. if(StringUtils.isBlank(projectRandCodeDto.getCaseId()) || StringUtils.isBlank(projectRandCodeDto.getRandCode())){
  178. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  179. }
  180. if(!projectRandCodeDto.getRandCode().matches(PatternEnum.RAND_CODE_PATTERN)){
  181. throw new BusinessException(ResultCode.RAND_ERROR);
  182. }
  183. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,projectRandCodeDto.getCaseId());
  184. if(!redisUtil.hasKey(redisKey)){
  185. throw new BusinessException(ResultCode.RAND_NOT_EXIST);
  186. }
  187. redisUtil.set(redisKey,projectRandCodeDto.getRandCode(),RedisKeyUtil.projectRandCodeKeyTime);
  188. }
  189. @Override
  190. public void addNewProject(TmProject tmProject) {
  191. if(StringUtils.isBlank(tmProject.getProjectSn())){
  192. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  193. }
  194. TmProject project = this.getByProjectSn(tmProject.getProjectSn());
  195. if(project != null){
  196. throw new BusinessException(ResultCode.PROJECT_EXITS);
  197. }
  198. TmUser tmUser = tmUserService.getLoginUser();
  199. if(tmUser == null || StringUtils.isBlank(tmUser.getDeptId())){
  200. throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
  201. }
  202. TmDepartment tmDepartment = tmDepartmentService.getById(tmUser.getDeptId());
  203. if(tmDepartment == null){
  204. throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
  205. }
  206. tmProject.setId(IdUtils.genId(IdPreEnum.PROJECT_PRE.getPre()));
  207. tmProject.setDeptId(tmUser.getDeptId());
  208. tmProject.setCreatorId(tmUser.getId());
  209. tmProject.setCreatorName(tmUser.getNickName());
  210. tmProject.setCreatorDeptId(tmUser.getDeptId());
  211. tmProject.setEditorId(tmUser.getId());
  212. tmProject.setEditorName(tmUser.getNickName());
  213. tmProject.setOrganizerDeptName(tmDepartment.getName());
  214. this.save(tmProject);
  215. CaseEntity caseEntity = new CaseEntity();
  216. caseEntity.setCaseTitle(tmProject.getProjectName());
  217. caseEntity.setUserName(tmUser.getUserName());
  218. caseEntity.setType(1);
  219. caseEntity.setTmProjectId(tmProject.getId());
  220. caseService.save(caseEntity);
  221. }
  222. @Override
  223. public void updateProject(TmProject tmProject) {
  224. if(StringUtils.isBlank(tmProject.getId())){
  225. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  226. }
  227. if(tmProject.getIsDelete() == 1){
  228. CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
  229. if(caseEntity == null){
  230. throw new BusinessException(ResultCode.PROJECT_CASE_NOT_EXITS);
  231. }
  232. caseService.delete(caseEntity.getCaseId());
  233. tmAttachmentService.deleteByProjectId(tmProject.getId());
  234. this.removeById(tmProject.getId());
  235. return;
  236. }
  237. TmUser tmUser = tmUserService.getLoginUser();
  238. tmProject.setEditorId(tmUser.getId());
  239. tmProject.setEditorName(tmUser.getNickName());
  240. this.updateById(tmProject);
  241. }
  242. @Override
  243. public void setOrUnTeach(CommonDto commonDto) {
  244. if(null == commonDto || StringUtils.isBlank(commonDto.getIds())){
  245. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  246. }
  247. List<String> idList = Arrays.asList(commonDto.getIds().split(","));
  248. if(CollectionUtils.isEmpty(idList)){
  249. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  250. }
  251. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  252. wrapper.in(TmProject::getId,idList);
  253. wrapper.set(TmProject::getIsTeached,commonDto.getStatus());
  254. this.update(wrapper);
  255. }
  256. @Override
  257. public TmProject getByProjectSn(String projectSn) {
  258. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  259. wrapper.eq(TmProject::getProjectSn,projectSn);
  260. return this.getOne(wrapper);
  261. }
  262. @Override
  263. public HashMap<String, CaseEntity> getCaseMap(List<String> ids) {
  264. HashMap<String, CaseEntity> map = new HashMap<>();
  265. if(ids.size() >0){
  266. LambdaQueryWrapper<CaseEntity> wrapper = new LambdaQueryWrapper<>();
  267. wrapper.in(CaseEntity::getTmProjectId,ids);
  268. List<CaseEntity> list = caseService.list(wrapper);
  269. if(list !=null && list.size() >0){
  270. list.forEach(entity -> map.put(entity.getTmProjectId(),entity));
  271. }
  272. }
  273. return map;
  274. }
  275. @Override
  276. public void updateIdenTityStatus(String tmProjectId, Integer status) {
  277. if(StringUtils.isNotBlank(tmProjectId)){
  278. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  279. wrapper.eq(TmProject::getId,tmProjectId);
  280. wrapper.set(TmProject::getStatus,status);
  281. this.update(wrapper);
  282. }
  283. }
  284. }