package com.platform.modules.app.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.platform.common.utils.R;
import com.platform.common.utils.RedisContants;
import com.platform.common.utils.RedisUtils;
import com.platform.common.utils.StringKit;
import com.platform.modules.live.entity.LiveSceneCountEntity;
import com.platform.modules.live.service.LiveSceneCountService;
import com.platform.modules.live.vo.SceneCountVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * APP现场计数接口
 *
 * @author weiyan
 */
@Api(value = "APP现场计数", tags = "APP现场计数")
@RestController
@RequestMapping("/app/live/scene")
public class AppLiveSceneCountController {
	
	@Autowired
	private LiveSceneCountService liveSceneCountService;
	@Autowired
	private RedisUtils redisUtils;
	
	@GetMapping("/counts")
    @ApiOperation("APP获取现场计数、并记录本次浏览数")
    @ApiImplicitParams({@ApiImplicitParam(value = "现场id", name = "sceneId", paramType = "query", dataType = "String")})
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveSceneCountEntity.class, responseContainer = "List")})
	public R sceneViews(String sceneId, HttpServletRequest request) throws JSONException {
		if (sceneId == null) {
			return R.error("现场id为空");
		}

		// 20200317_Mxy 对地址中id做截取操作
		String subId;

		if (sceneId.contains("&")){
			subId = StringUtils.substringBefore(sceneId, "&");
		}else {
			subId = sceneId;
		}

		if (StringUtils.isBlank(subId)){
			return R.error("参数错误");
		}
		
		return R.ok().put("count", liveSceneCountService.getSceneCountByRedis(Integer.valueOf(subId)));
	}
	
	@GetMapping("/getCounts")
    @ApiOperation("APP获取现场列表计数")
    @ApiImplicitParams({@ApiImplicitParam(value = "现场id,多个ID用','分割", name = "sceneIds", paramType = "query", dataType = "String")})
	@ApiResponses({@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = LiveSceneCountEntity.class, responseContainer = "List")})
	public R getSceneViews(String sceneIds, HttpServletRequest request) throws JSONException {
		if (sceneIds == null || sceneIds.trim().length() <= 0) {
			return R.error("现场id为空");
		}
		Integer[] ids = StringKit.convertStrToInt(sceneIds, ",");
		if(ids == null || ids.length <= 0) {
			return R.error("现场id为空");
		}
		List<SceneCountVo> list = new ArrayList<SceneCountVo>();
		String key = RedisContants.SCENE_COUNT;
		for(Integer id : ids) {
			String fieid = RedisContants.SCENE_COUNT + id;
			SceneCountVo sceneCountVo = redisUtils.hget(key, fieid, SceneCountVo.class);
			if(sceneCountVo == null) {
				sceneCountVo = new SceneCountVo();
				LiveSceneCountEntity count = liveSceneCountService.selectById(id);
				if(count == null) {
					sceneCountVo.setViews(0);
					sceneCountVo.setViewsShow(0);
					sceneCountVo.setComments(0);
					sceneCountVo.setCommentsShow(0);
				} else {
					sceneCountVo.setViews(count.getViews() == null ? 0 : count.getViews());
					sceneCountVo.setViewsShow(count.getViewsShow() == null ? 0 : count.getViewsShow());
					sceneCountVo.setComments(count.getComments() == null ? 0 : count.getComments());
					sceneCountVo.setCommentsShow(count.getCommentsShow() == null ? 0 : count.getCommentsShow());
				}
				sceneCountVo.setSceneId(id);
				redisUtils.hset(key, fieid, sceneCountVo);
			}
			list.add(sceneCountVo);
		}
		return R.ok().put("list", list);
	}

}
