package com.ruoyi.system.service.impl;

import com.ruoyi.common.enums.SysYesNo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CpsContentInfo;
import com.ruoyi.system.domain.CpsUsrContentRel;
import com.ruoyi.system.mapper.CpsContentInfoMapper;
import com.ruoyi.system.mapper.CpsUsrContentRelMapper;
import com.ruoyi.system.service.ICpsContentInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 发布内容Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-14
 */
@Service
public class CpsContentInfoServiceImpl implements ICpsContentInfoService
{
    @Autowired
    private CpsContentInfoMapper cpsContentInfoMapper;

    @Autowired
    private CpsUsrContentRelMapper cpsUsrContentRelMapper;

    /**
     * 查询发布内容
     *
     * @param id 发布内容主键
     * @return 发布内容
     */
    @Override
    public CpsContentInfo selectCpsContentInfoById(Long id)
    {
        return cpsContentInfoMapper.selectCpsContentInfoById(id);
    }

    /**
     * 查询发布内容列表
     *
     * @param cpsContentInfo 发布内容
     * @return 发布内容
     */
    @Override
    public List<CpsContentInfo> selectCpsContentInfoList(CpsContentInfo cpsContentInfo)
    {
        return cpsContentInfoMapper.selectCpsContentInfoList(cpsContentInfo);
    }

    /**
     * 新增发布内容
     *
     * @param cpsContentInfo 发布内容
     * @return 结果
     */
    @Override
    public int insertCpsContentInfo(CpsContentInfo cpsContentInfo)
    {
        cpsContentInfo.setCreateTime(DateUtils.getNowDate());
        return cpsContentInfoMapper.insertCpsContentInfo(cpsContentInfo);
    }

    /**
     * 修改发布内容
     *
     * @param cpsContentInfo 发布内容
     * @return 结果
     */
    @Override
    public int updateCpsContentInfo(CpsContentInfo cpsContentInfo)
    {
        cpsContentInfo.setUpdateTime(DateUtils.getNowDate());
        return cpsContentInfoMapper.updateCpsContentInfo(cpsContentInfo);
    }

    /**
     * 批量删除发布内容
     *
     * @param ids 需要删除的发布内容主键
     * @return 结果
     */
    @Override
    public int deleteCpsContentInfoByIds(Long[] ids)
    {
        return cpsContentInfoMapper.deleteCpsContentInfoByIds(ids);
    }

    /**
     * 删除发布内容信息
     *
     * @param id 发布内容主键
     * @return 结果
     */
    @Override
    public int deleteCpsContentInfoById(Long id)
    {
        return cpsContentInfoMapper.deleteCpsContentInfoById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCollectContent(Long id, String openId) {
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(id);
        if (ObjectUtils.isEmpty(cpsContentInfo)) {
            throw new ServiceException("未找到该资讯");
        }
        cpsContentInfoMapper.updateCollectContent(id);
        cpsUsrContentRelMapper.insertOrUpdateCpsUsrContent(new CpsUsrContentRel() {
            {
                setOpenId(openId);
                setContentId(id);
                setContentTitle(cpsContentInfo.getTitle());
                setThumbnail(cpsContentInfo.getThumbnail());
                setCollectFlag(SysYesNo.YES.getCode());
            }
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDisCollectContent(Long id, String openId) {
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(id);
        if (ObjectUtils.isEmpty(cpsContentInfo)) {
            throw new ServiceException("未找到该资讯");
        }
        cpsContentInfoMapper.updateDisCollectContent(id);
        cpsUsrContentRelMapper.insertOrUpdateCpsUsrContent(new CpsUsrContentRel() {
            {
                setOpenId(openId);
                setContentId(id);
                setContentTitle(cpsContentInfo.getTitle());
                setThumbnail(cpsContentInfo.getThumbnail());
                setCollectFlag(SysYesNo.NO.getCode());
            }
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLikeContent(Long id, String openId) {
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(id);
        if (ObjectUtils.isEmpty(cpsContentInfo)) {
            throw new ServiceException("未找到该资讯");
        }
        cpsContentInfoMapper.updateLikeContent(id);
        cpsUsrContentRelMapper.insertOrUpdateCpsUsrContent(new CpsUsrContentRel() {
            {
                setOpenId(openId);
                setContentId(id);
                setContentTitle(cpsContentInfo.getTitle());
                setThumbnail(cpsContentInfo.getThumbnail());
                setLikeFlag(SysYesNo.YES.getCode());
            }
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDisLikeContent(Long id, String openId) {
        CpsContentInfo cpsContentInfo = cpsContentInfoMapper.selectCpsContentInfoById(id);
        if (ObjectUtils.isEmpty(cpsContentInfo)) {
            throw new ServiceException("未找到该资讯");
        }
        cpsContentInfoMapper.updateDisLikeContent(id);
        cpsUsrContentRelMapper.insertOrUpdateCpsUsrContent(new CpsUsrContentRel() {
            {
                setOpenId(openId);
                setContentId(id);
                setContentTitle(cpsContentInfo.getTitle());
                setThumbnail(cpsContentInfo.getThumbnail());
                setLikeFlag(SysYesNo.NO.getCode());
            }
        });
    }
}
