package com.yem.wm.syn.eas.api.service.impl;

import com.yem.wm.syn.eas.api.dto.IntebilllogonAuditDTO;
import com.yem.wm.syn.eas.api.service.IYZJAuditService;
import com.yem.wm.syn.eas.api.util.OperationServicUtil;
import kd.bos.dataentity.entity.DynamicObject;
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.workflow.WorkflowServiceHelper;

public class YZJAuditServiceImpl implements IYZJAuditService {
    @Override
    public void audit(IntebilllogonAuditDTO data, Boolean success, String errorCode) {
        if (!success){
            throw new KDBizException("审批失败,云之家状态为"+success);
        }
        //只能审批云之家表单ID
        String formDefId = data.getFormDefId();
        //云之家表单实例id
        String flowInstId = data.getFlowInstId();
        //根据云之家表单ID获取ERP业务对象
        QFilter qFilter = new QFilter("yem_yzjid", QCP.equals, formDefId);
        QFilter qFilters = new QFilter("yem_isdisable", QCP.equals, '0');
        qFilter.and(qFilters);
        //智能审批注册
        DynamicObject[] intebilllogoncoll =
                BusinessDataServiceHelper.load("yem_ia_intebilllogon","fid,yem_formid,yem_yzjid,yem_isdisable", new QFilter[]{qFilter});
        if (intebilllogoncoll.length==0){
            throw new KDBizException("智能审批单据未找到云之家表单ID为"+formDefId+"的单据");
        }
        DynamicObject intebilllogon = intebilllogoncoll[0];
        DynamicObject yemForm = intebilllogon.getDynamicObject("yem_formid");
        //实体标识
        String entityNum = yemForm.getString("number");
        //根据云之家表单实例ID获取对应的表单 审核
        DynamicObject dynamicObject =
                BusinessDataServiceHelper.loadSingle(entityNum, new QFilter[]{new QFilter("yem_forminstid", QCP.equals, flowInstId)});
        if (dynamicObject==null){
            throw new KDBizException("未找到云之家表单实例ID为"+flowInstId+"的单据");
        }
        OperationServicUtil.executeOperates("audit",new DynamicObject[]{dynamicObject},"审核失败");

    }
}
