package com.yem.wm.im.placeexamine.vaildator;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.*;

/**
 * @author zhouc
 * @date 2024/7/9 22:34
 * @className AlaceexamineUnAuditValidator
 * @description
 */
public class AlaceexamineUnAuditValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
        for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
            Map map = new HashMap<>();//存放跟单任务控制台内码
            DynamicObject dataEntity = extendedDataEntity.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            long yemSourcebillid = dataEntity.getLong("yem_sourcebillid");//外销合同id
            long id = dataEntity.getLong("id");//当前单据id
            DynamicObject yem_es_salesorder = BusinessDataServiceHelper.loadSingle(yemSourcebillid, "yem_es_salesorder");
            if (yem_es_salesorder != null) {
                DynamicObjectCollection yem_Shippingplan = yem_es_salesorder.getDynamicObjectCollection("yem_shippingplan_entry");
                for (int i = 0; i < yem_Shippingplan.size(); i++) {
                    DynamicObject Shippingplan = yem_Shippingplan.get(i);
                    DynamicObject yemPlaceexamine = Shippingplan.getDynamicObject("yem_placeexamine");
                    if (yemPlaceexamine != null) {
                        if (yemPlaceexamine.getLong("id") == id) {
                            DynamicObjectCollection yemPlandetailEntry = Shippingplan.getDynamicObjectCollection("yem_plandetail_entry");
                            for (int j = 0; j < yemPlandetailEntry.size(); j++) {
                                DynamicObject PlandetailEntry = yemPlandetailEntry.get(j);
                                if (PlandetailEntry != null) {
                                    DynamicObject yem_tracktaskconsole = PlandetailEntry.getDynamicObject("yem_tracktaskconsole");
                                    if (yem_tracktaskconsole != null) {
                                        if (!map.containsKey(yem_tracktaskconsole.getLong("id"))) {
                                            map.put(yem_tracktaskconsole.getLong("id"), yem_tracktaskconsole);
                                        }
                                    }

                                }
                            }
                            break;
                        }
                    }
                }
                Iterator iterator = map.keySet().iterator();
                while (iterator.hasNext()) {
                    String key = iterator.next().toString();
                    DynamicObject yem_tracktaskconsole = BusinessDataServiceHelper.loadSingle(key, "yem_tracktaskconsole");
                    //备料信息
                    DynamicObjectCollection yem_es_salesorder_r_s = yem_tracktaskconsole.getDynamicObjectCollection("yem_es_salesorder_r");
                    if (null != yem_es_salesorder_r_s && yem_es_salesorder_r_s.size() > 0) {
                        this.addErrorMessage(extendedDataEntity,"单据编号【" + yem_tracktaskconsole.getString("billno") + "】跟单任务控制台已下备料，不允许取消！");
                    }

                    String billstatus = yem_tracktaskconsole.getString("billstatus");
                    if (!"A".equals(billstatus)) {
                        this.addErrorMessage(extendedDataEntity,"单据编号【" + yem_tracktaskconsole.getString("billno") + "】跟单任务已提交/审核，不允许取消！");
                    }
                    //下游订单产品需求单
                    QFilter qFilter = new QFilter("yem_sourcebillid", QCP.equals, yem_tracktaskconsole.getLong("id"));
                    DynamicObject[] yem_orderproreqbill_s = BusinessDataServiceHelper.load("yem_orderproreqbill", "id,billno,billstatus",
                            new QFilter[]{qFilter}, null);
                    if (null != yem_orderproreqbill_s && yem_orderproreqbill_s.length > 0) {
                        this.addErrorMessage(extendedDataEntity,"单据编号【" + yem_tracktaskconsole.getString("billno") + "】跟单任务下游存在订单产品需求单，不允许取消！");
                    }
                }
            }
        }
    }
}
