package com.unis.base.server.controller;

import com.unis.base.common.vo.UserSealInfoVo;
import com.unis.base.server.service.UserSealRoleService;
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 lombok.extern.slf4j.Slf4j;
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;

import java.util.List;

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

    @Autowired
    private UserSealRoleService sealRoleService;

    @GetMapping(value="/list_seal_info/{account_name}")
    @ApiOperation(value = "获取；", notes = "参数：")
    public ResponseWrapper listSealInfoByAccountName(@PathVariable("account_name") String accountName) {
        try {
            List<UserSealInfoVo> data = sealRoleService.listSealInfoByAccountName(accountName);
            return ResponseWrapper.success(data);
        } 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)) {
                List<UserSealInfoVo> data = sealRoleService.listSealInfoByAccountName(userAccount);
                return ResponseWrapper.success(data);
            } 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.isNotBlank(userAccount)) {
                List<UserSealInfoVo> data = sealRoleService.listAllSealInfo();
                return ResponseWrapper.success(data);
            } else {
                return ResponseWrapper.deny();
            }
        } catch (ExpiredJwtException jwtException) {
            jwtException.printStackTrace();
            return ResponseWrapper.error("token 失效");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseWrapper.error();
    }

}
