package com.yem.wm.im.orderproreqbill.botp;

import com.yem.wm.es.Util.ViewInterfaceDocking;
import com.yem.wm.utils.StringUtils;
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.ExtendedDataEntitySet;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.entity.datamodel.IDataModel;
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.List;
import java.util.Map;

/**
 * @author zhouc
 * @date 2023/8/14 19:13
 * @className tracktaskconsoleToorderproreqbillPugIn
 * @description 跟单任务控制台下推订单产品需求单
 */
public class tracktaskconsoleToorderproreqbillPugIn extends AbstractConvertPlugIn {
    @Override
    public void afterConvert(AfterConvertEventArgs e) {
        super.afterConvert(e);
        ExtendedDataEntitySet entitySet = e.getTargetExtDataEntitySet();
        ExtendedDataEntity[] entitys = entitySet.FindByEntityKey(this.getTgtMainType().toString());
        for (int i = 0; i < entitys.length; ++i) {
            ExtendedDataEntity extend = entitys[i];
            DynamicObject bill = extend.getDataEntity();
            orderproreqbill(bill);
        }
    }

    public void orderproreqbill(DynamicObject bill) {
        //订单产品需求单明细信息
        DynamicObjectCollection yem_es_materialinfo = bill.getDynamicObjectCollection("yem_es_materialinfo");
        if (yem_es_materialinfo.size() > 0) {
            long yem_sourcebillid_d = yem_es_materialinfo.get(0).getLong("yem_sourcebillid_d");//源单内码
            //根据源单内码获取源单数据
            QFilter qFilter = new QFilter("id", QCP.equals, yem_sourcebillid_d);
//            DynamicObject tracktaskconsole = BusinessDataServiceHelper.loadSingle("yem_tracktaskconsole", "id,billno", qFilter.toArray());
            DynamicObject tracktaskconsole = BusinessDataServiceHelper.loadSingle(yem_sourcebillid_d, "yem_tracktaskconsole");
            if (tracktaskconsole != null) {
                //跟单任务控制台商品信息
                DynamicObjectCollection tracktinfo = tracktaskconsole.getDynamicObjectCollection("yem_es_materialinfo");
                //订单产品需求单明细信息
                for (int i = 0; i < yem_es_materialinfo.size(); i++) {
                    DynamicObject materialinfo = yem_es_materialinfo.get(i);
                    //根据视图查询配件物料制造商、仓库、库位库区、供应商
                    bringWithMaterial(materialinfo, bill);
                    //订单产品需求单多功能字单据体
                    DynamicObjectCollection submaterialinfo = materialinfo.getDynamicObjectCollection("yem_orderproreqbill_s"); //子单据体
                    submaterialinfo.clear();
                    DynamicObjectCollection subretroinfo = materialinfo.getDynamicObjectCollection("yem_retrofitting"); //子单据体
                    subretroinfo.clear();
                    //订单产品需求单明细信息来源单据分录内码-商品信息内码
                    long yem_trackentryid = materialinfo.getLong("yem_trackentryid");
                    //订单产品需求单明细信息数量
                    BigDecimal yem_qty = materialinfo.getBigDecimal("yem_qty");
                    for (int j = 0; j < tracktinfo.size(); j++) {
                        DynamicObject tracktinfo_s = tracktinfo.get(j);
                        if (tracktinfo_s.getLong("id") == yem_trackentryid) {
                            //跟单任务控制台多功能配置子单据体
                            DynamicObjectCollection yem_multiconfig = tracktinfo_s.getDynamicObjectCollection("yem_multiconfig");
                            for (int k = 0; k < yem_multiconfig.size(); k++) {
                                DynamicObject multiconfig = yem_multiconfig.get(k);
//                                DynamicObject subogj = submaterialinfo.addNew();
                                DynamicObject subogj = new DynamicObject(submaterialinfo.getDynamicObjectType());
                                subogj.set("yem_configurtype", multiconfig.getString("yem_configurtype"));
                                subogj.set("yem_productstree", multiconfig.getDynamicObject("yem_productmodel_m"));
                                subogj.set("yem_resourceid", materialinfo.getDynamicObject("yem_resourcenog"));
                                subogj.set("yem_configurname", multiconfig.getString("yem_configurname"));
                                subogj.set("yem_arguments", multiconfig.getString("yem_arguments"));
                                subogj.set("yem_remarkss", multiconfig.getString("yem_remarkss"));
                                subogj.set("yem_unitsetnumber", multiconfig.getBigDecimal("yem_unitsetnumber"));
                                subogj.set("yem_unitset", yem_qty);
                                subogj.set("yem_amounts", yem_qty.multiply(multiconfig.getBigDecimal("yem_unitsetnumber")));
                                submaterialinfo.add(subogj);
                            }
                            //跟单任务控制加装明细子单据体
                            DynamicObjectCollection yem_retrofitting = tracktinfo_s.getDynamicObjectCollection("yem_retrofitting");
                            for (int k = 0; k < yem_retrofitting.size(); k++) {
                                DynamicObject multiconfig = yem_retrofitting.get(k);
//                                DynamicObject subogj = submaterialinfo.addNew();
                                DynamicObject subogj = new DynamicObject(subretroinfo.getDynamicObjectType());
                                subogj.set("yem_resourceid1", materialinfo.getDynamicObject("yem_resourcenog"));
                                subogj.set("yem_materiel1", multiconfig.getDynamicObject("yem_materiel1"));
                                subogj.set("yem_unit1", multiconfig.getDynamicObject("yem_unit1"));
                                subogj.set("yem_singleqty", multiconfig.getBigDecimal("yem_singleqty"));
                                subogj.set("yem_qty1", yem_qty);
                                subogj.set("yem_qtyamt", yem_qty.multiply(multiconfig.getBigDecimal("yem_singleqty")));
                                subogj.set("yem_desc", multiconfig.getString("yem_desc"));
                                subretroinfo.add(subogj);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 明细根据物料自动携带制造商、仓库、库位库区、供应商
     *
     * @param materialinfo 商品信息分录
     * @param bill
     */
    private void bringWithMaterial(DynamicObject materialinfo, DynamicObject bill) {
        DynamicObject materialObj = materialinfo.getDynamicObject("yem_realpurmateriel");
        String materialNumber = "";
        if (materialObj != null) {
            materialNumber = materialObj.getString("number");
        }
        //取调入调出仓库编码
        DynamicObject yem_warehouse = materialinfo.getDynamicObject("yem_warehouse");
        String yem_warehouseNum = "";
        if (yem_warehouse != null) yem_warehouseNum = yem_warehouse.getString("number");
        DynamicObject yem_outwhcode = materialinfo.getDynamicObject("yem_outwhcode");
        String yem_outwhcodeNum = "";
        if (yem_outwhcode != null) yem_outwhcodeNum = yem_outwhcode.getString("number");

        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> map1 = new HashMap<>();
        long yem_bd_productsid = bill.getDynamicObject("yem_bd_products").getLong("id");
        //不是制造中心仓库类型根据CMMP视图携带调入调出库位、库区、仓管员
        if (!"Z".equals(bill.getString("yem_isvmi"))) {
            if (materialObj != null) {
                map.put("mater_code", materialNumber);
                map.put("wh_code", yem_warehouseNum);
                map1.put("mater_code", materialNumber);
                map1.put("wh_code", yem_outwhcodeNum);
                List<Map> list = ViewInterfaceDocking.docking(ViewInterfaceDocking.MATERIAL_WAREHOUSE, map, yem_bd_productsid);
                if (YEM.isNotEmpty(list) && !list.isEmpty()) {
//                    DynamicObject whObj = BusinessDataServiceHelper.loadSingle("yem_warehouse", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("wh_code"))});
                    //库位
                    DynamicObject yem_whlocation = BusinessDataServiceHelper.loadSingle("yem_whlocation", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("position_code"))});
                    //库区
                    DynamicObject yem_warearea = BusinessDataServiceHelper.loadSingle("yem_warearea", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("area_code"))});
                    //仓库
//                    materialinfo.set("yem_warehouse", whObj);
                    //库区
                    materialinfo.set("yem_warearea", yem_warearea);
                    //库位
                    materialinfo.set("yem_whlocation", yem_whlocation);
                    //仓管员
//                    DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("stocker_code"))});
//                    materialinfo.set("yem_whman", user);
                }
                List<Map> list1 = ViewInterfaceDocking.docking(ViewInterfaceDocking.MATERIAL_WAREHOUSE, map1, yem_bd_productsid);
                if (YEM.isNotEmpty(list1) && !list1.isEmpty()) {
//                    DynamicObject whObj = BusinessDataServiceHelper.loadSingle("yem_warehouse", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("wh_code"))});
                    DynamicObject warearea = BusinessDataServiceHelper.loadSingle("yem_warearea", new QFilter[]{new QFilter("number", QCP.equals, list1.get(0).get("position_code"))});
                    DynamicObject whlocationObj = BusinessDataServiceHelper.loadSingle("yem_whlocation", new QFilter[]{new QFilter("number", QCP.equals, list1.get(0).get("area_code"))});
                    //仓库
//                    materialinfo.set("yem_warehouse", whObj);
                    //库区
                    materialinfo.set("yem_outareacode", warearea);
                    //库位
                    materialinfo.set("yem_outpositioncode", whlocationObj);
                    //仓管员
//                    DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("stocker_code"))});
//                    materialinfo.set("yem_outstockercode", user);
                }
                getMaterailAllpro(materialinfo, map, yem_bd_productsid);
            }
        }
        if ("yem_orderproreqbill_CG".equals(bill.getString("yem_billtype"))) {
            getMaterailAllpro(materialinfo, map, yem_bd_productsid);
        }
    }

    /**
     * 根据物料编码获取供应商、制造商、质检员、采购员
     *
     * @param materialinfo
     * @param map
     * @param yem_bd_productsid
     */
    private static void getMaterailAllpro(DynamicObject materialinfo, HashMap<String, Object> map, long yem_bd_productsid) {
        List<Map> list;
        list = ViewInterfaceDocking.docking(ViewInterfaceDocking.MATERIAL_INFO, map, yem_bd_productsid);
        if (YEM.isNotEmpty(list)) {
            //供应商
            DynamicObject supObj = BusinessDataServiceHelper.loadSingle("bd_supplier", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("sup_code"))});
            materialinfo.set("yem_sup", supObj);
            //制造商
            materialinfo.set("yem_manufacturer", list.get(0).get("supplier_code"));
            materialinfo.set("yem_manufacturername", list.get(0).get("supplier_name"));
        }
        //质检员
        list = ViewInterfaceDocking.docking(ViewInterfaceDocking.QUALITY_USER_INFO, map, yem_bd_productsid);
        if (YEM.isNotEmpty(list) && list.size() == 1) {
            DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("checker_code"))});
            materialinfo.set("yem_qualitierr", user);
        }
        //采购员
        list = ViewInterfaceDocking.docking(ViewInterfaceDocking.MATERIAL_BUYER, map, yem_bd_productsid);
        if (YEM.isNotEmpty(list) && list.size() == 1) {
            DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, list.get(0).get("buyer_code"))});
            materialinfo.set("yem_buyerr", user);
        }
    }
}
