package com.hhh.cloud.framework.demo.other;

import com.alibaba.fastjson.JSONObject;
import com.hhh.cloud.framework.megrez.starter.auth.service.ITokenService;
import com.hhh.cloud.framework.megrez.utils.UserContextHolder;
import com.hhh.cloud.framework.megrez.utils.model.UserJwt;
import com.hhh.cloud.framework.util.enumeration.CodeEnum;
import com.hhh.cloud.framework.util.exception.BusinessException;
import com.hhh.cloud.framework.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OtherController {

    @Autowired
    private ITokenService iTokenService;

    /**
     * 获取用户信息
     * @return Result
     */
    @GetMapping("/getUser")
    public Result<UserJwt> getUser() {
        UserJwt userJwt = UserContextHolder.get().getUserJwt();
        return new Result<>(CodeEnum.SUCCESS.get(), userJwt, "当前登录用户信息");
    }

    /***
     * 读取应用凭证
     * @return String
     */
    @GetMapping("agentToken")
    public String agentToken() {
        return iTokenService.getAgentToken();
    }

    /**
     * 异常处理
     * @return Result
     */
    @PostMapping("/errorTs")
    public Result errorTs() {
        throw new BusinessException("测试错误");
    }

    /**
     * controller层成功返回样例
     * @param jsonObject
     * @return Result
     */
    @PostMapping("/dataOsSuccess")
    public Result<JSONObject> dataOsSuccess(@RequestBody JSONObject jsonObject) {
        jsonObject.put("re", "13527821562");
        return new Result<>(CodeEnum.SUCCESS.get(), jsonObject, "测试成功");
    }

    /**
     * controller层失败返回样例
     * @param jsonObject
     * @return Result
     */
    @PostMapping("/dataOsError")
    public Result<JSONObject> dataOsError(@RequestBody JSONObject jsonObject) {
        jsonObject.put("re", "13527821562");
        return new Result<>(CodeEnum.BUSINESS_ERROR.get(), jsonObject, "测试失败");
    }
}
