package com.yem.wm.es.contractamtlist.form;

import com.yem.wm.es.contractamtlist.util.FunUtil;
import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * @author xwudd
 * @Description
 * @date 17:42
 **/
public class ContractAmtListEdit extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        DynamicObject obj = parentModel.getDataEntity(true);
        String name = parentModel.getDataEntityType().getName();
        Map<String, Object> customParams = view.getFormShowParameter().getCustomParams();
        String flag = (String)customParams.get("flag");
        if("yem_es_salesorder".equals(name)){
            DynamicObject[] saleOrders = new DynamicObject[1];
            saleOrders[0] = obj;
            FunUtil.setContractInfo(dataEntity,saleOrders,obj,null);
        }
        if("yem_es_exportpresent".equals(name)){
            List<Long> ids = new ArrayList();
            DynamicObjectCollection exportPresents = obj.getDynamicObjectCollection("yem_es_exportpre_el");
            for(DynamicObject exportPresent : exportPresents){
                long coreBillId = exportPresent.getLong("yem_corebillid_d");
                ids.add(coreBillId);
            }
            String selectFields = DynamicObjectUtil.getSelectfields("yem_es_salesorder", false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_es_salesorder","yem_es_materialinfo",false);
            selectFields = DynamicObjectUtil.getEntrySelectfields(selectFields,"yem_es_salesorder","yem_es_salescontrac_s",false);
            DynamicObject[] salesOrders = BusinessDataServiceHelper.load("yem_es_salesorder", selectFields,
                    new QFilter[]{new QFilter("id", QCP.in, ids)});
            FunUtil.setContractInfo(dataEntity,salesOrders,obj,null);
        }
        if("yem_shippingdetails".equals(name)){
            long coreBillId = obj.getLong("yem_corebillid");
            DynamicObject saleOrder = FunUtil.getSaleOrder(coreBillId);
            DynamicObject[] saleOrders = new DynamicObject[1];
            saleOrders[0] = saleOrder;
            FunUtil.setContractInfo(dataEntity,saleOrders,obj,flag);
        }
        if("yem_es_delivernotice".equals(name)){
            long coreBillId = obj.getLong("yem_corebillid");
            DynamicObject saleOrder = FunUtil.getSaleOrder(coreBillId);
            DynamicObject[] saleOrders = new DynamicObject[1];
            saleOrders[0] = saleOrder;
            FunUtil.setContractInfo(dataEntity,saleOrders,obj,flag);
        }
        setVis(view,name,flag);
        view.updateView();
    }



    public void setVis(IFormView view,String name,String flag){
        switch(name){
            case "yem_shippingdetails" :
                view.setVisible(false,"yem_thisdeliveramt","yem_thisdeliverdebtamt");
                break;
            case "yem_es_delivernotice" :
                view.setVisible(false,"yem_thisshipamt","yem_thisshipdebtamt","yem_thisshipapplyresmta");
                break;
        }
    }
}
