package com.yem.em.ass.assatlasdet.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;

/**
 * @author zhouc
 * @date 2024/1/13 18:02
 * @className AssAtlasdetDeletevalidatorEdit
 * @description 总成图册明细删除校验
 */
public class AssAtlasdetDeletevalidatorEdit extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
//            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_partnumber = dataEntity.getString("number");
            DynamicObject gettigx = gettigx(yem_partnumber);
            if (gettigx != null) {
                this.addErrorMessage(dataEntitie,
                        String.format("编码为【" + yem_partnumber + "】存在替换关系，禁止反审核！"));
            }
        }
    }

    public DynamicObject gettigx(String yem_repno) {
        DynamicObject object = null;
        QFilter qFilter = new QFilter("status", QCP.equals, "C");
        qFilter.and(new QFilter("yem_entryentity.yem_repno", QCP.equals, yem_repno));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_replace", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_replace", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_replace", selectfields, new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            object = data.get(0);
        }
        return object;
    }
}
