package com.yem.wm.im.trackplace.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.HashMap;
import java.util.Map;

import static com.yem.wm.es.contactbook.op.ContactBookSaveOp.uploadAttachmentPanel;

/**
 * @author zhouc
 * @date 2023/9/27 20:15
 * @className TrackplaceSaveOp
 * @description 产品经理下单确认【保存】服务插件
 */
public class TrackplaceSaveOp extends AbstractOperationServicePlugIn {
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        if ("save".equals(e.getOperationKey()) && null != e.getDataEntities()) {
            DynamicObject[] bills = e.getDataEntities();
            for (int i = 0; i < bills.length; ++i) {
                DynamicObject bill = bills[i];
                bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());
                back_salesorder(bill);
                DynamicObject to = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), "yem_tracktaskconsole");
                AttachmentServiceHelper.remove("yem_tracktaskconsole", bill.getPkValue());
                uploadAttachmentPanel(bill, to);
            }
        }
    }

    /**
     * 反写外销合同资源号以及加装明细
     *
     * @param bill
     */
    public void back_salesorder(DynamicObject bill) {
        String billno = bill.getString("billno");
        //产品经理下单确认明细
        DynamicObjectCollection yem_es_materialinfo = bill.getDynamicObjectCollection("yem_es_materialinfo");
        for (int i = 0; i < yem_es_materialinfo.size(); i++) {
            Map map = new HashMap<>();
            DynamicObject materialinfo = yem_es_materialinfo.get(i);
            DynamicObject resourcenog = materialinfo.getDynamicObject("yem_resourcenog");//资源号
            DynamicObject yem_materiel = materialinfo.getDynamicObject("yem_materiel");//物料编码
            if (resourcenog != null) {
                long sourcebillid = materialinfo.getLong("yem_sourcebillid_d");//源单据ID
                long sourceentryid = materialinfo.getLong("yem_sourceentryid");//来源单据分录ID
                //根据源单id加载源单
                DynamicObject yem_es_salesorder = BusinessDataServiceHelper.loadSingle(sourcebillid, "yem_es_salesorder");
                if (yem_es_salesorder != null) {
                    //更新资源号以及加装明细
                    DynamicObjectCollection yem_salesordermaif = yem_es_salesorder.getDynamicObjectCollection("yem_es_materialinfo");
                    for (int j = 0; j < yem_salesordermaif.size(); j++) {
                        DynamicObject salesordermaif = yem_salesordermaif.get(j);
                        long id = salesordermaif.getLong("id");
                        if (sourceentryid == id) {
//                            //更新资源号
//                            salesordermaif.set("yem_resourcenog", resourcenog);//资源号
//                            salesordermaif.set("yem_materiel", yem_materiel);//物料编码
//                            if (yem_materiel != null) {//根据物料赋值物料相关
//                                salesordermaif.set("yem_materialname", yem_materiel.getString("name"));//商品名称
//                                salesordermaif.set("yem_materialenname", yem_materiel.getString("name"));//商品英文名称
//                                salesordermaif.set("yem_specification", yem_materiel.getString("modelnum"));//规格型号
//                                salesordermaif.set("yem_unit", yem_materiel.getDynamicObject("baseunit"));//计量单位
//                                salesordermaif.set("yem_baseunit", yem_materiel.getDynamicObject("baseunit"));//基本计量单位
//                            }
//                            //外销合同更新加装明细 //yem_retrofitting
                            DynamicObjectCollection yem_saleretrofitting = salesordermaif.getDynamicObjectCollection("yem_retrofitting");

                            //产品经理加装明细
                            DynamicObjectCollection yem_retrofitting = materialinfo.getDynamicObjectCollection("yem_retrofitting");
                            if (yem_retrofitting.size() > 0) {
                                yem_saleretrofitting.clear();
                                for (int k = 0; k < yem_retrofitting.size(); k++) {
                                    DynamicObject dynamicObject = yem_retrofitting.get(k);
                                    String retrid = dynamicObject.getString("id");
                                    DynamicObject aleretrofitting = yem_saleretrofitting.addNew();
                                    aleretrofitting.set("yem_prodno", billno);
                                    aleretrofitting.set("yem_retrofittingid", retrid);
                                    aleretrofitting.set("yem_albumassemblyno", dynamicObject.getDynamicObject("yem_albumassemblyno"));
                                    aleretrofitting.set("yem_resourceid1", dynamicObject.getDynamicObject("yem_resourceid1"));
                                    aleretrofitting.set("yem_materiel1", dynamicObject.getDynamicObject("yem_materiel1"));
                                    aleretrofitting.set("yem_unit1", dynamicObject.getDynamicObject("yem_unit1"));
                                    aleretrofitting.set("yem_singleqty", dynamicObject.getBigDecimal("yem_singleqty"));
                                    aleretrofitting.set("yem_qty1", dynamicObject.getBigDecimal("yem_qty1"));
                                    aleretrofitting.set("yem_qtyamt", dynamicObject.getBigDecimal("yem_qtyamt"));
                                    aleretrofitting.set("yem_desc", dynamicObject.getString("yem_desc"));
                                }
                            }
                        }
                    }
                    //更新
//                    SaveServiceHelper.save(yem_es_salesorder);
                    SaveServiceHelper.save(new DynamicObject[]{yem_es_salesorder});
                }
            }
        }
    }
}
