package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OrderFoodMapper;
import com.ruoyi.system.domain.OrderFood;
import com.ruoyi.system.service.IOrderFoodService;

/**
 * 订餐预约Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class OrderFoodServiceImpl implements IOrderFoodService {
    @Autowired
    private OrderFoodMapper orderFoodMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询订餐预约
     *
     * @param orderFoodId 订餐预约主键
     * @return 订餐预约
     */
    @Override
    public OrderFood selectOrderFoodByOrderFoodId(Long orderFoodId) {
        return orderFoodMapper.selectOrderFoodByOrderFoodId(orderFoodId);
    }

    /**
     * 查询订餐预约列表
     *
     * @param orderFood 订餐预约
     * @return 订餐预约
     */
    @Override
    public List<OrderFood> selectOrderFoodList(OrderFood orderFood) {
        return orderFoodMapper.selectOrderFoodList(orderFood);
    }

    @Override
    public AjaxResult selectOrderFoodListRiLi(OrderFood orderFood) {
        List<OrderFood> orderFoods = orderFoodMapper.selectOrderFoodList(orderFood);
        Map<Date, List<OrderFood>> foodTime = new HashMap<>();
        if (orderFoods != null && orderFoods.size() > 0){
            foodTime = orderFoods.stream().collect(Collectors.groupingBy(OrderFood::getOrderFoodTime));
        }
        List<String> dayList = DateUtils.getDay(orderFood.getOrderFoodTime());
        List<Map> mapList = new ArrayList<>();
        if (dayList.size()> 0){
            for (String day : dayList) {
                Map<String,Object> map = new HashMap<>(5);
                map.put("date",day);
                map.put("am","未预约");
                map.put("pm","未预约");
                if (foodTime.size() > 0){
                    List<OrderFood> orderFoodsList = foodTime.get(DateUtils.parseDate(day));
                    if (orderFoodsList != null && orderFoodsList.size() > 0){
                        for (OrderFood food : orderFoodsList) {
                            String orderFoodType = food.getOrderFoodType();
                            if ("1".equals(orderFoodType)){
                                map.put("am","已预约");
                                map.put("amId",food.getOrderFoodId());
                            }
                            else if ("2".equals(orderFoodType)){
                                map.put("pm","已预约");
                                map.put("pmId",food.getOrderFoodId());
                            }
                        }
                    }
                }
                mapList.add(map);
            }
        }
        return AjaxResult.success(mapList);
    }

    /**
     * 新增订餐预约
     *
     * @param orderFood 订餐预约
     * @return 结果
     */
    @Override
    public AjaxResult insertOrderFood(OrderFood orderFood) {
        //判断这个人这个时间是否已经预约
        Long userId = SecurityUtils.getUserId();
        OrderFood orderFoodNew = new OrderFood();
        orderFoodNew.setOrderFoodUserId(userId);
        orderFoodNew.setOrderFoodType(orderFood.getOrderFoodType());
        orderFoodNew.setOrderFoodTime(orderFood.getOrderFoodTime());
        List<OrderFood> orderFoods = orderFoodMapper.selectOrderFoodListR(orderFoodNew);
        if (orderFoods != null && orderFoods.size() > 0) {
            return AjaxResult.error("请勿重复预约");
        }
        orderFood.setCreateTime(DateUtils.getNowDate());
        orderFood.setOrderFoodUserId(userId);
        orderFood.setOrderFoodUser(SecurityUtils.getUsername());
        orderFood.setOrderFoodDeptId(SecurityUtils.getDeptId());
        //
        SysDept sysDept = sysDeptMapper.selectDeptById(SecurityUtils.getDeptId());
        orderFood.setOrderFoodDept(sysDept.getDeptName());
        int rows = orderFoodMapper.insertOrderFood(orderFood);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改订餐预约
     *
     * @param orderFood 订餐预约
     * @return 结果
     */
    @Override
    public AjaxResult updateOrderFood(OrderFood orderFood) {
        //判断这个人这个时间是否已经预约
        Long userId = SecurityUtils.getUserId();
        OrderFood orderFoodNew = new OrderFood();
        orderFoodNew.setOrderFoodUserId(userId);
        orderFoodNew.setOrderFoodType(orderFood.getOrderFoodType());
        orderFoodNew.setOrderFoodTime(orderFood.getOrderFoodTime());
        List<OrderFood> orderFoods = orderFoodMapper.selectOrderFoodListR(orderFoodNew);
        if (orderFoods != null && orderFoods.size() > 0) {
            return AjaxResult.error("请勿重复预约");
        }
        orderFood.setUpdateTime(DateUtils.getNowDate());
        int rows = orderFoodMapper.updateOrderFood(orderFood);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除订餐预约
     *
     * @param orderFoodIds 需要删除的订餐预约主键
     * @return 结果
     */
    @Override
    public int deleteOrderFoodByOrderFoodIds(Long[] orderFoodIds) {
        return orderFoodMapper.deleteOrderFoodByOrderFoodIds(orderFoodIds);
    }

    /**
     * 删除订餐预约信息
     *
     * @param orderFoodId 订餐预约主键
     * @return 结果
     */
    @Override
    public int deleteOrderFoodByOrderFoodId(Long orderFoodId) {
        return orderFoodMapper.deleteOrderFoodByOrderFoodId(orderFoodId);
    }
}
