package com.unis.base.app.controller;

import com.unis.base.client.UserSealRoleFeign;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.ResponseWrapper;
import io.jsonwebtoken.ExpiredJwtException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author by PIG
 * 2022/6/23 10
 */
@RestController
@RequestMapping("/seal")
@Api( tags = "获取用户印章信息")
public class UserSealRoleController {

    @Autowired
    private UserSealRoleFeign sealRoleFeign;

    @GetMapping(value="/list_seal_info/{account_name}")
    @ApiOperation(value = "根据用户账号获取印章信息", notes = "参数：")
    public ResponseWrapper listSealInfoByAccountName(@PathVariable("account_name") String accountName) {
        try {
            return sealRoleFeign.listSealInfoByAccountName(accountName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseWrapper.error();
    }

    @GetMapping(value="/list_current_user_seal")
    @ApiOperation(value = "获取当前登陆人印章信息", notes = "参数：")
    public ResponseWrapper listSealInfoByCurrent() {
        try {
            String userAccount = CurrentUtil.getUserAccount();
            if (StringUtils.isNotEmpty(userAccount)) {
                return sealRoleFeign.listSealInfoByAccountName(userAccount);
            } else {
                return ResponseWrapper.deny();
            }
        } catch (ExpiredJwtException jwtException) {
            jwtException.printStackTrace();
            return ResponseWrapper.error("token 失效");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseWrapper.error();
    }

    @GetMapping(value="/list_all_seal")
    @ApiOperation(value = "获取所有印章信息", notes = "参数：")
    public ResponseWrapper listAllSealInfo() {
        try {
            String userAccount = CurrentUtil.getUserAccount();
            if (StringUtils.equalsIgnoreCase("cbl",userAccount)) {
                return sealRoleFeign.listAllSealInfo();
            } else {
                return ResponseWrapper.deny();
            }
        } catch (ExpiredJwtException jwtException) {
            jwtException.printStackTrace();
            return ResponseWrapper.error("token 失效");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseWrapper.error();
    }

}
