package com.sifang.modules.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.Service.HuaRongManagementService;
import com.sifang.modules.customer.Service.HuaRongMixRoofSettlementService;
import com.sifang.modules.customer.Service.HuaRongReleOrderService;
import com.sifang.modules.customer.constant.PartnerConstants;
import com.sifang.modules.customer.dao.CrmAcceptingReportDetailDao;
import com.sifang.modules.customer.dao.CrmCustomerEntryDesignDao;
import com.sifang.modules.customer.dao.CrmCustomerEntryProjectDao;
import com.sifang.modules.customer.dao.CrmCustomerIncomingApplicationDao;
import com.sifang.modules.customer.entity.CrmAcceptingReportDetail;
import com.sifang.modules.customer.entity.CrmCustomerEntryDesign;
import com.sifang.modules.customer.entity.CrmCustomerIncomingApplication;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.partner.service.HuaRongApiService;
import com.sifang.modules.partner.utils.HuaRongCustomer;
import com.sifang.modules.partner.utils.HuaRongStatusFormVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @title: HuaRongGetStatusTask
 * @Version 1.0
 */
@Component
public class HuaRongGetStatusTask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    CustomerEntryService customerEntryService;

    @Autowired
    CrmCustomerIncomingApplicationDao incomingApplicationDao;

    @Autowired
    private HuaRongApiService huaRongApiService;

    @Autowired
    private HuaRongReleOrderService huaRongReleOrderService;

    @Autowired
    private CrmAcceptingReportDetailDao crmAcceptingReportDetailDao;

    @Autowired
    private HuaRongMixRoofSettlementService huaRongMixRoofSettlementService;

    @Autowired
    private CrmCustomerEntryProjectDao crmCustomerEntryProjectDao;

    @Autowired
    CrmCustomerEntryDesignDao crmCustomerEntryDesignDao;


    @Scheduled(cron = "0 0/10 * * * ?")
    void getInputPieceStatus(){
        logger.info(">>>>>>>>getInputPieceStatus  定时任务开始");
        long begin = System.currentTimeMillis();
        List<CrmCustomerIncomingApplication> applicationList = incomingApplicationDao.getInputPieceInProgress(PartnerConstants.PARTNER_ID_HR);
        for (CrmCustomerIncomingApplication crmCustomerIncomingApplication : applicationList) {
            try {
                List<HuaRongStatusFormVo> result = huaRongApiService.queryRongStatus(PartnerConstants.PARTNER_ID_HR, crmCustomerIncomingApplication.getPartnersContractNumber());

                HuaRongCustomer customer = result.get(0).getCustomerList().get(0);
                customerEntryService.updateFirstStateHuaRong(crmCustomerIncomingApplication.getEntryId(),customer);
            }
            catch (Exception e) {
                logger.error(e.getMessage(), e);
            }

        }
        long end = System.currentTimeMillis();
        logger.info("getInputPieceStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
        }

    @Scheduled(cron = "0 0/15 * * * ?")
    void getHuaRongSecondStatus(){
        logger.info(">>>>>>>>getHuaRongSecondStatus  定时任务开始");
        long begin = System.currentTimeMillis();
        List<Record> inputPiece = Db.find(" SELECT entry_id FROM crm_customer_incoming_application WHERE farmer_sign_status IN('DS','MR','RJ') " +
                "AND partner_id='04' ");
        if(inputPiece==null || inputPiece.isEmpty()){
            return;
        }
        for(Record record : inputPiece){
            try {
                customerEntryService.dealHRQueryData(record.getStr("entry_id"),"2");
            }
            catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getHuaRongSecondStatus 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    @Scheduled(cron = "* 0/10 * * * ?")
    void getHuaRongReleOrderInfo(){
        logger.info(">>>>>>>>getHuaRongReleOrderData  定时任务开始");
        long begin = System.currentTimeMillis();
        try {
            huaRongReleOrderService.queryReleOrderInfo();
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long end = System.currentTimeMillis();
        logger.info("getHuaRongReleOrderData 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    @Scheduled(cron = "0 33 23 * * ?")
    public void getHuaRongMixRoofSettlement(){
        logger.info(">>>>>>>>getHuaRongMixRoofSettlement  定时任务开始");
        long begin = System.currentTimeMillis();
        List<Record> records = Db.find(" SELECT *FROM crm_customer_entry WHERE partner_id = '04' AND wg_settle_flag = '0' AND `status` IN ('6','7','10','a') ");
        for (Record record : records) {
            String projectIds[] = record.getStr("project_id").split(",");
            try {
                huaRongMixRoofSettlementService.addHuaRongSettlement(projectIds, record.getStr("id"));
            }catch (Exception e){
                logger.error("getHuaRongMixRoofSettlement 执行异常，error :{}  projectIds :{} " ,e.getMessage(), projectIds);
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getHuaRongMixRoofSettlement 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    @Scheduled(cron = "0 53 23 * * ?")
    public void getHuaRongMixRoofSettlementBwSettleTask(){
        logger.info(">>>>>>>>getHuaRongMixRoofSettlementBwSettleTask  定时任务开始");
        long begin = System.currentTimeMillis();
        List<Record> records = Db.find(" SELECT *FROM crm_customer_entry WHERE partner_id = '04' AND bw_settle_flag = '0' AND status = 'a' AND project_num > 1 ");
        for (Record record : records) {
            String projectIds[] = record.getStr("project_id").split(",");
            try {
                huaRongMixRoofSettlementService.getHuaRongMixRoofSettlementBwSettleTask(projectIds, record.getStr("id"));
            }catch (Exception e){
                logger.error("getHuaRongMixRoofSettlementBwSettleTask 执行异常，error :{}  projectIds :{} " ,e.getMessage(), projectIds);
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getHuaRongMixRoofSettlementBwSettleTask 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

}
