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

import com.yem.wm.utils.DynamicObjectUtil;
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.QueryServiceHelper;

import java.util.Date;

/**
 * @author zhouc
 * @date 2024/1/10 22:10
 * @className CompSubUnAuditvalidatorEdit
 * @description 替换关系反审核校验
 */
public class CompSubUnAuditvalidatorEdit extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            String yem_sourcetype = dataEntity.getString("yem_sourcetype");
//            if(yem_sourcetype.equals("BOM")){
//                this.addErrorMessage(dataEntitie,
//                        String.format("当前单据来源类型为【BOM变更单】，禁止反审核！"));
//            }
            DynamicObjectCollection yem_entryentity = dataEntity.getDynamicObjectCollection("yem_entryentity");
            for (int i = 0; i < yem_entryentity.size(); i++) {
                DynamicObject object = yem_entryentity.get(i);
                int seq = i + 1;
                String yem_repno = object.getString("yem_repno");
                String yem_repnumber = object.getString("yem_repnumber");
                DynamicObject gettigx = gettigx(yem_repno, yem_repnumber, dataEntity);
                if (gettigx != null) {
                    this.addErrorMessage(dataEntitie,
                            String.format("明细信息第【" + seq + "】行," +
                                    "在替换部件编码【" + yem_repno + "】下的替换零件【" + yem_repnumber + "】存在替换关系，禁止反审核！"));
                }
            }
        }
    }

    public DynamicObject gettigx(String yem_repno, String yem_repnumber, DynamicObject dataEntity) {
        DynamicObject object = null;
        QFilter qFilter = new QFilter("number", QCP.equals, yem_repno);//
//        qFilter.and(new QFilter("status", QCP.equals, "C"));
        qFilter.and(new QFilter("yem_entryentity.yem_partnumber", QCP.equals, yem_repnumber));
        qFilter.and(new QFilter("yem_entryentity.yem_partisreplace", QCP.equals, true));
        qFilter.and(new QFilter("id", QCP.not_equals, dataEntity.getLong("id")));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_assatlasdet", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assatlasdet", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlasdet", selectfields, new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            object = data.get(0);
        }
        return object;
    }

}
