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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.Calendar;
import java.util.Date;

/**
 * @author xwudd
 * @Description
 * @date 11:54
 **/
public class CsrCreditSubmitLineValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            String from = dataEntity.getDynamicObjectType().getName();
            if (from.equals("yem_im_csrcreditline")) {
                DynamicObject method = dataEntity.getDynamicObject("yem_payment");
                DynamicObject customer = dataEntity.getDynamicObject("yem_customer");
                DynamicObject org = dataEntity.getDynamicObject("org");
                Date bizDate = dataEntity.getDate("yem_bizdate");
                String swiftcode = dataEntity.getString("yem_swiftcode");
                Date yearBegin = getYearBegin(bizDate);
                Date yearLast = getYearLast(bizDate);
                QFilter qFilter = new QFilter("yem_customer", QCP.equals, customer.getPkValue());
                qFilter.and(new QFilter("org", QCP.equals, org.getPkValue()));
                qFilter.and(new QFilter("yem_bizdate", QCP.large_than, yearBegin));
                qFilter.and(new QFilter("yem_bizdate", QCP.less_equals, yearLast));
                qFilter.and(new QFilter("billstatus", QCP.in, new String[]{"B", "C"}));
                qFilter.and(new QFilter("yem_payment.id", QCP.equals, method.getLong("id")));
                if ("信用证".equals(method.getString("name"))) {
                    qFilter.and(new QFilter("yem_swiftcode", QCP.equals, swiftcode));
                }
                DynamicObject[] objs = BusinessDataServiceHelper.load(dataEntity.getDynamicObjectType().getName(), "id", qFilter.toArray());
                if (objs.length > 0) {
                    if ("信用证".equals(method.getString("name"))) {
                        this.addErrorMessage(dataEntitie, "同一年内，相同业务公司+客户+支付方式+SWIFT CODE，已存在一个客户信保额度申请单，请检查数据！");
                    } else {
                        this.addErrorMessage(dataEntitie, "同一年内，相同业务公司+客户+支付方式，已存在一个客户信保额度申请单，请检查数据！");
                    }
                }
            }
//           if(from.equals("yem_im_csrcreditline")){
//               BigDecimal yem_replylimit = dataEntity.getBigDecimal("yem_replylimit");
//               BigDecimal yem_totaloccupancy = dataEntity.getBigDecimal("yem_totaloccupancy");
//               BigDecimal yem_totalwriteoffamt = dataEntity.getBigDecimal("yem_totalwriteoffamt");
//           }
        }

    }

    /**
     * 获取当年最后一天
     *
     * @return
     */
    public static Date getYearBegin(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(1);
        calendar.set(year, 1, 1, 0, 0, 0);
        return calendar.getTime();
    }

    /**
     * 获取当年最后一天
     *
     * @return
     */
    public static Date getYearLast(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(1);
        calendar.set(year, 11, 31, 23, 59, 59);
        return calendar.getTime();
    }
}
