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

import com.skr.mdm.api.admin.service.AdminService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.qo.admin.AdminInfoQO;
import com.skr.mdm.bean.qo.admin.AdminQO;
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.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author nfq
 * @date 2020/9/9 15:36
 */
@RestController
@RequestMapping("admin")
@Api(tags = "管理员管理")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("info")
    @ApiOperation(value = "管理员信息")
//    @PreAuthorize("hasAnyAuthority('sys:config:admin:info')")
    public ResponseResult adminInfo(@ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.adminInfo(loginAdminDTO.getId());
    }

    @PostMapping("info/update")
    @ApiOperation(value = "管理员信息修改")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:info:update')")
    public ResponseResult updateAdminInfo(@RequestBody AdminInfoQO adminInfoQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.updateAdminInfo(adminInfoQO, loginAdminDTO);
    }

    @PostMapping("role")
    @ApiOperation(value = "角色列表")
    public ResponseResult allRoles(@ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.allRoles(loginAdminDTO.getCompanyId(), loginAdminDTO.getRoleId());
    }

    @PostMapping("add")
    @ApiOperation(value = "创建管理员")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:add')")
    public ResponseResult addAdmin(@RequestBody AdminQO adminQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.addAdmin(adminQO, loginAdminDTO);
    }

    @PostMapping("list")
    @ApiOperation(value = "管理员列表")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:list')")
    public ResponseResult listAdmin(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                    @RequestParam(required = false) Integer searchType, @RequestParam(required = false) String searchValue,
                                    @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.listAdmin(pageNum, pageSize, loginAdminDTO.getCompanyId(), searchType, searchValue, loginAdminDTO.getId());
    }

    @PostMapping("update")
    @ApiOperation(value = "编辑管理员")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:update')")
    public ResponseResult updateAdmin(@RequestBody AdminQO adminQO, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.updateAdmin(adminQO, loginAdminDTO);
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除管理员")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:delete')")
    public ResponseResult deleteAdmin(@RequestParam Integer id, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.deleteAdmin(id, loginAdminDTO);
    }

    @PostMapping("lock")
    @ApiOperation(value = "冻结/解冻")
    @PreAuthorize("hasAnyAuthority('sys:config:admin:lock')")
    public ResponseResult lockOrUnlock(@RequestParam Integer id, @RequestParam Integer usable, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return adminService.lockOrUnlock(id, usable, loginAdminDTO);
    }
}
