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

import com.alibaba.csp.sentinel.util.StringUtil;
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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author power
 * @ClassName
 * @description: 收款单业务登记 提交校验器
 * @date 2023/10/17
 */
public class GatheringSubmitOPValidator extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[]  datas = this.getDataEntities();
        for(ExtendedDataEntity temp:datas) {
            DynamicObject bill = temp.getDataEntity();
            DynamicObject billType = bill.getDynamicObject("yem_billtype");
            String billTypeNumber = YEM.isNotEmpty(billType)?billType.getString("number"):"";
            if(!"yem_gathering_TZF".equals(billTypeNumber)){
//                sumReceivableAmtVal(temp);
                shipClaimAmtVal(temp);
            }

        }
    }

    /**
     * @description 出运认领金额超额校验
     * @author xwudd
     * @param[1] temp
     * return
     * @time 2023/11/7 15:23
     */

    public void shipClaimAmtVal(ExtendedDataEntity temp){
        Set<Long> ids = new HashSet<>();
        Map<String,Map<String,BigDecimal>> allMap = new HashMap();
        DynamicObject dataEntity = temp.getDataEntity();
        DynamicObjectCollection receiptDetails = dataEntity.getDynamicObjectCollection("yem_receiptdetail");
        for(DynamicObject receiptDetail : receiptDetails){
            DynamicObjectCollection claimDetails = receiptDetail.getDynamicObjectCollection("yem_claimdetail");
            for (DynamicObject claimDetail : claimDetails){
                DynamicObject shippingDetails = claimDetail.getDynamicObject("yem_shippingdetailsno");
                if(YEM.isNotEmpty(shippingDetails)){
                    ids.add(shippingDetails.getLong("id"));
                    String billNo = shippingDetails.getString("billno");
                    if(allMap.containsKey(billNo)){
                        Map<String, BigDecimal> map = allMap.get(billNo);
                        BigDecimal shipClaimAmt = claimDetail.getBigDecimal("yem_shipclaimamt");
                        map.put("shipClaimAmt",shipClaimAmt.add((BigDecimal) map.get("shipClaimAmt")));
                    }else{
                        Map<String, BigDecimal> map = new HashMap();
                        BigDecimal shipAmt = claimDetail.getBigDecimal("yem_shipamt");
                        BigDecimal shipClaimAmt = claimDetail.getBigDecimal("yem_shipclaimamt");
                        map.put("shipAmt",shipAmt);
                        map.put("shipClaimAmt",shipClaimAmt);
                        allMap.put(billNo,map);
                    }
                }
            }
        }

        QFilter qFilter = new QFilter("yem_receiptdetail.yem_claimdetail.yem_shippingdetailsno", QCP.in, ids);
        qFilter.and(new QFilter("billstatus",QCP.in,new String[]{"B","C"}));
        qFilter.and(new QFilter("yem_bizstatus",QCP.equals,"A"));
        qFilter.and(new QFilter("id",QCP.not_equals,dataEntity.getPkValue()));
        String name = dataEntity.getDynamicObjectType().getName();
        String selectfields = DynamicObjectUtil.getSelectfields(name, false);
        selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,name,"yem_receiptdetail",false);
        selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,name,"yem_receiptdetail","yem_claimdetail",false);
        DynamicObject[] gatherings = BusinessDataServiceHelper.load(name, selectfields, qFilter.toArray());
        for(DynamicObject gathering : gatherings){
            receiptDetails = gathering.getDynamicObjectCollection("yem_receiptdetail");
            for(DynamicObject receiptDetail : receiptDetails){
                DynamicObjectCollection claimDetails = receiptDetail.getDynamicObjectCollection("yem_claimdetail");
                for(DynamicObject claimDetail : claimDetails){
                    DynamicObject shippingDetails = claimDetail.getDynamicObject("yem_shippingdetailsno");
                    if(YEM.isNotEmpty(shippingDetails)){
                        String billNo = shippingDetails.getString("billno");
                        if(allMap.containsKey(billNo)){
                            Map<String, BigDecimal> map = allMap.get(billNo);
                            BigDecimal shipAmt = claimDetail.getBigDecimal("yem_shipclaimamt");
                            map.put("shipClaimAmt",shipAmt.add((BigDecimal) map.get("shipClaimAmt")));
                        }
                    }
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        Set<String> billNos = allMap.keySet();
        for(String billNo : billNos){
            Map<String, BigDecimal> valueMap = allMap.get(billNo);
            BigDecimal shipAmt = valueMap.get("shipAmt");
            BigDecimal shipClaimAmt = valueMap.get("shipClaimAmt");
            if(shipClaimAmt.compareTo(shipAmt) > 0){
                sb.append(String.format("出运明细单号%s，出运累计认领金额%s,出运金额%s，超出出运金额%s，请调整数据重试！\n",billNo,shipClaimAmt.setScale(2),shipAmt.setScale(2),shipClaimAmt.subtract(shipAmt).setScale(2)));
            }
        }
        if(sb.length() > 0){
            this.addErrorMessage(temp,sb.toString());
        }
    }
}
