package org.jeecg.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.app.dto.AppOrderInfoDTO;
import org.jeecg.modules.app.form.CreateOrderForm;
import org.jeecg.modules.app.form.PageOrdersForm;
import org.jeecg.modules.app.form.UserPayForm;
import org.jeecg.modules.app.service.IOrderService;
import org.jeecg.modules.app.vo.OrderVO;
import org.jeecg.modules.app.vo.PageOrdersVO;
import org.jeecg.modules.app.vo.QueryOrderVerifyRecordsVO;
import org.jeecg.modules.pay.vo.NotifyRequest;
import org.jeecg.modules.pay.vo.NotifyResponse;
import org.simpleframework.xml.core.Validate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Slf4j
@Tag(name = "APP订单相关接口")
@RestController
@RequestMapping("/app/order")
public class OrderController {
    @Resource
    private IOrderService appOrderService;
    /**
     * @Author SheepHy
     * @Description 学校场地预约-预览页
     * @Date 10:10 2025/7/14
     * @Param placeId startTime
     * @return Result<OrderVO.PreviewOrderPlaceSchool>
     **/
    @Operation(summary="学校场地预约-预览页")
    @GetMapping("/previewOrderPlaceSchool")
    public Result<OrderVO.PreviewOrderPlaceSchool> previewOrderPlaceSchool(@RequestParam("placeId") @Schema(description = "场地ID")String placeId,
                                                                           @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
                                                                           @DateTimeFormat(pattern = "yyyy-MM-dd")
                                                                           @RequestParam("startTime") @Schema(description = "当前选择日期（年月日）")Date startTime){
        return Result.ok(appOrderService.previewOrderPlaceSchool(placeId,startTime));
    }

    /**
     * @Author SheepHy
     * @Description 课程限购校验
     * @Date 16:22 2025/7/14
     * @Param courseId 课程ID
     * @return boolean
     **/
    @Operation(summary="课程限购校验")
    @GetMapping("/checkCourseLimitNum")
    public Result<Boolean> checkCourseLimitNum(@RequestParam("courseId") @Schema(description = "课程ID")String courseId,
                                               @RequestParam("limitNum") @Schema(description = "购买数")int limitNum){
        return Result.OK(appOrderService.checkCourseLimitNum(courseId, limitNum));
    }

    /**
     * @Author SheepHy
     * @Description 课程购买-预览页（学校课程、体育馆课程、教练课程）
     * @Date 15:43 2025/7/14
     * @Param courseId
     * @return OrderVO.PreviewOrderCourseSchool {@link OrderVO.PreviewOrderCourse}
     **/
    @Operation(summary="课程购买-预览页（学校课程、体育馆课程、教练课程）")
    @GetMapping("/previewOrderCourse")
    public Result<OrderVO.PreviewOrderCourse> previewOrderCourse(@RequestParam("courseId") @Schema(description = "课程ID")String courseId){
        return Result.ok(appOrderService.previewOrderCourse(courseId));
    }

    /**
     * @Author SheepHy
     * @Description 体育馆包场-预览页
     * @Date 16:13 2025/7/14
     * @Param placeId
     * @return OrderVO.PreviewOrderPlaceGymnasiumChartered {@link OrderVO.PreviewOrderPlaceGymnasiumChartered}
     **/
    @Operation(summary="体育馆包场-预览页")
    @GetMapping("/previewOrderPlaceGymnasiumChartered")
    public Result<OrderVO.PreviewOrderPlaceGymnasiumChartered> previewOrderPlaceGymnasiumChartered(@RequestParam("rulesId") @Schema(description = "场次IDS多个逗号拼接")String rulesId){
        return Result.ok(appOrderService.previewOrderPlaceGymnasiumChartered(rulesId));
    }

    /**
     * @Author SheepHy
     * @Description 学校场地预约-创建
     * @Date 10:10 2025/7/14
     * @Param rulesId 时间段ID
     * @return OrderVO.createOrderPlaceSchool
     **/
    @Operation(summary="学校场地预约-创建")
    @GetMapping("/createOrderPlaceSchool")
    public Result<OrderVO.createOrderPlaceSchool> createOrderPlaceSchool(@RequestParam("rulesId") @Schema(description = "场次IDS多个逗号拼接")String rulesId,
                                                                         @RequestParam("userIds") @Schema(description = "使用用户IDS多个逗号拼接")String userIds){
        return Result.ok(appOrderService.createOrderPlaceSchool(rulesId, userIds));
    }

