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

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

import com.jpsoft.shinestar.modules.base.dao.MessageReceiverDAO;
import com.jpsoft.shinestar.modules.base.dao.MessageReportDAO;
import com.jpsoft.shinestar.modules.base.entity.MessagePerson;
import com.jpsoft.shinestar.modules.base.entity.MessageReceiver;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import org.junit.Test;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.MessageInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.MessageInfo;
import com.jpsoft.shinestar.modules.base.service.MessageInfoService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="messageInfoService")
public class MessageInfoServiceImpl implements MessageInfoService {
	@Resource(name="messageInfoDAO")
	private MessageInfoDAO messageInfoDAO;
	@Resource(name="messageReceiverDAO")
	private MessageReceiverDAO messageReceiverDAO;

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

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

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

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

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

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

		return page;
	}

	@Override
	public int sendForPerson(Long personId,Long sender,String title,String content,String type){
		int messageCount = 0;
		int receiverCount = 0;

		MessageInfo messageInfo = new MessageInfo();
		messageInfo.setId(UUID.randomUUID().toString());
		messageInfo.setDelFlag(false);
		messageInfo.setCreateBy(sender+"");
		messageInfo.setCreateTime(new Date());
		messageInfo.setTitle(title);
		messageInfo.setContent(content);
		messageInfo.setStatus(true);
		messageInfo.setType(type);
		messageCount = messageInfoDAO.insert(messageInfo);
		if(messageCount > 0) {
			MessageReceiver messageReceiver = new MessageReceiver();
			messageReceiver.setId(UUID.randomUUID().toString());
			messageReceiver.setDelFlag(false);
			messageReceiver.setCreateBy(sender + "");
			messageReceiver.setCreateTime(new Date());
			messageReceiver.setPersonId(personId);
			messageReceiver.setMessageId(messageInfo.getId());
			messageReceiver.setReadStatus(false);
			receiverCount = messageReceiverDAO.insert(messageReceiver);
		}
		return receiverCount;
	}
}