package com.zsElectric.boot.business.controller.applet;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zsElectric.boot.business.model.entity.RechargeLevel;
import com.zsElectric.boot.business.model.form.applet.AppLevelOrderForm;
import com.zsElectric.boot.business.model.form.applet.AppUserPayForm;
import com.zsElectric.boot.business.model.query.applet.AppUserOrderInfoQuery;
import com.zsElectric.boot.business.model.vo.UserOrderInfoVO;
import com.zsElectric.boot.business.service.RechargeLevelService;
import com.zsElectric.boot.business.service.UserOrderInfoService;
import com.zsElectric.boot.common.constant.SystemConstants;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单相关接口")
@Slf4j
@RestController
@RequestMapping("/applet/v1/order")
@RequiredArgsConstructor
public class AppletOrderController {

    private final RechargeLevelService rechargeLevelService;
    private final UserOrderInfoService userOrderInfoService;

    /**
     * 获取充电档位
     *
     * @return
     */
    @Operation(summary = "获取充电档位")
    @GetMapping("/getReChargeLevel")
    public Result<List<RechargeLevel>> getReChargeLevel() {
        List<RechargeLevel> list = rechargeLevelService.list(Wrappers.lambdaQuery(RechargeLevel.class).eq(RechargeLevel::getStatus, SystemConstants.STATUS_ONE));
        return Result.success(list);
    }

    @Operation(summary = "购券记录（券订单记录）")
    @GetMapping("/getTicketRecords")
    public Result<IPage<UserOrderInfoVO>> getTicketRecords(@RequestBody AppUserOrderInfoQuery queryParams) {
        IPage<UserOrderInfoVO> page = userOrderInfoService.getTicketRecords(queryParams);
        return Result.success(page);
    }

    /**
     * 创建订单
     *
     * @param appLevelOrderForm
     * @return
     */
    @Operation(summary = "创建订单")
    @PostMapping("/createOrder")
    public Result<AppUserPayForm> createOrder(@RequestBody AppLevelOrderForm appLevelOrderForm) {
        AppUserPayForm payForm = userOrderInfoService.createOrder(appLevelOrderForm);
        return Result.success(payForm);
    }

    /**
     * 订单-支付
     *
     * @param orderId
     * @return
     */
    @Operation(summary = "订单-支付")
    @PutMapping("/payOrder/{orderId}")
    public Result<AppUserPayForm> payOrder(@PathVariable("orderId") String orderId) {
        return Result.success(userOrderInfoService.payOrder(orderId));
    }

    /**
     * 支付回调
     *
     * @param request
     * @return
     */
    @Operation(summary = "支付回调")
    @RequestMapping("/wechatPayNotify")
    public Map<String, String> wechatPayNotify(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {
        log.info("--------------------------------------------支付回调");
        return userOrderInfoService.wechatPayNotify(request, response);
    }

    /**
     * 订单支付是否成功查询
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "订单支付是否成功查询")
    @GetMapping("/orderQuery/{orderNo}")
    public Result<String> orderQuery(@PathVariable("orderNo") String orderNo) throws Exception {
        return Result.success(userOrderInfoService.orderQuery(orderNo));
    }

    /**
     * 订单主动取消
     *
     * @param orderId
     * @return
     */
    @Operation(summary = "订单主动取消")
    @PutMapping("/cancelOrder/{orderId}")
    public Result<String> cancelOrder(@PathVariable(name = "orderId") String orderId) {
        return Result.success(userOrderInfoService.cancelOrder(orderId));
    }

    /**
     * 账户退款
     * @return
     */
    @Operation(summary = "账户退款")
    @PutMapping("/refundOrder")
    public Result<String> refundOrder() throws Exception {
        return Result.success(userOrderInfoService.refundOrder());
    }

    @Operation(summary = "订单退款回调")
    @PostMapping("/callback/refundOrderNotify")
    public Map<String, Object> refundOrderNotify(HttpServletRequest request, HttpServletResponse response) {
        return userOrderInfoService.refundCallback(request, response);
    }
}
