package com.yem.wm.es.declaredocx.task;

import kd.bos.context.RequestContext;
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.exception.KDBizException;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.BFTrackerServiceHelper;
import kd.bos.servicehelper.botp.ConvertServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

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

/**
 * @author 张立攀
 * @version 1.0
 * @description ，报关状态为结关且未下推出口发票的报关单 生成出口发票
 * @date 2023-12-14 17:01
 */
public class DeclaredocxTotipTask extends AbstractTask {
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        DynamicObject[] load = BusinessDataServiceHelper.load("yem_es_declaredocx", "", new QFilter[]{
//                new QFilter("yem_declarestatused", QCP.equals, "F"),
//                new QFilter("billstatus", QCP.equals, "C")
                 new QFilter("billno", QCP.equals, "CKBGDZ-240803-0001")
        });
        for (DynamicObject loadSon : load) {
//            boolean yemEsDeclaredocx = BFTrackerServiceHelper.isPush("yem_es_declaredocx", (long) loadSon.getPkValue());
//            if(!yemEsDeclaredocx){
            pushCreateBill(loadSon, "1731413160633589760", "yem_exportinvoice");
//            }
        }
    }


    /**
     * 下推生成出口发票
     */
    private static void pushCreateBill(DynamicObject processingorderNow, String rule, String toNumber) {
        PushArgs pushArgs = setPushArgs(processingorderNow, toNumber, rule);
        ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
        if (pushResult.isSuccess()) {
            MainEntityType targetMainType = EntityMetadataCache.getDataEntityType(toNumber);
            List<DynamicObject> targetBillObjs = pushResult.loadTargetDataObjects(new IRefrencedataProvider() {
                @Override
                public void fillReferenceData(Object[] objs, IDataEntityType dType) {
                    BusinessDataReader.loadRefence(objs, dType);
                }
            }, targetMainType);
            for (DynamicObject dt : targetBillObjs) {
                OperationResult save = OperationServiceHelper.executeOperate("save", dt.getDynamicObjectType().getName(), new DynamicObject[]{dt}, OperateOption.create());
                if (save.isSuccess()) {
//                                            OperationResult submit = OperationServiceHelper.executeOperate("submit", debCredNoteNumber, new Object[]{debCredNote.getPkValue()}, OperateOption.create());
//                                            if (submit.isSuccess()) {
//                                                OperationResult audit = OperationServiceHelper.executeOperate("audit", debCredNoteNumber, new Object[]{debCredNote.getPkValue()}, OperateOption.create());
//                                                if (audit.isSuccess()) {
//
//                                                }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);
                }
            }


        }
    }

    /**
     * 设置下推引擎
     */
    public static PushArgs setPushArgs(DynamicObject dataEntitie, String targetNumber, String ruleId) {
        String name = dataEntitie.getDynamicObjectType().getName();
        PushArgs pushArgs = new PushArgs();
        pushArgs.setSourceEntityNumber(name);
        pushArgs.setTargetEntityNumber(targetNumber);
        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;
    }
}
