package com.yem.wm.fm.gathering.validator;

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
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 java.util.HashMap;
import java.util.Map;

/**
 * @author xwudd
 * @Description
 * @date 17:38
 **/
public class UnCreatePlusMinusValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            String name = dataEntity.getDynamicObjectType().getName();
            DynamicObject[] plusMinus = BusinessDataServiceHelper.load(name, "id,billno,billstatus,yem_billtype",
                    new QFilter[]{new QFilter("yem_sourcebilltype", QCP.equals, name),
                            new QFilter("yem_sourcebillid", QCP.equals, dataEntity.getPkValue())});
            if(plusMinus.length > 0){
                for(DynamicObject plusMinu : plusMinus){
                    DynamicObject billType = plusMinu.getDynamicObject("yem_billtype");
                    String billTypeNumber = YEM.isNotEmpty(billType)?billType.getString("number"):"";
                    String billStatus = plusMinu.getString("billstatus");
                    if("yem_gathering_TZZ".equals(billTypeNumber) ){
                        if("B".equals(billStatus)){
                            this.addErrorMessage(dataEntitie,"该收款单生成的正向调整单已提交，不允许反关闭！");
                        }
                        if("C".equals(billStatus)){
                            this.addErrorMessage(dataEntitie,"该收款单生成的正向调整单已审核，不允许反关闭！");
                        }
                    }
                }

            }else{
                this.addErrorMessage(dataEntitie,"未查到该收款单有正负调整单，不允许反关闭！");
            }
        }
    }
}
