TmProjectServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. @Override
  63. public Object pageList(ProjectRequestDto tmProject) {
  64. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  65. //教学场景
  66. List<String> deptIds;
  67. if(tmProject.getQueryType() != null && tmProject.getQueryType() == 2){
  68. wrapper.eq(TmProject::getIsTeached,1);
  69. deptIds = tmDepartmentService.getByZdDeptIds();
  70. }else {
  71. String share = SaHolder.getRequest().getHeader("share");
  72. if(StringUtils.isNotBlank(share) && "1".equals(share)){ //分享请求头
  73. deptIds = tmDepartmentService.list().stream().map(TmDepartment::getId).collect(Collectors.toList());
  74. wrapper.eq(TmProject::getMapShow,false);
  75. wrapper.isNotNull(TmProject::getLatlng);
  76. }else {
  77. deptIds = tmDepartmentService.getDeptIds();
  78. }
  79. }
  80. if(deptIds.size() <=0){
  81. throw new BusinessException(ResultCode.NOT_DEPT);
  82. }
  83. //承办单位
  84. if(StringUtils.isNotBlank(tmProject.getOrganizerDeptName())){
  85. List<TmDepartment> list = tmDepartmentService.getLikeName(tmProject.getOrganizerDeptName());
  86. if(list.size() <=0){
  87. return PageInfo.PageInfo(new Page<>(tmProject.getPageNum(),tmProject.getPageSize()));
  88. }
  89. List<String> collect = list.stream().map(TmDepartment::getId).collect(Collectors.toList());
  90. deptIds = deptIds.stream().filter(collect::contains).collect(Collectors.toList());
  91. }
  92. List<String> deptIds2 = tmDepartmentService.getSonByDeptIdAndDeptIds(deptIds, tmProject.getDeptId());
  93. wrapper.in(TmProject::getDeptId,deptIds2);
  94. //起火项目对象
  95. if(StringUtils.isNotBlank(tmProject.getProjectSn())){
  96. wrapper.like(TmProject::getProjectSn , tmProject.getProjectSn());
  97. }
  98. //起火对象
  99. if(StringUtils.isNotBlank(tmProject.getProjectName())){
  100. wrapper.like(TmProject::getProjectName , tmProject.getProjectName());
  101. }
  102. //起火地址
  103. if(StringUtils.isNotBlank(tmProject.getProjectAddress())){
  104. wrapper.like(TmProject::getProjectAddress , tmProject.getProjectAddress());
  105. }
  106. //起火场所
  107. if(StringUtils.isNotBlank(tmProject.getProjectSiteCode())){
  108. if(!StringUtils.equals("0" , tmProject.getProjectSiteCode())){
  109. wrapper.likeRight(TmProject::getProjectSiteCode , tmProject.getProjectSiteCode());
  110. }
  111. }
  112. //起火对象
  113. if(StringUtils.isNotBlank(tmProject.getOrganizerUsers())){
  114. wrapper.like(TmProject::getOrganizerUsers , tmProject.getOrganizerUsers());
  115. }
  116. //事故日期
  117. if(StringUtils.isNotBlank(tmProject.getAccidentDate())){
  118. wrapper.ge(TmProject::getAccidentDate,tmProject.getAccidentDateStart());
  119. wrapper.le(TmProject::getAccidentDate,tmProject.getAccidentDateEnd());
  120. }
  121. //起火原因
  122. if(StringUtils.isNotBlank(tmProject.getFireReason())){
  123. wrapper.like(TmProject::getFireReason , tmProject.getFireReason());
  124. }
  125. //是否是教学项目列表
  126. if(null != tmProject.getIsTeached()){
  127. wrapper.eq(TmProject::getIsTeached , tmProject.getIsTeached());
  128. }
  129. //项目状态
  130. if(StringUtils.isNotBlank(tmProject.getStatus())){
  131. wrapper.eq(TmProject::getStatus , tmProject.getStatus());
  132. }
  133. wrapper.orderByDesc(TmProject::getCreateTime);
  134. Page<TmProject> page = this.page(new Page<>(tmProject.getPageNum(), tmProject.getPageSize()), wrapper);
  135. List<String> ids = page.getRecords().stream().map(TmProject::getId).collect(Collectors.toList());
  136. Set<String> deptDbIds = page.getRecords().stream().map(TmProject::getDeptId).collect(Collectors.toSet());
  137. HashMap<String, TmDepartment> mapByDeptIds = tmDepartmentService.getMapByDeptIds(deptDbIds);
  138. HashMap<String ,CaseEntity> map = this.getCaseMap(ids);
  139. for (TmProject record : page.getRecords()) {
  140. CaseEntity caseEntity = map.get(record.getId());
  141. if(caseEntity != null){
  142. record.setCaseId(caseEntity.getCaseId());
  143. }
  144. TmDepartment tmDepartment = mapByDeptIds.get(record.getDeptId());
  145. if(tmDepartment != null){
  146. record.setOrganizerDeptName(tmDepartment.getName());
  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. tmProject.setCaseNewName(setCaseNewName(tmProject));
  227. this.save(tmProject);
  228. CaseEntity caseEntity = new CaseEntity();
  229. caseEntity.setCaseTitle(tmProject.getProjectName());
  230. caseEntity.setUserName(tmUser.getUserName());
  231. caseEntity.setType(1);
  232. caseEntity.setTmProjectId(tmProject.getId());
  233. caseEntity.setMapUrl(tmProject.getMapUrl());
  234. caseEntity.setLatAndLong(tmProject.getLatAndLong());
  235. caseService.save(caseEntity);
  236. }
  237. private String setCaseNewName(TmProject tmProject) {
  238. String name = "";
  239. if(StringUtils.isNotBlank(tmProject.getProjectAddress())){
  240. name += tmProject.getProjectAddress();
  241. }
  242. if(tmProject.getAccidentDate() != null){
  243. name += "("+DateUtil.format(tmProject.getAccidentDate(),"yyyy-MM-dd") +")";
  244. }
  245. if(StringUtils.isNotBlank(tmProject.getProjectName())){
  246. name += tmProject.getProjectName();
  247. }
  248. return name;
  249. }
  250. @Override
  251. public void updateProject(TmProject tmProject) {
  252. if(StringUtils.isBlank(tmProject.getId())){
  253. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  254. }
  255. if(tmProject.getIsDelete() == 1){
  256. CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
  257. if(caseEntity == null){
  258. throw new BusinessException(ResultCode.PROJECT_CASE_NOT_EXITS);
  259. }
  260. caseService.delete(caseEntity.getCaseId());
  261. tmAttachmentService.deleteByProjectId(tmProject.getId());
  262. this.removeById(tmProject.getId());
  263. return;
  264. }
  265. TmProject byProjectSn = this.getByProjectSn(tmProject.getProjectSn());
  266. if(byProjectSn != null && !byProjectSn.getId().equals(tmProject.getId())){
  267. throw new BusinessException(ResultCode.PROJECT_EXITS);
  268. }
  269. TmUser tmUser = tmUserService.getLoginUser();
  270. tmProject.setEditorId(tmUser.getId());
  271. tmProject.setEditorName(tmUser.getNickName());
  272. tmProject.setUpdateTime(null);
  273. tmProject.setCaseNewName(setCaseNewName(tmProject));
  274. this.updateById(tmProject);
  275. CaseEntity caseEntity = caseService.getByTmProjectId(tmProject.getId());
  276. caseEntity.setCaseTitle(tmProject.getProjectName());
  277. caseEntity.setMapUrl(tmProject.getMapUrl());
  278. caseEntity.setLatAndLong(tmProject.getLatAndLong());
  279. caseService.updateById(caseEntity);
  280. }
  281. @Override
  282. public void setOrUnTeach(CommonDto commonDto) {
  283. if(null == commonDto || StringUtils.isBlank(commonDto.getIds())){
  284. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  285. }
  286. List<String> idList = Arrays.asList(commonDto.getIds().split(","));
  287. if(CollectionUtils.isEmpty(idList)){
  288. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  289. }
  290. if(commonDto.getStatus() != 1){ //撤销教学项目
  291. List<TmProject> tmProjects = this.listByIds(idList);
  292. Set<String> projectDeptIds = tmProjects.stream().map(TmProject::getDeptId).collect(Collectors.toSet());
  293. TmUser loginUser = tmUserService.getLoginUser();
  294. TmRole tmRole = tmRoleService.getByUserId(loginUser.getId());
  295. if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_DEPT.getKey())){
  296. List<TmDepartment> sonByDeptId = tmDepartmentService.getSonByDeptId(loginUser.getDeptId());
  297. List<String> deptIds = sonByDeptId.stream().map(TmDepartment::getId).collect(Collectors.toList());
  298. deptIds.add(loginUser.getDeptId());
  299. for (String projectDeptId : projectDeptIds) {
  300. if(!deptIds.contains(projectDeptId)){
  301. throw new BusinessException(ResultCode.NOT_PER);
  302. }
  303. }
  304. }
  305. if(tmRole.getRoleKey().equals(RoleKeyEnum.ADMIN_ORDINARY.getKey())){
  306. for (String projectDeptId : projectDeptIds) {
  307. if(!projectDeptId.equals(loginUser.getDeptId())){
  308. throw new BusinessException(ResultCode.NOT_PER);
  309. }
  310. }
  311. }
  312. }
  313. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  314. wrapper.in(TmProject::getId,idList);
  315. wrapper.set(TmProject::getIsTeached,commonDto.getStatus());
  316. this.update(wrapper);
  317. }
  318. @Override
  319. public TmProject getByProjectSn(String projectSn) {
  320. LambdaQueryWrapper<TmProject> wrapper = new LambdaQueryWrapper<>();
  321. wrapper.eq(TmProject::getProjectSn,projectSn);
  322. return this.getOne(wrapper);
  323. }
  324. @Override
  325. public HashMap<String, CaseEntity> getCaseMap(List<String> ids) {
  326. HashMap<String, CaseEntity> map = new HashMap<>();
  327. if(ids.size() >0){
  328. LambdaQueryWrapper<CaseEntity> wrapper = new LambdaQueryWrapper<>();
  329. wrapper.in(CaseEntity::getTmProjectId,ids);
  330. List<CaseEntity> list = caseService.list(wrapper);
  331. if(list !=null && list.size() >0){
  332. list.forEach(entity -> map.put(entity.getTmProjectId(),entity));
  333. }
  334. }
  335. return map;
  336. }
  337. @Override
  338. public void updateIdenTityStatus(String tmProjectId, Integer status) {
  339. if(StringUtils.isNotBlank(tmProjectId)){
  340. LambdaUpdateWrapper<TmProject> wrapper = new LambdaUpdateWrapper<>();
  341. wrapper.eq(TmProject::getId,tmProjectId);
  342. wrapper.set(TmProject::getStatus,status);
  343. this.update(wrapper);
  344. }
  345. }
  346. @Override
  347. public HashMap<String, TmProject> getMapByIds(Set<String> tmProIds) {
  348. HashMap<String, TmProject> map = new HashMap<>();
  349. if(tmProIds.size() >0){
  350. List<TmProject> tmProjects = this.listByIds(tmProIds);
  351. for (TmProject tmProject : tmProjects) {
  352. map.put(tmProject.getId(),tmProject);
  353. }
  354. }
  355. return map;
  356. }
  357. @Override
  358. public List<DataGroupVo> groupByDeptId(DataParam param) {
  359. return getBaseMapper().groupByDeptId(param.getStartTime(),param.getEndTime());
  360. }
  361. @Override
  362. public List<DataGroupVo> groupByMonth(DataParam param,List<String> deptIds) {
  363. return getBaseMapper().groupByMonth(param.getStartTime(),param.getEndTime(),deptIds);
  364. }
  365. @Override
  366. public List<DataGroupVo> groupByPlace(DataParam param,List<String> deptIds) {
  367. return getBaseMapper().groupByPlace(param.getStartTime(),param.getEndTime(),deptIds);
  368. }
  369. @Override
  370. public List<DataGroupVo> groupByReason(DataParam param,List<String> deptIds) {
  371. return getBaseMapper().groupByReason(param.getStartTime(),param.getEndTime(),deptIds);
  372. }
  373. }