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

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
import com.ssj.bean.weixin.user.domain.UserIntegral;
import com.ssj.dao.weixin.user.dao.UserIntegralDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.idworker.IdWorker;
import com.ssj.service.weixin.push.service.PushTemplateService;
import com.ssj.service.weixin.user.service.UserIntegralService;
import com.ssj.service.weixin.user.service.UserService;
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 java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service(version="1.0")
@Transactional
public class UserIntegralServiceImpl extends BaseServiceImpl<UserIntegral, String> implements UserIntegralService{
	  private   Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserIntegralDao  userIntegralDao;
	
	@Autowired
	private UserService  userService;
	
	@Autowired
	private TokenManager tokenManager;
	 @Autowired
	private PushTemplateService  pushTemplateService;
	@Override
	public PagingAndSortingRepository<UserIntegral, String> getDao() {
		return userIntegralDao;
	}

	@Override
	public UserIntegral findByUserId(String userId) {
		UserIntegral  integral = userIntegralDao.findByUserId(userId);
		if(integral==null){
			integral = new UserIntegral();
			integral.setUserId(userId);
			integral.setInIntegral(new BigDecimal(0));
			integral.setExIntegral(new BigDecimal(0));
			integral.setIntegral(new BigDecimal(0));
			this.save(integral);
		}
		return integral;
	}

	@Override
	public synchronized Response operatingIntegral(String userId, Integer type,
			BigDecimal integral) {
		 Response response = new Response();
		 UserIntegral  userintegral = userIntegralDao.findByUserId(userId);
		 if(userintegral==null){
				userintegral = new UserIntegral();
				userintegral.setUserId(userId);
				userintegral.setInIntegral(new BigDecimal(0));
				userintegral.setExIntegral(new BigDecimal(0));
				userintegral.setIntegral(new BigDecimal(0));
				userintegral.setType(0);
		 }
		if(type==1){
			userintegral.setInIntegral(userintegral.getInIntegral().add(integral));
		}else{
			userintegral.setExIntegral(userintegral.getExIntegral().add(integral));
		}
		BigDecimal total=userintegral.getInIntegral().subtract(userintegral.getExIntegral());
		if(userintegral.getType()==null)userintegral.setType(0);
		if(type==1){
			//增加积分，推送消息
			//if(userintegral.getType()<=2){
				//如果积分大于 //推送一条消息
				//if(total.compareTo(new BigDecimal(7))>=0){
				if((userintegral.getType()==0 && total.compareTo(new BigDecimal(3))>=0)
						|| (userintegral.getType()==1 && total.compareTo(new BigDecimal(7))>=0)
						|| (userintegral.getType()==2 && total.compareTo(new BigDecimal(11))>=0)
						){
					int userType =userintegral.getType();
					userType++;
					userintegral.setType(userType);
					Map<String,String> data =new HashMap<String, String>();
					/*
					data.put("first", "尊敬的会员，您已累计打卡"+userintegral.getInIntegral()+"次，可以进行塾公礼包大转盘抽奖一次啦！\n");
					data.put("keyword1","塾公6大礼包");
					data.put("keyword2", DateHelper.format(new Date(),"yyyy年MM月dd日"));
					data.put("remark", "\n连续打卡第3、10、21天可以参与抽奖一次，点击详情即可参与抽奖，快来把奖品带回家吧!");
					*/
					String template_id=(String) SystemResourceLocator.getValue("sendTemplate1010");
					if(userintegral.getInIntegral().intValue()==3){
						data.put("first", "努力的人，运气都不会太差！\n 您已累计3天完成打卡，送你一次抽取塾公礼包的机会。\n 点击文字进入抽奖页面，把大奖带回家吧！");
						data.put("keyword1","塾公礼包");
						data.put("keyword2","2017年11月18日");
						data.put("remark", "\n偷偷告诉你，再打卡7天还有抽奖机会噢！");
					}else if(userintegral.getInIntegral().intValue()==10){
						data.put("first", "唯有坚持，才能遇到更好的自己！ \n 您已累计10天完成打卡，送你一次抽取塾公礼包的机会。\n 点击文字进入抽奖页面，把大奖带回家吧！");
						data.put("keyword1","塾公礼包");
						data.put("keyword2",DateHelper.format(new Date(), "yyyy年MM月dd日"));
						data.put("remark", "\n偷偷告诉你，再打卡11天还有抽奖机会噢！");
						
					}else if(userintegral.getInIntegral().intValue()==21){
						data.put("first", "21天，收获一个崭新的你 \n 你已经累计21天完成打卡，送你一次抽取塾公礼包的机会。\n 点击文字进入抽奖页面，把大奖带回家吧！");
						data.put("keyword1","塾公礼包");
						data.put("keyword2","2017年11月29日");
						data.put("remark", "\n偷偷告诉你，发送打卡心得和打卡截图可以赢取大奖噢！\n\n老师们会将你发送的内容整理成投票推文，投票第一名可以获得智能护眼灯，第二名可以获得塾公全套礼包，第三名可以活动塾公背毯仔一个，所以参与活动的同学们可以获得3套古诗学习卡包。");
						
					}
					
					String url=(String) SystemResourceLocator.getValue("sys_url")+"/weixin/hd/game_469cc26c-0e5e-4517-a078-3be289642e09.html";
					SendTemplateShort sendTemplateShort=new SendTemplateShort(userService.getAccount(userId),template_id,url,data);
					pushTemplateService.savePushWxTemplate(sendTemplateShort);
					
				}
			//}
		}
		userintegral.setIntegral(total);
		this.save(userintegral);
		
		response.success("操作成功");
		return  response;
		
	}

	@Override
	public void operatingIntegralLog(String userId, Integer type,
			BigDecimal integral) {
		StringBuffer content=new StringBuffer();
		if(type==1){
			content.append("您的回复被点赞，获得").append(integral).append("积分");
		}else if(type==2){
			content.append("您回复一个问题，获得").append(integral).append("积分");
		}else if(type==3){
			content.append("您发起一个提问，获得").append(integral).append("积分");
		}else if(type==4){
			content.append("您旁听了一个问题，获得").append(integral).append("积分");
		}else if(type==5){
			integral=integral.multiply(new BigDecimal(-1));
			content.append("您抽奖一次，消费").append(integral).append("积分");
		}

		userIntegralDao.insertIntegralLog(userId, "账户积分变动", content.toString(), integral, IdWorker.generateId());
	}


	@Override
	public void emptyPoints() {
		userIntegralDao.emptyPoints();
	}

}
