package com.yem.wm.es.Util;

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author xwudd
 * @date 15:03
 * @Description
 **/
public class PJUtils {

    /**
     * @description 汇总数量到 订单总台数
     * @author xwudd
     * return
     * @time 2023/7/29 16:38
     */

    public static void countOrderSumNumberPJ(IDataModel model){
        BigDecimal total = BigDecimal.ZERO;
        DynamicObject billType = (DynamicObject)model.getValue("yem_billtype");
        if(YEM.isNotEmpty(billType)){
            String number = billType.getString("number");
            int materialInfoRows = model.getEntryRowCount("yem_es_materialinfo");
            for (int i = 0; i < materialInfoRows; i++) {
                String businessSort = (String)model.getValue("yem_businesssort", i);
                if("P".equals(businessSort)){
                    total = total.add((BigDecimal)model.getValue("yem_qty",i));
                }
            }

        }
        model.setValue("yem_ordersumnumber",total);
    }


    /**
     * @description 配件计算费用信息分录信息
     * @author xwudd
     * return
     * @time 2023/8/4 15:47
     */

    public static void countCostSumAmountPJ(IDataModel model){
        int salesRrderRows = model.getEntryRowCount("yem_costinfor");
        BigDecimal bigCabinetQty = (BigDecimal) model.getValue("yem_bigcabinetnumber");
        BigDecimal minCabinetQty = (BigDecimal) model.getValue("yem_mincabinetnumber");
        Integer singleQty = (Integer) model.getValue("yem_singlenumber");
        BigDecimal rate = (BigDecimal) model.getValue("yem_exrate");

        for(int i = 0;i < salesRrderRows;i++){
            DynamicObject billInGWay = (DynamicObject)model.getValue("yem_bd_chargemode",i);
            String number = YEM.isEmpty(billInGWay)?"":billInGWay.getString("number");
            BigDecimal amt = BigDecimal.ZERO;
            if("JFFS-0002".equals(number)){
                BigDecimal bigCabinetPrice = (BigDecimal) model.getValue("yem_bigcabinetprice",i);
                BigDecimal minCabinetPrice = (BigDecimal) model.getValue("yem_mincabinetprice",i);
                amt = bigCabinetPrice.multiply(bigCabinetQty).add(minCabinetPrice.multiply(minCabinetQty));
            }
            if("JFFS-0003".equals(number)){
                BigDecimal ticketPrice = (BigDecimal) model.getValue("yem_ticketprice",i);
                amt = ticketPrice.multiply(new BigDecimal(singleQty));

            }
            if(rate.compareTo(BigDecimal.ZERO) > 0){
                model.setValue("yem_costsumamount",amt.divide(rate, RoundingMode.HALF_DOWN),i);
            }
            model.setValue("yem_costsumamountcoin",amt,i);
        }
    }



}
