package com.jeesharp.business.modules.pay.paywechatsubsidy.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.api.PaySubsidyChannelApi;
import com.jeesharp.business.modules.pay.paywechatsubsidy.dto.PayWechatSubsidyCancelDTO;
import com.jeesharp.business.modules.pay.paywechatsubsidy.dto.PayWechatSubsidyCreateDTO;
import com.jeesharp.business.modules.pay.paywechatsubsidy.dto.PayWechatSubsidyReturnDTO;
import com.jeesharp.business.modules.pay.subsidy.dto.PaySubsidyCancelRequestDTO;
import com.jeesharp.business.modules.pay.subsidy.dto.PaySubsidyCreateRequestDTO;
import com.jeesharp.business.modules.pay.subsidy.dto.PaySubsidyResultDTO;
import com.jeesharp.business.modules.pay.subsidy.dto.PaySubsidyReturnRequestDTO;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 微信补差管理端应用服务：运营手动触发补差/回退/取消/查询。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class PayWechatSubsidyApplicationService {

    private final PaySubsidyChannelApi paySubsidyChannelApi;

    @Transactional(readOnly = false)
    public PaySubsidyResultDTO create(PayWechatSubsidyCreateDTO dto) {
        PaySubsidyCreateRequestDTO request = new PaySubsidyCreateRequestDTO();
        request.setSubMchId(dto.getSubMchId().trim());
        request.setTransactionId(dto.getTransactionId().trim());
        request.setOutSubsidyNo(dto.getOutSubsidyNo().trim());
        request.setAmountFen(dto.getAmountFen());
        request.setDescription(dto.getDescription());
        PaySubsidyResultDTO result = paySubsidyChannelApi.createSubsidy(request);
        assertSuccess(result, "请求补差失败");
        return result;
    }

    public PaySubsidyResultDTO query(String subMchId, String transactionId) {
        if (StrUtil.isBlank(subMchId) || StrUtil.isBlank(transactionId)) {
            throw new BizErrorException("sub_mchid 与 transaction_id 不能为空");
        }
        PaySubsidyResultDTO result = paySubsidyChannelApi.querySubsidy(null, subMchId.trim(), transactionId.trim());
        assertSuccess(result, "查询补差失败");
        return result;
    }

    @Transactional(readOnly = false)
    public PaySubsidyResultDTO returnSubsidy(PayWechatSubsidyReturnDTO dto) {
        PaySubsidyReturnRequestDTO request = new PaySubsidyReturnRequestDTO();
        request.setSubMchId(dto.getSubMchId().trim());
        request.setTransactionId(dto.getTransactionId().trim());
        request.setRefundId(dto.getRefundId().trim());
        request.setOutOrderNo(dto.getOutOrderNo().trim());
        request.setAmountFen(dto.getAmountFen());
        request.setDescription(dto.getDescription());
        PaySubsidyResultDTO result = paySubsidyChannelApi.returnSubsidy(request);
        assertSuccess(result, "补差回退失败");
        return result;
    }

    @Transactional(readOnly = false)
    public PaySubsidyResultDTO cancel(PayWechatSubsidyCancelDTO dto) {
        PaySubsidyCancelRequestDTO request = new PaySubsidyCancelRequestDTO();
        request.setSubMchId(dto.getSubMchId().trim());
        request.setTransactionId(dto.getTransactionId().trim());
        request.setDescription(dto.getDescription());
        PaySubsidyResultDTO result = paySubsidyChannelApi.cancelSubsidy(request);
        assertSuccess(result, "取消补差失败");
        return result;
    }

    private static void assertSuccess(PaySubsidyResultDTO result, String defaultMsg) {
        if (result == null || !Boolean.TRUE.equals(result.getSuccess())) {
            throw new BizErrorException(result != null && StrUtil.isNotBlank(result.getErrorMsg())
                    ? result.getErrorMsg() : defaultMsg);
        }
    }
}
