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

import com.yem.em.bd.bomchange.validator.BomChangeAuditvalidatorEdit;
import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.StringUtils;
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/10/24 11:47
 * @className SubstitutionAuditEdit
 * @description 替换关系【审核】服务插件
 */
public class SubstitutionAuditEdit 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_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");
    }

    @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");
                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);
                        AtlasUtils.Selectsubstitution(bill.getString("yem_pnumber"), bill, entryentity, "Head","yem_em_substitution");
                    }
                    AtlasUtils.UpReplaceStatus(bill.getString("yem_pnumber"),"Par","Audit");
                }
            }
        }
    }
}
