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

import com.yem.wm.fm.gathering.validator.CreatePlusMinusValidator;
import kd.bos.data.BusinessDataReader;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.botp.runtime.ConvertOperationResult;
import kd.bos.entity.botp.runtime.PushArgs;
import kd.bos.entity.datamodel.IRefrencedataProvider;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.entity.validate.ValidateResultCollection;
import kd.bos.exception.KDBizException;
import kd.bos.isc.util.dt.D;
import kd.bos.servicehelper.botp.ConvertServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xwudd
 * @Description
 * @date 15:05
 **/
public class CreatePlusMinusOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_salcontractno");
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_bizstatus");
        fieldKeys.add("yem_bizstatus");

    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        // disable by zzs 2024-06-14
//        e.addValidator(new CreatePlusMinusValidator());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            String name = dataEntitie.getDynamicObjectType().getName();
            PushArgs pushArgs = setPushArgs(dataEntitie, "1808037580529220608");
            ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
            if(pushResult.isSuccess()) {
                MainEntityType targetMainType = EntityMetadataCache.getDataEntityType(name);
                List<DynamicObject> targetBillObjs = pushResult.loadTargetDataObjects(new IRefrencedataProvider() {
                    @Override
                    public void fillReferenceData(Object[] objs, IDataEntityType dType) {
                        BusinessDataReader.loadRefence(objs, dType);
                    }
                }, targetMainType);
                OperationResult save = OperationServiceHelper.executeOperate("save", name, new DynamicObject[]{targetBillObjs.get(0)}, OperateOption.create());
                if(save.isSuccess()){
                    OperationResult submit = OperationServiceHelper.executeOperate("submit", name, new Object[]{targetBillObjs.get(0).getPkValue()}, OperateOption.create());
                    if(submit.isSuccess()){
                        OperationResult audit = OperationServiceHelper.executeOperate("audit", name, new Object[]{targetBillObjs.get(0).getPkValue()}, OperateOption.create());
                        if(audit.isSuccess()){
                            pushArgs = setPushArgs(dataEntitie, "1808014680292067328");
                            pushResult = ConvertServiceHelper.push(pushArgs);
                            if(pushResult.isSuccess()) {
                                targetMainType = EntityMetadataCache.getDataEntityType(name);
                                targetBillObjs = pushResult.loadTargetDataObjects(new IRefrencedataProvider() {
                                    @Override
                                    public void fillReferenceData(Object[] objs, IDataEntityType dType) {
                                        BusinessDataReader.loadRefence(objs, dType);
                                    }
                                }, targetMainType);
                                save = OperationServiceHelper.executeOperate("save", name, new DynamicObject[]{targetBillObjs.get(0)}, OperateOption.create());
                                if(save.isSuccess()) {
                                    dataEntitie.set("yem_bizstatus","B");
                                    SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
                                }else{
                                    List<IOperateInfo> allErrorOrValidateInfo = save.getAllErrorOrValidateInfo();
                                    String message = save.getMessage();
                                    throw new KDBizException("保存正向收款调整单失败," + message + "，" + allErrorOrValidateInfo);
                                }
                            }else{
                                String message = pushResult.getMessage();
                                throw new KDBizException("下推正向收款调整单失败," + message);
                            }
                        }else{
                            List<IOperateInfo> allErrorOrValidateInfo = audit.getAllErrorOrValidateInfo();
                            String message = audit.getMessage();
                            throw new KDBizException("审核负向收款调整单失败," + message + "，" + allErrorOrValidateInfo);
                        }
                    }else{
                        List<IOperateInfo> allErrorOrValidateInfo = submit.getAllErrorOrValidateInfo();
                        String message = submit.getMessage();
                        throw new KDBizException("提交负向收款调整单失败," + message + "，" + allErrorOrValidateInfo);
                    }
                }else{
                    List<IOperateInfo> allErrorOrValidateInfo = save.getAllErrorOrValidateInfo();
                    String message = save.getMessage();
                    throw new KDBizException("保存负向收款调整单失败," + message + "，" + allErrorOrValidateInfo);
                }
            }else{
                String message = pushResult.getMessage();
                throw new KDBizException("下推负向收款调整单失败," + message);
            }
        }
    }

    public PushArgs setPushArgs(DynamicObject dataEntitie,String ruleId){
        String name = dataEntitie.getDynamicObjectType().getName();
        PushArgs pushArgs = new PushArgs();
        pushArgs.setSourceEntityNumber(name);
        pushArgs.setTargetEntityNumber(name);
        pushArgs.setRuleId(ruleId);
        pushArgs.setHasRight(true);
        pushArgs.setBuildConvReport(true);
        List<ListSelectedRow> selectedRows = new ArrayList<>();
        ListSelectedRow srcBill1 = new ListSelectedRow(dataEntitie.getPkValue());
        selectedRows.add(srcBill1);
        pushArgs.setSelectedRows(selectedRows);
        return pushArgs;
    }
}
