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

import com.yem.wm.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @author zhouc
 * @date 2023/10/26 14:29
 * @className SubstitutionSubmitvalidatorEdit
 * @description 替换关系提交校验器
 */
public class SubstitutionSubmitvalidatorEdit extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            String yem_pnumber = dataEntity.getString("yem_pnumber");
            String yem_pname = dataEntity.getString("yem_pname");
            String yem_penname = dataEntity.getString("yem_penname");
            int yemPqty = dataEntity.getInt("yem_pqty");
            String yem_tnumber = dataEntity.getString("yem_tnumber");
            String yem_tname = dataEntity.getString("yem_tname");
            String yem_tenname = dataEntity.getString("yem_tenname");
            int yem_tqty = dataEntity.getInt("yem_tqty");
            DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
            if (yem_entryentity.size() > 0) {
                DynamicObject dynamicObject = yem_entryentity.get(0);
                String yem_onumber = dynamicObject.getString("yem_onumber");
                if (StringUtils.isNotEmpty(yem_onumber) && !yem_pnumber.equals(yem_onumber)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【原零件编码】与原零件信息的【零件编码】不相同，保存失败！"));
                }
                String yem_oname = dynamicObject.getString("yem_oname");
                if (StringUtils.isNotEmpty(yem_oname) && !yem_pname.equals(yem_oname)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【原零件名称】与原零件信息的【零件名称】不相同，保存失败！"));
                }
                String yem_oenname = dynamicObject.getString("yem_oenname");
                if (StringUtils.isNotEmpty(yem_oenname) && !yem_penname.equals(yem_oenname)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【原零件英文名称】与原零件信息的【英文名称】不相同，保存失败！"));
                }
                int yem_oqty = dynamicObject.getInt("yem_oqty");
                if (yem_oqty != yemPqty) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【原数量】与原零件信息的【数量】不相同，保存失败！"));
                }
                String yem_repnumber = dynamicObject.getString("yem_repnumber");
                if (StringUtils.isNotEmpty(yem_repnumber) && !yem_tnumber.equals(yem_repnumber)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【替换零件编码】与替换零件信息的【零件编码】不相同，保存失败！"));
                }
                String yem_repname = dynamicObject.getString("yem_repname");
                if (StringUtils.isNotEmpty(yem_repname) && !yem_tname.equals(yem_repname)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【替换零件名称】与替换零件信息的【零件名称】不相同，保存失败！"));
                }
                String yem_repenname = dynamicObject.getString("yem_repenname");
                if (StringUtils.isNotEmpty(yem_repenname) && !yem_tenname.equals(yem_repenname)) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【替换英文名称】与替换零件信息的【英文名称】不相同，保存失败！"));
                }
                int yem_repenqty = dynamicObject.getInt("yem_repenqty");
                if (yem_repenqty != yem_tqty) {
                    this.addErrorMessage(dataEntitie, String.format("明细信息第【1】行【替换数量】与替换零件信息的【数量】不相同，保存失败！"));
                }
                String yem_replace = dynamicObject.getString("yem_replace");
                if (StringUtils.isNotEmpty(yem_repnumber)) {
                    if (yem_replace == null || yem_replace == "") {
                        this.addWarningMessage(dataEntitie, String.format("明细信息第【1】行未选择【替换关系】，是否继续？"));
                    }
                }
            }

            QFilter qFilter = new QFilter("yem_pnumber", QCP.equals, yem_pnumber);//替换零件编码
//            qFilter.and(new QFilter("yem_replace1", QCP.not_equals, "C"));
            DynamicObject[] yem_em_substitution = BusinessDataServiceHelper.load("yem_em_substitution",
                    "id,yem_tnumber"
                    , new QFilter[]{qFilter}, null);
            List list = new ArrayList<>();
            if (yem_em_substitution.length > 0) {
                for (int i = 0; i < yem_em_substitution.length; i++) {
                    DynamicObject dynamicObject = yem_em_substitution[i];
                    if (!list.contains(dynamicObject.getString("yem_tnumber"))) {
                        list.add(dynamicObject.getString("yem_tnumber"));
                    }
                }
            }
            if (list.size() > 0) {
                this.addWarningMessage(dataEntitie, String.format("原零件【" + yem_pnumber + "】已存在【" + String.join(",", list) + "】替换关系，禁止再次新增替换关系，请确定是否继续？"));
            }
        }
    }
}
