package com.jwipc.job.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.jwipc.job.dto.SupplierFreezeParamDTO;
import com.jwipc.job.dto.SupplierFreezeResultSapDTO;
import com.jwipc.job.logic.impl.EmailSendLogicLogicImpl;
import com.jwipc.spe.entity.SupAppraisePlanSupplier;
import com.jwipc.spe.model.dto.ScoreReportDTO;
import com.jwipc.spe.model.query.ScoreReportQuery;
import com.jwipc.spe.service.ISupAppraisePlanService;
import com.jwipc.spe.service.ISupAppraisePlanSupplierService;
import com.jwipc.supplier.service.ISupplierQualificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 绩效考核连续三月不达标预警
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class SupplierMeritsNeedWarnJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private ISupAppraisePlanService supAppraisePlanService;
    @Autowired
    private ISupAppraisePlanSupplierService supAppraisePlanSupplierService;


    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<ScoreReportQuery> queries = new ArrayList<>();
        LocalDate now = LocalDate.now();
        ScoreReportQuery query1 = new ScoreReportQuery();
        query1.setYear(String.valueOf(now.plusMonths(-1).getYear()));
        query1.setMonth(String.valueOf(now.plusMonths(-1).getMonth().getValue()));
        queries.add(query1);

        ScoreReportQuery query2 = new ScoreReportQuery();
        query2.setYear(String.valueOf(now.plusMonths(-2).getYear()));
        query2.setMonth(String.valueOf(now.plusMonths(-2).getMonth().getValue()));
        queries.add(query2);

        ScoreReportQuery query3 = new ScoreReportQuery();
        query3.setYear(String.valueOf(now.plusMonths(-3).getYear()));
        query3.setMonth(String.valueOf(now.plusMonths(-3).getMonth().getValue()));
        queries.add(query3);

        List<ScoreReportDTO> list = supAppraisePlanService.selectNeedWarnList(queries);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> supplierIds = list.stream().filter(item -> item.getCdLevelCount().compareTo(3) >= 0).map(ScoreReportDTO::getSupplierId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(supplierIds)) {
                LambdaQueryWrapper<SupAppraisePlanSupplier> planSupplierLambdaQueryWrapper = new LambdaQueryWrapper<>();
                planSupplierLambdaQueryWrapper.in(SupAppraisePlanSupplier::getSupplierId, supplierIds);
                planSupplierLambdaQueryWrapper.ne(SupAppraisePlanSupplier::getCdFlag, 1);
                List<SupAppraisePlanSupplier> supAppraisePlanSuppliers = supAppraisePlanSupplierService.list(planSupplierLambdaQueryWrapper);
                if (CollectionUtils.isNotEmpty(supAppraisePlanSuppliers)) {
                    supAppraisePlanSuppliers.forEach(item -> {
                        item.setCdFlag(1);
                    });
                    supAppraisePlanSupplierService.updateBatchById(supAppraisePlanSuppliers);
                }
            }
        }

    }

}
