TmProjectServiceImpl.java 18 KB

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