package com.yem.wm.im.yearlybudget.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;

import java.util.HashMap;
import java.util.Map;
/**
 * @author 张立攀
 * @ClassName YearlyBudgetValidator
 * @description: 年度预算单-自定义校验器
 * @date 2023年07月21日
 * @version: 1.0
 */
public class YearlyBudgetValidator extends AbstractValidator {
    //单据状态
    public final static String  salesVolume = "yem_salesvolume";
    public final static String  entry = "yem_im_detailedinfor";
    public final static String  sonEntry = "yem_im_monthlyplan";
    public final static String[] idents = {"yem_january", "yem_february", "yem_march", "yem_april", "yem_may", "yem_june",
            "yem_july", "yem_august", "yem_september", "yem_october", "yem_november", "yem_december"};

    @Override
    public void validate() {
        ExtendedDataEntity[]  datas = this.getDataEntities();
        StringBuffer info = new StringBuffer();
        boolean flag = false;
        int row = 1;

        for(ExtendedDataEntity temp:datas) {
            DynamicObject dataEntity = temp.getDataEntity();
            DynamicObjectCollection dyColl = dataEntity.getDynamicObjectCollection("yem_im_detailedinfor");
            Map<String,Object> map = new HashMap<String,Object>();
                for(DynamicObject dy:dyColl) {
                    String yemsalesman = dy.getString("yem_salesman.name");
                    String yemclient = dy.getString("yem_client.name");
                    String yemproductmodel = dy.getString("yem_productmodel.name");
                    map.put(yemsalesman+yemclient+yemproductmodel,null);
                }
                if(map.size() < dyColl.size()){
                    this.addErrorMessage(temp,"校验明细信息中销售员+客户+产品型号 有重复");
                    break;
                }

                for(DynamicObject dy:dyColl){
                    DynamicObjectCollection sonColl = dy.getDynamicObjectCollection("yem_im_monthlyplan");
                    //每行父单据体对应子单据体月度总和
                    int count = 0;
                    for(DynamicObject sonTemp:sonColl){
                        for (String tempStr:idents){
                            count += sonTemp.getInt(tempStr);
                        }
                    }
                   int currCount =  dy.getInt("yem_salesvolume");
                   if(count > currCount){
                       flag = true;
                       info.append("明细信息 第"+row+"行对应的月度计划合计数"+count+" 大于当前行销量\r\n");
                   }
                   row++;
                }
                info.append("是否要继续操作?");
                if(flag){
                    this.addWarningMessage(temp, info.toString());
                }

        }


    }



//    @Override
//    public String getEntityKey() {
//        // TODO Auto-generated method stub
//        return this.entityKey;
//    }


}