    /**
     * @Author SheepHy
     * @Description 学校包场-预览时间选择
     * @Date 14:45 2025/7/17
     * @Param placeId
     * @return OrderVO.PreviewOrderPlaceSchoolTime {@link OrderVO.PreviewOrderPlaceSchoolTime}
     **/
    @Operation(summary="学校包场-预览时间选择")
    @GetMapping("/previewOrderPlaceSchoolTime")
    public Result<List<OrderVO.PreviewOrderPlaceSchoolTime>> previewOrderPlaceSchoolTime(@RequestParam("placeId") @Schema(description = "当前选择场次ID")String placeId){
        return Result.ok(appOrderService.previewOrderPlaceSchoolTime(placeId));
    }

    /**
     * 订单-创建
     * @param createOrderForm
     * @return
     */
    @Operation(summary = "订单-创建")
    @PostMapping("/createOrder")
    public Result<UserPayForm> createOrder(@Validate @RequestBody CreateOrderForm createOrderForm) throws IOException {
        return Result.ok(appOrderService.createOrder(createOrderForm));
    }

    /**
     * 支付回调
     * @param notifyRequestForm
     * @return
     */
    @Operation(summary = "支付回调")
    @PostMapping("/orderNotify")
    public NotifyResponse orderNotify(@RequestBody NotifyRequest notifyRequestForm) throws IOException {
        return appOrderService.orderNotify(notifyRequestForm);
    }

    /**
     * 订单支付是否成功查询
     * @param orderCode
     * @return
     */
    @Operation(summary = "订单支付是否成功查询")
    @GetMapping("/orderQuery")
    public Result<String> orderQuery(@RequestParam(name = "orderCode")String orderCode) throws IOException {
        return Result.OK(appOrderService.orderQuery(orderCode));
    }

    /**
     * 分页查询我的订单列表
     * @param pageOrdersForm
     * @return
     */
    @Operation(summary = "分页查询我的订单列表")
    @PostMapping("/pageOrders")
    public Result<IPage<PageOrdersVO>> pageOrders(@RequestBody PageOrdersForm pageOrdersForm){
        return Result.OK(appOrderService.pageOrders(pageOrdersForm));
    }

    /**
     * 订单主动取消
     * @param orderId
     * @return
     */
    @Operation(summary = "订单主动取消")
    @PutMapping("/cancelOrder")
    public Result<String> cancelOrder(@RequestParam(name = "orderCode")String orderId) throws IOException {
        return Result.OK(appOrderService.cancelOrder(orderId));
    }

    /**
     * 订单-小程序支付后回显信息
     *
     * @param orderId
     * @return
     */
    @Operation(summary = "订单-小程序支付后回显信息")
    @GetMapping("/queryOrderInfo")
    public Result<AppOrderInfoDTO> queryOrderInfo(@Schema(description = "订单ID") @RequestParam(name = "orderId") String orderId) {
        return Result.ok(appOrderService.queryOrderInfo(orderId));
    }

    /**
     * 订单-查询课程课时核销记录
     *
     * @param orderId
     * @param ticketNo
     * @return
     */
    @Operation(summary = "订单-查询课程课时核销记录")
    @GetMapping("/queryOrderVerifyRecords")
    public Result<List<QueryOrderVerifyRecordsVO>> queryOrderVerifyRecords(@Schema(description = "订单ID") @RequestParam(name = "orderId") String orderId,
                                                                     @Schema(description = "券号") @RequestParam(name = "ticketNo") String ticketNo) {
        return Result.ok(appOrderService.queryOrderVerifyRecords(orderId,ticketNo));
    }

    /**
     * @Author SheepHy
     * @Description 体育馆无固定场-预览页
     * @Date 17:22 2025/8/4
     * @Param
     * @return
     **/
    @Operation(summary="体育馆无固定场-预览页")
    @GetMapping("/PreviewOrderPlaceGymnasiumNoFixation")
    public Result<OrderVO.PreviewOrderPlaceGymnasiumNoFixation> previewOrderPlaceGymnasiumNoFixation(@RequestParam("placeId") @Schema(description = "场地ID")String placeId){
        return Result.ok(appOrderService.previewOrderPlaceGymnasiumNoFixation(placeId));
    }
}
