package ink.wgink.module.wechat.controller.api.miniapp;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.wechat.pojo.pay.v3.PaySign;
import ink.wgink.module.wechat.pojo.pay.v3.jsapi.pojo.PayPlaceOrder;
import ink.wgink.module.wechat.pojo.pay.v3.order.OrderSearch;
import ink.wgink.module.wechat.service.pay.v3.jsapi.IJsapiService;
import ink.wgink.module.wechat.service.pay.v3.order.IOrderCloseService;
import ink.wgink.module.wechat.service.pay.v3.order.IOrderSearchService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.util.OrderUtil;
import io.swagger.annotations.*;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: MiniappPayAppController
 * @Description: 小程序支付
 * @Author: wanggeng
 * @Date: 2021/8/20 5:48 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "小程序支付")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/miniapp/pay-demo")
public class MiniappPayDemoController {

    @Autowired
    private IJsapiService jsapiService;
    @Autowired
    private IOrderSearchService orderSearchService;
    @Autowired
    private IOrderCloseService orderCloseService;

    @ApiOperation(value = "下订单测试", notes = "下订单测试接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save-place-order-demo")
    public PaySign savePlaceOrderDemo() throws Exception {
        String atomicOrder = OrderUtil.getAtomicOrder("0001");
        System.out.println("订单号：" + atomicOrder);

        PayPlaceOrder payPlaceOrder = new PayPlaceOrder();
        payPlaceOrder.setDescription("测试商品");
        payPlaceOrder.setOutTradeNo(atomicOrder);
        payPlaceOrder.setTimeExpire(DateTime.now().plusMinutes(1).toString(DateTimeFormat.forPattern(ISystemConstant.DATE_YYYY_MM_DD_T_HH_MM_SS_TIMEZONE)));
        payPlaceOrder.setNotifyUrl("https://wg.wgink.ink/study");

        PayPlaceOrder.Amount amount = new PayPlaceOrder.Amount();
        amount.setTotal(1);
        payPlaceOrder.setAmount(amount);

        PayPlaceOrder.Payer payer = new PayPlaceOrder.Payer();
        payer.setOpenid("oZBi65WehgT4W9ZxztEuRgp0QKvU");
        payPlaceOrder.setPayer(payer);

        return jsapiService.placeOrder(payPlaceOrder);
    }

    @ApiOperation(value = "商家订单查询测试", notes = "商家订单查询测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessOrderId", value = "商家订单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-business-order/{businessOrderId}")
    public OrderSearch getBusinessOrder(@PathVariable("businessOrderId") String businessOrderId) throws Exception {
        return orderSearchService.getBusinessOrder(businessOrderId);
    }

    @ApiOperation(value = "微信订单查询测试", notes = "微信订单查询测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transactionId", value = "微信订单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-order/{transactionId}")
    public OrderSearch getOrder(@PathVariable("transactionId") String transactionId) throws Exception {
        return orderSearchService.getOrder(transactionId);
    }

    @ApiOperation(value = "商家订单关闭测试", notes = "商家订单关闭测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessOrderId", value = "商家订单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("close-order/{businessOrderId}")
    public SuccessResult closeOrder(@PathVariable("businessOrderId") String businessOrderId) throws Exception {
        orderCloseService.closeOrder(businessOrderId);
        return new SuccessResult();
    }

}
