/*
package com.ssj.aijia.game.controller;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Objects;
import com.ssj.aijia.game.controller.req.VoteRankReq;
import com.ssj.aijia.game.controller.res.VoteRequest;
import com.ssj.aijia.game.controller.res.VoteSaveRequest;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.framework.core.security.annotation.IgnoreSecurity;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.RedisUtil;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.weixin.util.AdvancedUtil;
import com.ssj.framework.weixin.util.WeixinOauth2Token;
import com.ssj.framework.weixin.util.WxUtil;
import com.ssj.service.weixin.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



*/
/**
 * @author  wuwen 
 * @version 2019年12月30日10:41:50
 * 类说明  2020年会投票
 *//*

@Api(value = "2020年会投票", tags = "2020年会投票")
@RestController
@RequestMapping("/api/vote")
public class VoteDataController {
	
	private static Logger log = LoggerFactory.getLogger(VoteDataController.class);
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Reference(version = "1.0" ,timeout = 60000)
	private UserService userService;
	
	@Autowired
	private TokenManager tokenManager;
	
	//Redis_indexdb
	private Integer indexdb=15;
	
	//开始时间的Key
	private static String startTime="vote:startTime";
	
	//结束时间的Key
	private static String endTime="vote:endTime";
	
	//投票的对象列表Key
	private static String voteData="vote:voteData:";
	
	//投过票的userId集合
	private static String voteUser="vote:voteUser";
	

	
	*/
/**
	  * 初始化节目数据
	  * @return
	  *//*

	@IgnoreSecurity
	@RequestMapping(value="/init")
	@ResponseBody
	public ResponseEntity init(Model model,HttpServletRequest request, HttpServletResponse response)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
		
		VoteRankReq voteRankReq_1=new VoteRankReq();
		voteRankReq_1.setId("1");
		voteRankReq_1.setTitle("《哪吒之小塾降世》");
		voteRankReq_1.setDesc("深圳爱加 情景剧");
		voteRankReq_1.setScore(0);
		voteRankReq_1.setRecords(new ArrayList<String>());
		
		
		VoteRankReq voteRankReq_2=new VoteRankReq();
		voteRankReq_2.setId("2");
		voteRankReq_2.setTitle("《小塾 Family》");
		voteRankReq_2.setDesc("社区合作中心 歌舞剧");
		voteRankReq_2.setScore(0);
		voteRankReq_2.setRecords(new ArrayList<String>());
		
		
		VoteRankReq voteRankReq_3=new VoteRankReq();
		voteRankReq_3.setId("3");
		voteRankReq_3.setTitle("《王牌对王牌》");
		voteRankReq_3.setDesc("课程师资中心 情景剧");
		voteRankReq_3.setScore(0);
		voteRankReq_3.setRecords(new ArrayList<String>());
		
		VoteRankReq voteRankReq_4=new VoteRankReq();
		voteRankReq_4.setId("4");
		voteRankReq_4.setTitle("《1313国际科技晚会》");
		voteRankReq_4.setDesc("深圳爱加 情景剧");
		voteRankReq_4.setScore(0);
		voteRankReq_4.setRecords(new ArrayList<String>());
		
		
		VoteRankReq voteRankReq_5=new VoteRankReq();
		voteRankReq_5.setId("5");
		voteRankReq_5.setTitle("《作业里有什么》");
		voteRankReq_5.setDesc("课程师资中心 大合唱");
		voteRankReq_5.setScore(0);
		voteRankReq_5.setRecords(new ArrayList<String>());
		
		
		
		if(!redisUtil.exists(voteData+voteRankReq_1.getId())) {
			redisUtil.set(voteData+voteRankReq_1.getId(), JSONObject.toJSONString(voteRankReq_1), indexdb);
		}
		if(!redisUtil.exists(voteData+voteRankReq_2.getId())) {
			redisUtil.set(voteData+voteRankReq_2.getId(), JSONObject.toJSONString(voteRankReq_2), indexdb);
		}
		if(!redisUtil.exists(voteData+voteRankReq_3.getId())) {
			redisUtil.set(voteData+voteRankReq_3.getId(), JSONObject.toJSONString(voteRankReq_3), indexdb);
		}
		if(!redisUtil.exists(voteData+voteRankReq_4.getId())) {
			redisUtil.set(voteData+voteRankReq_4.getId(), JSONObject.toJSONString(voteRankReq_4), indexdb);
		}
		if(!redisUtil.exists(voteData+voteRankReq_5.getId())) {
			redisUtil.set(voteData+voteRankReq_5.getId(), JSONObject.toJSONString(voteRankReq_5), indexdb);
		}
		responseEntity.success("操作成功");
		return responseEntity;
	}
	
	
	
	*/
