package org.cs.mgr.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import org.cs.core.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import org.cs.util.Pager;
import org.cs.mgr.user.service.IMgmMessageService;
import org.cs.mgr.user.dao.IMgmMessageDao;
import org.cs.mgr.user.model.MgmMessage;

/**
 * 
 * @ClassName: IMgmMessageService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:56
 */
@Service("mgmMessageService")
public class MgmMessageService extends BaseService<MgmMessage> implements IMgmMessageService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmMessageDao mgmMessageDao;
	
	@Override
	@Transactional(readOnly = true)
	public Pager getMgmMessages(Pager page){
		return this.mgmMessageDao.queryMgmMessages(page);
	}
	
	/**
	 * 自动生成
	 * @Title: delMgmMessage 
	 * @Description: TODO
	 * @param id						主键id
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean delMgmMessage(Integer id){
	
		MgmMessage mgmMessage = this.mgmMessageDao.findById(id);
		if(mgmMessage != null){
			mgmMessage.setIsDelete(true);				
			this.mgmMessageDao.update(mgmMessage);
		}
		return true;
	}
	
	
	/**
	 * 自动生成
	 * @Title: addMgmMessage 
	 * @Description: TODO
	 * @param mgmMessage						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean addMgmMessage(MgmMessage mgmMessage){
	
		if(mgmMessage != null){
			this.mgmMessageDao.insert(mgmMessage);
		}
		return true;
	}
	
	
	/**
	 *  自动生成
	 * @Title: updateMgmMessage 
	 * @Description: TODO
	 * @param mgmMessage						对象
	 * @return
	 * @return: boolean
	 */
	@Override
	@Transactional(readOnly = false)
	public boolean updateMgmMessage(MgmMessage mgmMessage){
	
		MgmMessage mgmMessage2 = this.mgmMessageDao.findById(mgmMessage.getId());
		if(mgmMessage2 != null){
			mgmMessage.mergeTo(mgmMessage2);
			this.mgmMessageDao.update(mgmMessage2);
		}
		
		return true;
	}
	
	/**
	 * 
	 * @Title: getMgmMessageList  
	 * @Description:消息列表
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public Pager getMgmMessageList(Map<String,String> map){
		String userId=map.get("userId");
		Integer page = Integer.valueOf(map.get("page"));
		return this.mgmMessageDao.queryMgmMessageByUserId(new Pager(page),userId);
	} 
	
	/**
	 * 
	 * @Title: getMgmMessageList  
	 * @Description:消息列表
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public List<MgmMessage> getMgmMessageList(String userId){
		return this.mgmMessageDao.queryMgmMessageByUserId(userId);
	} 
	
	/**
	 * 
	 * @Title:    
	 * @Description:未读消息数
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	public int countMgmMessage(String userId){
		return this.mgmMessageDao.countMgmMessage(userId);
	} 
	
	/**
	 * 
	 * @Title:    
	 * @Description:修改为已读状态
	 * @param: @param map
	 * @param: @return         
	 * @see  
	 */
	@Override
	@Transactional(readOnly = false)
	public JSONObject updateMgmMessageState(String id){
		MgmMessage message=this.mgmMessageDao.findById(Integer.parseInt(id));
		message.setState("1");
		this.mgmMessageDao.update(message);
		return getJson(1, message, "修改成功");
	} 
}
