package shop.alien.lawyer.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.lawyer.util.AliSms;
import shop.alien.lawyer.util.ali.AliApi;

/**
 * @author ssk
 * @version 1.0
 * @date 2024/12/11 17:34
 */
@Slf4j
@Api(tags = {"二期-阿里接口"})
@ApiSort(9)
@CrossOrigin
@RestController
@RequestMapping("/ali")
@RequiredArgsConstructor
public class AliController {

    private final AliSms aliSmsConfig;

    private final AliApi aliPayConfig;

    private final AliApi aliApi;

    @ApiOperation("发送短信")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/sendSms")
    public R sendSms(String phone) {
        Integer code = aliSmsConfig.sendSms(phone);
        log.info("AliController.sendSms?phone={}&code={}", phone, code);
        if (code != null) {
            return R.data("");
        }
        return R.fail("短信发送失败");
    }




    @ApiOperation("支付")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "金额", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "subject", value = "类目", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/zhifu")
    public R<JSONObject> zhifu(@RequestParam(value = "price") String price, @RequestParam(value = "subject") String subject) {
        return R.data(aliApi.promotionPackagePay(price, subject));
    }

    @ApiOperation("退款")
    @ApiOperationSupport(order = 13)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo", value = "订单号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "refundAmount", value = "退款金额", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "refundReason", value = "退款原因", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "partialRefundCode", value = "部分退款编码", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/processRefund")
    public String processRefund(@RequestParam(value = "outTradeNo") String outTradeNo,
                                @RequestParam(value = "refundAmount") String refundAmount,
                                @RequestParam(value = "refundReason") String refundReason,
                                @RequestParam(value = "partialRefundCode") String partialRefundCode) {
        return aliApi.processRefund(outTradeNo, refundAmount, refundReason, partialRefundCode);
    }


    @ApiOperation("律师端or用户退款")
    @ApiOperationSupport(order = 14)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo", value = "订单号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "refundAmount", value = "退款金额", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "refundReason", value = "退款原因", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "partialRefundCode", value = "部分退款编码", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/processLawRefund")
    public R<String> processLawRefund(@RequestParam(value = "outTradeNo") String outTradeNo,
                                @RequestParam(value = "refundAmount") String refundAmount,
                                @RequestParam(value = "refundReason") String refundReason,
                                @RequestParam(value = "partialRefundCode") String partialRefundCode) {
        return R.data(aliApi.processRefund(outTradeNo, refundAmount, refundReason, partialRefundCode));
    }



    @ApiOperation("根据订单号查询订单支付状态")
    @ApiOperationSupport(order = 15)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "outTradeNo", value = "订单号", dataType = "String", paramType = "query", required = true),
    })
    @GetMapping("/queryPayment")
    public R<String> queryPayment(String outTradeNo)  {

        return aliApi.queryPayment(outTradeNo);
    }

}
