KingDeeHelper.java 7.04 KB
package com.diligrp.tax.proxy.helper;

import com.diligrp.tax.central.domain.BaseMapping;
import com.diligrp.tax.central.exception.TaxAgentServiceException;
import com.diligrp.tax.central.type.TaxSystemType;
import com.diligrp.tax.central.utils.JsonUtils;
import com.diligrp.tax.proxy.message.KingDeeQuery;
import com.diligrp.tax.proxy.message.KingDeeSubmit;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.entity.OperateParam;
import com.kingdee.bos.webapi.entity.RepoError;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangmeiyang
 * @CreateTime: 2025-10-29 09:42
 * @Version: todo
 */
@Slf4j
public class KingDeeHelper {

    private static final Gson GSON = new Gson();

    /**
     * 获取 King Dee API
     *
     * @param identifyInfo 识别信息
     * @return {@link K3CloudApi }
     */
    public static K3CloudApi getKingDeeApi(IdentifyInfo identifyInfo) {
        return new K3CloudApi(identifyInfo, false);
    }

    /**
     * 获取 repo ret
     *
     * @param res 分辨率
     * @return {@link RepoRet }<{@link ? }>
     */
    private static RepoRet<?> getRepoRet(String res) {
        RepoRet<?> repoRet = GSON.fromJson(res, RepoRet.class);
        if (!repoRet.getResult().getResponseStatus().isIsSuccess()) {
            List<String> list = repoRet.getResult().getResponseStatus().getErrors().stream().map(RepoError::getMessage).toList();
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, String.join(";", list));
        }
        return repoRet;
    }

    /**
     * 创建非审计机构
     *
     * @param model  型
     * @param api    应用程序接口
     * @param formId 表单 ID
     * @return {@link RepoRet }<{@link ? }>
     */
    public static RepoRet<?> nonAuditSend(Map<String, Object> model, K3CloudApi api, String formId) {
        var message = new KingDeeSubmit();
        message.setModel(model);
        message.setIsAutoSubmitAndAudit(false);
        message.setIsAutoAdjustField(true);
        var body = JsonUtils.toJsonString(message);
        try {
            String res = api.save(formId, body);
            log.info("金蝶无需审核接口完成,formId={},res={}", formId, res);
            return getRepoRet(res);
        } catch (Exception e) {
            log.error("金蝶无需审核接口调用异常", e);
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, e);
        }
    }

    /**
     * 审计发送
     *
     * @param mapping      映射
     * @param identifyInfo 识别信息
     * @param formId       表单 ID
     * @return {@link RepoRet }<{@link ? }>
     */
    public static RepoRet<?> auditSend(BaseMapping mapping, IdentifyInfo identifyInfo, String formId) {
        var message = new KingDeeSubmit();
        message.setModel(JsonUtils.convertValue(mapping, new TypeReference<>() {}));
        message.setIsAutoSubmitAndAudit(true);
        message.setIsAutoAdjustField(true);
        var body = JsonUtils.toJsonString(message);
        try {
            log.info("kingdee auditSend: {}", body);
            String res;
            K3CloudApi api = getKingDeeApi(identifyInfo);
            res = api.save(formId, body);
            log.info("金蝶审核接口完成,formId={},res={}", formId, res);
            return getRepoRet(res);
        } catch (Exception e) {
            log.error("金蝶审核接口调用异常", e);
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, e);
        }
    }

    /**
     * 审计发送
     *
     * @param model  型
     * @param api    应用程序接口
     * @param formId 表单 ID
     * @return {@link RepoRet }<{@link ? }>
     */
    public static RepoRet<?> auditSend(Map<String, Object> model, K3CloudApi api, String formId) {
        var message = new KingDeeSubmit();
        message.setModel(model);
        message.setIsAutoSubmitAndAudit(true);
        message.setIsAutoAdjustField(true);
        var body = JsonUtils.toJsonString(message);
        try {
            log.info("kingdee auditSend: {}", body);
            String res;
            res = api.save(formId, body);
            log.info("金蝶审核接口完成,formId={},res={}", formId, res);
            return getRepoRet(res);
        } catch (Exception e) {
            log.error("金蝶审核接口调用异常", e);
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, e);
        }
    }

    /**
     * 创建 un audit 参数
     *
     * @param ids    身份证
     * @param api    应用程序接口
     * @param formId 表单 ID
     * @return {@link RepoRet }<{@link ? }>
     */
    public static RepoRet<?> unAuditSend(String ids, K3CloudApi api, String formId) {
        var operateParam = new OperateParam();
        operateParam.setIds(ids);
        try {
            RepoRet<?> repoRet = api.unAudit(formId, operateParam);
            if (!repoRet.getResult().getResponseStatus().isIsSuccess()) {
                List<String> list = repoRet.getResult().getResponseStatus().getErrors().stream().map(RepoError::getMessage).toList();
                throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, String.join(";", list));
            }
            return repoRet;
        } catch (Exception e) {
            log.error("金蝶反审核接口调用异常", e);
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, e);
        }
    }

    public static <T extends BaseMapping> List<Object> querySend(T t, String formId, K3CloudApi kingDeeApi) {
        var filterString = t.getVerifyInformation();
        var fieldKeys = t.getReturnKeys();
        return querySend(formId, filterString, fieldKeys, kingDeeApi);
    }

    /**
     * 创建查询参数
     *
     * @param formId       表单 ID
     * @param filterString 过滤器字符串
     * @param fieldKeys    字段键
     * @param api          应用程序接口
     * @return {@link List }<{@link List }<{@link Object }>>
     */
    private static List<Object> querySend(String formId, String filterString, String fieldKeys, K3CloudApi api) {
        var query = new KingDeeQuery();
        query.setFormId(formId);
        query.setFilterString(filterString);
        query.setFieldKeys(fieldKeys);
        try {
            List<List<Object>> lists = api.executeBillQuery(JsonUtils.toJsonString(query));
            log.info("金蝶查证接口返回,formId={},json={}", formId, lists);
            if (lists.isEmpty()) {
                return Collections.emptyList();
            }
            return lists.getFirst();
        } catch (Exception e) {
            log.error("金蝶查证接口调用异常", e);
            throw new TaxAgentServiceException(TaxSystemType.REMOTE_SERVICE_CALLS_ARE_EXCEPTIONAL, e);
        }
    }
}