package com.hs.api.controller;

import com.hs.api.common.Constants;
import com.hs.api.common.Result;
import com.hs.api.common.annotations.CurrentUser;
import com.hs.api.common.annotations.Token;
import com.hs.api.model.SerDepartment;
import com.hs.api.model.SysRole;
import com.hs.api.model.SysUser;
import com.hs.api.model.reqmodel.Disease;
import com.hs.api.model.reqmodel.MenuReq;
import com.hs.api.model.respmodel.SysMenuList;
import com.hs.api.service.SysMenuService;
import com.hs.api.service.SysUserOrgRsService;
import com.hs.api.service.SysUserRoleRsService;
import com.hs.common.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "用户API", produces = "produces", consumes = "consumes", protocols = "protocols")
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private SysUserRoleRsService sysUserRoleRsService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserOrgRsService sysUserOrgRsService;

    /**
     * 根据token获取用户信息
     * @return
     */
    @GetMapping("/getByToken")
    @ApiIgnore
    public Object getByToken(HttpServletRequest request) {
        String token = request.getHeader(Constants.TOKEN_KEY);
        SysUser user = (SysUser) RedisUtil.get(token);
        return Result.success(user);
    }

    @GetMapping()
    @ApiIgnore
    public Object user(@CurrentUser @ApiIgnore SysUser user) {
        return Result.success(user);
    }


    /**
     * 根据用户id查询用户角色
     *
     * @param userId 用户id
     * @return
     */
    @PostMapping("roles")
    @Token
    @ApiOperation("查询用户角色")
    public Object getRoleListByUser(@ApiIgnore @CurrentUser Long userId) throws Exception {
        List<SysRole> sysRoleList = sysUserRoleRsService.getRoleListByUser(userId);
        return Result.success(sysRoleList);
    }

    /**
     * 根据用户id查询用户菜单
     *
     * @param userId 用户id
     * @return
     */
    @PostMapping("menus")
    @Token
    @ApiOperation("查询用户菜单")
    public Object getMenuByUser(@ApiIgnore @CurrentUser Long userId) throws Exception {
        List<SysMenuList> sysMenuList = sysMenuService.getMenu(userId);
        return Result.success(sysMenuList);
    }

    @PostMapping("menu/list")
    @Token
    @ApiOperation("查询综合菜单")
    public Object getMenuByType(@RequestBody MenuReq req) throws Exception {
        List<SysMenuList> sysMenuList = sysMenuService.getMenuByType(req.getMenuType());
        return Result.success(sysMenuList);
    }

    /**
     * 根据用户id查询用户机构
     *
     * @param userId 用户id
     * @return
     */
    @PostMapping("dept")
    @Token
    @ApiOperation("查询用户科室")
    public Object getOrgByUser(@ApiIgnore @CurrentUser Long userId, @RequestBody Disease.DiseaseIDorLevel disease) throws Exception {
        List<SerDepartment> sysMenuList = sysUserOrgRsService.getUserOrg(userId, disease.getDisease(), disease.getDate());
        return Result.success(sysMenuList);
    }
}
