package com.sifang.modules.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.insurance.service.CustomerEntryInsuranceService;
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.util.List;

@Component
public class InsuranceTask {

    private static final Logger logger = LoggerFactory.getLogger(InsuranceTask.class);

    @Autowired
    CustomerEntryInsuranceService customerEntryInsuranceService;

    /**
     * 生成续保记录
     */
    @Scheduled(cron = "0 10 03 * * ?")
    public void getInsuranceTask(){
        logger.info(">>>>>>>>getlnsuranceTask  定时任务开始");
        long begin = System.currentTimeMillis();
        List<Record> records = Db.find(" SELECT *FROM crm_customer_entry_insurance WHERE `status` = '1' AND insurance_type = '0' AND policy_type = '0'");
        for (Record record : records) {
            try {
                customerEntryInsuranceService.getlnsuranceTask(record.getStr("id"));
            }catch (Exception e){
                logger.error("getlnsuranceTask 执行异常，error :{}  id :{} " ,e.getMessage(), record.getStr("id"));
            }
        }
        long end = System.currentTimeMillis();
        logger.info("getlnsuranceTask 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }
}
