package com.yem.wm.syn.eas.api.util;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperateErrorInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.validate.ValidateResult;
import kd.bos.entity.validate.ValidateResultCollection;
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.DeleteServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.List;

public class OperationServicUtil {

    public static void executeOperate(String operationKey,DynamicObject[] pricelibrary,String operation){
        DynamicObject pricelibr = pricelibrary[0];
        String type = pricelibr.getDataEntityType().getName();
        OperationResult submit = OperationServiceHelper.executeOperate(operationKey, type, pricelibrary, OperateOption.create());
        if (!submit.isSuccess()){
            ValidateResultCollection validateResult = submit.getValidateResult();
            List<ValidateResult> validateErrors = validateResult.getValidateErrors();
            StringBuilder stringBuilder = new StringBuilder();
            for (ValidateResult validateError : validateErrors) {
                List<OperateErrorInfo> allErrorInfos = validateError.getAllErrorInfo();
                for (OperateErrorInfo allErrorInfo: allErrorInfos) {

                    String message = allErrorInfo.getMessage();
                    stringBuilder.append(message+";");
                }
            }

            String billno = pricelibr.getString("billno");
            DynamicObject pricel = BusinessDataServiceHelper.loadSingle("yem_pricelibrary", new QFilter[]{new QFilter("billno", QCP.equals, billno)});
            if (pricel!=null){
                DeleteServiceHelper.delete(pricel.getDataEntityType(),new Object[]{pricel.getPkValue()});
            }
            throw new KDBizException(operation+stringBuilder);
        }
    }

    public static String executeOperates(String operationKey,DynamicObject[] pricelibrary,String operation){
        DynamicObject pricelibr = pricelibrary[0];
        String type = pricelibr.getDataEntityType().getName();
        OperationResult submit = OperationServiceHelper.executeOperate(operationKey, type, pricelibrary, OperateOption.create());
        if (!submit.isSuccess()){
            ValidateResultCollection validateResult = submit.getValidateResult();
            List<ValidateResult> validateErrors = validateResult.getValidateErrors();
            StringBuilder stringBuilder = new StringBuilder();
            if (submit.getMessage()!=null){
                stringBuilder.append(submit.getMessage());
            }
            for (ValidateResult validateError : validateErrors) {
                List<OperateErrorInfo> allErrorInfos = validateError.getAllErrorInfo();
                for (OperateErrorInfo allErrorInfo: allErrorInfos) {

                    String message = allErrorInfo.getMessage();
                    stringBuilder.append(message+";");
                }
            }
            List<OperateErrorInfo> allErrorInfos = submit.getAllErrorInfo();
            for (OperateErrorInfo allErrorInfo: allErrorInfos) {
                String message = allErrorInfo.getMessage();
                stringBuilder.append(message);
            }
            return operation+stringBuilder;
            //throw new KDBizException(operation+stringBuilder);
        }
        return null;
    }


}
