package com.sifang.modules.task;

import cn.hutool.core.date.DateUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.IdGen;
import com.sifang.modules.sys.util.SqlBuilder;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

public class CustomerEntryTask  extends TimerTask {

    @Override
    public void run() {

    }

    public void checkEntryRentList() throws ParseException {
        SqlBuilder sqlBuilder = new SqlBuilder();

        sqlBuilder.line("SELECT r.*,e.merge_time,e.project_id,e.epc_price,e.pay_cycle,date_format(r.meet_time,'%Y-%m-%d %H:%i:%s') as meetTime,e.receiving_report_id FROM crm_customer_entry_rent r");
        sqlBuilder.line("LEFT JOIN `crm_customer_entry` e ON e.id=r.`entry_id`");
        sqlBuilder.line("WHERE r.rent_staus='0' AND DATE_FORMAT(r.meet_time, '%Y-%m-%d')<=CURDATE()");
        List<Record> list = null;
        try {
            list = Db.find(sqlBuilder.build(),sqlBuilder.params());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(list.size()>0){
            for(Record rd :list){

                String entryId = rd.getStr("entry_id") ;
                //安装总快熟
                int component_num = Db.queryInt("SELECT component_num FROM  `crm_accepting_report` WHERE id=?",rd.getStr("receiving_report_id"));
                String pay_cycle = rd.getStr("pay_cycle");//租金支付周期 1按月支付 2按季支付 3按年支付
              //计算出应付的日期
                String meet_time="";
                if("1".equals(pay_cycle)){
                     meet_time = DateUtils.subMonth(rd.getStr("meetTime"),1);
                } else if ("2".equals(pay_cycle)) {
                    meet_time = DateUtils.subMonth(rd.getStr("meetTime"),3);
                } else if ("3".equals(pay_cycle)) {
                    meet_time = DateUtils.subMonth(rd.getStr("meetTime"),12);
                }else{
                    continue;
                }
                //计算当期的天数
                long daysB = DateUtils.daysBetween(rd.getStr("meetTime"),meet_time);
                //并网时间
                Date merge_time = rd.getDate("merge_time");
                int year = DateUtils.getYear(merge_time,new Date());//并网日期到当前时间的年限
                if(year<1){
                    year=1;
                }
                BigDecimal unitPic = Db.queryBigDecimal("SELECT end_year FROM crm_project_rent_variable WHERE project_id=? AND start_year<=?<end_year AND STATUS='0'",rd.getStr("project_id"),year);
                if(unitPic==null){
                    continue;
                }
                BigDecimal amount = unitPic.multiply(new BigDecimal(daysB)).multiply(new BigDecimal(component_num)).divide(new BigDecimal(365),2, BigDecimal.ROUND_HALF_UP);

                //修改当期的状态值
                Db.update("UPDATE `crm_customer_entry_rent` SET pay_status='0',rent_staus='0' WHERE id=?",rd.getStr("id"));
                Db.update("INSERT INTO `crm_customer_entry_rent` VALUES(?,?,?,'',NULL,'0',?, ?,?,?)", IdGen.uuid(),entryId,rd.getBigDecimal("current_payable"),meet_time,amount,rd.getStr("power_station_code"),pay_cycle);

            }
        }

    }



}
