package com.goafanti.message.service.impl;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.JtMessageConsumerMapper;
import com.goafanti.common.dao.JtMessageProducerMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.model.JtMessageConsumer;
import com.goafanti.common.model.JtMessageProducer;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.message.service.JtMessageProducerService;

@Service
public class JtMessageProducerServiceImpl  extends BaseMybatisDao<JtMessageProducerMapper> implements JtMessageProducerService{
	@Autowired
	private JtMessageProducerMapper jtMessageProducerMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Autowired
	private JtMessageConsumerMapper jtMessageConsumerMapper;
	

	@Override
	public int addMessage(JtMessageProducer messageProducer) {
		//处理消息内容
		messageProducer.setId(UUID.randomUUID().toString());
		//获得当前登录用户名字
		String create = adminMapper.selectByPrimaryKey(TokenManager.getAdminId()).getName();
		if(StringUtils.isBlank(create)){
			create = userMapper.selectByPrimaryKey(TokenManager.getUserId()).getUsername();
		}
		if(StringUtils.isBlank(create)){
			return -1;
		}
		messageProducer.setCreater(create);
		if(null != messageProducer.getCreateTime()){
			messageProducer.setCreateTime(new Date());
		}
		return jtMessageProducerMapper.insertSelective(messageProducer);
	}
	
	@Override
	public int addSendMessage(String messageId) {
		// 获得所有正常使用的注册用户
		List<JtMessageConsumer> list = userMapper.getAllUser();
		Iterator<JtMessageConsumer> it = list.iterator();
		while (it.hasNext()) {
			JtMessageConsumer mc = (JtMessageConsumer) it.next();
			mc.setId(UUID.randomUUID().toString());
			mc.setMessageId(messageId);
		}
		JtMessageProducer messageProducer = new JtMessageProducer();
		messageProducer.setId(messageId);
		messageProducer.setSendTime(new Date());
		messageProducer.setIsSend(true);
		messageProducer.setIsDraft(false);
		jtMessageProducerMapper.updateByPrimaryKeySelective(messageProducer);
		return jtMessageConsumerMapper.addBatch(list);
	}

	@Override
	public JtMessageProducer getMessageById(String messageId) {
		return jtMessageProducerMapper.selectByPrimaryKey(messageId);
	}

	@Override
	public int updateMessageById(JtMessageProducer messageProducer) {
		int count = 0;
		// 推送消息
		if(messageProducer.getIsSend() && !messageProducer.getDeleteSign() && !messageProducer.getIsDraft()){
			if(null == messageProducer.getSendTime()) messageProducer.setSendTime(new Date());
			count += addSendMessage(messageProducer.getId());
		}
		// 修改消息
		messageProducer.setCreateTime(null);//不修改创建时间
		count = jtMessageProducerMapper.updateByPrimaryKeySelective(messageProducer);
		return count;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<JtMessageProducer> getMessageList(JtMessageProducer messageProducer, String sendStartTime, String sendEndTime, Integer pageNo,Integer pageSize) {
		if(pageNo==null || pageNo<1)pageNo=1;
		if(pageSize==null ||pageSize<1)pageSize=10; 
		Map<String, Object> params=new HashMap<String,Object>();
		//params.put("create", TokenManager.getUserId());
		if(StringUtils.isNotBlank(messageProducer.getTitle()))params.put("title",messageProducer.getTitle());
		if(null != messageProducer.getIsDraft())params.put("isDraft",messageProducer.getIsDraft());
		if(null != messageProducer.getIsSend())params.put("isSend",messageProducer.getIsSend());
		if(StringUtils.isNotBlank(sendStartTime))params.put("sendStartTime",sendStartTime);
		if(StringUtils.isNotBlank(sendEndTime))params.put("sendEndTime",sendEndTime);
		return (Pagination<JtMessageProducer>) findPage("getMyMessageList", "getMyMessageCount", params, pageNo, pageSize);
	}


}
