package com.lantone.security.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.api.CommonResult;
import com.lantone.common.domain.UserDto;
import com.lantone.security.dto.GetHospitalTreeDTO;
import com.lantone.security.dto.GetUserDTO;
import com.lantone.security.dto.GetUserPageDTO;
import com.lantone.security.vo.AddUserVO;
import com.lantone.security.vo.GetUserPageVO;
import com.lantone.security.vo.LoginVO;
import com.lantone.security.vo.ModifyPasswordVO;
import com.lantone.security.vo.UpdateUserVO;
import com.lantone.security.facade.UserManagementFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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 javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户管理API
 * @author: songxl
 * @time: 2021/7/20 12:39
 */
@RestController
@Api(value = "用户管理API", tags = {"用户管理API"})
@RequestMapping("/userManage")
public class UserManagementController {

    @Autowired
    private UserManagementFacade userManagementFacade;

    @ApiOperation(value = "登录以后返回token")
    @PostMapping(value = "/login")
    public CommonResult login(@Valid @RequestBody LoginVO loginVO) {
        return userManagementFacade.login(loginVO);
    }
    @ApiOperation(value = "登出")
    @PostMapping(value = "/close")
    public CommonResult<Boolean> close(@RequestParam @Valid @NotNull(message = "用户编号为空") String userId) {
        return userManagementFacade.close(userId);
    }

    @ApiOperation("根据用户名获取通用用户信息")
    @PostMapping(value = "/loadByUsername")
    public CommonResult<UserDto> loadUserByUsername(@RequestParam String username) {
        return CommonResult.success(userManagementFacade.loadUserByUsername(username));
    }

    @ApiOperation(value = "添加用户 [by:songxl]")
    @PostMapping("/addUser")
    @Transactional
    public CommonResult<Boolean> addUser(@RequestBody @Valid AddUserVO addUserVO) {
        return CommonResult.success(userManagementFacade.addUser(addUserVO));
    }

    @ApiOperation(value = "修改用户 [by:songxl]")
    @PostMapping("/updateUser")
    @Transactional
    public CommonResult<Boolean> updateUser(@RequestBody @Valid UpdateUserVO updateUserVO) {
        return CommonResult.success(userManagementFacade.updateUser(updateUserVO));
    }

    @ApiOperation(value = "删除用户 [by:songxl]")
    @PostMapping("/deleteUser")
    @Transactional
    public CommonResult<Boolean> deleteUser(@RequestParam @Valid @NotNull(message = "用户编号为空") Long userId) {
        return CommonResult.success(userManagementFacade.deleteUser(userId));
    }


    @ApiOperation(value = "禁用|启用用户 [by:songxl]")
    @PostMapping("/disableUser")
    @Transactional
    public CommonResult<Boolean> disableUser(@RequestParam @Valid @NotNull(message = "用户编号为空") Long userId, @RequestParam @Valid @NotBlank(message = "启用禁用status为空") String status) {
        return CommonResult.success(userManagementFacade.disableUser(userId, status));
    }

    @ApiOperation(value = "查看用户 [by:songxl]")
    @PostMapping("/getUserById")
    public CommonResult<GetUserDTO> getUserById(@RequestParam @Valid @NotNull(message = "用户编号为空") Long userId) {
        return CommonResult.success(userManagementFacade.getUserById(userId));
    }

    @ApiOperation(value = "获取登录用户管理的组织机构树 [by:songxl]")
    @PostMapping("/getHospitalTree")
    public CommonResult<List<GetHospitalTreeDTO>> getLoginUserHospitalTree() {
        return CommonResult.success(userManagementFacade.getLoginUserHospitalTree());
    }

    @ApiOperation(value = "获取当前用户所在组织的用户列表 [by:songxl]")
    @PostMapping("/getUserPage")
    public CommonResult<IPage<GetUserPageDTO>> getUserPage(@RequestBody @Valid GetUserPageVO getUserPageVO) {
        return CommonResult.success(userManagementFacade.getUserPage(getUserPageVO));
    }

    @ApiOperation(value = "登录成功获取用户的系统医院信息 [by:songxl]")
    @PostMapping("/getUserHospitals")
    public CommonResult<Map<String, Object>> getUserHospitals() {
        return CommonResult.success(userManagementFacade.getUserHospitals());
    }

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