package org.linlinjava.jz.db.service.hm;

import org.linlinjava.jz.db.dao.HmNoticeUserMapper;
import org.linlinjava.jz.db.domain.HmNoticeUser;
import org.linlinjava.jz.db.domain.HmNoticeUserExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class HmNoticeUserService {

	@Resource
	private HmNoticeUserMapper hmNoticeUserMapper;

	public void insert(HmNoticeUser user) {
		user.setCreateTime(LocalDateTime.now());
		hmNoticeUserMapper.insert(user);
	}
	
	public int update(HmNoticeUser noticeUser) {
		return hmNoticeUserMapper.updateByPrimaryKeySelective(noticeUser);
	}

	public List<HmNoticeUser> getNoticeuserList(Long userId) {
		HmNoticeUserExample example = new HmNoticeUserExample();
		example.or().andAppUserIdEqualTo(userId);
		example.orderBy("id desc");

		return hmNoticeUserMapper.selectByExample(example);
	}
	
	
	public List<HmNoticeUser> getNoticeIduserIdList(Long noticeId,Long userId) {
		HmNoticeUserExample example = new HmNoticeUserExample();
		example.or().andAppUserIdEqualTo(userId).andNoticeIdEqualTo(noticeId);
		example.orderBy("id desc");

		return hmNoticeUserMapper.selectByExample(example);
	}
	
	/**
	 * 获取未读的通知
	 * @param noticeId
	 * @param userId
	 * @return
	 */
	public List<HmNoticeUser> getNoReadNoticeIduserIdList(Long noticeId,Long userId) {
		HmNoticeUserExample example = new HmNoticeUserExample();
		example.or().andAppUserIdEqualTo(userId).andNoticeIdEqualTo(noticeId).andIsReadEqualTo(0);
		 
		return hmNoticeUserMapper.selectByExample(example);
	}
	
	public  HmNoticeUser getLastNoticeIduserIdList(Long noticeId,Long userId) {
		HmNoticeUserExample example = new HmNoticeUserExample();
		example.or().andAppUserIdEqualTo(userId).andNoticeIdEqualTo(noticeId);
		example.orderBy("id desc");

		return hmNoticeUserMapper.selectOneByExample(example);
	}
}
