package com.yem.wm.syn.eas.submit.op;

import com.yem.wm.syn.eas.submit.utils.FilterUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.entity.validate.ValidationErrorInfo;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

public class OrderProReqPlugIn extends AbstractOperationServicePlugIn {
    private final static Log log = LogFactory.getLog(OrderProReqPlugIn.class);
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_dept");
        fieldKeys.add("yem_es_materialinfo");
        fieldKeys.add("yem_materiel");
        fieldKeys.add("yem_realpurmateriel");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        List<ExtendedDataEntity> selectedRows = e.getSelectedRows();
        ArrayList<ExtendedDataEntity> ArrayList = new ArrayList<>();
        for (ExtendedDataEntity selectedRow: selectedRows) {
            DynamicObject dataEntity = selectedRow.getDataEntity();
            Object pk = dataEntity.getPkValue();
            DynamicObject yemBilltype = dataEntity.getDynamicObject("yem_billtype");
            //如果单据类型不等于整机外购
            if (!"yem_orderproreqbill_WG".equals(yemBilltype.getString("number"))){
                ArrayList.add(selectedRow);
                continue;
            }

            //财务组织
            DynamicObject yemCompanyorg = dataEntity.getDynamicObject("yem_dept");
            String orgname = yemCompanyorg.getString("name");
            //单据编码
            String billno = dataEntity.getString("billno");
            ArrayList<String> list = new ArrayList<>();
            StringBuilder msg = new StringBuilder();
            //物料
            DynamicObjectCollection entry = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            for (DynamicObject entryEntity:entry) {
                DynamicObject material = entryEntity.getDynamicObject("yem_materiel");
                Boolean materials = FilterUtils.setMaterial(yemCompanyorg, material);
                if (materials){
                    String materialNum = material.getString("number");
                    msg.append("财务组织"+orgname+"物料编码为:"+materialNum+"不存在,请在EAS核准!\n");
                    list.add(materialNum);
                }

                DynamicObject realpurmateriel = entryEntity.getDynamicObject("yem_realpurmateriel");
                Boolean realpurmateriels = FilterUtils.setMaterial(yemCompanyorg, realpurmateriel);
                if (realpurmateriels){
                    String materialNum = realpurmateriel.getString("number");
                    msg.append("财务组织"+orgname+"实际采购物料编码为:"+materialNum+"不存在,请在EAS核准!\n");
                    list.add(materialNum);
                }
            }

            if (list.size()==0){
                ArrayList.add(selectedRow);
            }else{
                this.operationResult.addErrorInfo(new ValidationErrorInfo("",pk, 0,0,"err","提交失败","单据编码:"+billno+"提交失败,"+msg, ErrorLevel.Error));
            }
        }

        e.getSelectedRows().clear();
        if (ArrayList.size()==0){
            e.setCancel(true);
        }else {
            e.getSelectedRows().addAll(ArrayList);
        }

    }
}