/**
	  * 登录，返回是否投过,活动状态
	  * @return
	  *//*

	@IgnoreSecurity
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity login(@RequestParam String code)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("accessToken", "");
		data.put("userId", "");
		data.put("status", getVoteStatus(redisUtil.get(startTime,indexdb), redisUtil.get(endTime,indexdb)));//活动状态
		data.put("isVote", 1);//用户是否已投票
		data.put("startTime", redisUtil.get(startTime,indexdb));//活动开始时间
		data.put("endTime", redisUtil.get(endTime,indexdb));//活动结束时间
		
   		WeixinOauth2Token token=AdvancedUtil.getOauth2AccessToken(code);
   		if(token!=null && StringUtils.isNotEmpty(token.getOpenId())) {
   			User user=userService.findByAccount(token.getOpenId());
   			if(user==null){
   				user=new User();
   				user.setId(UUID.randomUUID().toString());
   				user.setAccount(token.getOpenId());
   				user.setUnionid(token.getUnionId());
   				userService.save(user);
   			}
   			String rdsession = WxUtil.get3rdSession();
   			String accessToken = tokenManager.createToken(rdsession, user.getId(),user.getAccount(), 8, "sessionKey", "WX");
   			data.put("accessToken", accessToken);
   			data.put("userId", user.getId());
   			data.put("isVote", getIsVote(user.getId()));
   			log.info("【年会投票】登录接口：{}",JSONObject.toJSONString(data));
   			responseEntity.success(data,"查询成功");
   		}else {
   			log.info("【年会投票】登录接口：{}",JSONObject.toJSONString(data));
   			responseEntity.failure(ResponseConstant.CODE_401, "请先登录再使用此功能");
   		}
		return responseEntity;
	}
		
	
	
	*/
/**
	  * 节目排名列表。
	  * @return
	  *//*

	@RequestMapping(value = "/rankingList", method = RequestMethod.POST)
    @ApiOperation(value = "节目排名列表", httpMethod = "POST", notes = "节目排名列表")
	public ResponseEntity ranking(@RequestParam(name = "accessToken") String accessToken)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
		Map<String, Object> data = new HashMap<String, Object>();
		
		List<VoteRankReq> voteRankReqs=new ArrayList<VoteRankReq>();
		Set<String> voteDataKeys=redisUtil.keys(voteData+"*", indexdb);
		for (String key : voteDataKeys) {
			VoteRankReq voteRankReq=JSONObject.parseObject(redisUtil.get(key, indexdb), VoteRankReq.class);
			voteRankReqs.add(voteRankReq);
		}
		Collections.sort(voteRankReqs, new Comparator<VoteRankReq>(){
            public int compare(VoteRankReq o1, VoteRankReq o2) {
                if(o1.getScore() < o2.getScore()){
                    return 1;
                }
                if(o1.getScore() == o2.getScore()){
                    return 0;
                }
                return -1;
            }
        });
		for (VoteRankReq voteRankReq : voteRankReqs) {
			voteRankReq.setRecords(new ArrayList<String>());
		}
		data.put("list", voteRankReqs);
		
		responseEntity.success(data,"查询成功");
		return responseEntity;
	}
	
	
	
	*/
/**
	  * 节目状态查询
	  * @return
	  *//*

	@RequestMapping(value = "/status", method = RequestMethod.POST)
    @ApiOperation(value = "节目状态查询", httpMethod = "POST", notes = "节目状态查询")
	public ResponseEntity status(@RequestParam(name = "accessToken") String accessToken)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
		String userId=tokenManager.getUserId(accessToken);
		if (StringUtils.isBlank(userId)) {
			return responseEntity.failure(ResponseConstant.CODE_401, "请先登录再使用此功能");
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("status", getVoteStatus(redisUtil.get(startTime,indexdb), redisUtil.get(endTime,indexdb)));//活动状态
		data.put("isVote", getIsVote(userId));//用户是否已投票
		data.put("startTime", redisUtil.get(startTime,indexdb));//活动开始时间
		data.put("endTime", redisUtil.get(endTime,indexdb));//活动结束时间
		
		log.info("【年会投票】节目状态接口：{}",JSONObject.toJSONString(data));
		responseEntity.success(data,"查询成功");
		return responseEntity;
	}
	
	

	
	*/
