package com.sifang.modules.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.dao.CompletionDao;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.completionrectification.service.CompletionService;
import com.sifang.modules.customer.Service.GuangFaAcceptReformedService;
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 java.text.ParseException;
import java.util.List;

/**
 * @title: GuangFaAcceptReformedTask
 * @Author Tan
 * @Date: 2024/10/15 14:15
 * @Version 1.0
 */
@Component
public class GuangFaAcceptReformedTask {

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

    @Autowired
    private CompletionService completionService;

    @Autowired
    private GuangFaAcceptReformedService guangFaAcceptReformedService;

    @Autowired
    private CompletionDao completionDao;

    @Scheduled(cron = "0 0/10 * * * ?")
    void getqueryGuangFaAccept() {
        logger.info("getqueryGuangFaAccept 竣工整改");
        long begin = System.currentTimeMillis();
        StringBuffer sf = new StringBuffer();
        sf.append(" select completion_no FROM crm_completion_info ");
        sf.append(" where completion_status <>'40' ");
        List<Record> list = Db.find(sf.toString());
        StringBuilder sb = new StringBuilder();
        logger.info("集合size"+list.size());
        if(list.size()>0){
            for(Record record : list){
                try {
                    sb.append(record.getStr("completion_no")).append(",");
                }catch (Exception e){
                    logger.error("getqueryGuangFaAccept 竣工整改异常",e);
                }
            }
        }
        String acceptNoList = sb.substring(0, sb.length() - 1); // 去掉最后一个逗号
        guangFaAcceptReformedService.acceptResultQry(acceptNoList);
        long end = System.currentTimeMillis();
        logger.info("getqueryGuangFaAccept 竣工整改，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    void getqueryGuangFaReformed() {
        logger.info("getqueryGuangFaReformed 整改");
        long begin = System.currentTimeMillis();
        StringBuffer sf = new StringBuffer();
        sf.append(" select rectification_no FROM crm_rectification_info ");
        sf.append(" where rectification_status <>'40' ");
        List<Record> list = Db.find(sf.toString());
        StringBuilder sb = new StringBuilder();
        logger.info("集合size"+list.size());
        String reformedNoList = "";
        if(list.size()>0){
            for(Record record : list){
                try {
                    sb.append(record.getStr("rectification_no")).append(",");
                }catch (Exception e){
                    logger.error("getqueryGuangFaReformed 整改异常",e);
                }
            }
            reformedNoList = sb.substring(0, sb.length() - 1); // 去掉最后一个逗号
            guangFaAcceptReformedService.queryReformedList(reformedNoList);
        }
        long end = System.currentTimeMillis();
        logger.info("getqueryGuangFaReformed 整改，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

}