|
|
@@ -0,0 +1,315 @@
|
|
|
+package com.zd.laboratory.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.zd.common.core.redis.RedisService;
|
|
|
+import com.zd.common.core.security.TokenService;
|
|
|
+import com.zd.common.core.utils.StringUtils;
|
|
|
+import com.zd.laboratory.domain.LabTimedExhaust;
|
|
|
+import com.zd.laboratory.domain.LabTimedExhaustJoinsub;
|
|
|
+import com.zd.laboratory.domain.vo.*;
|
|
|
+import com.zd.laboratory.mapper.LabTimedExhaustJoinsubMapper;
|
|
|
+import com.zd.laboratory.mapper.LabTimedExhaustMapper;
|
|
|
+import com.zd.laboratory.service.ILabTimedExhaustService;
|
|
|
+import com.zd.model.constant.BaseConstants;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 定时排风Service接口
|
|
|
+ *
|
|
|
+ * @author cyl
|
|
|
+ * @date 2023/3/16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class LabTimedExhaustServiceImpl implements ILabTimedExhaustService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LabTimedExhaustMapper labTimedExhaustMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TokenService tokenService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LabTimedExhaustJoinsubMapper labTimedExhaustJoinsubMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询定时排风列表
|
|
|
+ *
|
|
|
+ * @param labTimedExhaust 定时排风
|
|
|
+ * @return 定时排风集合
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List <LabTimedExhaust> selectLabTimedExhaustList(LabTimedExhaust labTimedExhaust) {
|
|
|
+ return labTimedExhaustMapper.selectLabTimedExhaustList(labTimedExhaust);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增定时排风
|
|
|
+ *
|
|
|
+ * @param labTimedExhaustVo 定时排风
|
|
|
+ * @return 是否成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LabTimedExhaust insertLabTimedExhaust(LabTimedExhaustVo labTimedExhaustVo) {
|
|
|
+ LabTimedExhaust timedExhaust = getTimedExhaustInfo(labTimedExhaustVo);
|
|
|
+ timedExhaust.setUserId(tokenService.getLoginUser().getUserid());
|
|
|
+ timedExhaust.setCreateBy(tokenService.getLoginUser().getNickName());
|
|
|
+ timedExhaust.setCreateTime(new Date());
|
|
|
+ timedExhaust.setUpdateBy(tokenService.getLoginUser().getNickName());
|
|
|
+ timedExhaust.setUpdateTime(new Date());
|
|
|
+ labTimedExhaustMapper.insertLabTimedExhaust(timedExhaust);
|
|
|
+ return timedExhaust;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改定时排风
|
|
|
+ *
|
|
|
+ * @param labTimedExhaustVo 修改定时排风
|
|
|
+ * @return 是否成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LabTimedExhaust updateLabTimedExhaust(LabTimedExhaustVo labTimedExhaustVo) {
|
|
|
+ LabTimedExhaust timedExhaust = getTimedExhaustInfo(labTimedExhaustVo);
|
|
|
+ timedExhaust.setUpdateBy(tokenService.getLoginUser().getNickName());
|
|
|
+ timedExhaust.setUpdateTime(new Date());
|
|
|
+ labTimedExhaustMapper.updateLabTimedExhaust(timedExhaust);
|
|
|
+ return timedExhaust;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 定时排风启用停用
|
|
|
+ *
|
|
|
+ * @param labTimedExhaust 定时排风
|
|
|
+ * @return 是否成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int operateExhaust(LabTimedExhaust labTimedExhaust) {
|
|
|
+ labTimedExhaust.setUpdateBy(tokenService.getLoginUser().getNickName());
|
|
|
+ labTimedExhaust.setUpdateTime(new Date());
|
|
|
+ return labTimedExhaustMapper.operateExhaust(labTimedExhaust);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询定时排风详细信息
|
|
|
+ *
|
|
|
+ * @param id 定时排风主键
|
|
|
+ * @return 定时排风详细信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LabTimedExhaustVo selectLabTimedExhaustById(Long id) {
|
|
|
+ LabTimedExhaust timedExhaust = labTimedExhaustMapper.selectLabTimedExhaustById(id);
|
|
|
+ return getTimedExhaustVo(timedExhaust);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List <LabSubjectVO> getLabExhaustNoJoinSub(LabExhaustJoinSubVo labExhaustJoinSubVo) {
|
|
|
+ labExhaustJoinSubVo.setUserId(tokenService.getLoginUser().getUserid());
|
|
|
+ return labTimedExhaustMapper.getLabExhaustNoJoinSub(labExhaustJoinSubVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List <LabSubjectVO> getLabExhaustJoinSub(LabExhaustJoinSubVo labExhaustJoinSubVo) {
|
|
|
+ labExhaustJoinSubVo.setUserId(tokenService.getLoginUser().getUserid());
|
|
|
+ return labTimedExhaustMapper.getLabExhaustJoinSub(labExhaustJoinSubVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int deleteLabTimedExhaustByIds(Long[] ids) {
|
|
|
+ //先删除定时排风关联的实验室
|
|
|
+ labTimedExhaustJoinsubMapper.deleteLabTimedExhaustJoinsubByExhaustId(ids[0]);
|
|
|
+ //在删除定时排风数据
|
|
|
+ labTimedExhaustMapper.deleteLabTimedExhaustByIds(ids[0]);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int automaticExhaust() {
|
|
|
+ //查询定时排风列表
|
|
|
+ LabTimedExhaust labTimedExhaust = new LabTimedExhaust();
|
|
|
+ labTimedExhaust.setExhaustType(1);
|
|
|
+ List <LabTimedExhaust> timedExhaustList = labTimedExhaustMapper.selectLabTimedExhaustList(labTimedExhaust);
|
|
|
+ List<LabTimedExhaustVo> timedExhaustVoList = Optional.ofNullable(timedExhaustList).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .map(a->getTimedExhaustVo(a)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List <LabTimedExhaustJoinsub> joinsubList = labTimedExhaustJoinsubMapper.selectLabTimedExhaustJoinsubList(new LabTimedExhaustJoinsub());
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ System.out.println(formatter.format(calendar.getTime()));
|
|
|
+ //后去年
|
|
|
+ int year = calendar.get(Calendar.YEAR);
|
|
|
+ int monthInt = calendar.get(Calendar.MONTH)+1;
|
|
|
+ String monthStr;
|
|
|
+ if (monthInt<10){
|
|
|
+ monthStr = "0"+monthInt;
|
|
|
+ }else {
|
|
|
+ monthStr = ""+monthInt;
|
|
|
+ }
|
|
|
+ int dateInt = calendar.get(Calendar.DATE);
|
|
|
+ String dateStr;
|
|
|
+ if (dateInt<10){
|
|
|
+ dateStr = "0"+dateInt;
|
|
|
+ }else {
|
|
|
+ dateStr = ""+dateInt;
|
|
|
+ }
|
|
|
+ Integer newDateInt = Integer.parseInt(year+""+monthStr+""+""+dateStr);
|
|
|
+
|
|
|
+ //todo 函数校验当日是否落选到时间区间 这里由于需求变动,在跳过节假日的时候,数据上报是不是节假日的日期时间段
|
|
|
+ Predicate<List <LabExecutionDateVo>> predicate = a->a.stream()
|
|
|
+ .map(b->{
|
|
|
+ Integer beginDate = Integer.parseInt(b.getBeginDate().replaceAll("-",""));
|
|
|
+ Integer endDate = Integer.parseInt(b.getEndDate().replaceAll("-",""));
|
|
|
+ if(beginDate.intValue()<=newDateInt.intValue() && newDateInt.intValue()<=endDate.intValue()){
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ })
|
|
|
+ .filter(b-> StringUtils.isNotNull(b))
|
|
|
+ .collect(Collectors.toList()).size()>0;
|
|
|
+
|
|
|
+ Function <List<LabTimedExhaustVo>,List<LabTimedExhaustVo>> consumer = a->a.stream()
|
|
|
+ .filter(b->{
|
|
|
+ if(StringUtils.isNotNull(b.getExecutionDateVoList()) && b.getExecutionDateVoList().size()>0){
|
|
|
+ if(b.getExecutionDateType().intValue()==0){
|
|
|
+ return predicate.test(b.getExecutionDateVoList());
|
|
|
+ }else if(b.getExecutionDateType().intValue()==1){
|
|
|
+ return predicate.test(b.getExecutionDateVoList());
|
|
|
+ }else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ //todo 函数校验当日落选后,根据时间段,执行对应的
|
|
|
+ Optional.ofNullable(joinsubList).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .forEach(a->Optional.ofNullable(consumer.apply(timedExhaustVoList)).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .filter(b->a.getTimedExhaustId().longValue()==b.getId().longValue())
|
|
|
+ .forEach(b->{
|
|
|
+ executionFun(a.getSubId(),b.getExhaustPeriodVoList(),calendar);
|
|
|
+ })
|
|
|
+ );
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void executionFun(final Long subId,List <LabExhaustPeriodVo> exhaustPeriodVoList,Calendar calendar){
|
|
|
+ Predicate<LabExhaustPeriodVo> predicate = a->{
|
|
|
+// Integer[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
|
|
|
+ int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ if (w < 0)
|
|
|
+ w = 0;
|
|
|
+ if(a.getTimedType().intValue()==1){
|
|
|
+ //工作日执行,周一到周五
|
|
|
+ Integer[] weekDays = { -1, 1, 2, 3, 4, 5, -1 };
|
|
|
+ if(weekDays[w].intValue()!=-1){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }else if(a.getTimedType().intValue()==2){
|
|
|
+ return true;
|
|
|
+ }else if(a.getTimedType().intValue()==3){
|
|
|
+ //自定义执行
|
|
|
+ Map<Integer,Integer> map = new HashMap <>();
|
|
|
+ if(StringUtils.isNotNull(a.getCustomTime())){
|
|
|
+ Arrays.asList(a.getCustomTime().split(",")).stream()
|
|
|
+ .forEach(x->{
|
|
|
+ map.put(Integer.parseInt(x),Integer.parseInt(x));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //自定义执行
|
|
|
+ Integer[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
|
|
|
+ if(map.get(weekDays[w].intValue()) !=null && weekDays[w].intValue()==map.get(weekDays[w].intValue()).intValue()){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ Optional.ofNullable(exhaustPeriodVoList).orElseGet(Collections::emptyList)
|
|
|
+ .stream()
|
|
|
+ .filter(a->predicate.test(a))
|
|
|
+ .forEach(a->{
|
|
|
+ //这里执行redis存储,如果是开始时间,那么就存实验室和开关“开”。
|
|
|
+ String[] beginTimeStr = a.getBeginTime().split(":");
|
|
|
+ long lDate1 = calendar.getTime().getTime();
|
|
|
+ Calendar newCalendar = Calendar.getInstance();
|
|
|
+ Long newBeginTime = calendarTime(lDate1,beginTimeStr,newCalendar);
|
|
|
+ if(newBeginTime>=0){
|
|
|
+ executionTimer(newBeginTime,subId,1);
|
|
|
+ }
|
|
|
+ //这里执行redis存储,如果是结束时间,那么就存实验室和开关“关”。
|
|
|
+ String[] endTimeStr = a.getEndTime().split(":");
|
|
|
+ Long newEndTime = calendarTime(lDate1,endTimeStr,newCalendar);
|
|
|
+ if(newEndTime>=0){
|
|
|
+ executionTimer(Long.parseLong(newEndTime+""),subId,0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long calendarTime(long lDate1,String[] timeStr,Calendar calendar){
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeStr[0]));
|
|
|
+ calendar.set(Calendar.MINUTE, Integer.parseInt(timeStr[1]));
|
|
|
+ long lDate2 = calendar.getTime().getTime();
|
|
|
+ long diff = (lDate2 - lDate1);
|
|
|
+ long day = diff / (24 * 60 * 60 * 1000);
|
|
|
+ long hour = diff / (60 * 60 * 1000) - day * 24;
|
|
|
+ long min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
|
|
|
+ return (hour*60L)+(min);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void executionTimer(final Long newBeginTime,final Long subId,final Integer type){
|
|
|
+ LabDealyNotifyVo labDealyNotifyVo = new LabDealyNotifyVo();
|
|
|
+ labDealyNotifyVo.setRandomNum(UUID.randomUUID().toString());
|
|
|
+ labDealyNotifyVo.setSubId(subId);
|
|
|
+ labDealyNotifyVo.setOpenOrCloseType(type);
|
|
|
+ JSONObject jsonObj = new JSONObject();
|
|
|
+ jsonObj.put(BaseConstants.DELAY_QUEUE,labDealyNotifyVo);
|
|
|
+ redisService.setCacheObject(BaseConstants.DELAY_QUEUE+"~"+ jsonObj,jsonObj, newBeginTime, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+
|
|
|
+ public LabTimedExhaustVo getTimedExhaustVo(LabTimedExhaust timedExhaust){
|
|
|
+ LabTimedExhaustVo labTimedExhaustVo = new LabTimedExhaustVo();
|
|
|
+ BeanUtils.copyProperties(timedExhaust, labTimedExhaustVo);
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ object.put("objectPeriod",JSONArray.parseArray(timedExhaust.getExhaustPeriod()));
|
|
|
+ object.put("objectDate",JSONArray.parseArray(timedExhaust.getExecutionDate()));
|
|
|
+
|
|
|
+ labTimedExhaustVo.setExhaustPeriodVoList(object.getObject("objectPeriod",new TypeReference <List<LabExhaustPeriodVo>>(){}));
|
|
|
+ labTimedExhaustVo.setExecutionDateVoList(object.getObject("objectDate",new TypeReference <List<LabExecutionDateVo>>(){}));
|
|
|
+ return labTimedExhaustVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public LabTimedExhaust getTimedExhaustInfo(LabTimedExhaustVo labTimedExhaustVo){
|
|
|
+ LabTimedExhaust timedExhaust = new LabTimedExhaust();
|
|
|
+ BeanUtils.copyProperties(labTimedExhaustVo, timedExhaust);
|
|
|
+ if(labTimedExhaustVo.getExhaustPeriodVoList().size()>0){
|
|
|
+ String exhaustPeriodStr = JSON.toJSONString(labTimedExhaustVo.getExhaustPeriodVoList());
|
|
|
+ timedExhaust.setExhaustPeriod(exhaustPeriodStr);
|
|
|
+ }
|
|
|
+ if(labTimedExhaustVo.getExecutionDateVoList().size()>0){
|
|
|
+ String executionDateStr = JSON.toJSONString(labTimedExhaustVo.getExecutionDateVoList());
|
|
|
+ timedExhaust.setExecutionDate(executionDateStr);
|
|
|
+ }
|
|
|
+ return timedExhaust;
|
|
|
+ }
|
|
|
+}
|