package nckd.jimin.jyyy.fi.plugin.operate.validator;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
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;
import kd.bos.util.StringUtils;
import nckd.jimin.jyyy.fi.common.constant.ErPrintUserConstant;
import nckd.jimin.jyyy.fi.common.constant.ErReimBurseBillConstant;

import java.util.Arrays;
import java.util.List;

public class ReimBurseBillReceiveTicketValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = getDataEntities();
        String operateKey = getOperateKey();
        switch (operateKey){
            case ErReimBurseBillConstant.OPERATE.HANDIN_TICKET:
                Arrays.stream(dataEntities).forEach(r -> checkHandinTicket(r));
                break;
            case ErReimBurseBillConstant.OPERATE.RECEIVE_TICKET:
                Arrays.stream(dataEntities).forEach(r -> checkReceiptTicket(r));
                break;
        }

    }

    protected void checkHandinTicket(ExtendedDataEntity dataEntity){
        // 单据状态为已审核
        DynamicObject billInfo = dataEntity.getDataEntity();
        List<String> status = Arrays.asList("E", "F", "G");
        if(!status.contains(billInfo.getString(ErReimBurseBillConstant.KEY_BILLSTATUS))){
            this.addErrorMessage(dataEntity, String.format("单据%s交票失败：只有审核通过、等待付款、已付款状态的单据允许交票。",billInfo.getString(ErReimBurseBillConstant.KEY_BILLNO)));
            return;
        }

        //收票人为空
        if(StringUtils.isNotEmpty(billInfo.getString(ErReimBurseBillConstant.KEY_NCKD_HANDIN_PERSON))){
            this.addErrorMessage(dataEntity, String.format("单据%s交票失败：不允许重复交票。",billInfo.getString(ErReimBurseBillConstant.KEY_BILLNO)));
            return;
        }
    }
    protected void checkReceiptTicket(ExtendedDataEntity dataEntity){
        DynamicObject billInfo = dataEntity.getDataEntity();

        // 收票人不能为空
        if(StringUtils.isEmpty(billInfo.getString(ErReimBurseBillConstant.KEY_NCKD_HANDIN_PERSON))){
            this.addErrorMessage(dataEntity, String.format("单据%s收票失败：请先完成交票功能",billInfo.getString(ErReimBurseBillConstant.KEY_BILLNO)));
            return;
        }

        if(StringUtils.isNotEmpty(billInfo.getString(ErReimBurseBillConstant.KEY_NCKD_RECEIPT_PERSON))){
            this.addErrorMessage(dataEntity, String.format("单据%s收票失败：单据不允许重复收票。",billInfo.getString(ErReimBurseBillConstant.KEY_BILLNO)));
            return;
        }

        List<String> status = Arrays.asList("E", "F", "G");
        if(!status.contains(billInfo.getString(ErReimBurseBillConstant.KEY_BILLSTATUS))){
            this.addErrorMessage(dataEntity, String.format("单据%s收票失败：只有审核通过、等待付款、已付款状态的单据允许收票。",billInfo.getString(ErReimBurseBillConstant.KEY_BILLNO)));
            return;
        }

    }
}
