package com.jeeplus.hotelorder.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.hotelorder.dto.OrderCreateDTO;
import com.jeeplus.hotelorder.dto.OrderDashboardDTO;
import com.jeeplus.hotelorder.order.domain.HotelOrder;
import com.jeeplus.hotelorder.order.service.HotelOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/order")
@Api(tags = "订单管理")
public class HotelOrderController {

    @Resource
    private HotelOrderService hotelOrderService;

    @GetMapping("dashboard")
    @ApiOperation(value = "订单看板统计")
    public ResponseEntity<?> dashboard() {
        OrderDashboardDTO dashboard = hotelOrderService.getDashboard();
        return ResponseUtil.newInstance()
                .add("pendingCount", dashboard.getPendingCount())
                .add("preparingCount", dashboard.getPreparingCount())
                .add("readyCount", dashboard.getReadyCount())
                .add("deliveringCount", dashboard.getDeliveringCount())
                .add("todayTotal", dashboard.getTodayTotal())
                .add("todayRevenue", dashboard.getTodayRevenue())
                .ok("操作成功");
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询订单列表")
    public ResponseEntity<?> findPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            HotelOrder query) {
        Page<HotelOrder> page = new Page<>(current, size);
        IPage<HotelOrder> result = hotelOrderService.findPage(page, query);
        return ResponseUtil.newInstance()
                .add("records", result.getRecords())
                .add("total", result.getTotal())
                .add("current", result.getCurrent())
                .add("pages", result.getPages())
                .ok("操作成功");
    }

    @GetMapping("detail/{id}")
    @ApiOperation(value = "查询订单详情")
    public ResponseEntity<?> getDetail(@PathVariable String id) {
        HotelOrder order = hotelOrderService.getDetail(id);
        return ResponseUtil.newInstance()
                .add("order", order)
                .ok("操作成功");
    }

    @GetMapping("listByRoom/{roomNo}")
    @ApiOperation(value = "按房间号查询订单列表")
    public ResponseEntity<?> findByRoomNo(@PathVariable String roomNo) {
        return ResponseUtil.newInstance()
                .add("list", hotelOrderService.findByRoomNo(roomNo))
                .ok("操作成功");
    }

    @PostMapping("create")
    @ApiOperation(value = "创建订单(客户端下单)")
    public ResponseEntity<?> createOrder(@RequestBody OrderCreateDTO dto) {
        HotelOrder order = hotelOrderService.createOrder(dto);
        return ResponseUtil.newInstance()
                .add("order", order)
                .ok("下单成功");
    }

    @PostMapping("accept/{id}")
    @ApiOperation(value = "餐厅接单")
    public ResponseEntity<?> acceptOrder(@PathVariable String id) {
        return ResponseUtil.newInstance().ok(hotelOrderService.acceptOrder(id));
    }

    @PostMapping("reject/{id}")
    @ApiOperation(value = "餐厅拒单")
    public ResponseEntity<?> rejectOrder(@PathVariable String id, @RequestParam(required = false) String reason) {
        return ResponseUtil.newInstance().ok(hotelOrderService.rejectOrder(id, reason));
    }

    @PostMapping("prepare/{id}")
    @ApiOperation(value = "开始备餐(厨房)")
    public ResponseEntity<?> prepareOrder(@PathVariable String id) {
        return ResponseUtil.newInstance().ok(hotelOrderService.prepareOrder(id));
    }

    @PostMapping("ready/{id}")
    @ApiOperation(value = "出餐完成(厨房)")
    public ResponseEntity<?> readyOrder(@PathVariable String id) {
        return ResponseUtil.newInstance().ok(hotelOrderService.readyOrder(id));
    }

    @PostMapping("deliver/{id}")
    @ApiOperation(value = "开始配送(餐厅前台)")
    public ResponseEntity<?> deliverOrder(@PathVariable String id) {
        return ResponseUtil.newInstance().ok(hotelOrderService.deliverOrder(id));
    }

    @PostMapping("complete/{id}")
    @ApiOperation(value = "完成配送")
    public ResponseEntity<?> completeOrder(@PathVariable String id) {
        return ResponseUtil.newInstance().ok(hotelOrderService.completeOrder(id));
    }

    @PostMapping("customerRefund/{id}")
    @ApiOperation(value = "客户自助退单")
    public ResponseEntity<?> customerRefund(@PathVariable String id, @RequestParam(required = false) String reason) {
        return ResponseUtil.newInstance().ok(hotelOrderService.customerRefund(id, reason));
    }

    @PostMapping("hotelRefund/{id}")
    @ApiOperation(value = "酒店前台退单")
    public ResponseEntity<?> hotelRefund(@PathVariable String id, @RequestParam(required = false) String reason) {
        return ResponseUtil.newInstance().ok(hotelOrderService.hotelRefund(id, reason));
    }

    @GetMapping("todayCount")
    @ApiOperation(value = "统计今日订单数")
    public ResponseEntity<?> countTodayOrders() {
        return ResponseUtil.newInstance()
                .add("count", hotelOrderService.countTodayOrders())
                .ok("操作成功");
    }
}
