package com.yem.wm.fm.gathering.validator;

import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author xwudd
 * @Description
 * @date 11:12
 **/
public class ClaimCentersSaveValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            DynamicObject billType = dataEntity.getDynamicObject("yem_billtype");
            String billTypeNum = YEM.isNotEmpty(billType)?billType.getString("number"):"";
            if(!"yem_gathering_TZF".equals(billTypeNum)){
                salContractClaAmtExcessVal(dataEntitie);
            }
        }
    }


    private void salContractClaAmtExcessVal(ExtendedDataEntity dataEntitie){
        DynamicObject dataEntity = dataEntitie.getDataEntity();
        Map<Long, Map<String,BigDecimal>> allMap = new HashMap<>();
        Map<Long,String> billNoMap = new HashMap();
        DynamicObjectCollection receiptDetails = dataEntity.getDynamicObjectCollection("yem_receiptdetail");
        for(DynamicObject receiptDetail : receiptDetails){
            DynamicObjectCollection claimDetails = receiptDetail.getDynamicObjectCollection("yem_claimdetail");
            if (claimDetails.isEmpty()) continue;
            for(DynamicObject claimDetail : claimDetails){
                Object saleOrderObj = claimDetail.get("yem_salcontractno");
                if(YEM.isNotEmpty(saleOrderObj)){
                    DynamicObject saleOrder = null;
                    if (saleOrderObj instanceof Long) {
                        saleOrder = BusinessDataServiceHelper.loadSingle(saleOrderObj,"yem_es_salesorder");
                    } else {
                        saleOrder = (DynamicObject) saleOrderObj;
                    }
                    long id = saleOrder.getLong("id");
                    billNoMap.put(id,saleOrder.getString("billno"));
                    BigDecimal salContractAmt = claimDetail.getBigDecimal("yem_salcontractamt");
                    BigDecimal salContractClaAmt = claimDetail.getBigDecimal("yem_salcontractclaamt");
                    if (allMap.containsKey(id)) {
                        Map<String, BigDecimal> map = allMap.get(id);
                        map.put("salContractClaAmt",salContractClaAmt.add(map.get("salContractClaAmt")));
                    }else{
                        Map<String, BigDecimal> map = new HashMap<>();
                        map.put("salContractAmt",salContractAmt);
                        map.put("salContractClaAmt",salContractClaAmt);
                        allMap.put(id,map);
                    }
                }
            }
        }
        Set<Long> ids = allMap.keySet();
        String selectFields = DynamicObjectUtil.getSelectfields("yem_gathering", false);
        selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_gathering","yem_receiptdetail",false);
        selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_gathering","yem_receiptdetail","yem_claimdetail",false);
        DynamicObject[] gatherings = BusinessDataServiceHelper.load("yem_gathering", selectFields,
                new QFilter[]{new QFilter("yem_receiptdetail.yem_claimdetail.yem_salcontractno", QCP.in, ids),
                        new QFilter("billstatus", QCP.equals, "C"),
                        new QFilter("id", QCP.not_equals, dataEntity.getPkValue())});
        for(DynamicObject gathering : gatherings){
            receiptDetails = gathering.getDynamicObjectCollection("yem_receiptdetail");
            for(DynamicObject receiptDetail : receiptDetails) {
                DynamicObjectCollection claimDetails = receiptDetail.getDynamicObjectCollection("yem_claimdetail");
                for (DynamicObject claimDetail : claimDetails) {
                    DynamicObject saleOrder = claimDetail.getDynamicObject("yem_salcontractno");
                    if(YEM.isNotEmpty(saleOrder)){
                        long id = saleOrder.getLong("id");
                        BigDecimal salContractClaAmt = claimDetail.getBigDecimal("yem_salcontractclaamt");
                        if (allMap.containsKey(id)) {
                            Map<String, BigDecimal> map = allMap.get(id);
                            map.put("salContractClaAmt", salContractClaAmt.add(map.get("salContractClaAmt")));
                        }
                    }
                }
            }
        }
        for(Long id : ids){
            Map<String, BigDecimal> map = allMap.get(id);
            String billNo = billNoMap.get(id);
            BigDecimal salContractAmt = map.get("salContractAmt");
            BigDecimal salContractClaAmt = map.get("salContractClaAmt");
            if(salContractClaAmt.compareTo(salContractAmt) > 0){
                throw new KDBizException(String.format("外销合同号%s 的累计合同认领金额%s，合同金额%s，超出%s，收款认领超额，不允许认领！", billNo, salContractClaAmt.setScale(2, RoundingMode.HALF_UP), salContractAmt.setScale(2, RoundingMode.HALF_UP), salContractClaAmt.subtract(salContractAmt).setScale(2, RoundingMode.HALF_UP)));
            }
        }
    }
}
