package com.yem.wm.im.priceLibrary.validator;/*
 *
 * @auther: xwudd
 * @Description
 * @Date: 2023/7/20
 */

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.*;

/**
 * @Description 价格库审核校验器
 *              1.组合字段唯一性校验维度为：相同单据类型、相同产品类型、相同产品型号（整机校验）/相同物料编码（配件校验），生效期重合的数据只允许有一条数据
 **/
public class PriceLibraryAuditValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
//            List<QFilter> qFilter = new ArrayList<>();
//            DynamicObject dataEntity = dataEntitie.getDataEntity();
//            DynamicObject billType = dataEntity.getDynamicObject("yem_billtype");//单据类型
//            if(billType != null){
//                qFilter.add(new QFilter("yem_billtype.id", QCP.equals,billType.getLong("id")));
//            }
//            DynamicObject products = dataEntity.getDynamicObject("yem_bd_products");//产品类型
//            if(products != null){
//                qFilter.add(new QFilter("yem_bd_products.id", QCP.equals,products.getLong("id")));
//            }
//            qFilter.add(new QFilter("billstatus",QCP.equals,"C"));
//            Date startDate = dataEntity.getDate("yem_startdate");
//            Date endDate = dataEntity.getDate("yem_enddate");
//            QFilter dateLimitQFilter = new QFilter("yem_startdate", QCP.large_than, startDate).and(new QFilter("yem_startdate", QCP.less_equals, endDate));
//            dateLimitQFilter.or(new QFilter("yem_startdate",QCP.less_equals,startDate).and(new QFilter("yem_enddate",QCP.large_equals,startDate)));
//            qFilter.add(dateLimitQFilter);
//            String number = billType.getString("number");
//            List<String> names = new ArrayList<>();
//            if("yem_pricelibrary_basic".equals(number)){//整机
//                DynamicObjectCollection basicModels = dataEntity.getDynamicObjectCollection("yem_basicmodel");
//                for(DynamicObject basicModel : basicModels){
//                    DynamicObject model = basicModel.getDynamicObject("yem_model");
//                    if(model != null){
//                        Long id = model.getLong("id");
//                        qFilter.add(new QFilter("yem_basicmodel.yem_model",QCP.equals,id));
//                        DynamicObject[] loads = BusinessDataServiceHelper.load(dataEntity.getDynamicObjectType().getName(), "id,billno", qFilter.toArray(qFilter.toArray(new QFilter[qFilter.size()])));
//                        if(loads.length > 0){
//                            names.add(model.getString("name"));
//                        }
//                    }
//                }
//                if(names.size() > 0){
//                    this.addErrorMessage(dataEntitie,String.format("单据类型为：%s,产品类型为：%s,产品型号为：%s，生效期重合的数据只允许有一条数据",billType.getString("name"),products.getString("name"),String.join(",",names)));
//                }
//
//            }
//            if("yem_pricelibrary_acc".equals(number)){//配件
//                DynamicObjectCollection accessorys = dataEntity.getDynamicObjectCollection("yem_accessory");
//                for(DynamicObject accessory : accessorys){
//                    DynamicObject material = accessory.getDynamicObject("yem_material");
//                    if(material != null){
//                        Long id = material.getLong("id");
//                        qFilter.add(new QFilter("yem_accessory.yem_material",QCP.equals,id));
//                        DynamicObject[] loads = BusinessDataServiceHelper.load(dataEntity.getDynamicObjectType().getName(), "id,billno", qFilter.toArray(qFilter.toArray(new QFilter[qFilter.size()])));
//                        if(loads.length > 0){
//                            names.add(material.getString("name"));
//                        }
//                    }
//                }
//                if(names.size() > 0){
//                    this.addErrorMessage(dataEntitie,String.format("单据类型为：%s,产品类型为：%s,物料编码为：%s，生效期重合的数据只允许有一条数据",billType.getString("name"),products.getString("name"),String.join(",",names)));
//                }
//            }

        }
    }
}
