package com.yc.ship.module.trade.controller.app.order;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.admin.order.vo.order.PayOrderReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.PayOrderRespVO;
import com.yc.ship.module.trade.framework.annotation.PlatTenantEnv;
import com.yc.ship.module.trade.service.pay.TradeOrderPayService;
import com.yc.ship.module.trade.service.refund.TradeRefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.TRADE;


/**
 * &#064;Description ：交易服务controller
 *
 * @author ：qsl
 * &#064;Date ：2025/6/17 20:52
 */
@Tag(name = "交易服务controller")
@RestController
@RequestMapping("/trade")
@Validated
@Slf4j
public class TradeOrderController {

    @Resource
    private TradeRefundService tradeRefundService;
    @Resource
    private TradeOrderPayService tradeOrderPayService;


    @PostMapping("/payNotify")
    @Operation(summary = "支付通知")
    @OperateLog(type = TRADE)
    public String payNotify(@RequestParam Map<String, Object> map) {
        CommonResult<?> commonResult = tradeOrderPayService.handlePayNotify(map);
        log.error("支付成功通知，参数{},返回参数{}", map, commonResult.isSuccess());
        return commonResult.isSuccess() ? "success" : "fail";
    }

    @PostMapping("/deposiPayNotify")
    @Operation(summary = "定金支付通知")
    @OperateLog(type = TRADE)
    public String deposiPayNotify(@RequestParam Map<String, Object> map) {
        CommonResult<?> commonResult = tradeOrderPayService.handleDeposiPayNotify(map);
        log.error("定金支付通知，参数{},返回参数{}", map, commonResult.isSuccess());
        return commonResult.isSuccess() ? "success" : "fail";
    }

    @PostMapping("/bcPayNotify")
    @Operation(summary = "补缴费用支付通知")
    @OperateLog(type = TRADE)
    public String bcPayNotify(@RequestParam Map<String, Object> map) {
        CommonResult<?> commonResult = tradeOrderPayService.handleBcPayNotify(map);
        log.error("补差价支付通知，参数{},返回参数{}", map, commonResult.isSuccess());
        return commonResult.isSuccess() ? "success" : "fail";
    }

    @PostMapping("/refundNotify")
    @Operation(summary = "退款通知")
    @OperateLog(type = TRADE)
    public String refundNotify(@RequestParam Map<String, Object> map) {
        CommonResult<?> commonResult = tradeRefundService.handleRefundNotify(map);
        return commonResult.isSuccess() ? "success" : "fail";
    }

    @GetMapping("/payOrderQuery")
    @Operation(summary = "支付反查，0待支付(轮询) 1已支付 2未支付 8888已支付等待结果(轮询) ")
    @OperateLog(type = TRADE)
    @PlatTenantEnv
    public CommonResult<?> payOrderQuery(@RequestParam Long payOrderId) {
        return tradeOrderPayService.payOrderQuery(payOrderId);
    }

    @GetMapping("/queryOrderContract")
    @Operation(summary = "查询合同URL")
    @OperateLog(type = TRADE)
    @PlatTenantEnv
    public CommonResult<?> queryOrderContract(@RequestParam Long payOrderId) {
        return tradeOrderPayService.queryOrderContract(payOrderId);
    }

    @PostMapping("/payOuterOrder")
    @Operation(summary = "外部订单支付")
    @OperateLog(type = TRADE)
    public CommonResult<PayOrderRespVO> payOuterOrder(@Valid @RequestBody PayOrderReqVO payOrderReqVO) {
        CommonResult<PayOrderRespVO> commonResult = tradeOrderPayService.payOuterOrder(payOrderReqVO);
        return commonResult;
    }

}
