package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
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;

    /**
     * 查询订餐预约
     *
     * @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);
    }

    /**
     * 新增订餐预约
     *
     * @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.selectOrderFoodList(orderFoodNew);
        if (orderFoods != null && orderFoods.size() > 0) {
            return AjaxResult.error("请勿重复预约");
        }
        orderFood.setCreateTime(DateUtils.getNowDate());
        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.selectOrderFoodList(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);
    }
}
