package com.diagbot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.JwtDTO;
import com.diagbot.dto.LoginDTO;
import com.diagbot.dto.ModifyPasswordVO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.SysUserDeptDTO;
import com.diagbot.dto.SysUserQueryDTO;
import com.diagbot.dto.SysUserRoleDTO;
import com.diagbot.entity.Token;
import com.diagbot.enums.LockEnum;
import com.diagbot.facade.SysUserFacade;
import com.diagbot.facade.TokenFacade;
import com.diagbot.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.transaction.annotation.Transactional;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;


/**
 * @Description: 用户账号API
 * @author: gaodm
 * @time: 2018/8/2 13:56
 */
@RestController
@Api(value = "用户账号API", tags = { "用户账号API" })
@RequestMapping("/sys/user")
@SuppressWarnings("unchecked")
public class SysUserController {

    @Autowired
    private SysUserFacade userFacade;
    @Autowired
    private TokenFacade tokenFacade;

    @ApiOperation(value = "验证token有效性[by:gaodm]",
            notes = "token：token信息，必填<br>")
    @PostMapping("/verifyToken")
    @SysLogger("verifyToken")
    @ApiIgnore
    public RespDTO<Boolean> verifyToken(@RequestBody Token token) {
        Boolean data = tokenFacade.verifyToken(token.getToken(), 1);
        return RespDTO.onSuc(data);
    }


    @ApiOperation(value = "登录获取jwt[by:gaodm]",
            notes = "username：用户名,必填<br>" +
                    "password：密码, 必填, 默认密码：a123456<br> ")
    @PostMapping("/getJwt")
    @SysLogger("getJwt")
    public RespDTO<JwtDTO> getJwt(HttpServletRequest request,@RequestBody UserLoginVO userLoginVO) {
        JwtDTO data = userFacade.getJwt(request,userLoginVO);
        return RespDTO.onSuc(data);
    }
    @ApiOperation(value = "登出[by:gaodm]",
            notes = "")
    @PostMapping("/logout")
    @SysLogger("logout")
    public RespDTO<Boolean> logout() {
        Boolean data = userFacade.logout();
        return RespDTO.onSuc(data);
    }
    @ApiOperation(value = "解锁[by:gaodm]",
            notes = "")
    @PostMapping("/unlock")
    @SysLogger("unlock")
    public RespDTO<Boolean> unlock(@RequestParam Long id) {
        Boolean data = userFacade.lockUser(id, LockEnum.UNLOCK.getKey());
        return RespDTO.onSuc(data);
    }
    @ApiOperation(value = "获取验证码[by:cy]")
    @GetMapping("/getCaptcha")
    @SysLogger("getCaptcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
           userFacade.getCaptcha(request, response);
    }

    @ApiOperation(value = "获取标识--选择登录页面[by:cy]")
    @PostMapping("/getHospitalMark")
    @SysLogger("getHospitalMark")
    public RespDTO<Long> getHospitalMark() {
        Long mark = userFacade.getHospitalMark();
        return RespDTO.onSuc(mark);
    }
    @ApiOperation(value = "登录获取jwt[by:gaodm]",
            notes = "username：用户名,必填<br>")
    @PostMapping("/getJwtNoPass")
    @SysLogger("getJwtNoPass")
    public RespDTO<JwtDTO> getJwtNoPass(@RequestBody UserLoginNoPassVO userLoginNoPassVO) {
        JwtDTO data = userFacade.getJwtNoPass(userLoginNoPassVO.getUsername());
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "刷新jwt[by:gaodm]",
            notes = "refreshToken：刷新令牌,必填<br>")
    @PostMapping("/refreshJwt")
    @SysLogger("refreshJwt")
    public RespDTO<JwtDTO> refreshJwt(@RequestBody JwtVO jwtVO) {
        JwtDTO data = userFacade.refreshJwt(jwtVO.getToken());
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "验证Token[by:gaodm]",
            notes = "checkToken：令牌,必填<br>")
    @PostMapping("/checkToken")
    @SysLogger("checkToken")
    public RespDTO<OAuth2AccessToken> checkToken(@RequestBody JwtVO jwtVO) {
        OAuth2AccessToken data = userFacade.checkToken(jwtVO.getToken());
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "修改密码[by:gaodm]",
            notes = "password：原密码,必填<br>" +
                    "modifyPassword：新密码, 必填<br> ")
    @PostMapping("/midifyPassword")
    @SysLogger("midifyPassword")
    @Transactional
    public RespDTO<Boolean> midifyPassword(@RequestBody ModifyPasswordVO modifyPasswordVO) {
        Boolean flag = userFacade.modifyPassword(modifyPasswordVO.getPassword(), modifyPasswordVO.getModifyPassword());
        return RespDTO.onSuc(flag);
    }

