package com.yem.wm.es.shippingdetails.op;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.kingdee.cosmic.ctrl.common.CtrlUtil;
import com.yem.wm.bc.companytitle.form.CompanyTitleEdit;
import com.yem.wm.es.Util.BoxTypeDataGen;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * @author 张立攀
 * @version 1.0
 * @description   出运明细 保存按钮
 * @date 2023-09-05 10:49
 */
public class ShippingDetailSaveOp extends AbstractOperationServicePlugIn
{

    private static final Log log = LogFactory.getLog(CompanyTitleEdit.class);

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e)
    {
        e.getFieldKeys().add("yem_es_materialinfo");
        e.getFieldKeys().add("yem_es_salesorder_z");
        e.getFieldKeys().add("yem_ifdelivered");
        e.getFieldKeys().add("yem_sourcebillno");
        e.getFieldKeys().add("yem_soureceid");

    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e)
    {
        DynamicObject[] dataEntities1 = e.getDataEntities();
        for (DynamicObject temp : dataEntities1) {
            String yemsourcebillno = temp.getString("yem_sourcebillno");
            if (YEM.isNotEmpty(yemsourcebillno)) {
                DynamicObjectCollection yem_es_materialinfo = temp.getDynamicObjectCollection("yem_es_materialinfo");
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", "", new QFilter[]{
                        new QFilter("billno", QCP.equals, yemsourcebillno),
                        new QFilter("billstatus", QCP.equals, "C")
                });
                if (dynamicObject != null) {
                    dynamicObject = BusinessDataServiceHelper.loadSingle(dynamicObject.getPkValue(), dynamicObject.getDynamicObjectType().getName());
                    DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("yem_es_materialinfo");
                    String selectfields = DynamicObjectUtil.getSelectfields("yem_tracktaskconsole", false);
                    selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_tracktaskconsole", "yem_es_salesorder_r", false);
                    selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_tracktaskconsole", "yem_es_materialinfo", false);
                    selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields, "yem_tracktaskconsole", "yem_es_salesorder_r", "yem_es_salesorder_z", false);
                    //更新 跟单任务控制台 是否发运 字段
                    DynamicObject[] tracktaskconsoles = BusinessDataServiceHelper.load("yem_tracktaskconsole", selectfields,
                            new QFilter[]{
//                                    new QFilter("billstatus", QCP.equals, "A"),
                                    new QFilter("yem_sourcebillid", QCP.equals, dynamicObject.getPkValue())});
                    //出运整机分录 来源id集合
                    List<Long> nextList = new ArrayList();
                    //外销合同中 出运反写后的整机信息分录集合
                    List<DynamicObject> endSale = new ArrayList();
                    for (DynamicObject nextColl : yem_es_materialinfo) {
                        DynamicObjectCollection nextEntry = nextColl.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (DynamicObject nEntry : nextEntry) {
                            long yemSoureceid = nEntry.getLong("yem_soureceid");
                            nextList.add(yemSoureceid);
                        }
                    }
                    for (DynamicObject topColl : dynamicObjectCollection) {
                        DynamicObjectCollection topEntry = topColl.getDynamicObjectCollection("yem_es_salesorder_z");
                        int rowIndex = 0;
                        for (DynamicObject tEntry : topEntry) {
                            //出运中是否 存在外销的行数据
                            boolean flag = true;
                            if (tEntry != null) {
                                if (nextList.size() == 0) {
                                    tEntry.set("yem_ifdelivered", false);
                                    topEntry.set(rowIndex, tEntry);
                                    continue;
                                }
                                for (long lg : nextList) {
                                    long pkValue = (long) tEntry.getPkValue();
                                    if (YEM.isNotEmpty(lg) && lg == pkValue) {
                                        tEntry.set("yem_ifdelivered", true);
                                        topEntry.set(rowIndex, tEntry);
                                        flag = false;
                                    }
                                }
                                if (flag) {
                                    tEntry.set("yem_ifdelivered", false);
                                    topEntry.set(rowIndex, tEntry);
                                }


                            }
                            rowIndex++;
                            endSale.add(tEntry);
                        }

                    }
                    //更新 跟单任务台字段

                    for (DynamicObject tracktask : tracktaskconsoles) {
                        DynamicObjectCollection materialinfo = tracktask.getDynamicObjectCollection("yem_es_materialinfo");
                        DynamicObjectCollection salesorder_r = tracktask.getDynamicObjectCollection("yem_es_salesorder_r");
                        List<DynamicObject> trackList = new ArrayList();
                        for (DynamicObject mater : materialinfo) {
                            for (DynamicObject topColl : dynamicObjectCollection) {
                                long yemSourceentryid = mater.getLong("yem_sourceentryid");
                                if (yemSourceentryid == (long) topColl.getPkValue()) {
                                    trackList.add(mater);
                                }
                            }
                        }

                        for (DynamicObject object : materialinfo) {
                            for (DynamicObject saler : salesorder_r) {
                                long pk = (long) object.getPkValue();
                                long yemEntrtyid = saler.getLong("yem_entrtyid");
                                if (pk == yemEntrtyid) {
                                    DynamicObjectCollection saleZ = saler.getDynamicObjectCollection("yem_es_salesorder_z");
                                    int row = 0;
                                    for (DynamicObject saleZEntry : saleZ) {
                                        long zKey = (long) saleZEntry.getPkValue();
                                        for (DynamicObject endZ : endSale) {
                                            long yemTracktaskmachinid = endZ.getLong("yem_tracktaskmachinid");
                                            if (YEM.isNotEmpty(yemTracktaskmachinid) && yemTracktaskmachinid == zKey) {
                                                boolean yemIfdelivered = endZ.getBoolean("yem_ifdelivered");
                                                saleZEntry.set("yem_ifdelivered", yemIfdelivered);
                                                saleZ.set(row, saleZEntry);
                                            }
                                        }
                                        row++;
                                    }
                                }
                            }
                        }


                    }
                    SaveServiceHelper.update(new DynamicObject[]{dynamicObject});
                    SaveServiceHelper.update(tracktaskconsoles);
                }


            }
        }
        if ("save".equals(e.getOperationKey())) {
            // 单据保存时根据明细信息  大柜和小柜数量不为空的明细行  的箱型箱量，去重合并拼接生成
            for (DynamicObject entity : dataEntities1) {
                BoxTypeDataGen.genBoxTypeData(entity);
            }
        }
    }
}
