package com.persagy.calendar.handle; import java.util.HashSet; import java.util.List; import java.util.Set; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.persagy.calendar.pojo.dto.WorkCalendar; import com.persagy.calendar.pojo.dto.WorkCalendarRule; import com.persagy.calendar.pojo.vo.CustomCalendarVO; import com.persagy.calendar.pojo.vo.WorkCalendarRuleCreateVO; import com.persagy.calendar.pojo.vo.WorkCalendarRuleUpdateVO; import com.persagy.calendar.service.IWorkCalendarRuleService; import com.persagy.calendar.service.IWorkCalendarService; import com.persagy.common.enums.ResponseCode; import com.persagy.common.exception.BusinessException; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.BooleanUtil; /** * @version 定义规则信息 * @description * @company persagy * @author zhangqiankun * @since 2020年10月3日: 下午1:03:19 */ @Component public class WorkCalendarRuleHandler { @Autowired private IWorkCalendarService workCalendarService; @Autowired private IWorkCalendarRuleService workCalendarRuleService; /** * 批量创建规则信息 */ @Transactional public void batchCreateCalednarRule(CustomCalendarVO createVO) { boolean result = false; WorkCalendarRule workCalendarRule = null; List batchInfo = createVO.getBatchInfo(); Set calendarIds = new HashSet(); for (WorkCalendarRuleCreateVO rule : batchInfo) { // 优先判断工作历信息是否存在 if (!calendarIds.contains(rule.getCalendarId())) { WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), createVO.getProjectId()); if (workCalendar == null) { throw new BusinessException("工作历主信息不存在"); } } workCalendarRule = new WorkCalendarRule(); BeanUtils.copyProperties(rule, workCalendarRule); workCalendarRule.setId(null); workCalendarRule.setGroupCode(createVO.getGroupCode()); workCalendarRule.setProjectId(createVO.getProjectId()); workCalendarRule.setUpdateUser(createVO.getUserId()); // 为兼容IBMS,紧急修改内容: shop 类型的工作历规则,一个项目的dictType+dictCode+value已存在的话,直接更新 if ("shop".equals(rule.getDictType()) && CollectionUtil.isNotEmpty(workCalendarRule.getValue())) { String ruleId = this.workCalendarRuleService.getCalendarRule(workCalendarRule); if (ruleId != null ) { // 直接更新 workCalendarRule.setId(ruleId); result = workCalendarRuleService.updateWorkCalendarRule(workCalendarRule); if (!result) { throw new BusinessException("工作历规则信息保存失败"); } calendarIds.add(rule.getCalendarId()); continue; } } // 是否需要删除旧规则 if (BooleanUtil.isTrue(createVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) { workCalendarRuleService.removeWorkCalendarRule(null, rule.getCalendarId(), rule.getDictType()); //throw new BusinessException("工作历ID为:" + rule.getCalendarId() + "的工作历,删除规则信息失败"); } result = this.workCalendarRuleService.createWorkCalendarRule(workCalendarRule); if (!result) { throw new BusinessException("工作历规则信息创建失败"); } calendarIds.add(rule.getCalendarId()); } calendarIds.clear(); } /** * 批量更新规则信息 */ @Transactional public void batchUpdateCalednarRule(CustomCalendarVO updateVO) { boolean result = false; WorkCalendarRule workCalendarRule = null; List batchInfo = updateVO.getBatchInfo(); Set calendarIds = new HashSet(); for (WorkCalendarRuleUpdateVO rule : batchInfo) { // 优先判断工作历信息是否存在 if (!calendarIds.contains(rule.getCalendarId())) { WorkCalendar workCalendar = this.workCalendarService.getById(rule.getCalendarId(), updateVO.getProjectId()); if (workCalendar == null) { throw new BusinessException("工作历主信息不存在"); } } // 再判断原规则数据是否存在 WorkCalendarRule calendar = workCalendarRuleService.getById(rule.getId(), updateVO.getProjectId()); if (calendar == null) { throw new BusinessException(ResponseCode.C0320.getDesc()); } // 是否需要删除旧规则 if (BooleanUtil.isTrue(updateVO.getIsDelete()) && !calendarIds.contains(rule.getCalendarId())) { workCalendarRuleService.removeWorkCalendarRule(null, rule.getCalendarId(), rule.getDictCode()); //throw new BusinessException("工作历ID为:" + rule.getCalendarId() + "的工作历,删除规则信息失败"); } workCalendarRule = new WorkCalendarRule(); BeanUtils.copyProperties(rule, workCalendarRule); workCalendarRule.setGroupCode(updateVO.getGroupCode()); workCalendarRule.setProjectId(updateVO.getProjectId()); workCalendarRule.setUpdateUser(updateVO.getUserId()); result = workCalendarRuleService.updateWorkCalendarRule(workCalendarRule); if (!result) { throw new BusinessException("工作历规则信息更新失败"); } calendarIds.add(rule.getCalendarId()); } calendarIds.clear(); } }