WorkCalendarLabelHandler.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package com.persagy.calendar.handle;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Set;
  6. import org.springframework.beans.BeanUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  11. import com.google.common.collect.Lists;
  12. import com.persagy.calendar.constant.WorkCalendarConstant;
  13. import com.persagy.calendar.pojo.dto.WorkCalendar;
  14. import com.persagy.calendar.pojo.dto.WorkCalendarDict;
  15. import com.persagy.calendar.pojo.dto.WorkCalendarLabel;
  16. import com.persagy.calendar.pojo.dto.WorkCalendarLabel.Builder;
  17. import com.persagy.calendar.pojo.dto.WorkCalendarObject;
  18. import com.persagy.calendar.pojo.vo.WorkCalendarLabelQueryVO;
  19. import com.persagy.calendar.pojo.vo.WorkCalendarLabelUpdateVO;
  20. import com.persagy.calendar.pojo.vo.WorkCalendarMoreDateCreateVO;
  21. import com.persagy.calendar.pojo.vo.WorkLabelBatchUpdateVO;
  22. import com.persagy.calendar.service.IWorkCalendarDictService;
  23. import com.persagy.calendar.service.IWorkCalendarLabelService;
  24. import com.persagy.calendar.service.IWorkCalendarObjectService;
  25. import com.persagy.calendar.service.IWorkCalendarService;
  26. import com.persagy.common.enums.ResponseCode;
  27. import com.persagy.common.exception.BusinessException;
  28. import com.persagy.common.utils.DateUtil;
  29. import com.persagy.common.utils.IdGenerator;
  30. import com.persagy.common.utils.ResponseResult;
  31. import com.persagy.common.utils.ResponseUtil;
  32. import com.persagy.common.utils.StringUtil;
  33. import cn.hutool.core.collection.CollectionUtil;
  34. import cn.hutool.core.date.DateField;
  35. import cn.hutool.core.date.DateTime;
  36. import cn.hutool.core.date.DateUnit;
  37. import cn.hutool.core.util.BooleanUtil;
  38. /**
  39. * @version
  40. * @description
  41. * @company persagy
  42. * @author zhangqiankun
  43. * @since 2021年3月3日: 下午6:14:49
  44. */
  45. @Component
  46. public class WorkCalendarLabelHandler {
  47. public static final String LABEL_COLUMN = "GROUP_CODE, PROJECT_ID, CALENDAR_ID, DICT_TYPE, DICT_CODE, DIY_LABEL, DIY_VALUE, LABEL_DATE, CONVERT(SUBSTR(LABEL_DATE, 5, 2), SIGNED) AS labelMonth, CREATE_TIME, UPDATE_TIME, UPDATE_USER";
  48. @Autowired
  49. private IWorkCalendarService workCalendarService;
  50. @Autowired
  51. private IWorkCalendarLabelService workCalendarLabelService;
  52. @Autowired
  53. private IWorkCalendarDictService workCalendarDictService;
  54. @Autowired
  55. private WorkCalendarDateHandler workCalendarDateHandler;
  56. @Autowired
  57. private IWorkCalendarObjectService workCalendarObjectService;
  58. /**
  59. * 日期标签属性列表查询
  60. *
  61. * @param queryVO
  62. * @return
  63. */
  64. public List<WorkCalendarLabel> queryCalendarLabelList(WorkCalendarLabelQueryVO queryVO) {
  65. // 获取字典类型的级别
  66. int dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(queryVO.getGroupCode(), queryVO.getDictType());
  67. // 如果条件为对象ID,且属于工作历级别的类型,反查出工作历ID,再进行查询
  68. if (StringUtil.isNotBlank(queryVO.getObjId()) && dictTypeLevel == 2) {
  69. //查询出此对象对应的工作历ID和项目ID
  70. QueryWrapper<WorkCalendarObject> objectWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
  71. .groupCodeEq(queryVO.getGroupCode()).objectIdEq(queryVO.getObjId())
  72. .projectIdEq(queryVO.getProjectId()).builderQueryWrapper();
  73. WorkCalendarObject calendarObject = this.workCalendarObjectService.getOne(objectWrapper);
  74. if (calendarObject == null) {
  75. return null;
  76. }
  77. queryVO.setCalendarId(calendarObject.getCalendarId());
  78. }
  79. QueryWrapper<WorkCalendarLabel> queryWrapper = new WorkCalendarLabel.Builder().createQueryWrapper().groupCodeEq(queryVO.getGroupCode())
  80. .projectIdEq(queryVO.getProjectId(), dictTypeLevel).calendarIdEq(queryVO.getCalendarId(), dictTypeLevel)
  81. .dictTypeEq(queryVO.getDictType()).dictCodeEq(queryVO.getDictCode()).diyLabelEq(queryVO.getDiyLabel())
  82. .labelDateGe(queryVO.getLabelDateStart()).labelDateLe(queryVO.getLabelDateEnd()).builderQueryWrapper();
  83. return this.workCalendarLabelService.list(queryWrapper.select(LABEL_COLUMN).orderByAsc(Lists.newArrayList("LABEL_DATE", "ID")));
  84. }
  85. /**
  86. * 查询标签的有效期
  87. *
  88. * @param queryVO
  89. * @return
  90. */
  91. public List<WorkCalendarLabel> queryCalendarLabelValid(WorkCalendarLabelQueryVO queryVO) {
  92. // 获取字典类型的级别
  93. int dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(queryVO.getGroupCode(), queryVO.getDictType());
  94. if (StringUtil.isNotBlank(queryVO.getObjId()) && dictTypeLevel == 2) {
  95. //查询出此对象对应的工作历ID和项目ID
  96. QueryWrapper<WorkCalendarObject> objectWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
  97. .groupCodeEq(queryVO.getGroupCode()).objectIdEq(queryVO.getObjId())
  98. .projectIdEq(queryVO.getProjectId()).builderQueryWrapper();
  99. WorkCalendarObject calendarObject = this.workCalendarObjectService.getOne(objectWrapper);
  100. if (calendarObject == null) {
  101. return null;
  102. }
  103. queryVO.setCalendarId(calendarObject.getCalendarId());
  104. }
  105. QueryWrapper<WorkCalendarLabel> queryWrapper = new WorkCalendarLabel.Builder().createQueryWrapper().groupCodeEq(queryVO.getGroupCode())
  106. .projectIdEq(queryVO.getProjectId(), dictTypeLevel).calendarIdEq(queryVO.getCalendarId(), dictTypeLevel)
  107. .dictTypeEq(queryVO.getDictType()).dictCodeEq(queryVO.getDictCode()).diyLabelEq(queryVO.getDiyLabel())
  108. .labelDateGe(queryVO.getLabelDateStart()).labelDateLe(queryVO.getLabelDateEnd()).builderQueryWrapper();
  109. queryWrapper.select(queryVO.getGroupBy() + ", MIN(LABEL_DATE) AS labelDateStart, MAX(LABEL_DATE) AS labelDateEnd");
  110. queryWrapper.groupBy(queryVO.getGroupBy());
  111. return this.workCalendarLabelService.list(queryWrapper);
  112. }
  113. /**
  114. * 创建或更新日期标签属性
  115. *
  116. * @param createVO
  117. */
  118. @Transactional
  119. public void updateLabelInfo(WorkCalendarLabelUpdateVO updateVO) {
  120. // 获取字典类型的级别
  121. int dictTypeLevel = this.workCalendarDictService.getDictTypeLevel(updateVO.getGroupCode(), updateVO.getDictType());
  122. boolean result = false;
  123. if (BooleanUtil.isTrue(updateVO.getIsDelete())) {
  124. Builder builder = new WorkCalendarLabel.Builder().createQueryWrapper();
  125. QueryWrapper<WorkCalendarLabel> queryWrapper = builder.groupCodeEq(updateVO.getGroupCode())
  126. .projectIdEq(updateVO.getProjectId(), dictTypeLevel).calendarIdEq(updateVO.getCalendarId(), dictTypeLevel)
  127. .labelDateGe(updateVO.getLabelDateStart()).labelDateLe(updateVO.getLabelDateEnd())
  128. .dictTypeEq(updateVO.getDictType()).diyLabelEq(updateVO.getDiyLabel()).builderQueryWrapper();
  129. this.workCalendarLabelService.remove(queryWrapper);
  130. return;
  131. } else if (BooleanUtil.isTrue(updateVO.getIsUpdate())) {
  132. Builder builder = new WorkCalendarLabel.Builder().createQueryWrapper();
  133. QueryWrapper<WorkCalendarLabel> queryWrapper = builder.groupCodeEq(updateVO.getGroupCode())
  134. .projectIdEq(updateVO.getProjectId(), dictTypeLevel).calendarIdEq(updateVO.getCalendarId(), dictTypeLevel)
  135. .labelDateGe(updateVO.getLabelDateStart()).labelDateLe(updateVO.getLabelDateEnd())
  136. .dictTypeEq(updateVO.getDictType()).diyLabelEq(updateVO.getDiyLabel()).builderQueryWrapper();
  137. this.workCalendarLabelService.remove(queryWrapper);
  138. }
  139. WorkCalendarLabel calendarLabel = new WorkCalendarLabel();
  140. BeanUtils.copyProperties(updateVO, calendarLabel);
  141. String now = DateUtil.format(new Date(), DateUtil.FORMAT_DATE_YYYYMMDDHHMMSS);
  142. calendarLabel.setCreateTime(now);
  143. calendarLabel.setUpdateTime(now);
  144. calendarLabel.setUpdateUser(updateVO.getUserId());
  145. calendarLabel.setGroupCode(updateVO.getGroupCode());
  146. if (dictTypeLevel == 0) {
  147. // 集团级
  148. calendarLabel.setCalendarId("0");
  149. calendarLabel.setProjectId("0");
  150. } else if (dictTypeLevel == 1) {
  151. // 项目级
  152. calendarLabel.setCalendarId("0");
  153. }
  154. DateTime dateStart = DateUtil.parse(updateVO.getLabelDateStart(), DateUtil.FORMAT_DATE_YYYYMMDD);
  155. DateTime dateEnd = DateUtil.parse(updateVO.getLabelDateEnd(), DateUtil.FORMAT_DATE_YYYYMMDD);
  156. long betweenDay = DateUtil.between(dateStart, dateEnd, DateUnit.DAY) + 1;
  157. for (int i = 0; i < betweenDay; i++) {
  158. calendarLabel.setId(null);
  159. calendarLabel.setLabelDate(DateUtil.format(dateStart, DateUtil.FORMAT_DATE_YYYYMMDD));
  160. if (WorkCalendarConstant.CUSTOM_CALENDAR_DATE_YES.equals(updateVO.getDiyLabel())) {
  161. calendarLabel.setDictType(WorkCalendarConstant.CUSTOM_CALENDAR_DICT_TYPE);
  162. }
  163. result = this.workCalendarLabelService.save(calendarLabel);
  164. if (!result) {
  165. throw new BusinessException("日期标签属性设置失败");
  166. }
  167. dateStart.offset(DateField.DAY_OF_YEAR, 1);
  168. }
  169. // 设置成功后,将自定义的标签插入到字典表中
  170. if (WorkCalendarConstant.CUSTOM_CALENDAR_DATE_YES.equals(updateVO.getDiyLabel())) {
  171. // 当自定义标签时,存入字典表
  172. List<WorkCalendarDict> calendarDicts = new ArrayList<WorkCalendarDict>();
  173. Set<String> diyValues = updateVO.getDiyValue();
  174. for (String diyValue : diyValues) {
  175. WorkCalendarDict calendarDict = new WorkCalendarDict();
  176. calendarDict.setId(IdGenerator.getUUID());
  177. calendarDict.setCalendarId(updateVO.getCalendarId());
  178. calendarDict.setProjectId(updateVO.getProjectId());
  179. calendarDict.setGroupCode(updateVO.getGroupCode());
  180. calendarDict.setDictType(WorkCalendarConstant.CUSTOM_CALENDAR_DICT_TYPE);
  181. calendarDict.setDictDesc(diyValue);
  182. calendarDicts.add(calendarDict);
  183. }
  184. this.workCalendarDictService.batchCreateDictTypeByDelete(calendarDicts);
  185. }
  186. }
  187. /**
  188. * 重新定义日期段内每天的属性标签
  189. *
  190. * @param updateVO
  191. */
  192. @Transactional
  193. public void updateCalendarDateAttribute(WorkLabelBatchUpdateVO updateVO) {
  194. List<WorkCalendarMoreDateCreateVO> times = updateVO.getTimes();
  195. if (CollectionUtil.isNotEmpty(times)) {
  196. // 自定义时间时,一定是属于某个工作历下的,不用判断标签所属
  197. for (WorkCalendarMoreDateCreateVO moreDateCreateVO : times) {
  198. // 判断工作历类型是否存在
  199. WorkCalendar calendar = this.workCalendarService.getById(moreDateCreateVO.getCalendarId(), moreDateCreateVO.getGroupCode(), moreDateCreateVO.getProjectId());
  200. if (calendar == null) {
  201. throw new BusinessException("工作历类型不存在");
  202. }
  203. moreDateCreateVO.setGroupCode(updateVO.getGroupCode());
  204. this.workCalendarDateHandler.batchCreateCalendarMoreDate(moreDateCreateVO, calendar);
  205. }
  206. }
  207. List<WorkCalendarLabelUpdateVO> labels = updateVO.getLabels();
  208. if (CollectionUtil.isNotEmpty(labels)) {
  209. for (WorkCalendarLabelUpdateVO labelUpdateVO : labels) {
  210. labelUpdateVO.setGroupCode(updateVO.getGroupCode());
  211. this.updateLabelInfo(labelUpdateVO);
  212. }
  213. }
  214. }
  215. /**
  216. * 检查数据的有效性
  217. * @param updateVO
  218. * @return null - 有效
  219. */
  220. public static ResponseResult checkDataValidity(WorkCalendarLabelUpdateVO updateVO) {
  221. if (WorkCalendarConstant.CUSTOM_CALENDAR_DATE_NO.equals(updateVO.getDiyLabel())) {
  222. if (StringUtil.isBlank(updateVO.getDictType())) {
  223. return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "字典类型不可为空");
  224. }
  225. if (BooleanUtil.isFalse(updateVO.getIsDelete())) {
  226. if (StringUtil.isBlank(updateVO.getDictCode())) {
  227. return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "字典编码不可为空");
  228. }
  229. if (CollectionUtil.isNotEmpty(updateVO.getDiyValue())) {
  230. return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "非自定义情况下,diyValue必须为空");
  231. }
  232. }
  233. } else {
  234. if (BooleanUtil.isFalse(updateVO.getIsDelete())) {
  235. if (CollectionUtil.isEmpty(updateVO.getDiyValue())) {
  236. return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "自定义标签值不可为空");
  237. }
  238. if (StringUtil.isNotBlank(updateVO.getDictType()) || StringUtil.isNotBlank(updateVO.getDictCode())) {
  239. return ResponseUtil.errorResult(ResponseCode.A0400.getCode(), "自定义情况下,字典类型和字典编码必须为空");
  240. }
  241. }
  242. }
  243. return null;
  244. }
  245. }