package com.mcwl.web.controller.pay.AliPay;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.common.models.AlipayTradeCancelResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.mcwl.common.JSONUtils;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.controller.BaseController;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.core.redis.RedisCache;
import com.mcwl.common.domain.IdsParam;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.pay.domain.OrderTrade;
import com.mcwl.pay.service.AliPayService;
import com.mcwl.pay.service.OrderTradeService;
import com.mcwl.resource.domain.dto.ProductRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.web.controller.pay
 * @Filename：OrderTradeController
 * @Description 支付模块
 * @Date：2025/1/3 14:46
 */

@RestController
@RequestMapping("/order")
@Validated
@Api(tags = "订单模块")
public class OrderTradeController extends BaseController {

    @Autowired
    private Config config;

    @Autowired
    private OrderTradeService orderTradeService;

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 购买产品
     */
    @PostMapping("/buy")
    @ApiOperation(value = "购买产品")
    public R<Object> buy(@Valid @RequestBody ProductRes productRes) throws Exception {
        orderTradeService.productHandler(productRes);
        return R.ok();
    }


    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询订单列表")
    public TableDataInfo list(OrderTrade orderTrade) {
        startPage();
        List<OrderTrade> list = orderTradeService.selectMallProductList(orderTrade);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增订单")
    public R<Object> add(@RequestBody OrderTrade orderTrade) {
        // 获取当前用户
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"用户未登录");
        }
        orderTrade.setUserId(userId);
        return R.ok(orderTradeService.insertMallProduct(orderTrade));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改订单")
    public R<Object> update(@RequestBody OrderTrade orderTrade) {
        // 获取当前用户
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"用户未登录");
        }
        orderTrade.setUserId(userId);
        orderTradeService.updateMallProduct(orderTrade);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping
    @ApiOperation(value = "删除订单")
    public R<Object> remove(@RequestBody IdsParam ids) {
        orderTradeService.deleteMallProductByIds(ids);
        return R.ok();
    }


    /**
     * 查询交易状态
     */
    @GetMapping("/queryTradeStatus")
    @ApiOperation(value = "查询交易状态")
    public R<Object> queryTradeStatus(@Valid @NotBlank(message = "订单号不能为空") String outTradeNo) throws Exception {
        return aliPayService.queryTradeStatus(outTradeNo);


    }


    /**
     * 撤销交易
     */
//    @GetMapping("/cancelTrade")
//    public AjaxResult cancelTrade(String outTradeNo) throws Exception {
//        // 关闭交易
//        AlipayTradeCancelResponse cancel = Factory.Payment.Common().cancel(outTradeNo);
//        if (cancel.getCode().equals("10000")) {
//            return AjaxResult.success("关闭成功");
//        }
//        return AjaxResult.error("关闭失败");
//    }

    /**
     * 关闭交易
     */
//    @GetMapping("/closeTrade")
//    public AjaxResult closeTrade(String outTradeNo) throws Exception {
//        // 关闭交易
//        AlipayTradeCloseResponse close = Factory.Payment.Common().close(outTradeNo);
//        if (close.getCode().equals("10000")) {
//            return AjaxResult.success("关闭成功");
//        }
//        return AjaxResult.error("关闭失败");
//    }


}
