package com.xhpc.system.service.impl;

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

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.system.domain.SysNotice;
import com.xhpc.system.mapper.SysNoticeMapper;
import com.xhpc.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {




        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }


    @Override
    public List<Map<String, Object>> show(Long userId) {
        //If status's value equals zero it means read, if not not read.
        List<Map<String,Object>> list=noticeMapper.selectTypeContentCreateTimeId();
        List<Map<String,Object>> readId=noticeMapper.selectNotReadIds(userId);
        for(Map<String,Object> value1:list){
            int temp1= (int) value1.get("noticeId");
            boolean flag=false;
            for(Map<String,Object> value2:readId){
                int temp2=(int)value2.get("noticeId");
                if(temp1==temp2){
                    flag=true;
                    value1.put("status",1);
                    break;
                }
            }
            if(!flag){
                value1.put("status",0);
            }

        }

        return list;
    }

    @Override
    public int showNotRead(Integer userId) {

        return noticeMapper.countNotRead(userId);
    }


    @Override
    public AjaxResult addReadItem(Integer noticeId, Long userId) {

        List<Map<String,Object>> notReadIds=noticeMapper.selectNotReadIds(userId);
        boolean flag=false;
        for(Map<String,Object> value:notReadIds){
            int temp1=(int)value.get("noticeId");
            if(noticeId==temp1){
                flag=true;
                break;
            }

        }
        if (flag) {
            int res = noticeMapper.insertIntoRead(noticeId, userId);
            if (res == 0) {
                return AjaxResult.error("插入失败");
            }
        }
        return AjaxResult.success();
    }


    @Override
    public Map<String, Object> showDetail(Integer noticeId) {

        return noticeMapper.selectContent(noticeId);
    }
}
