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 com.wyz.demo.vo.SelfPickedOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
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 = "/test", produces = "application/json")
    public String test(@RequestBody String s){
        return "hello:"+s;
    }


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


    /**
     * 查询自己接的单子
     */
    @RequestMapping(value="/picked")
    public List<SelfPickedOrderVO> getOwnPickedOrders(HttpSession sess){
        String username = (String)sess.getAttribute("username");
        if(username==null){
            log.warn("[查看我的接单] : session中没有用户名：还未登录");
            return null;
        }else{
            List<SelfPickedOrderVO> orders = orderService.getPickedOrdersByUsername(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);
    }

    /**
     * 接单者-取消接单：只需要改订单的状态
     */
    @RequestMapping(value = "/cancelPicked")
    public void cancelPickedOrder(@RequestParam("orderId") int orderId){
        orderService.cancelPickedOrder(orderId);
    }


    /**
     * 接单者-确认送达
     */
    @RequestMapping(value = "/confirmPicked")
    public void confirmPickedOrder(@RequestParam("orderId") int orderId){
        orderService.confirmPickedOrder(orderId);
    }


    /**
     * 下单者-取消订单
     */
    @RequestMapping(value = "/cancel")
    public void cancelOrder(@RequestParam("orderId") int orderId){
        orderService.cancelOrder(orderId);
    }

    /**
     * 下单者-确认完成
     */
    @RequestMapping(value = "/confirmCompleted")
    public void confirmOrder(@RequestParam("orderId") int orderId){
        orderService.confirmOrder(orderId);
    }



}