    @ApiOperation(value = "获取用户、医院、菜单信息[by:gaodm]",
            notes = "")
    @PostMapping("/getUserOrgMenu")
    @SysLogger("getUserOrgMenu")
    public RespDTO<LoginDTO> getUserOrgMenu() {
        LoginDTO data = userFacade.getUserOrgMenu();
        return RespDTO.onSuc(data);
    }

    //-------------用户维护BEGAIN------------------------
    @ApiOperation(value = "用户列表翻页信息查询[by:gaodm]",
            notes = "")
    @PostMapping("/userPage")
    @SysLogger("userPage")
    public RespDTO<IPage<SysUserQueryDTO>> userPage(@RequestBody SysUserQueryVO sysUserQueryVO) {
        return RespDTO.onSuc(userFacade.userPageFac(sysUserQueryVO));
    }

    @ApiOperation(value = "获取用户科室[by:gaodm]",
            notes = "")
    @PostMapping("/getUserDepts")
    @SysLogger("getUserDepts")
    public RespDTO<SysUserDeptDTO> getUserDepts(@RequestBody @Valid SysUserBaseVO sysUserBaseVO) {
        return RespDTO.onSuc(userFacade.getUserDepts(sysUserBaseVO));
    }

    @ApiOperation(value = "获取用户角色[by:gaodm]",
            notes = "")
    @PostMapping("/getUserRoles")
    @SysLogger("getUserRoles")
    public RespDTO<SysUserRoleDTO> getUserRoles(@RequestBody @Valid SysUserBaseVO sysUserBaseVO) {
        return RespDTO.onSuc(userFacade.getUserRoles(sysUserBaseVO));
    }

    @ApiOperation(value = "用户启用[by:gaodm]",
            notes = "")
    @PostMapping("/enable")
    @SysLogger("enable")
    @Transactional
    public RespDTO<Boolean> enable(@RequestBody @Valid SysUserBaseVO sysUserBaseVO) {
        return RespDTO.onSuc(userFacade.enable(sysUserBaseVO));
    }

    @ApiOperation(value = "用户停用[by:gaodm]",
            notes = "")
    @PostMapping("/disable")
    @SysLogger("disable")
    @Transactional
    public RespDTO<Boolean> disable(@RequestBody @Valid SysUserBaseVO sysUserBaseVO) {
        return RespDTO.onSuc(userFacade.disable(sysUserBaseVO));
    }

    @ApiOperation(value = "编辑用户科室[by:gaodm]",
            notes = "")
    @PostMapping("/editUserDepts")
    @SysLogger("editUserDepts")
    @Transactional
    public RespDTO<Boolean> editUserDepts(@RequestBody @Valid SysUserDeptVO sysUserDeptVO) {
        return RespDTO.onSuc(userFacade.editUserDepts(sysUserDeptVO));
    }

    @ApiOperation(value = "编辑用户角色[by:gaodm]",
            notes = "")
    @PostMapping("/editUserRoles")
    @SysLogger("editUserRoles")
    @Transactional
    public RespDTO<Boolean> editUserRoles(@RequestBody @Valid SysUserRoleVO sysUserRoleVO) {
        return RespDTO.onSuc(userFacade.editUserRoles(sysUserRoleVO));
    }

    @ApiOperation(value = "新增用户",
            notes = "")
    @PostMapping("/createUser")
    @SysLogger("createUser")
    public RespDTO<Boolean> createUser(@RequestBody @Valid SysUserVO sysUserVO) {
        return RespDTO.onSuc(userFacade.createUser(sysUserVO));
    }

    //-------------用户维护END---------------------------

}
