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

import java.util.List;
import java.util.Objects;

import com.bonus.canteen.core.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.cook.mapper.CookCollectionDishesMapper;
import com.bonus.canteen.core.cook.domain.CookCollectionDishes;
import com.bonus.canteen.core.cook.service.ICookCollectionDishesService;

/**
 * 菜品收藏Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookCollectionDishesServiceImpl implements ICookCollectionDishesService {
    @Autowired
    private CookCollectionDishesMapper cookCollectionDishesMapper;

    public CookCollectionDishes selectCookCollectionDishesById(Long id) {
        return cookCollectionDishesMapper.selectCookCollectionDishesByDishesId(id);
    }

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

    /**
     * 查询菜品收藏列表
     * 
     * @param cookCollectionDishes 菜品收藏
     * @return 菜品收藏
     */
    @Override
    public List<CookCollectionDishes> selectCookCollectionDishesList(CookCollectionDishes cookCollectionDishes) {
        return cookCollectionDishesMapper.selectCookCollectionDishesList(cookCollectionDishes);
    }

    /**
     * 新增菜品收藏
     * 
     * @param cookCollectionDishes 菜品收藏
     * @return 结果
     */
    @Override
    public int insertCookCollectionDishes(CookCollectionDishes cookCollectionDishes) {
        cookCollectionDishes.setCreateTime(DateUtils.getNowDate());
        try {
            return cookCollectionDishesMapper.insertCookCollectionDishes(cookCollectionDishes);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改菜品收藏
     * 
     * @param cookCollectionDishes 菜品收藏
     * @return 结果
     */
    @Override
    public int updateCookCollectionDishes(CookCollectionDishes cookCollectionDishes) {
        cookCollectionDishes.setUpdateTime(DateUtils.getNowDate());
        try {
            return cookCollectionDishesMapper.updateCookCollectionDishes(cookCollectionDishes);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除菜品收藏
     * 
     * @param dishesIds 需要删除的菜品收藏主键
     * @return 结果
     */
    @Override
    public int deleteCookCollectionDishesByDishesIds(Long[] dishesIds) {
        return cookCollectionDishesMapper.deleteCookCollectionDishesByDishesIds(dishesIds);
    }

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