package com.yem.wm.im.debcrednotenew.op;

import com.yem.wm.im.debcrednotenew.utils.DebCredUtils;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
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.args.EndOperationTransactionArgs;
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.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.hr.hies.common.enu.Operate;

import java.util.List;

/**
 * @author zhouc
 * @date 2024/3/28 19:50
 * @className PlaceexaMineNewAuditOp
 * @description 下单审批审核（新）服务插件
 */
public class PlaceexaMineNewAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEnties = e.getDataEntities();
        for (DynamicObject temp : dataEnties) {
            temp = BusinessDataServiceHelper.loadSingle(temp.getPkValue(), temp.getDynamicObjectType().getName());
            String yem_debcreditid = temp.getString("yem_debcreditid");
            if (yem_debcreditid.length() > 0) {
                return;
            }
            String yem_contractnumber = temp.getString("yem_contractnumber");
            QFilter qFilter = new QFilter("billstatus", QCP.equals, "C");//
            qFilter.and(new QFilter("billno", QCP.equals, yem_contractnumber));
            DynamicObject yem_es_salesorder = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", "id,billno,yem_partscredit,yem_isgiftgiveparts", new QFilter[]{qFilter});
            if (yem_es_salesorder != null) {
                String yem_partscredit = yem_es_salesorder.getString("yem_partscredit");
                String yem_isgiftgiveparts = yem_es_salesorder.getString("yem_isgiftgiveparts");
                if (yem_partscredit.equals("A") && yem_isgiftgiveparts.equals("A")) {
                    DynamicObject object = BusinessDataServiceHelper.loadSingle(yem_es_salesorder.getLong("id"), "yem_es_salesorder");
                    DynamicObject debcrednote = DebCredUtils.AddQXGenerate(object, temp);
                    OperationResult Operate = OperationServiceHelper.executeOperate("save", debcrednote.getDynamicObjectType().getName(), new DynamicObject[]{debcrednote}, OperateOption.create());
                    if (Operate.isSuccess()) {
                        OperationResult submitResult = OperationServiceHelper.executeOperate("submit", "yem_debcrednote", new Object[]{debcrednote.getPkValue()}, OperateOption.create());
                        if (submitResult.isSuccess()) {
                            OperationResult auditResult = OperationServiceHelper.executeOperate("audit", "yem_debcrednote", new Object[]{debcrednote.getPkValue()}, OperateOption.create());
                            if (auditResult.isSuccess()) {
//                                //切换正式需去掉
//                                OperationResult estimatedResult = OperationServiceHelper.executeOperate("zyestimated", "yem_debcrednote", new Object[]{debcrednote.getPkValue()}, OperateOption.create());
//                                if (estimatedResult.isSuccess()) {
                                temp.set("yem_debcreditno", debcrednote.getString("billno"));
                                temp.set("yem_debcreditid", debcrednote.getString("id"));
                                SaveServiceHelper.update(temp);
//                                }
                            } else {
                                List<IOperateInfo> allErrorOrValidateInfo = auditResult.getAllErrorOrValidateInfo();
                                String message = auditResult.getMessage();
                                throw new KDBizException("贷记单审核失败，" + message + "，" + allErrorOrValidateInfo);
                            }
                        } else {
                            List<IOperateInfo> allErrorOrValidateInfo = submitResult.getAllErrorOrValidateInfo();
                            String message = submitResult.getMessage();
                            throw new KDBizException("贷记单提交失败，" + message + "，" + allErrorOrValidateInfo);
                        }
                    } else {
                        List<IOperateInfo> allErrorOrValidateInfo = Operate.getAllErrorOrValidateInfo();
                        String message = Operate.getMessage();
                        throw new KDBizException("贷记单保存失败，" + message + "，" + allErrorOrValidateInfo);
                    }
                }
            }
        }
    }
}
