package com.yem.em.ass.compsub.op;

import com.yem.em.utils.AtlasUtils;
import com.yem.em.utils.SubRelationUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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 java.util.List;

/**
 * @author zhouc
 * @date 2023/11/1 23:12
 * @className CompSubAuditEdit
 * @description 部件替换关系【审核】服务插件
 */
public class CompSubAuditEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("number");
        keys.add("yem_pnumber");
        keys.add("yem_pname");
        keys.add("yem_penname");
        keys.add("yem_pqty");
        keys.add("yem_tnumber");
        keys.add("yem_tname");
        keys.add("yem_tenname");
        keys.add("yem_tqty");
        keys.add("yem_sourcetype");
        keys.add("yem_replacement");
        keys.add("yem_entryentity.yem_praonumber");
        keys.add("yem_entryentity.yem_praoname");

        keys.add("yem_entryentity.yem_onumber");
        keys.add("yem_entryentity.yem_oname");
        keys.add("yem_entryentity.yem_oenname");
        keys.add("yem_entryentity.yem_oqty");

        keys.add("yem_entryentity.yem_repnumber");
        keys.add("yem_entryentity.yem_repname");
        keys.add("yem_entryentity.yem_repenname");
        keys.add("yem_entryentity.yem_repenqty");
        keys.add("yem_entryentity.yem_replace");
        keys.add("yem_entryentity.yem_completetime");
    }

    @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) {
                String yem_sourcetype = bill.getString("yem_sourcetype");
                String yem_replacement = bill.getString("yem_replacement");//替换关系 Sub零件
                if (yem_sourcetype.equals("Add")) {
                    DynamicObjectCollection yem_entryentity = bill.getDynamicObjectCollection("yem_entryentity");
                    for (int i = 0; i < yem_entryentity.size(); i++) {
                        DynamicObject entryentity = yem_entryentity.get(i);
                        if (yem_replacement.equals("Sub")) {
                            AtlasUtils.Uprepassatlasdet(bill, entryentity, "Audit");
                        } else {
                            SubRelationUtils.UpdateCMMPTable(bill, entryentity, "Audit");
                        }
                    }
                }
            }
        }
    }
}
