package com.ruoyi.busi.service.impl;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.member.domain.MemberFootprint;
import com.ruoyi.member.service.IMemberFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.busi.mapper.BusiNoticeViewMapper;
import com.ruoyi.busi.domain.BusiNoticeView;
import com.ruoyi.busi.service.IBusiNoticeViewService;

/**
 * 通告浏览量Service业务层处理
 *
 * @author 朱春云
 * @date 2025-03-17
 */
@Service
public class BusiNoticeViewServiceImpl extends ServiceImpl<BusiNoticeViewMapper,BusiNoticeView>  implements IBusiNoticeViewService
{
    @Autowired
    private BusiNoticeViewMapper busiNoticeViewMapper;
    @Autowired
    private IMemberFootprintService memberFootprintService;


    @Override
    public IPage<BusiNoticeView> queryListPage(BusiNoticeView pageReqVO, Page<BusiNoticeView> page) {
        return busiNoticeViewMapper.queryListPage(pageReqVO, page);
    }
    /**
     * 浏览增加
     * @param noticeId 通告id
     * @param userId 用户id
     */
    @Override
    public void addView(String noticeId, Long userId) {
        //通告浏览量增加
        BusiNoticeView busiNoticeView = busiNoticeViewMapper.selectById(noticeId);
        if (busiNoticeView == null){
            busiNoticeView = new BusiNoticeView();
            busiNoticeView.setId(noticeId);
            busiNoticeView.setViewNum(1L);
            busiNoticeViewMapper.insert(busiNoticeView);
        }
        busiNoticeView.setViewNum(busiNoticeView.getViewNum()+1);
        busiNoticeViewMapper.updateById(busiNoticeView);
        if (userId != null){
            //通告浏览记录增加
            MemberFootprint memberFootprint = new MemberFootprint();
            memberFootprint.setNoticeId(noticeId);
            memberFootprint.setUserId(userId);
            memberFootprint.setCreateTime(new Date());
            memberFootprintService.save(memberFootprint);
        }

    }
}
