package com.ssj.service.weixin.push.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.ssj.bean.weixin.push.domain.PushTemplate;
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.rabbitmq.TopicRabbitConfig;
import com.ssj.framework.core.rabbitmq.producer.BaseRabbitSend;
import com.ssj.service.weixin.push.service.PushTemplateService;
import com.ssj.service.weixin.push.service.dao.PushTemplateDao;
import com.ssj.taskthread.Task;
import com.ssj.taskthread.ThreadPool;


/** 
 * @author  wuwen 
 * @version 2019年4月17日18:11:28
 * 类说明 
 */

@Service(version="1.0")
@Transactional
public class PushTemplateServiceImpl  extends BaseServiceImpl<PushTemplate, String> implements PushTemplateService {
	
	private static Logger logger = LoggerFactory.getLogger(PushTemplateServiceImpl.class);
	
    @Autowired
	private PushTemplateDao dao;
 

	public boolean savePushWxTemplate(SendTemplateShort sendTemplateShort) {
		boolean is=true;
		ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao));
		return is;
	}

	public void saveAndSendPushWxTemplate(SendTemplateShort sendTemplateShort) {
		ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao));
	}

	public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
		logger.info("直接保存推送信息data:"+JSONObject.toJSONString(sendTemplateShort));
		try {
			PushTemplate pushTemplate =new PushTemplate();
			pushTemplate.setClickUrl(sendTemplateShort.getUrl());
			pushTemplate.setInterVal(sendTemplateShort.getInterVal());
			pushTemplate.setOpenid(sendTemplateShort.getTouser());
			pushTemplate.setTopColor(sendTemplateShort.getTopColor());
			pushTemplate.setTotal(sendTemplateShort.getTotal());
			pushTemplate.setWxTplSid(sendTemplateShort.getTemplate_id_short());
			pushTemplate.setContent(JSONObject.toJSONString(sendTemplateShort.getData()));
			if(sendTemplateShort.getMiniprogram()!=null && StringUtils.isNotEmpty(sendTemplateShort.getMiniprogram().getAppid())){
				pushTemplate.setMiniprogram(JSONObject.toJSONString(sendTemplateShort.getMiniprogram()));
			}
			pushTemplate.setCreateTime(new Date());
			pushTemplate.setCurTotal(0);
			pushTemplate.setIsSuccess(0);
			pushTemplate.setStat(1);
			pushTemplate.setTryTime(0);
			pushTemplate.setRemark("");
			pushTemplate=this.save(pushTemplate);
			sendTemplateShort.setPushTemplateId(pushTemplate.getId());
		} catch (Exception e) {
			logger.error("保存推送信息异常...");
			return false;
		}
		return true;
	}
	
	
	

	@Override
	public void pushTemplate() {
	}

	@Override
	public void savePushTemplate(String objectId, String content) {
	}

	@Override
	public void pushWxTemplate() {
	}

	@Override
	public PagingAndSortingRepository<PushTemplate, String> getDao() {
		return dao;
	}
	
	

}


class SyncSaveAndSendPushDBTask extends Task{
	
	private static Logger logger = LoggerFactory.getLogger(SyncSaveAndSendPushDBTask.class);
	
	private SendTemplateShort sendTemplateShort;
	
	private PushTemplateDao dao;
	
	public SyncSaveAndSendPushDBTask(SendTemplateShort sendTemplateShort,PushTemplateDao dao) {
	    super();
	    this.sendTemplateShort=sendTemplateShort;
	    this.dao=dao;
	} 
	
	@Override
	public void run() {
		try {
			savePushWxTemplateByDb(sendTemplateShort);
		} catch (Exception e) {
			logger.error("推送信息保存异常...");
		}
		try {
			String uuid=BaseRabbitSend.convertAndTopicSend(
					TopicRabbitConfig.exchange_task,
					TopicRabbitConfig.topic_push_messages_wx, 
					sendTemplateShort);
			logger.error("已加入推送信息队列数据uuid："+uuid+" data:"+JSONObject.toJSONString(sendTemplateShort));
		} catch (Exception e) {
			logger.error("推送信息队列异常...");
		}
	}

	public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
		logger.info("直接保存推送信息data:"+JSONObject.toJSONString(sendTemplateShort));
		try {
			PushTemplate pushTemplate =new PushTemplate();
			pushTemplate.setClickUrl(sendTemplateShort.getUrl());
			pushTemplate.setInterVal(sendTemplateShort.getInterVal());
			pushTemplate.setOpenid(sendTemplateShort.getTouser());
			pushTemplate.setTopColor(sendTemplateShort.getTopColor());
			pushTemplate.setTotal(sendTemplateShort.getTotal());
			pushTemplate.setWxTplSid(sendTemplateShort.getTemplate_id_short());
			pushTemplate.setContent(JSONObject.toJSONString(sendTemplateShort.getData()));
			if(sendTemplateShort.getMiniprogram()!=null && StringUtils.isNotEmpty(sendTemplateShort.getMiniprogram().getAppid())){
				pushTemplate.setMiniprogram(JSONObject.toJSONString(sendTemplateShort.getMiniprogram()));
			}
			pushTemplate.setCreateTime(new Date());
			pushTemplate.setCurTotal(0);
			pushTemplate.setIsSuccess(0);
			pushTemplate.setStat(1);
			pushTemplate.setTryTime(0);
			pushTemplate.setRemark("");
			pushTemplate=dao.save(pushTemplate);
			sendTemplateShort.setPushTemplateId(pushTemplate.getId());
		} catch (Exception e) {
			logger.error("保存推送信息异常...");
			return false;
		}
		return true;
	}
	
	
	
}



