TmProjectServiceImpl.java 12 KB

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