package com.wyz.demo.service.impl;

import com.wyz.demo.dao.OrderRepository;
import com.wyz.demo.dao.UserRepository;
import com.wyz.demo.dto.OrderDTO;
import com.wyz.demo.po.OrderPO;
import com.wyz.demo.po.UserPO;
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.stereotype.Service;

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

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrderRepository orderRepository;

    /**
     * 获取全部订单 - 根据用户名
     * @Param username 用户名
     */
    @Override
    public List<SelfOrderVO> getOrdersByUsername(String username) {
        int sender_id = userRepository.getIdByUsername(username);
        List<OrderPO> orders = orderRepository.getOrdersByUserId(sender_id);


        List<SelfOrderVO> res = new ArrayList<>();
        for (OrderPO orderPO : orders) {
            SelfOrderVO orderVO = new SelfOrderVO();

            orderVO.setTimestamp(orderPO.getTimestamp());
            orderVO.setDescription(orderPO.getDescription());
            orderVO.setPrice(orderPO.getPrice());
            String receiver = userRepository.getUsernameById(orderPO.getReceiverId());
//            log.info("receiver-id:{}",orderPO.getReceiverId());
//            log.info("receiver:{}",receiver);
            orderVO.setReceiver(receiver);
            orderVO.setStatus(orderPO.getStatus());

            res.add(orderVO);
        }


        return res;
    }

    @Override
    public void addOrder(OrderDTO orderDTO, HttpSession sess) {

        OrderPO orderPO = new OrderPO();

        orderPO.setDescription(orderDTO.getDescription());
        orderPO.setWhen(orderDTO.getWhen());
        orderPO.setLocation(orderDTO.getLocation());
        orderPO.setSenderId(userRepository.getIdByUsername((String)sess.getAttribute("username")));
        orderPO.setPrice(orderDTO.getPrice());
        orderPO.setComment(orderDTO.getComment());

        orderRepository.addOrder(orderPO);
    }

    @Override
    public List<CommonOrderVO> getAllUnpickOrders() {

        List<OrderPO> orderPOs = orderRepository.getAllUnpickOrders();

        List<CommonOrderVO> commonOrderVOs = new ArrayList<>();

        for (OrderPO orderPO : orderPOs) {
//            log.info("获取全部订单：{}",orderPO);
            CommonOrderVO commonOrderVO = new CommonOrderVO();


            commonOrderVO.setId(orderPO.getId());
            commonOrderVO.setDescription(orderPO.getDescription());
            commonOrderVO.setWhen(orderPO.getWhen());
            commonOrderVO.setLocation(orderPO.getLocation());
            commonOrderVO.setSender(userRepository.getUsernameById(orderPO.getSenderId()));
            commonOrderVO.setPrice(orderPO.getPrice());

            commonOrderVOs.add(commonOrderVO);
        }

        return commonOrderVOs;
    }

    @Override
    public DetailOrderVO getDetailOrder(int orderId) {
        OrderPO orderPO = orderRepository.getOrderById(orderId);
        UserPO userPO = userRepository.getUserById(orderPO.getSenderId());


        DetailOrderVO detailOrderVO = new DetailOrderVO();

        detailOrderVO.setDescription(orderPO.getDescription());
        detailOrderVO.setWhen(orderPO.getWhen());
        detailOrderVO.setLocation(orderPO.getLocation());
        detailOrderVO.setSender(userPO.getUsername());
        detailOrderVO.setPhone(userPO.getPhone());
        detailOrderVO.setComment(orderPO.getComment());
        detailOrderVO.setPrice(orderPO.getPrice());


        return detailOrderVO;
    }

    @Override
    public void pickOrder(int order_id, HttpSession sess) {
        String receiver = (String)sess.getAttribute("username");
        int receiver_id = userRepository.getIdByUsername(receiver);
        orderRepository.pickOrder(order_id, receiver_id);
    }


}
