package com.qy.worksheetsystem.controller;

import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.model.MessageResultV2;
import com.qy.worksheetsystem.service.YunzhijiaService;
import com.qy.worksheetsystem.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import java.util.Map;

/**云之家
 * @BelongsProject: workSheetSystem
 * @BelongsPackage: com.qy.worksheetsystem.controller
 * @Author: lhbj
 * @CreateTime: 2024-12-03  11:55
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/work/yunzhijia")
@Api(value = "云之家",tags="云之家对接")
public class YunzhijiaController {

    @Autowired
    private YunzhijiaService yunzhijiaService;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @RequestMapping(value ="/getUserInfo",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "云之家APP会传递ticket参数给轻应用（把ticket参数追加到轻应用对应的url中）", required = true, paramType = "query", dataType = "String"),
    })
    public MessageResult getUserInfo(@RequestParam("ticket") String ticket) {
        Map<String,Object> map = yunzhijiaService.getUserInfo(ticket);
        MessageResult messageResult=null;
        if(map.isEmpty()) {
            messageResult = MessageResult.fail("认证失败！");
        }else{
            messageResult = MessageResult.success();
            messageResult.setData(map);
        }
        return messageResult;
    }

    @ApiOperation(value = "获取Token", notes = "获取Token")
    @RequestMapping(value ="/getToken",method = RequestMethod.GET)
    public MessageResult getToken(@RequestParam("personNum")String personNum, @RequestParam("userId")String userId){
        String token = JWTUtil.createToken(personNum,userId,200000);
        MessageResultV2 messageResult= MessageResultV2.success();
        messageResult.setData(token);
        return messageResult;
    }
}
