package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.enums.OrderNoPrefixEnum;
import com.czg.log.annotation.OperationLog;
import com.czg.market.dto.PpPackageOrderDTO;
import com.czg.market.service.PpPackageOrderService;
import com.czg.order.dto.CommonRefundDTO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.order.service.PayService;
import com.czg.utils.AssertUtil;
import com.czg.utils.CzgRandomUtils;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理端/套餐推广
 *
 * @author yjjie
 * @date 2025/12/18 19:26
 */
@RestController
@RequestMapping("/admin/ppOrder")
public class PpOrderController {

    @DubboReference
    private PpPackageOrderService ppPackageOrderService;

    @Resource
    private PayService payService;

    /**
     * 确认退单
     */
    @PostMapping("/confirmRefund")
    @Transactional
    public CzgResult<Boolean> confirmRefund(@RequestBody @Validated CommonRefundDTO param) {
        PpPackageOrderDTO detail = ppPackageOrderService.getOrderDetailById(param.getRecordId(), StpKit.USER.getLoginIdAsLong());
        if (detail == null) {
            return CzgResult.failure("订单不存在");
        }

        ppPackageOrderService.confirmRefund(param.getRecordId(), StpKit.USER.getShopId());

        //退钱
        String refPayOrderNo = CzgRandomUtils.snowflake(OrderNoPrefixEnum.REPP);
        payService.unifyRefund(detail.getShopId(), detail.getId(), detail.getPayOrderId(), refPayOrderNo,
                StrUtil.isBlankIfStr(detail.getRefundReason()) ? "套餐推广退款" : detail.getRefundReason(), detail.getFinalPrice());

        return CzgResult.success();
    }

    /**
     * 驳回退单
     */
    @PostMapping("/rejectRefund")
    public CzgResult<Boolean> rejectRefund(@RequestBody @Validated CommonRefundDTO param) {
        return CzgResult.success(ppPackageOrderService.cancelRefund(param.getRecordId(),
                StpKit.USER.getLoginIdAsLong(), param.getReason()));
    }

    /**
     * 套餐推广-核销
     * 参数: {"verifyCode": "123456"}
     */
    @PostMapping("checkout")
    @OperationLog("套餐推广-核销")
    @SaAdminCheckPermission(parentName = "套餐推广", value = "market:package:checkout", name = "套餐推广-核销")
    public CzgResult<Boolean> checkout(@RequestBody JSONObject param) {
        AssertUtil.isNull(param, "核销码不能为空");
        String verifyCode = param.getString("verifyCode");
        AssertUtil.isBlank(verifyCode, "核销码不能为空");
        return CzgResult.success(ppPackageOrderService.checkout(verifyCode, StpKit.USER.getShopId()));
    }

}
