TmProjectServiceImpl.java 15 KB

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