package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.base.entity.MessageInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.MessageReceiverDAO;
import com.jpsoft.shinestar.modules.base.entity.MessageReceiver;
import com.jpsoft.shinestar.modules.base.service.MessageReceiverService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="messageReceiverService")
public class MessageReceiverServiceImpl implements MessageReceiverService {
	@Resource(name="messageReceiverDAO")
	private MessageReceiverDAO messageReceiverDAO;

	@Override
	public MessageReceiver get(String id) {
		// TODO Auto-generated method stub
		return messageReceiverDAO.get(id);
	}

	@Override
	public int insert(MessageReceiver model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return messageReceiverDAO.insert(model);
	}

	@Override
	public int update(MessageReceiver model) {
		// TODO Auto-generated method stub
		return messageReceiverDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return messageReceiverDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = messageReceiverDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<MessageReceiver> list() {
		// TODO Auto-generated method stub
		return messageReceiverDAO.list();
	}
		
	@Override
	public Page<MessageReceiver> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<MessageReceiver> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            messageReceiverDAO.search(searchParams,sortList);
        });
        
        return page;
	}

	@Override
	public int countUnReadByPersonIdAndType(Long personId, String type) {
		return messageReceiverDAO.countUnReadByPersonIdAndType(personId, type);
	}

	@Override
	public Page<MessageReceiver> messagePageSearch(Map<String, Object> searchParams, int pageIndex, int pageSize, boolean count, List<Sort> sortList) {
		Page<MessageReceiver> page = PageHelper.startPage(pageIndex,pageSize,count).doSelectPage(()->{
			messageReceiverDAO.messagePageSearch(searchParams,sortList);
		});

		return page;
	}

	@Override
	public MessageReceiver findByMessageIdAndPersonId(String messageId, Long personId) {
		return messageReceiverDAO.findByMessageIdAndPersonId(messageId, personId);
	}
}