/**
	  * 节目投票保存
	  * data : [{"id":"1","level":"1"},{"id":"2","level":"3"},{"id":"3","level":"2"}]
	  * @return
	  *//*

	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "节目投票保存", httpMethod = "POST", notes = "节目投票保存")
	public ResponseEntity save(@RequestParam(name = "accessToken") String accessToken,@RequestBody VoteSaveRequest request)throws Exception {
		ResponseEntity responseEntity = new ResponseEntity();
		String userId=tokenManager.getUserId(accessToken);
		if (StringUtils.isBlank(userId)) {
			return responseEntity.failure(ResponseConstant.CODE_401, "请先登录再使用此功能");
		}
		
		Integer status=getVoteStatus(redisUtil.get(startTime,indexdb), redisUtil.get(endTime,indexdb));
		if(Objects.equal(status, 0)) {
			responseEntity.success("活动暂未开始");
			return responseEntity;
		}
		if(Objects.equal(status, 2)) {
			responseEntity.success("活动已结束");
			return responseEntity;
		}
		Integer isVote=getIsVote(userId);
		if(Objects.equal(isVote, 1)) {
			responseEntity.success("您已投票，不可重复投票");
			return responseEntity;
		}
		
		List<VoteRequest> voteRequests=request.getVoteDatas();
		for (VoteRequest voteRequest : voteRequests) {
			synchronized (this) {
				String id=voteRequest.getId();
				Integer level=voteRequest.getLevel();
				
				String vdstr=redisUtil.get(voteData+id,indexdb);
				
				if(StringUtils.isNotEmpty(vdstr)) {
					VoteRankReq voteRankReq=JSONObject.parseObject(vdstr, VoteRankReq.class);
					if(Objects.equal(level, 1)) {
						voteRankReq.setScore(voteRankReq.getScore()+10);
					}else if(Objects.equal(level, 2)) {
						voteRankReq.setScore(voteRankReq.getScore()+20);
					}else if(Objects.equal(level, 3)) {
						voteRankReq.setScore(voteRankReq.getScore()+30);
					}
					voteRankReq.getRecords().add(userId+":"+level);
					redisUtil.set(voteData+voteRankReq.getId(), JSONObject.toJSONString(voteRankReq), indexdb);
				}
				//有一条刻录就算投了，保存已投票记录
				redisUtil.sadd(indexdb, voteUser, userId);
			}
		}
		
		log.info("【年会投票】节目投票保存接口：{}",JSONObject.toJSONString(voteData));
		responseEntity.success("保存成功");
		return responseEntity;
	}
	
	
	*/
/**
	 * 0：未投票
	 * 1：已投票
	 * @return
	 *//*

	private Integer getIsVote(String userId) {
		try {
			Boolean is=redisUtil.sismember(indexdb, voteUser, userId);
			if(is) {
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
	*/
/**
	 * 0：未开始
	 * 1：进行中
	 * 2：已结束
	 * @param startTime
	 * @param endTime
	 * @return
	 *//*

	private static Integer getVoteStatus(String start,String end) {
		//没有开始时间，返回未开始
		if(StringUtils.isEmpty(start)) {
			return 0;
		}
		Date startTime=new Date(Long.valueOf(start));  
		if(startTime.getTime()>new Date().getTime()) {
			return 0;
		}
		Date endTime=null;
		if(StringUtils.isEmpty(end)) {
			Calendar cal = Calendar.getInstance();
	        cal.setTime(startTime);
	        cal.add(Calendar.MINUTE, 15);
	        endTime=cal.getTime();
		}else {
			 endTime=new Date(Long.valueOf(end));  
		}
		if(endTime.getTime()<new Date().getTime()) {
			return 2;
		}
		return 1;
	}
	
	

}
*/
