package com.playlet.web.controller.tool;

import com.playlet.common.core.domain.Result;
import com.playlet.web.service.WxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>微信服务端接口</p>
 * @author clunt
 */
@Controller
@Api(tags = "小程序*微信接口")
@RequestMapping("/tool/wx")
@CrossOrigin(origins = "*", maxAge = 3600)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WxController {

    private final WxService wxService;

    @ApiOperation(value = "获取openId", httpMethod = "POST")
    @PostMapping("/getOpenidByCode")
    @ResponseBody
    public Result<String> getOpenidByCode(@RequestParam(value = "code") String code){
        return Result.success(wxService.getOpenidByCode(code));
    }

    @ApiOperation(value = "获取企业openId", httpMethod = "POST")
    @PostMapping("/getCompanyOpenidByCode")
    @ResponseBody
    public Result<String> getCompanyOpenidByCode(@RequestParam(value = "code") String code){
        return Result.success(wxService.getCompanyOpenidByCode(code));
    }

    @ApiOperation(value = "前端入参appid等信息,获取openId", httpMethod = "POST")
    @PostMapping("/getOpenidByCodeAndId")
    @ResponseBody
    public Result<String> getOpenidByCodeAndAppId(@RequestParam(value = "code") String code,
                                                  @RequestParam(value = "appId") String appId,
                                                  @RequestParam(value = "secret") String secret){
        return Result.success(wxService.getOpenidByCode(code, appId, secret));
    }

    @ApiOperation(value = "获取用户信息", httpMethod = "POST")
    @PostMapping("/getWxInfo")
    @ResponseBody
    public Result<String> getWxInfo(@RequestParam(value = "openId") String openId,
                                                  @RequestParam(value = "accessToken") String accessToken){
        return Result.success(wxService.getWxInfo(openId, accessToken));
    }

    @ApiOperation(value = "获取非本企业用户信息", httpMethod = "POST")
    @PostMapping("/getCompanyWxInfo")
    @ResponseBody
    public Result<String> getCompanyWxInfo(@RequestParam(value = "openId") String openId,
                                    @RequestParam(value = "accessToken") String accessToken){
        // todo 缺少企业获取用户信息的实现
        return Result.success(wxService.getWxInfo(openId, accessToken));
    }

    @ApiOperation(value = "获取企业用户信息", httpMethod = "POST")
    @PostMapping("/getCompanyByUserId")
    @ResponseBody
    public Result<String> getCompanyByUserId(@RequestParam(value = "userTicket") String userTicket){
        return Result.success(wxService.getCompanyByUserId(userTicket));
    }

    @ApiOperation(value = "获取jsapi_ticket", httpMethod = "GET")
    @GetMapping("/getWxTicket")
    @ResponseBody
    public Result<Map<String, String>> getWxTicket(@RequestParam(value = "url") String url){
        return Result.success(wxService.getWxTicket(url));
    }

}
