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.*;

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 ="/getUserInfoById",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "人员id", required = true, paramType = "header", dataType = "String"),
    })
    public MessageResult getUserInfoById(@RequestHeader("id") String id) {
        Map<String,Object> map = yunzhijiaService.getUserInfoById(id);
        MessageResult messageResult=null;
        if(map.isEmpty()) {
            messageResult = MessageResult.fail("认证失败！");
        }else{
            messageResult = MessageResult.success();
            messageResult.setData(map);
        }
        return messageResult;
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @RequestMapping(value ="/getUserInfoByNumber",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "personNumber", value = "人员编码", required = true, paramType = "header", dataType = "String"),
    })
    public MessageResult getUserInfoByNumber(@RequestHeader("personNumber") String personNumber) {
        Map<String,Object> map = yunzhijiaService.getUserInfo(personNumber);
        MessageResult messageResult=null;
        if(map.isEmpty()) {
            messageResult = MessageResult.fail("认证失败！");
        }else{
            messageResult = MessageResult.success();
            messageResult.setData(map);
        }
        return messageResult;
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @RequestMapping(value ="/getUserInfoByTicket",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "云之家APP会传递ticket参数给轻应用（把ticket参数追加到轻应用对应的url中）", required = true, paramType = "query", dataType = "String"),
    })
    public MessageResult getUserInfoByTicket(@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(@RequestHeader("personNum")String personNum, @RequestHeader("userId")String userId){
        String token = JWTUtil.createToken(personNum,userId,200000);
        MessageResultV2 messageResult= MessageResultV2.success();
        messageResult.setData(token);
        return messageResult;
    }
}
