package com.ruoyi.web.controller.app;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.wx.vo.PrepayResponseVO;
import com.ruoyi.ss.order.domain.OrderQuery;
import com.ruoyi.ss.order.domain.OrderVO;
import com.ruoyi.ss.order.domain.dto.OrderDTO;
import com.ruoyi.ss.order.domain.dto.ProductDTO;
import com.ruoyi.ss.order.domain.dto.ReOrderDTO;
import com.ruoyi.ss.order.domain.dto.TopUpOrderDTO;
import com.ruoyi.ss.order.domain.vo.ReservedTimePeriod;
import com.ruoyi.ss.order.service.IOrderService;
import com.ruoyi.ss.order.service.IOrderValidator;
import com.ruoyi.ss.room.service.IRoomAssembler;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 订单Controller
 * @author qzz
 * 2024/3/5
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/app/order")
public class AppOrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderValidator orderValidator;

    @Autowired
    private IRoomAssembler roomAssembler;

    /**
     * 下单
     */
    @Log(title = "下单", businessType = BusinessType.CREATEORDER)
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody @Validated OrderDTO order)
    {
        logger.info("【下单】请求参数：order={}", JSON.toJSON(order));
        PrepayResponseVO orderServiceOrder = orderService.createOrder(order);
        return success(orderServiceOrder);
    }

    /**
     * 续单
     */
    @Log(title = "续单", businessType = BusinessType.REORDER)
    @PostMapping("/reOrder")
    public AjaxResult reOrder(@RequestBody @Validated ReOrderDTO order)
    {
        logger.info("【续单】请求参数：order={}", JSON.toJSON(order));
        PrepayResponseVO orderServiceOrder = orderService.reOrder(order);
        return success(orderServiceOrder);
    }

//    /**
//     * 充值订单
//     */
//    @Log(title = "充值订单", businessType = BusinessType.TOPUP)
//    @PostMapping("/topUp")
//    public AjaxResult topUp(@RequestBody @Validated TopUpOrderDTO topUpOrder)
//    {
//        logger.info("【充值订单】请求参数：topUpOrder={}", JSON.toJSON(topUpOrder));
//        PrepayResponseVO orderServiceOrder = orderService.topUp(topUpOrder);
//        return success(orderServiceOrder);
//    }

    /**
     * 货柜订单
     */
    @Log(title = "货柜订单", businessType = BusinessType.TOPUP)
    @PostMapping("/productOrder")
    public AjaxResult productOrder(@RequestBody @Validated ProductDTO productDTO)
    {
        logger.info("【货柜订单】请求参数：productDTO={}", JSON.toJSON(productDTO));
        PrepayResponseVO orderServiceOrder = orderService.productOrder(productDTO);
        return success(orderServiceOrder);
    }

    /**
     * 订单取消
     */
    @Log(title = "订单取消", businessType = BusinessType.ORDERCANCEL)
    @PostMapping("/orderCancel")
    public AjaxResult orderCancel(String orderNo)
    {
        logger.info("【订单取消】请求参数：orderNo={}", orderNo);
        Boolean aBoolean = orderService.orderCancel(orderNo);
        return toAjax(aBoolean);
    }

    /**
     * 结束订单
     */
    @Log(title = "结束订单", businessType = BusinessType.ORDEREND)
    @PostMapping("/orderEnd")
    public AjaxResult orderEnd(String orderNo)
    {
        logger.info("【结束订单】请求参数：orderNo={}", orderNo);
        Boolean aBoolean = orderService.orderEnd(orderNo);
        return toAjax(aBoolean);
    }

    /**
     * 获取进行中的订单房间
     */
    @GetMapping("/getInProgressOrderList")
    public AjaxResult getInProgressOrderList(Long roomId)
    {
        logger.info("【获取进行中的订单房间】请求参数：roomId={}", JSON.toJSON(roomId));
        if(ObjectUtil.isNull(roomId)){
            Long userId = getUserId();
            ServiceUtil.assertion(userId == null, "用户未登录");
            return success(orderService.getInProgressOrderListByUserId(userId));
        }
        return success(orderService.getInProgressOrderList(roomId));
    }


//    /**
//     * 更换包间
//     * 1. 订单号、新房间号
//     * 2. 校验：
//     *      2.1 不能与原房间号相同
//     *      2.2 新包间已预定的不能更换
//     */
//    @Log(title = "更换包间", businessType = BusinessType.REORDER)
//    @PostMapping("/changeRoom")
//    public AjaxResult changeRoom(OrderQuery order)
//    {
//        logger.info("【更换包间】请求参数：order={}", JSON.toJSON(order));
//        orderValidator.validateChangeRoom(order);
//        return toAjax(orderService.changeRoom(order));
//    }

    /**
     * 获取所有可更换包间
     * 1.
     * 2. 校验：
     *      2.1 同一个店铺
     *      2.2 类型相同
     *      2.3 价格相同
     *      2.4 订单时间内没有预订单的房间
     */
//    @GetMapping("/replaceableRooms")
//    public AjaxResult replaceableRooms(OrderQuery order)
//    {
//        logger.info("【获取所有可更换包间】请求参数：order={}", JSON.toJSON(order));
//        orderValidator.validateChangeRoom(order);
//        return toAjax(orderService.changeRoom(order));
//    }

    /**
     * 我的订单列表(用户端)
     */
    @GetMapping("/myOrderList")
    public TableDataInfo myOrderList(OrderQuery order)
    {
        logger.info("订单列表请求：OrderQuery=【{}】", JSON.toJSONString(order));
        order.setUserId(getUserId());
        if(order.getStatusList() == null || order.getStatusList().length == 0){
            order.setStatusList(null);
        }
        startPage();
        List<OrderVO> rlOrderVOS = orderService.selectRlOrderListNoIsolate(order);
        return getDataTable(rlOrderVOS);
    }

    /**
     * 订单列表(商户)
     */
    @GetMapping("/orderList")
    public TableDataInfo orderList(OrderQuery order)
    {
        logger.info("订单列表(商户)请求：OrderQuery=【{}】", JSON.toJSONString(order));
        order.setMerchantId(getUserId());
        if(order.getStatusList() == null || order.getStatusList().length == 0){
            order.setStatusList(null);
        }
        startPage();
        List<OrderVO> rlOrderVOS = orderService.selectRlOrderListNoIsolate(order);
        return getDataTable(rlOrderVOS);
    }

    /**
     * 根据房间id获取预约时间段
     */
    @GetMapping("/getReservedTimePeriods")
    public AjaxResult myOrderList(Long roomId, Date startTime)
    {
        logger.info("【根据房间id获取预约时间段】：roomId=【{}】,startTime=【{}】", roomId, startTime);
        startTime = startTime == null ? new Date() : startTime;
        ServiceUtil.assertion(roomId == null, "房间id/大厅设施id不能为空");
        List<ReservedTimePeriod> timePeriods = roomAssembler.getReservedTimePeriods(roomId, startTime);
        logger.info("【根据房间id获取预约时间段】 返回结果：timePeriods=【{}】", JSON.toJSONString(timePeriods));
        return success(timePeriods);
    }


}
