package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;

import com.hai.common.security.UserCenter;

import com.hai.common.utils.ResponseMsgUtil;
import com.hai.config.CommonSysConst;
import com.hai.config.QianZhuConfig;
import com.hai.entity.HighUser;
import com.hai.model.ResponseData;
import com.hai.service.HighUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 javax.annotation.Resource;



@Controller
@RequestMapping(value = "/tPig")
@Api(value = "千猪接口")
public class tPigController {

    @Resource
    private HighUserService highUserService;

    @Autowired
    private UserCenter userCenter;

    @RequestMapping(value = "/getTPigKFCToken", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【小程序】KFC获取千猪Token")
    public ResponseData getH5AccessToken(@RequestParam(name = "userId", required = true) Long userId) {
        try {
            HighUser highUser = highUserService.findByUserId(userId);

            JSONObject token = QianZhuConfig.getToken(highUser.getUnionId() , highUser.getName() , highUser.getPhone());
            String url = "";
            if (token.getBoolean("success")) {
                JSONObject data = token.getJSONObject("data");
                 url = CommonSysConst.getSysConfig().getQinzhuHtmlUrl()+"/kfc/?platformId="+CommonSysConst.getSysConfig().getQinzhuPlatformId()+"&token=" + data.getString("accessToken");
            }
            return ResponseMsgUtil.success(url);
        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getTPigCinemaToken", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【小程序】电影票获取千猪Token")
    public ResponseData getTPigCinemaToken(@RequestParam(name = "userId", required = true) Long userId) {
        try {
            HighUser highUser = highUserService.findByUserId(userId);

            JSONObject token = QianZhuConfig.getToken(highUser.getUnionId() , highUser.getName() , highUser.getPhone());
            String url = "";
            if (token.getBoolean("success")) {
                JSONObject data = token.getJSONObject("data");
                url = CommonSysConst.getSysConfig().getQinzhuHtmlUrl()+"/cinema/?platformId="+CommonSysConst.getSysConfig().getQinzhuPlatformId()+"&token="+data.getString("accessToken");
            }
            return ResponseMsgUtil.success(url);
        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getGatewayToken", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "【小程序】电话费")
    public ResponseData getGatewayToken(@RequestParam(name = "userId", required = true) Long userId) {
        try {
            HighUser highUser = highUserService.findByUserId(userId);

            JSONObject token = QianZhuConfig.getMobileToken(highUser.getUnionId() , highUser.getName() , highUser.getPhone());
            String url = "";
            if (token.getBoolean("success")) {
                JSONObject data = token.getJSONObject("data");
                url = CommonSysConst.getSysConfig().getQinzhuHtmlUrl()+"/mobile/?platformId="+CommonSysConst.getSysConfig().getQinzhuPlatformId()+"&token=" + data.getString("accessToken");
            }
            return ResponseMsgUtil.success(url);
        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

}
