package com.ruoyi.frequency.recommend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.frequency.recommend.entity.Recommend;
import com.ruoyi.frequency.recommend.mapper.RecommendMapper;
import com.ruoyi.frequency.recommend.service.RecommendService;
import com.ruoyi.utils.SnowIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 推荐Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend>  implements RecommendService
{
    @Autowired
    private RecommendMapper recommendMapper;

    /**
     * 查询推荐
     *
     * @param id 推荐主键
     * @return 推荐
     */
    @Override
    public Recommend selectRecommendById(Long id)
    {
        return recommendMapper.selectRecommendById(id);
    }

    /**
     * 查询推荐列表
     *
     * @param recommend 推荐
     * @return 推荐
     */
    @Override
    public List<Recommend> selectRecommendList(Recommend recommend)
    {
        return recommendMapper.selectRecommendList(recommend);
    }

    /**
     * 新增推荐
     *
     * @param recommend 推荐
     * @return 结果
     */
    @Override
    public int insertRecommend(Recommend recommend)
    {
        recommend.setCreateTime(DateUtils.getNowDate());
        return recommendMapper.insertRecommend(recommend);
    }

    /**
     * 修改推荐
     *
     * @param recommend 推荐
     * @return 结果
     */
    @Override
    public int updateRecommend(Recommend recommend)
    {
        recommend.setUpdateTime(DateUtils.getNowDate());
        return recommendMapper.updateRecommend(recommend);
    }

    /**
     * 批量删除推荐
     *
     * @param ids 需要删除的推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendByIds(Long[] ids)
    {
        this.lambdaUpdate().set(Recommend::getDelFlag,PublicCommon.删除).in(Recommend::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除推荐信息
     *
     * @param id 推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendById(Long id)
    {
        this.lambdaUpdate().set(Recommend::getDelFlag,PublicCommon.删除).eq(Recommend::getId,id).update();
        return 1;
    }

    @Override
    public void topping(Integer type, Long objectId) {

        Recommend recommend = new Recommend()
                .setId(SnowIdUtils.uniqueLong())
                .setType(type)
                .setObjectId(objectId);
        recommend.setUpdateData();
        recommendMapper.topping(recommend);
    }
}
