package com.bonus.canteen.core.menu.service.impl;

import java.util.List;
import com.bonus.canteen.core.nutrition.common.enums.YesOrNoEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuCollectionDishesMapper;
import com.bonus.canteen.core.menu.domain.MenuCollectionDishes;
import com.bonus.canteen.core.menu.service.IMenuCollectionDishesService;

/**
 * 菜品收藏Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-20
 */
@Service
public class MenuCollectionDishesServiceImpl implements IMenuCollectionDishesService {
    @Autowired
    private MenuCollectionDishesMapper menuCollectionDishesMapper;

    /**
     * 查询菜品收藏
     * 
     * @param dishesId 菜品收藏主键
     * @return 菜品收藏
     */
    @Override
    public MenuCollectionDishes selectMenuCollectionDishesById(Long dishesId) {
        MenuCollectionDishes result = menuCollectionDishesMapper.selectMenuCollectionDishesById(dishesId);
        int countDishes = this.menuCollectionDishesMapper.getCollectionCountByUserIdAndDishesId(SecurityUtils.getUserId(), dishesId);
        if (countDishes > 0) {
            result.setIsFavorites(YesOrNoEnum.YES.key());
        } else {
            result.setIsFavorites(YesOrNoEnum.NO.key());
        }
        return result;
    }

    /**
     * 查询菜品收藏列表
     * 
     * @param menuCollectionDishes 菜品收藏
     * @return 菜品收藏
     */
    @Override
    public List<MenuCollectionDishes> selectMenuCollectionDishesList(MenuCollectionDishes menuCollectionDishes) {
        return menuCollectionDishesMapper.selectMenuCollectionDishesList(menuCollectionDishes);
    }

    /**
     * 新增菜品收藏
     * 
     * @param menuCollectionDishes 菜品收藏
     * @return 结果
     */
    @Override
    public int insertMenuCollectionDishes(MenuCollectionDishes menuCollectionDishes) {
        menuCollectionDishes.setCreateTime(DateUtils.getNowDate());
        try {
            return menuCollectionDishesMapper.insertMenuCollectionDishes(menuCollectionDishes);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述, " + e.getMessage());
        }
    }

    /**
     * 修改菜品收藏
     * 
     * @param menuCollectionDishes 菜品收藏
     * @return 结果
     */
    @Override
    public int updateMenuCollectionDishes(MenuCollectionDishes menuCollectionDishes) {
        menuCollectionDishes.setUpdateTime(DateUtils.getNowDate());
        try {
            return menuCollectionDishesMapper.updateMenuCollectionDishes(menuCollectionDishes);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述, " + e.getMessage());
        }
    }

    /**
     * 批量删除菜品收藏
     * 
     * @param ids 需要删除的菜品收藏主键
     * @return 结果
     */
    @Override
    public int deleteMenuCollectionDishesByIds(String[] ids) {
        Long userId = SecurityUtils.getUserId();
        return menuCollectionDishesMapper.deleteMenuCollectionDishesByIds(userId, ids);
    }

    /**
     * 删除菜品收藏信息
     * 
     * @param id 菜品收藏主键
     * @return 结果
     */
    @Override
    public int deleteMenuCollectionDishesById(String id) {
        return menuCollectionDishesMapper.deleteMenuCollectionDishesById(id);
    }
}
