package com.yem.wm.im.csrCreditLine.task;

import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Date 2024/1/25 21:02
 * @Created by ZZSLL
 */

public class CsrCreditLineTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        String selectfield = DynamicObjectUtil.getSelectfields("yem_im_csrcreditline", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_im_csrcreditline", "yem_im_info", false);
        // 客户信保额度申请
        DynamicObject[] csrcreditlines = BusinessDataServiceHelper.load("yem_im_csrcreditline", selectfields, null);
        for (DynamicObject dynamicObject : csrcreditlines) {
            // yem_countdown = yem_unused - today
            Date unused = dynamicObject.getDate("yem_unused");
//            if (YEM.isNotEmpty(unused)) {
//                data(dynamicObject, unused, "yem_countdown");
                BigDecimal yemInactivityperiod = dynamicObject.getBigDecimal("yem_inactivityperiod");
                Date date = dynamicObject.getDate("yem_recentlapse");
                recentl(dynamicObject, yemInactivityperiod, date);
//            }
            DynamicObjectCollection collection = dynamicObject.getDynamicObjectCollection("yem_im_info");
            for (DynamicObject object : collection) {
                Date receivableDate = object.getDate("yem_paydate1");
                if (receivableDate == null) continue;
                daysBetween(object, receivableDate, "yem_countpayback");
            }
        }
        SaveServiceHelper.save(csrcreditlines);

        DynamicObject[] storagetrans = BusinessDataServiceHelper.load("yem_es_storagetrans", "id, billno, yem_creditdetail, yem_creditdetail.yem_paydate1, yem_creditdetail.yem_countpayback", null);
        for (DynamicObject storagetran : storagetrans) {
            DynamicObjectCollection collection = storagetran.getDynamicObjectCollection("yem_creditdetail");
            for (DynamicObject object : collection) {
                Date receivableDate = object.getDate("yem_paydate1");
                if (receivableDate == null) continue;
                daysBetween(object, receivableDate, "yem_countpayback");
            }
        }
        SaveServiceHelper.save(storagetrans);
    }

    public static void recentl(DynamicObject dynamicObject, BigDecimal yemInactivityperiod, Date date) {
        if (date != null) {
            if (yemInactivityperiod.compareTo(BigDecimal.ZERO) != 0) {
                long daysInMilliseconds = yemInactivityperiod.longValue() * TimeUnit.DAYS.toMillis(1);
                Date yemRecentlapse = new Date(date.getTime() + daysInMilliseconds);
                dynamicObject.set("yem_unused", yemRecentlapse);
                Date newdate = new Date();
                long millisDifference = yemRecentlapse.getTime() - newdate.getTime();
                long daysDifference = TimeUnit.MILLISECONDS.toDays(millisDifference);
                BigDecimal bigDecimalDaysDifference = BigDecimal.valueOf(daysDifference);
                dynamicObject.set("yem_countdown", bigDecimalDaysDifference.add(BigDecimal.ONE));
//                } else {
//                    csrcreditline.set("yem_unused", yemRecentlapse);
            }
        }
    }

    /**
     * 计算时间差额
     *
     * @param object
     * @param receivableDate
     * @param yemCountpayback
     */
    private static void data(DynamicObject object, Date receivableDate, String yemCountpayback) {
        Date date = new Date();
        if (date.after(receivableDate)) {
            // 计算它们之间的时间差（毫秒），然后转换为天
//            long diffInMillis = receivableDate.getTime() - date.getTime();
//            long diffInDays = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
            //2024-11-07
            LocalDate start = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate over = receivableDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long diffInDays = over.toEpochDay() - start.toEpochDay();
            object.set(yemCountpayback, diffInDays);
        }
    }

    public static void daysBetween(DynamicObject object, Date date, String field) {
        LocalDate localDate = date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        LocalDate today = LocalDate.now();
        object.set(field, ChronoUnit.DAYS.between(localDate, today));
    }
}
