package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hezhi.domain.TbNotice;
import com.ruoyi.hezhi.domain.vo.NoticeVO;
import com.ruoyi.hezhi.mapper.TbNoticeMapper;
import com.ruoyi.hezhi.service.ITbNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * 通知公告Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class TbNoticeServiceImpl implements ITbNoticeService
{
    @Autowired
    private TbNoticeMapper tbNoticeMapper;

    /**
     * 查询通知公告
     *
     * @param noticeId 通知公告主键
     * @return 通知公告
     */
    @Override
    public TbNotice selectTbNoticeByNoticeId(Long noticeId)
    {
        return tbNoticeMapper.selectTbNoticeByNoticeId(noticeId);
    }

    /**
     * 查询通知公告列表
     *
     * @param tbNotice 通知公告
     * @return 通知公告
     */
    @Override
    public List<TbNotice> selectTbNoticeList(TbNotice tbNotice)
    {
        return tbNoticeMapper.selectTbNoticeList(tbNotice);
    }

    /**
     * 新增通知公告
     *
     * @param tbNotice 通知公告
     * @return 结果
     */
    @Override
    public int insertTbNotice(TbNotice tbNotice)
    {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            tbNotice.setPublisherId(0L);
        }else {
            tbNotice.setPublisherId(SecurityUtils.getUserId());
        }
        tbNotice.setCreateTime(DateUtils.getNowDate());
        return tbNoticeMapper.insertTbNotice(tbNotice);
    }

    /**
     * 修改通知公告
     *
     * @param tbNotice 通知公告
     * @return 结果
     */
    @Override
    public int updateTbNotice(TbNotice tbNotice)
    {
        tbNotice.setUpdateTime(DateUtils.getNowDate());
        return tbNoticeMapper.updateTbNotice(tbNotice);
    }

    /**
     * 批量删除通知公告
     *
     * @param noticeIds 需要删除的通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTbNoticeByNoticeIds(Long[] noticeIds)
    {
        return tbNoticeMapper.deleteTbNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除通知公告信息
     *
     * @param noticeId 通知公告主键
     * @return 结果
     */
    @Override
    public int deleteTbNoticeByNoticeId(Long noticeId)
    {
        return tbNoticeMapper.deleteTbNoticeByNoticeId(noticeId);
    }

    /**
     * 获取公告列表
     * @return
     */
    @Override
    public List<NoticeVO> getNoticeList() {
        return tbNoticeMapper.selectNoticeList();
    }

    /**
     * 获取公告列表
     *
     * @param params 参数
     * @return 公告列表
     */
    @Override
    public List<Map<String, Object>> getPCNoticeList(Map<String, Object> params) {
        return tbNoticeMapper.getPCNoticeList(params);
    }

    /**
     * 查询公告详情
     *
     * @param params 参数
     * @return 公告列表
     */
    @Override
    public Map<String, Object> getNoticeInfo(Map<String, Object> params) {
        return tbNoticeMapper.getNoticeInfo(params);
    }

    /**
     * 浏览量+1
     *
     * @param noticeId 通知公告主键
     * @return 结果
     */
    @Override
    public int addViewsNum(Long noticeId) {
        return tbNoticeMapper.addViewsNum(noticeId);
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @Override
    public NoticeVO getDetail(Long id) {
        TbNotice detail = tbNoticeMapper.selectTbNoticeByNoticeId(id);
        NoticeVO vo = BeanUtil.toBean(detail,NoticeVO.class);
        if(ObjectUtils.isEmpty(vo)){
            throw new ServiceException("查询公告详情异常",500);
        }else{
            return vo;
        }
    }
}
