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

import com.yem.wm.syn.eas.ImInQuirylistPJService;
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 ImInQuiryListPlugIn extends AbstractOperationServicePlugIn {
    private final static Log log = LogFactory.getLog(ImInQuiryListPlugIn.class);
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_billtype");
        fieldKeys.add("yem_companyorg");
        fieldKeys.add("yem_im_detailedinfor");
        fieldKeys.add("yem_material");
    }

    @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_im_inquirylist_PJ".equals(yemBilltype.getString("number"))){
                ArrayList.add(selectedRow);
                continue;
            }

            //财务组织
            DynamicObject yemCompanyorg = dataEntity.getDynamicObject("yem_companyorg");
            String orgname = yemCompanyorg.getString("name");
            //单据编码
            String billno = dataEntity.getString("billno");
            ArrayList<String> list = new ArrayList<>();
            StringBuilder msg = new StringBuilder();
            //物料
            DynamicObjectCollection entry = dataEntity.getDynamicObjectCollection("yem_im_detailedinfor");
            for (DynamicObject entryEntity:entry) {
                DynamicObject material = entryEntity.getDynamicObject("yem_material");
                Boolean materials = FilterUtils.setMaterial(yemCompanyorg, material);
                if (materials){
                    String materialNum = material.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);
        }

    }
}
