package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.EventDrawService;
import com.chinacreator.video.restful.service.ReturnActiService;
import org.apache.commons.lang.StringUtils;
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.Map;


/**
 * 爱奇艺20、腾讯20
 * 限时5元领15元话费活动时间
 * @author su.yu
 * @date  2020.7.24
 */
@RestController
@RequestMapping(value = "/event")
public class ReturnActiController {
	
	private static Logger logger = Logger.getLogger(ReturnActiController.class);
	
	private static Logger log = Logger.getLogger("ReturnActi");
	
	@Autowired
	private ReturnActiService eventDrawService;
	
	/**
	 * 查询是否有抽奖资格
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/returnActivity", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> prizeauth(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe<Map<String,Object>> outpipe = new DataOutPipe<Map<String,Object>>();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "prizeauth");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
		Map<String,Object> res = null;
    	try {
    		if (StringUtils.isEmpty(params)){
				throw new BussinessException("80015", "参数异常，");
			}
    		JSONObject inputObj = JSON.parseObject(params);
    		log.info("校验开始"+inputObj);
    		eventDrawService.busiCheck(inputObj, logmap.get("forward").toString());
    		log.info("查询是否有活动资格");
			res= eventDrawService.eventStatus(inputObj);
			//保存到领取名单
			eventDrawService.insertActivity(res);
			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 {
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			outpipe.setData(res);
			log.info("限时5元领15元话费活动返回参数:"+JSON.toJSONString(outpipe));
			return outpipe;
		}
    }
}
