package com.ruoyi.ehsMyMessage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ehsNoticeMessage.domain.EhsNoticeMessage;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;
import com.ruoyi.ehsMyMessage.mapper.EhsMyMessageMapper;
import com.ruoyi.ehsMyMessage.domain.EhsMyMessage;
import com.ruoyi.ehsMyMessage.service.IEhsMyMessageService;

/**
 * 我的的消息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-16
 */
@Service
public class EhsMyMessageServiceImpl extends ServiceImpl<EhsMyMessageMapper, EhsMyMessage> implements IEhsMyMessageService {

	@Autowired
	private EhsMyMessageMapper ehsMyMessageMapper;

	/**
	 * 查询我的的消息
	 *
	 * @param myMessageId 我的的消息主键
	 * @return 我的的消息
	 */
	@Override
	public EhsMyMessage selectEhsMyMessageByMyMessageId(Long myMessageId)
	{
		return ehsMyMessageMapper.selectEhsMyMessageByMyMessageId(myMessageId);
	}

	/**
	 * 查询我的的消息列表
	 *
	 * @param ehsMyMessage 我的的消息
	 * @return 我的的消息
	 */
	@Override
	public List<EhsMyMessage> selectEhsMyMessageList(EhsMyMessage ehsMyMessage)
	{
		return ehsMyMessageMapper.selectEhsMyMessageList(ehsMyMessage);
	}


	private LambdaQueryWrapper<EhsMyMessage> buildQueryWrapper(EhsMyMessage query) {
		Map<String, Object> params = query.getParams();
		LambdaQueryWrapper<EhsMyMessage> lqw = Wrappers.lambdaQuery();
					lqw.eq(StringUtils.isNotBlank(query.getMyMessageKey()), EhsMyMessage::getMyMessageKey, query.getMyMessageKey());
					lqw.eq(StringUtils.isNotBlank(query.getMyMessageType()), EhsMyMessage::getMyMessageType, query.getMyMessageType());
					lqw.eq(StringUtils.isNotBlank(query.getMyMessageDesc()), EhsMyMessage::getMyMessageDesc, query.getMyMessageDesc());
					lqw.eq(StringUtils.isNotBlank(query.getMyMessageState()), EhsMyMessage::getMyMessageState, query.getMyMessageState());
					lqw.eq(query.getMyUserId() != null, EhsMyMessage::getMyUserId, query.getMyUserId());
				lqw.orderByDesc(EhsMyMessage::getCreateTime);
		return lqw;
	}
	public EhsMyMessage selectEhsMyMessageByMyMessageMax(EhsMyMessage ehsMyMessage){
		return ehsMyMessageMapper.selectEhsMyMessageByMyMessageMax(ehsMyMessage);
	}
	@Override
	public Long msgCount(EhsMyMessage query){
		return ehsMyMessageMapper.selectCount(buildQueryWrapper(query));
	}

	public List<EhsMyMessage> selectUnReadNotice(EhsMyMessage ehsMyMessage){
		return ehsMyMessageMapper.selectUnReadNotice(ehsMyMessage);
	}
	public boolean  saveMyMessage(EhsMyMessage msgNew){
		msgNew.setMyUserId(SecurityUtils.getUserId());
		msgNew.setMyMessageType("1");
		msgNew.setMyMessageDesc("您有新的消息通知未阅读，请及及时处理！");
		msgNew.setMyMessageState(StringUtils.isNotEmpty(msgNew.getMyMessageState())?msgNew.getMyMessageState():"N");

		/*int i = ehsMyMessageMapper.updatebyKeyUserId(msgNew);
		if(i==0){
			save(msgNew);
		}*/
		return save(msgNew);
	}
	public int updatebyKeyUserId(EhsMyMessage ehsMyMessage){
		return ehsMyMessageMapper.updatebyKeyUserId(ehsMyMessage);
	}

	public int updateHDbyKeyUserId(EhsMyMessage ehsMyMessage){
		return ehsMyMessageMapper.updateHDbyKeyUserId(ehsMyMessage);
	}
}
