package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.Vip;
import com.chinacreator.video.bean.VipInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.dto.QueryGroupVipOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.QueryFiveVipsService;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 腾讯，爱奇艺，优酷，芒果，b站会员领取状态查询
 * @author guangshu.li
 * @date 20210507
 */
@RestController
@RequestMapping(value = "/vip")
public class QueryFvieVipsController {

	private static Logger logger = Logger.getLogger(QueryFvieVipsController.class);

	private static Logger log = Logger.getLogger("queryFiveVipReq");
	
	@Autowired
	private QueryFiveVipsService service;
	
	/**
	 * 查询五大产品领取状态
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/queryFiveVip", method = RequestMethod.POST, produces = "application/json")
    public QueryGroupVipOutPipe queryVip(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
		QueryGroupVipOutPipe outpipe = new QueryGroupVipOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		//参数和ip校验
    		service.busiCheck(inputObj);
    		String channel = "boyuan";
    		String userid = inputObj.getString("userid");
			List<HashMap> vipInfos = service.addActivity(channel);
			System.out.println(vipInfos.toString());
			List<Vip> vips = service.queryVip(vipInfos,userid);
            outpipe.setVips(vips);
			resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			e.printStackTrace();
			resultcode = "8000";
			errorinfo = "出现异常，"+e.getMessage();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}
		} finally {			 			
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			logmap.put("rspData", outpipe.getVips());
			log.info(JSON.toJSONString(logmap));
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }

    
}
