package com.yem.wm.im.tradeapp.form;

import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;

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

/**
 * @description: 国贸贷款欠条申请
 * @author yhj
 * @date 2023/7/26 14:55
 * @version 1.0
 */
public class AdvancePaymentAmountFormPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IFormView view = this.getView();
        FormShowParameter fsp = view.getFormShowParameter();
        OperationStatus status = fsp.getStatus();
        if(status == OperationStatus.ADDNEW){
            jointCarAndQty();
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int rowIndex = changeData[0].getRowIndex();//获取分录行数
        switch (name){
            case "yem_contractvalue":
            case "yem_collterms":
                GetAdvance(cd);
                break;
            case "yem_quantityrequired":
            case "yem_productmodel":
                jointCarAndQty();
                break;
        }
    }

    /**
     * @description: 计算预收金额
     * @author yhj
     * @date 2023/7/26 14:57
     * @version 1.0
     */
    private void GetAdvance(ChangeData cd) {
        int row = cd.getRowIndex();//分录行
        BigDecimal ratio=new BigDecimal("0.01");
        //获取收款条件的值
        DynamicObject applier =  (DynamicObject)this.getModel().getValue("yem_collterms");
        //判断收款条件是否为空
        if (applier != null){
            //获取收款条件对象
            DynamicObjectCollection entryentity = applier.getDynamicObjectCollection("entry");//收款条件分录
            BigDecimal contractvalue = (BigDecimal) this.getModel().getValue("yem_contractvalue");//合同金额
                    if (entryentity.size() > 0){
                        for (DynamicObject dynamicObject : entryentity) {
                            boolean ispre = dynamicObject.getBoolean("ispre");//预收标志
                            if (ispre == true) {
                                BigDecimal rate = dynamicObject.getBigDecimal("rate");//收款比例
                                BigDecimal amt=rate.multiply(contractvalue);
                                BigDecimal amts=amt.multiply(ratio);
                                this.getModel().setValue("yem_advanceamt",amts,row);
                            }
                        }
                    }else {
                        this.getModel().setValue("yem_advanceamt",null,row);
                    }
        }else {
            this.getModel().setValue("yem_advanceamt",null,row);
        }
    }

    /**
     * @description 拼接车辆机型及数量
     * @author xwudd
     * return
     * @time 2023/11/9 16:55
     */

    public void jointCarAndQty(){
        IDataModel model = this.getModel();
        int detailInfoRows = model.getEntryRowCount("yem_im_tradeapp_em");
        List<String> list = new ArrayList();
        Map<String,BigDecimal> map = new HashMap();
        for (int i = 0; i < detailInfoRows; i++) {
            DynamicObject productModel = (DynamicObject)model.getValue("yem_productmodel",i);
            if(YEM.isNotEmpty(productModel)){
                String number = productModel.getString("number");
                BigDecimal qty = (BigDecimal) model.getValue("yem_quantityrequired",i);
                if(map.containsKey(number)){
                    map.put(number,qty.add(map.get(number)));
                }else{
                    map.put(number,qty);
                }
            }
        }
        Set<String> keys = map.keySet();
        for(String key : keys){
            BigDecimal qty = map.get(key).setScale(0);
            list.add(String.format("%s件%s",qty,key));
        }
        String join = String.join("+", list);
        model.setValue("yem_carmodelqty",join);
    }
}
