package com.skr.mdm.api.user.controller;

import com.skr.mdm.api.user.service.UserService;
import com.skr.mdm.bean.qo.user.UserAddUpdateQO;
import com.skr.mdm.bean.qo.user.UserDeptQO;
import com.skr.mdm.bean.qo.user.UserQO;
import com.skr.mdm.bean.vo.user.UserDeptVO;
import com.skr.mdm.bean.vo.user.UserEquipmentVO;
import com.skr.mdm.bean.vo.user.UserVO;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author nfq
 * @date 2022-11-15 18:20
 */
@Validated
@RestController
@RequestMapping("user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "设备注册选择用户", response = UserDeptVO.class)
    @PostMapping("dept/select")
    public ResponseResult listDeptUser(@RequestBody @Valid UserDeptQO userDeptQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.listDeptUser(userDeptQO, loginAdminDTO);
    }

    @ApiOperation(value = "用户列表", response = UserVO.class)
    @PostMapping("list")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public ResponseResult listUser(@RequestBody @Valid UserQO userQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.listUser(userQO, loginAdminDTO);
    }

    @ApiOperation(value = "用户设备列表", response = UserEquipmentVO.class)
    @PostMapping("equipment/list")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public ResponseResult listUserEquipmentVO(@RequestParam @NotNull(message = "用户ID不能为空") Integer userId) {
        return userService.listUserEquipmentVO(userId);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('sys:user:add')")
    public ResponseResult addUser(@RequestBody @Valid UserAddUpdateQO userAddUpdateQO,
                                  @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.addUser(userAddUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "编辑用户")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('sys:user:update')")
    public ResponseResult updateUser(@RequestBody @Valid UserAddUpdateQO userAddUpdateQO,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.updateUser(userAddUpdateQO, loginAdminDTO);
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    public ResponseResult deleteUser(@RequestParam @NotEmpty(message = "用户ID不能为空") List<Integer> userIdList,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.deleteUser(userIdList, loginAdminDTO);
    }

    @ApiOperation("用户导入")
    @PostMapping("import")
    @PreAuthorize("hasAnyAuthority('sys:user:import')")
    public ResponseResult importDept(@RequestParam("file") MultipartFile file,
                                     @NotNull(message = "是否自动创建部门不能为空") @RequestParam Integer isAutoCreateDept,
                                     @ApiIgnore Authentication authentication) throws Exception {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return userService.importUser(file, isAutoCreateDept, loginAdminDTO);
    }

    @ApiOperation("用户导出")
    @PostMapping("export")
    @PreAuthorize("hasAnyAuthority('sys:user:export')")
    public void exportUser(@RequestParam(required = false) List<Integer> deptIdList,
                           @ApiIgnore Authentication authentication,
                           HttpServletResponse response) throws IOException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        userService.exportUser(deptIdList, loginAdminDTO, response);
    }

    @ApiOperation("用户二维码导出")
    @PostMapping("qrCode/export")
    @PreAuthorize("hasAnyAuthority('sys:user:qrcode:export')")
    public void exportUserQrCode(@RequestParam(required = false) List<Integer> deptIdList,
                                 @ApiIgnore Authentication authentication,
                                 HttpServletResponse response) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        userService.exportUserQrCode(deptIdList, loginAdminDTO, response);
    }

}
