package com.yem.em.bd.bomchange.op;

import com.yem.em.bd.bomchange.validator.BomChangeAuditvalidatorEdit;
import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.StringUtils;
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.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2023/12/25 21:44
 * @className BomChangeNewAuditEdit
 * @description 新BOM变更单【审核】服务插件
 */
public class BomChangeNewAuditEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("useorg");
        keys.add("number");
        keys.add("yem_completetime");
        keys.add("yem_remark");
        keys.add("yem_producttype");
        keys.add("yem_entryentity.yem_subno");
        keys.add("yem_entryentity.yem_subname");
        keys.add("yem_entryentity.yem_replace");
        keys.add("yem_entryentity.yem_qty");
        keys.add("yem_entryentity.yem_enablesubno");
        keys.add("yem_entryentity.yem_enablesubname");
        keys.add("yem_entryentity.yem_enablesubqty");
        keys.add("yem_entryentity.yem_subname");
        keys.add("yem_entryentity.yem_enablesta");
        keys.add("yem_entryentity.yem_isass");
        keys.add("yem_entryentity.yem_isresource");
        keys.add("yem_entryentity.yem_parentno");
        keys.add("yem_entryentity.yem_parentname");
        keys.add("yem_entryentity.yem_enableddate");
        keys.add("yem_entryentity.yem_downdate");
        keys.add("yem_entryentity.yem_subenname");
        keys.add("yem_entryentity.yem_enablesubenname");
        keys.add("yem_entryentity.yem_enableparno");
        keys.add("yem_entryentity.yem_enableparname");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new BomChangeAuditvalidatorEdit());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            for (DynamicObject bill : bills) {
                SetEnName(bill);
                DynamicObjectCollection yem_entryentity = bill.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject entryentity = yem_entryentity.get(i);
                    String yem_enablesta = entryentity.getString("yem_enablesta");
                    String yem_enablesubno = entryentity.getString("yem_enablesubno");
                    String yem_replace = entryentity.getString("yem_replace");
                    boolean yem_isass = entryentity.getBoolean("yem_isass");
                    boolean yem_isresource = entryentity.getBoolean("yem_isresource");
                    String yem_parentno = entryentity.getString("yem_parentno");//父件编码
                    String yem_subno = entryentity.getString("yem_subno");//子件编码
                    if (yem_isass || yem_isresource) {
                        if (yem_enablesta.equals("B") && StringUtils.isNotEmpty(yem_enablesubno)) {
                            QFilter qFilter = new QFilter("number", QCP.equals, yem_parentno);//替换零件编码
                            qFilter.and(new QFilter("yem_parno", QCP.equals, yem_subno));
                            qFilter.and(new QFilter("yem_sourcetype", QCP.equals, "BOM"));
                            DynamicObject yem_em_replace = BusinessDataServiceHelper.loadSingle("yem_em_replace", "id"
                                    , new QFilter[]{qFilter});
                            if (yem_em_replace != null) {
                                DynamicObject replace = BusinessDataServiceHelper.loadSingle(yem_em_replace.getLong("id"), "yem_em_replace");
                                if (replace != null) {
                                    AtlasUtils.Addreplace(bill, entryentity, replace, "yem_em_replace", "update");
                                }
                            } else {
                                AtlasUtils.Addreplace(bill, entryentity, null, "yem_em_replace", "add");//新增
                            }
                        }
                        if (StringUtils.isNotEmpty(yem_replace)) {
                            if (yem_replace.equals("D") || yem_replace.equals("E")) {
                                QFilter qFilter = new QFilter("number", QCP.equals, yem_parentno);//替换零件编码
                                qFilter.and(new QFilter("yem_parno", QCP.equals, yem_subno));
                                qFilter.and(new QFilter("yem_sourcetype", QCP.equals, "BOM"));
                                DynamicObject yem_em_replace = BusinessDataServiceHelper.loadSingle("yem_em_replace", "id"
                                        , new QFilter[]{qFilter});
                                if (yem_em_replace != null) {
                                    DynamicObject replace = BusinessDataServiceHelper.loadSingle(yem_em_replace.getLong("id"), "yem_em_replace");
                                    if (replace != null) {
                                        AtlasUtils.Addreplace(bill, entryentity, replace, "yem_em_replace", "update");
                                    }
                                } else {
                                    AtlasUtils.Addreplace(bill, entryentity, null, "yem_em_replace", "add");//新增
                                }
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * 根据编码获取英文名称
     *
     * @param bill
     */
    public void SetEnName(DynamicObject bill) {
        List asslist = new ArrayList<>();
        List resource = new ArrayList<>();
        DynamicObjectCollection yem_entryentity = bill.getDynamicObjectCollection("yem_entryentity");
        for (int i = 0; i < yem_entryentity.size(); i++) {
            DynamicObject entryentity = yem_entryentity.get(i);
            String yem_enablesta = entryentity.getString("yem_subno");
            String yem_enablesubno = entryentity.getString("yem_enablesubno");
            //部件
            boolean yem_isass = entryentity.getBoolean("yem_isass");
            if (yem_isass) {
                asslist.add(yem_enablesta);
                asslist.add(yem_enablesubno);
            }
            //资源
            boolean yem_isresource = entryentity.getBoolean("yem_isresource");
            if (yem_isresource) {
                resource.add(yem_enablesta);
                resource.add(yem_enablesubno);
            }
        }
        DynamicObjectCollection yem_em_assatlasdet = null;
        QFilter filter = new QFilter("status", QCP.not_equals, null);
        //查询分录总成
        if (asslist.size() > 0) {
            filter.and(new QFilter("yem_entryentity.yem_partnumber", QCP.in, asslist));
            yem_em_assatlasdet = QueryServiceHelper.query("yem_em_assatlasdet",
                    "id,yem_entryentity.yem_partenname yem_enname,yem_entryentity.yem_partnumber number"
                    , new QFilter[]{filter}, null);
        }
        //查询表头总成
        if (resource.size() > 0) {
            filter.and(new QFilter("number", QCP.in, resource));
            yem_em_assatlasdet = QueryServiceHelper.query("yem_em_assatlasdet", "id,name,number,yem_enname"
                    , new QFilter[]{filter}, null);
        }

        if (yem_em_assatlasdet != null) {
            Map map = new HashMap<>();
            for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
                DynamicObject dynamicObject = yem_em_assatlasdet.get(i);
                map.put(dynamicObject.getString("number"), dynamicObject.getString("yem_enname"));
            }
            if (map.size() > 0) {
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject entryentity = yem_entryentity.get(i);
                    String yem_enablesta = entryentity.getString("yem_subno");
                    String yem_enablesubno = entryentity.getString("yem_enablesubno");
                    if (map.containsKey(yem_enablesta)) {
                        entryentity.set("yem_subenname", map.get(yem_enablesta));
                    }
                    if (map.containsKey(yem_enablesubno)) {
                        entryentity.set("yem_enablesubenname", map.get(yem_enablesubno));
                    }
                }
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        bill.getDynamicObjectType().getName(),
                        new DynamicObject[]{bill}, OperateOption.create());
                if (Operate.isSuccess()) {

                }
            }
        }
    }
}
