TmProjectServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.RandomUtil;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.fdkankan.fusion.common.PageInfo;
  10. import com.fdkankan.fusion.common.ResultCode;
  11. import com.fdkankan.fusion.common.enums.IdPreEnum;
  12. import com.fdkankan.fusion.common.enums.RoleKeyEnum;
  13. import com.fdkankan.fusion.common.util.IdUtils;
  14. import com.fdkankan.fusion.common.util.PatternEnum;
  15. import com.fdkankan.fusion.common.util.RedisKeyUtil;
  16. import com.fdkankan.fusion.entity.*;
  17. import com.fdkankan.fusion.exception.BusinessException;
  18. import com.fdkankan.fusion.mapper.ITmProjectMapper;
  19. import com.fdkankan.fusion.request.*;
  20. import com.fdkankan.fusion.response.DataGroupVo;
  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. @Autowired
  57. ITmRoleService tmRoleService;
  58. @Autowired
  59. ITmUserRoleService tmUserRoleService;
  60. @Autowired
  61. ICaseSettingsService caseSettingsService;
  62. @Override
  63. public Object pageList(ProjectRequestDto tmProject) {
  64. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  65. //教学场景
  66. List<String> deptIds = new ArrayList<>();
  67. if(tmProject.getQueryType() != null && tmProject.getQueryType() == 2){
  68. wrapper.eq(TmProject::getIsTeached,1);
  69. deptIds = tmDepartmentService.getByZdDeptIds();
  70. }
  71. if(deptIds.size() <=0){
  72. throw new BusinessException(ResultCode.NOT_DEPT);
  73. }
  74. //承办单位
  75. if(StringUtils.isNotBlank(tmProject.getOrganizerDeptName())){
  76. List<TmDepartment> list = tmDepartmentService.getLikeName(tmProject.getOrganizerDeptName());
  77. if(list.size() <=0){
  78. return PageInfo.PageInfo(new Page<>(tmProject.getPageNum(),tmProject.getPageSize()));
  79. }
  80. List<String> collect = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
  81. deptIds = deptIds.stream().filter(collect::contains).collect(Collectors.toList());
  82. }
  83. List<String> deptIds2 = tmDepartmentService.getSonByDeptIdAndDeptIds(deptIds, tmProject.getDeptId());
  84. wrapper.in(TmProject::getDeptId,deptIds2);
  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. //事故日期
  108. if(StringUtils.isNotBlank(tmProject.getAccidentDate())){
  109. wrapper.ge(TmProject::getAccidentDate,tmProject.getAccidentDateStart());
  110. wrapper.le(TmProject::getAccidentDate,tmProject.getAccidentDateEnd());
  111. }
  112. //起火原因
  113. if(StringUtils.isNotBlank(tmProject.getFireReason())){
  114. wrapper.like(TmProject::getFireReason , tmProject.getFireReason());
  115. }
  116. //是否是教学项目列表
  117. if(null != tmProject.getIsTeached()){
  118. wrapper.eq(TmProject::getIsTeached , tmProject.getIsTeached());
  119. }
  120. //项目状态
  121. if(StringUtils.isNotBlank(tmProject.getStatus())){
  122. wrapper.eq(TmProject::getStatus , tmProject.getStatus());
  123. }
  124. wrapper.orderByDesc(TmProject::getCreateTime);
  125. Page<TmProject> page = this.page(new Page<>(tmProject.getPageNum(), tmProject.getPageSize()), wrapper);
  126. List<String> ids = page.getRecords().stream().map(TmProject::getId).collect(Collectors.toList());
  127. Set<String> deptDbIds = page.getRecords().stream().map(TmProject::getDeptId).collect(Collectors.toSet());
  128. HashMap<String, TmDepartment> mapByDeptIds = tmDepartmentService.getMapByDeptIds(deptDbIds);
  129. HashMap<String ,CaseEntity> map = this.getCaseMap(ids);
  130. for (TmProject record : page.getRecords()) {
  131. CaseEntity caseEntity = map.get(record.getId());
  132. if(caseEntity != null){
  133. record.setCaseId(caseEntity.getCaseId());
  134. record.setLatAndLong(caseEntity.getLatAndLong());
  135. record.setMapUrl(caseEntity.getMapUrl());
  136. }
  137. TmDepartment tmDepartment = mapByDeptIds.get(record.getDeptId());
  138. if(tmDepartment != null){
  139. record.setOrganizerDeptName(tmDepartment.getName());
  140. }
  141. List<CaseSettings> caseSettings = caseSettingsService.getByFusionId(record.getCaseId());
  142. if(!caseSettings.isEmpty()){
  143. CaseSettings caseSettings1 = caseSettings.get(0);
  144. if(caseSettings1 != null){
  145. record.setCover(caseSettings1.getCover());
  146. }
  147. }
  148. }
  149. return PageInfo.PageInfo(page);
  150. }
  151. @Override
  152. public Object getDetailWithoutAuth(ProjectRequest param) {
  153. if (ObjectUtil.isNotNull(param.getCaseId()) && StringUtils.isBlank(param.getRandCode())) {
  154. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  155. }
  156. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,param.getCaseId());
  157. if(!redisUtil.hasKey(redisKey)){
  158. throw new BusinessException(ResultCode.PROJECT_KEY_NOT_EXITS);
  159. }
  160. if (StringUtils.isBlank(param.getRandCode())) {
  161. throw new BusinessException(ResultCode.PROJECT_PASSWORD_NOT_EXITS);
  162. }
  163. String redisRandCode = redisUtil.get(redisKey);
  164. if(!StringUtils.equals(redisRandCode , param.getRandCode())){
  165. throw new BusinessException(ResultCode.PROJECT_PASSWORD_ERROR);
  166. }
  167. return true;
  168. }
  169. @Override
  170. public Object getRandCode(String caseId) {
  171. if(StringUtils.isBlank(caseId) ){
  172. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  173. }
  174. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,caseId);
  175. if(redisUtil.hasKey(redisKey)){
  176. redisUtil.expire(redisKey,RedisKeyUtil.projectRandCodeKeyTime);
  177. return redisUtil.get(redisKey);
  178. }
  179. CaseEntity caseEntity = caseService.getById(caseId);
  180. if(caseEntity ==null){
  181. throw new BusinessException(ResultCode.CASE_NOT_EXITS);
  182. }
  183. String code = RandomUtil.randomString(4);
  184. redisUtil.set(redisKey, code,RedisKeyUtil.projectRandCodeKeyTime);
  185. return code;
  186. }
  187. @Override
  188. public void updateRandomCode(ProjectRandCodeDto projectRandCodeDto) {
  189. if(StringUtils.isBlank(projectRandCodeDto.getCaseId()) || StringUtils.isBlank(projectRandCodeDto.getRandCode())){
  190. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  191. }
  192. if(!projectRandCodeDto.getRandCode().matches(PatternEnum.RAND_CODE_PATTERN)){
  193. throw new BusinessException(ResultCode.RAND_ERROR);
  194. }
  195. String redisKey = String.format(RedisKeyUtil.RAND_CODE_KEY,projectRandCodeDto.getCaseId());
  196. if(!redisUtil.hasKey(redisKey)){
  197. throw new BusinessException(ResultCode.RAND_NOT_EXIST);
  198. }
  199. redisUtil.set(redisKey,projectRandCodeDto.getRandCode(),RedisKeyUtil.projectRandCodeKeyTime);
  200. }
  201. @Override
  202. public void addNewProject(TmProject tmProject) {
  203. if(StringUtils.isBlank(tmProject.getProjectSn())){
  204. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  205. }
  206. TmProject project = this.getByProjectSn(tmProject.getProjectSn());
  207. if(project != null){
  208. throw new BusinessException(ResultCode.PROJECT_EXITS);
  209. }
  210. TmUser tmUser = tmUserService.getLoginUser();
  211. if(tmUser == null || StringUtils.isBlank(tmUser.getDeptId())){
  212. throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
  213. }
  214. TmDepartment tmDepartment = tmDepartmentService.getById(tmUser.getDeptId());
  215. if(tmDepartment == null){
  216. throw new BusinessException(ResultCode.DEPT_NOT_EXITS);
  217. }
  218. tmProject.setId(IdUtils.genId(IdPreEnum.PROJECT_PRE.getPre()));
  219. tmProject.setDeptId(tmUser.getDeptId());
  220. tmProject.setCreatorId(tmUser.getId());
  221. tmProject.setCreatorName(tmUser.getNickName());
  222. tmProject.setCreatorDeptId(tmUser.getDeptId());
  223. tmProject.setEditorId(tmUser.getId());
  224. tmProject.setEditorName(tmUser.getNickName());
  225. tmProject.setOrganizerDeptName(tmDepartment.getName());
  226. this.save(tmProject);
  227. CaseEntity caseEntity = new CaseEntity();
  228. caseEntity.setCaseTitle(tmProject.getProjectName());
  229. caseEntity.setUserName(tmUser.getUserName());
  230. caseEntity.setType(1);
  231. caseEntity.setTmProjectId(tmProject.getId());
  232. caseEntity.setMapUrl(tmProject.getMapUrl());
  233. caseEntity.setLatAndLong(tmProject.getLatAndLong());
  234. caseService.save(caseEntity);
  235. }
  236. @Override
  237. public void updateProject(TmProject tmProject) {
  238. if(StringUtils.isBlank(tmProject.getId())){
  239. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  240. }
  241. if(tmProject.getIsDelete() == 1){
  242. CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
  243. if(caseEntity == null){
  244. throw new BusinessException(ResultCode.PROJECT_CASE_NOT_EXITS);
  245. }
  246. caseService.delete(caseEntity.getCaseId());
  247. tmAttachmentService.deleteByProjectId(tmProject.getId());
  248. this.removeById(tmProject.getId());
  249. return;
  250. }
  251. TmProject byProjectSn = this.getByProjectSn(tmProject.getProjectSn());
  252. if(byProjectSn != null && !byProjectSn.getId().equals(tmProject.getId())){
  253. throw new BusinessException(ResultCode.PROJECT_EXITS);
  254. }
  255. TmUser tmUser = tmUserService.getLoginUser();
  256. tmProject.setEditorId(tmUser.getId());
  257. tmProject.setEditorName(tmUser.getNickName());
  258. tmProject.setUpdateTime(null);
  259. this.updateById(tmProject);
  260. CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
  261. caseEntity.setCaseTitle(tmProject.getProjectName());
  262. caseEntity.setTmProjectId(tmProject.getId());
  263. caseEntity.setMapUrl(tmProject.getMapUrl());
  264. caseEntity.setLatAndLong(tmProject.getLatAndLong());
  265. caseService.updateById(caseEntity);
  266. }
  267. @Override
  268. public void setOrUnTeach(CommonDto commonDto) {
  269. if(null == commonDto || StringUtils.isBlank(commonDto.getIds())){
  270. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  271. }
  272. List<String> idList = Arrays.asList(commonDto.getIds().split(","));
  273. if(CollectionUtils.isEmpty(idList)){
  274. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  275. }
  276. if(commonDto.getStatus() != 1){ //撤销教学项目
  277. List<TmProject> tmProjects = this.listByIds(idList);
  278. Set<String> projectDeptIds = tmProjects.stream().map(TmProject::getDeptId).collect(Collectors.toSet());
  279. TmUser loginUser = tmUserService.getLoginUser();
  280. TmRole tmRole = tmRoleService.getByUserId(loginUser.getId());
  281. if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_DEPT.getKey())){
  282. List<TmDepartment> sonByDeptId = tmDepartmentService.getSonByDeptId(loginUser.getDeptId());
  283. List<String> deptIds = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
  284. deptIds.add(loginUser.getDeptId());
  285. for (String projectDeptId : projectDeptIds) {
  286. if(!deptIds.contains(projectDeptId)){
  287. throw new BusinessException(ResultCode.NOT_PER);
  288. }
  289. }
  290. }
  291. if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_ORDINARY.getKey())){
  292. for (String projectDeptId : projectDeptIds) {
  293. if(!projectDeptId.equals(loginUser.getDeptId())){
  294. throw new BusinessException(ResultCode.NOT_PER);
  295. }
  296. }
  297. }
  298. }
  299. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  300. wrapper.in(TmProject::getId,idList);
  301. wrapper.set(TmProject::getIsTeached,commonDto.getStatus());
  302. this.update(wrapper);
  303. }
  304. @Override
  305. public TmProject getByProjectSn(String projectSn) {
  306. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  307. wrapper.eq(TmProject::getProjectSn,projectSn);
  308. wrapper.eq(TmProject::getIsDelete,0);
  309. List<TmProject> list = this.list(wrapper);
  310. if(list == null || list.isEmpty()){
  311. return null;
  312. }
  313. return list.get(0);
  314. }
  315. @Override
  316. public List<TmProject> getLikeByProjectSn(String projectSn) {
  317. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  318. wrapper.like(TmProject::getProjectSn,projectSn);
  319. wrapper.eq(TmProject::getIsDelete,0);
  320. List<TmProject> list = this.list(wrapper);
  321. return list;
  322. }
  323. @Override
  324. public HashMap<String, CaseEntity> getCaseMap(List<String> ids) {
  325. HashMap<String, CaseEntity> map = new HashMap<>();
  326. if(ids.size() >0){
  327. LambdaQueryWrapper<CaseEntity> wrapper = new LambdaQueryWrapper<>();
  328. wrapper.in(CaseEntity::getTmProjectId,ids);
  329. List<CaseEntity> list = caseService.list(wrapper);
  330. if(list !=null && list.size() >0){
  331. list.forEach(entity -> map.put(entity.getTmProjectId(),entity));
  332. }
  333. }
  334. return map;
  335. }
  336. @Override
  337. public void updateIdenTityStatus(String tmProjectId, Integer status) {
  338. if(StringUtils.isNotBlank(tmProjectId)){
  339. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  340. wrapper.eq(TmProject::getId,tmProjectId);
  341. wrapper.set(TmProject::getStatus,status);
  342. this.update(wrapper);
  343. }
  344. }
  345. @Override
  346. public HashMap<String, TmProject> getMapByIds(Set<String> tmProIds) {
  347. HashMap<String, TmProject> map = new HashMap<>();
  348. if(tmProIds.size() >0){
  349. List<TmProject> tmProjects = this.listByIds(tmProIds);
  350. for (TmProject tmProject : tmProjects) {
  351. map.put(tmProject.getId(),tmProject);
  352. }
  353. }
  354. return map;
  355. }
  356. @Override
  357. public List<DataGroupVo> groupByDeptId(DataParam param) {
  358. return getBaseMapper().groupByDeptId(param.getStartTime(),param.getEndTime());
  359. }
  360. @Override
  361. public List<DataGroupVo> groupByMonth(DataParam param,List<String> deptIds) {
  362. return getBaseMapper().groupByMonth(param.getStartTime(),param.getEndTime(),deptIds);
  363. }
  364. @Override
  365. public List<DataGroupVo> groupByPlace(DataParam param,List<String> deptIds) {
  366. return getBaseMapper().groupByPlace(param.getStartTime(),param.getEndTime(),deptIds);
  367. }
  368. @Override
  369. public List<DataGroupVo> groupByReason(DataParam param,List<String> deptIds) {
  370. return getBaseMapper().groupByReason(param.getStartTime(),param.getEndTime(),deptIds);
  371. }
  372. }