WorkCalendarRuleHandler.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.persagy.calendar.handle;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5. import org.springframework.beans.BeanUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import com.persagy.calendar.pojo.dto.WorkCalendar;
  10. import com.persagy.calendar.pojo.dto.WorkCalendarRule;
  11. import com.persagy.calendar.pojo.vo.CustomCalendarVO;
  12. import com.persagy.calendar.pojo.vo.WorkCalendarRuleCreateVO;
  13. import com.persagy.calendar.pojo.vo.WorkCalendarRuleUpdateVO;
  14. import com.persagy.calendar.service.IWorkCalendarRuleService;
  15. import com.persagy.calendar.service.IWorkCalendarService;
  16. import com.persagy.common.enums.ResponseCode;
  17. import com.persagy.common.exception.BusinessException;
  18. import cn.hutool.core.collection.CollectionUtil;
  19. import cn.hutool.core.util.BooleanUtil;
  20. /**
  21. * @version 定义规则信息
  22. * @description
  23. * @company persagy
  24. * @author zhangqiankun
  25. * @since 2020年10月3日: 下午1:03:19
  26. */
  27. @Component
  28. public class WorkCalendarRuleHandler {
  29. @Autowired
  30. private IWorkCalendarService workCalendarService;
  31. @Autowired
  32. private IWorkCalendarRuleService workCalendarRuleService;
  33. /**
  34. * 批量创建规则信息
  35. */
  36. @Transactional
  37. public void batchCreateCalednarRule(CustomCalendarVO<WorkCalendarRuleCreateVO> createVO) {
  38. boolean result = false;
  39. WorkCalendarRule workCalendarRule = null;
  40. List<WorkCalendarRuleCreateVO> batchInfo = createVO.getBatchInfo();
  41. Set<String> calendarIds = new HashSet<String>();
  42. for (WorkCalendarRuleCreateVO rule : batchInfo) {
  43. // 优先判断工作历信息是否存在
  44. if (!calendarIds.contains(rule.getCalendarId())) {
  45. WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), createVO.getProjectId());
  46. if (workCalendar == null) {
  47. throw new BusinessException("工作历主信息不存在");
  48. }
  49. }
  50. workCalendarRule = new WorkCalendarRule();
  51. BeanUtils.copyProperties(rule, workCalendarRule);
  52. workCalendarRule.setId(null);
  53. workCalendarRule.setGroupCode(createVO.getGroupCode());
  54. workCalendarRule.setProjectId(createVO.getProjectId());
  55. workCalendarRule.setUpdateUser(createVO.getUserId());
  56. // 为兼容IBMS,紧急修改内容: shop 类型的工作历规则,一个项目的dictType+dictCode+value已存在的话,直接更新
  57. if ("shop".equals(rule.getDictType()) && CollectionUtil.isNotEmpty(workCalendarRule.getValue())) {
  58. String ruleId = this.workCalendarRuleService.getCalendarRule(workCalendarRule);
  59. if (ruleId != null ) {
  60. // 直接更新
  61. workCalendarRule.setId(ruleId);
  62. result = workCalendarRuleService.updateWorkCalendarRule(workCalendarRule);
  63. if (!result) {
  64. throw new BusinessException("工作历规则信息保存失败");
  65. }
  66. calendarIds.add(rule.getCalendarId());
  67. continue;
  68. }
  69. }
  70. // 是否需要删除旧规则
  71. if (BooleanUtil.isTrue(createVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) {
  72. workCalendarRuleService.removeWorkCalendarRule(null, rule.getCalendarId(), rule.getDictType());
  73. //throw new BusinessException("工作历ID为:" + rule.getCalendarId() + "的工作历,删除规则信息失败");
  74. }
  75. result = this.workCalendarRuleService.createWorkCalendarRule(workCalendarRule);
  76. if (!result) {
  77. throw new BusinessException("工作历规则信息创建失败");
  78. }
  79. calendarIds.add(rule.getCalendarId());
  80. }
  81. calendarIds.clear();
  82. }
  83. /**
  84. * 批量更新规则信息
  85. */
  86. @Transactional
  87. public void batchUpdateCalednarRule(CustomCalendarVO<WorkCalendarRuleUpdateVO> updateVO) {
  88. boolean result = false;
  89. WorkCalendarRule workCalendarRule = null;
  90. List<WorkCalendarRuleUpdateVO> batchInfo = updateVO.getBatchInfo();
  91. Set<String> calendarIds = new HashSet<String>();
  92. for (WorkCalendarRuleUpdateVO rule : batchInfo) {
  93. // 优先判断工作历信息是否存在
  94. if (!calendarIds.contains(rule.getCalendarId())) {
  95. WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), updateVO.getProjectId());
  96. if (workCalendar == null) {
  97. throw new BusinessException("工作历主信息不存在");
  98. }
  99. }
  100. // 再判断原规则数据是否存在
  101. WorkCalendarRule calendar = workCalendarRuleService.getById(rule.getId(), updateVO.getProjectId());
  102. if (calendar == null) {
  103. throw new BusinessException(ResponseCode.C0320.getDesc());
  104. }
  105. // 是否需要删除旧规则
  106. if (BooleanUtil.isTrue(updateVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) {
  107. workCalendarRuleService.removeWorkCalendarRule(null, rule.getCalendarId(), rule.getDictCode());
  108. //throw new BusinessException("工作历ID为:" + rule.getCalendarId() + "的工作历,删除规则信息失败");
  109. }
  110. workCalendarRule = new WorkCalendarRule();
  111. BeanUtils.copyProperties(rule, workCalendarRule);
  112. workCalendarRule.setGroupCode(updateVO.getGroupCode());
  113. workCalendarRule.setProjectId(updateVO.getProjectId());
  114. workCalendarRule.setUpdateUser(updateVO.getUserId());
  115. result = workCalendarRuleService.updateWorkCalendarRule(workCalendarRule);
  116. if (!result) {
  117. throw new BusinessException("工作历规则信息更新失败");
  118. }
  119. calendarIds.add(rule.getCalendarId());
  120. }
  121. calendarIds.clear();
  122. }
  123. }