/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/22
 */
package org.springblade.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.InspectionStatusEnum;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.service.IEquipmentInspectionService;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.warning.service.IRtuWarningService;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/***
 * Date:2022/5/22
 * Title: 巡检计划检查任务
 * Description：
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class InspectionPlanCheckTask implements Callable<Integer> {

    private IEquipmentInspectionPlanService planService;
    private IEquipmentInspectionService inspectionService;
    private LocalDateTime checkTime;

    public InspectionPlanCheckTask(IEquipmentInspectionPlanService planService, IEquipmentInspectionService inspectionService, LocalDateTime checkTime) {
        this.planService = planService;
        this.inspectionService = inspectionService;
        this.checkTime = checkTime;
    }

    @Override
    public Integer call() {
        try {
            LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
            wrapper.eq(EquipmentInspectionPlanEntity::getPlanStatus, 1);
            wrapper.eq(EquipmentInspectionPlanEntity::getPlanScheduledType, 2);
            List<EquipmentInspectionPlanEntity> planList = planService.list(wrapper);
            if (Func.notNull(planList)) {
                for (EquipmentInspectionPlanEntity planEntity : planList) {
                    if (planEntity.getTimeType() == 1) {
                        //按天
                        LocalDate inspectionDate = checkTime.toLocalDate();
                        LambdaQueryWrapper<EquipmentInspectionInfoEntity> inspectionWrapper = Wrappers.<EquipmentInspectionInfoEntity>query().lambda();
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionPlanId, planEntity.getId());
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionDate, Date.from(inspectionDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                        inspectionWrapper.last("limit 1");
                        EquipmentInspectionInfoEntity detail = inspectionService.getOne(inspectionWrapper);
                        if (Func.isNull(detail)) {
                            EquipmentInspectionInfoEntity dto = new EquipmentInspectionInfoEntity();
                            dto.setInspectionPlanId(planEntity.getId());
                            dto.setInspectionTitle(planEntity.getPlanTitle());
                            dto.setInspectionStatus(InspectionStatusEnum.STATUS_TODO.getCode());
                            dto.setInspectionDate(Date.from(inspectionDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                            dto.setTenantId(planEntity.getTenantId());
                            dto.setOrgId(planEntity.getOrgId());
                            inspectionService.save(dto);
                        }
                    } else if (planEntity.getTimeType() == 2) {
                        //按周
                        LocalDate inspectionDate = checkTime.toLocalDate();
                        DayOfWeek dayOfWeek = inspectionDate.getDayOfWeek();
                        int dayDifference = DayOfWeek.MONDAY.getValue() - dayOfWeek.getValue();
                        LocalDate monday = inspectionDate.plusDays(dayDifference);
                        LambdaQueryWrapper<EquipmentInspectionInfoEntity> inspectionWrapper = Wrappers.<EquipmentInspectionInfoEntity>query().lambda();
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionPlanId, planEntity.getId());
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionDate, Date.from(monday.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                        inspectionWrapper.last("limit 1");
                        EquipmentInspectionInfoEntity detail = inspectionService.getOne(inspectionWrapper);
                        if (Func.isNull(detail)) {
                            EquipmentInspectionInfoEntity dto = new EquipmentInspectionInfoEntity();
                            dto.setInspectionPlanId(planEntity.getId());
                            dto.setInspectionTitle(planEntity.getPlanTitle());
                            dto.setInspectionStatus(InspectionStatusEnum.STATUS_TODO.getCode());
                            dto.setInspectionDate(Date.from(monday.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                            dto.setTenantId(planEntity.getTenantId());
                            dto.setOrgId(planEntity.getOrgId());
                            inspectionService.save(dto);
                        }
                    } else if (planEntity.getTimeType() == 3) {
                        //按月
                        LocalDate inspectionDate = checkTime.toLocalDate().withDayOfMonth(1);
                        LambdaQueryWrapper<EquipmentInspectionInfoEntity> inspectionWrapper = Wrappers.<EquipmentInspectionInfoEntity>query().lambda();
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionPlanId, planEntity.getId());
                        inspectionWrapper.eq(EquipmentInspectionInfoEntity::getInspectionDate, Date.from(inspectionDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                        inspectionWrapper.last("limit 1");
                        EquipmentInspectionInfoEntity detail = inspectionService.getOne(inspectionWrapper);
                        if (Func.isNull(detail)) {
                            EquipmentInspectionInfoEntity dto = new EquipmentInspectionInfoEntity();
                            dto.setInspectionPlanId(planEntity.getId());
                            dto.setInspectionTitle(planEntity.getPlanTitle());
                            dto.setInspectionStatus(InspectionStatusEnum.STATUS_TODO.getCode());
                            dto.setInspectionDate(Date.from(inspectionDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
                            dto.setTenantId(planEntity.getTenantId());
                            dto.setOrgId(planEntity.getOrgId());
                            inspectionService.save(dto);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return 0;
    }
}
