package com.wyz.demo.controller;

import com.wyz.demo.dto.OrderDTO;
import com.wyz.demo.service.OrderService;
import com.wyz.demo.vo.CommonOrderVO;
import com.wyz.demo.vo.DetailOrderVO;
import com.wyz.demo.vo.SelfOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 查询某个用户自己的订单
     */
    @RequestMapping(value = "/own", produces = "application/json")
    public List<SelfOrderVO> getOwnOrders(HttpSession sess){
        String username = (String)sess.getAttribute("username");
        if(username==null){
            log.error("{session中没有用户名：还未登录}");
            return null;
        }else{
            List<SelfOrderVO> orders = orderService.getOrdersByUsername(username);
            return orders;
        }
    }


    /**
     * 提交订单
     */
    @PostMapping(value = "/commit")
    public void commit(@RequestBody OrderDTO orderDTO, HttpSession sess){
        log.info("新订单为：{}",orderDTO);
        orderService.addOrder(orderDTO,sess);
    }


    /**
     * 返回全部待接单的订单，在主页上显示
     */
    @RequestMapping(value = "/all")
    public List<CommonOrderVO> getAllUnpickOrders(){
        return orderService.getAllUnpickOrders();
    }


    /**
     * 获取订单详情
     */
    @GetMapping(value = "/detail")
    public DetailOrderVO getDetailOrder(@RequestParam("orderId") int orderId){
        log.info("请求订单号：{} 的详细信息",orderId);

        return orderService.getDetailOrder(orderId);
    }


    /**
     * 接单
     */
    @RequestMapping(value = "/pick")
    public void pickOrder(@RequestParam("orderId") int orderId, HttpSession sess){
        orderService.pickOrder(orderId, sess);
    }


}
