package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.Message;
import com.cloudcross.ssp.service.IMessageService;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;

@Service
public class MessageService implements IMessageService {
	@Autowired
	private GenericIBatisDao myBatisDao;
	
	@Override
	public Message findById(Long id) {
		return myBatisDao.get("messageSqlMapper.selectById", id);
	}
	@Override
	public Message selectByRead(Long id) {
		return myBatisDao.get("messageSqlMapper.selectByRead", id);
	}

	@Override
	public int countByParams(Map<String, Object> paramMap) {
	if(paramMap.get("searchValue")!=null && paramMap.get("searchValue")!=""){
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("value", searchValue);
		}else{
			paramMap.put("value", null);
		}
		
		return myBatisDao.get("messageSqlMapper.countByParams", paramMap);
	}

	@Override
	public List<Message> findByParams(Map<String, Object> paramMap, Pager pager) {
		if(paramMap.get("searchValue")!=null && paramMap.get("searchValue")!=""){
			String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
			paramMap.put("value", searchValue);
		}else{
			paramMap.put("value", null);
		}
		paramMap.put("pager", pager);
	
		return myBatisDao.getList("messageSqlMapper.selectByParams", paramMap);
	}

	@Override
	public boolean add(Message t) {
		//原来类型错误，里面是new Integer(0),我改成false
		t.setRead(false);
		t.setSendTime(new Date());
		myBatisDao.save("messageSqlMapper.addMessage", t);
		return true;
	}

	@Override
	public boolean edit(Message t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateRead(List<Long> IdList, int read) {
		myBatisDao.save("messageSqlMapper.updateRead",
				MapBuilder.create(HashMap.class)
					.add("IdList", IdList)
					.add("modifyDate", new Date())
					.add("read", read).map());
		return true;
	}

	@Override
	public int countByStatus(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public List<Message> findByType(Map<String, Object> paramMap) {
		String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
		paramMap.put("key", paramMap.get("searchKey"));
		paramMap.put("value", searchValue);
		return myBatisDao.getList("messageSqlMapper.selectByStatus", paramMap);
	}
	@Override
	public List<Message> findByType(int type) {
		
		return myBatisDao.getList("messageSqlMapper.findByType", type);
	}

	@Override
	public boolean updateStatus(List<Long> idList, int status) {
	
		return false;
	}
	
}
