package com.ylx.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.order.domain.dto.*;
import com.ylx.order.domain.vo.BookingSlotsVO;
import com.ylx.order.domain.vo.OrderDateQueryVo;
import com.ylx.order.domain.vo.OrderDetailVO;
import com.ylx.order.domain.vo.RecentMerchantVO;
import com.ylx.order.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer/order")
@Api(tags = {"客户端订单模块"})
@Slf4j
@PreAuthorize("@customerAuth.isCustomer()")
public class CustomerOrderController {

    @Resource
    private TOrderService orderService;

    @ApiOperation("客户端用户提交订单")
    @PostMapping("/submit")
    public R<Map<String, Object>> submitOrder(@Validated @RequestBody OrderSubmitDTO dto) {
        Map<String, Object> data = this.orderService.submitOrder(dto);
        return R.ok(data);
    }

    @ApiOperation("根据日期范围等条件查询订单")
    @PostMapping("/queryByDate")
    public R<Page<OrderDateQueryVo>> queryOrderByDate(@Validated @RequestBody OrderDateQueryDTO dto) {
        Page<OrderDateQueryVo> page = this.orderService.queryOrderList(dto);
        return R.ok(page);
    }

    @ApiOperation("逻辑删除订单（移入回收站）")
    @PostMapping("/delete")
    public R<?> deleteOrder(@Validated @RequestBody OrderDeleteDTO dto) {
        this.orderService.logicDeleteOrder(dto.getId());
        return R.ok("删除成功");
    }


    @ApiOperation("根据订单ID查询订单详情")
    @GetMapping("/detail/{orderId}")
    public R<OrderDetailVO> getOrderDetailById(
            @PathVariable @ApiParam(value = "订单ID", required = true, example = "1") Long orderId) {
        return R.ok(this.orderService.getOrderDetailById(orderId));
    }

    @ApiOperation("用户取消订单")
    @PostMapping("/cancel")
    public R<?> cancelOrder(@RequestBody @Validated OrderCancleDTO dto) {
        // 调用服务层取消订单，返回影响行数或订单信息
        int result = this.orderService.cancelOrder(dto);
        return result > 0 ? R.ok("订单已取消") : R.fail("订单取消失败");
    }

    @ApiOperation("客户端是否可以预约当前时段")
    @PostMapping("/booking/check")
    public R<Boolean> bookingCheck(@RequestBody @Validated BookingCheckDTO dto) {
        Boolean result = this.orderService.bookingCheck(dto);
        return R.ok(result);
    }

    @ApiOperation("客户端预约时段")
    @PostMapping("/booking/slots")
    public R<List<BookingSlotsVO>> bookingSlots(@RequestBody @Validated BookingSlotsDTO dto) {
        List<BookingSlotsVO> result = this.orderService.bookingSlots(dto);
        return R.ok(result);
    }

    @ApiOperation("获取近期下单商户列表")
    @PostMapping("/recent/merchants")
    public R<Page<RecentMerchantVO>> getRecentMerchants(@RequestBody RecentOrderQueryDTO dto) {
        Page<RecentMerchantVO> pageData = this.orderService.getRecentMerchants(dto);
        return R.ok(pageData);
    }

}
