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

import com.skr.mdm.api.system.service.DepartmentService;
import com.skr.mdm.bean.qo.system.DepartmentDeleteQO;
import com.skr.mdm.bean.qo.system.DepartmentMoveQO;
import com.skr.mdm.bean.qo.system.DepartmentQO;
import com.skr.mdm.bean.vo.DepartmentTreeVO;
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.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author nfq
 * @date 2022-11-15 14:40
 */
@Validated
@RestController
@RequestMapping("department")
@Api(tags = "部门")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "部门树", response = DepartmentTreeVO.class)
    @PostMapping("tree")
    public ResponseResult deptTree(@RequestParam(required = false) Integer adminId,
                                   @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        if (adminId == null) {
            adminId = loginAdminDTO.getId();
        }
        return departmentService.deptTree(adminId, Boolean.FALSE, loginAdminDTO);
    }

    @ApiOperation(value = "部门树-用户", response = DepartmentTreeVO.class)
    @PostMapping("tree/user")
    public ResponseResult deptTreeUser(@RequestParam(required = false) Integer adminId,
                                       @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        if (adminId == null) {
            adminId = loginAdminDTO.getId();
        }
        return departmentService.deptTree(adminId, Boolean.TRUE, loginAdminDTO);
    }

    @ApiOperation("添加")
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('sys:config:department:add')")
    public ResponseResult addDepartment(@RequestBody @Valid DepartmentQO departmentQO,
                                        @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return departmentService.addDept(departmentQO, loginAdminDTO);
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('sys:config:department:update')")
    public ResponseResult updateDept(@RequestBody @Valid DepartmentQO departmentQO,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return departmentService.updateDept(departmentQO, loginAdminDTO);
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('sys:config:department:delete')")
    public ResponseResult deleteDept(@RequestBody @Valid DepartmentDeleteQO departmentDeleteQO,
                                     @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return departmentService.deleteDept(departmentDeleteQO.getDeptIdList(), loginAdminDTO);
    }

    @ApiOperation("移动")
    @PostMapping("move")
    @PreAuthorize("hasAnyAuthority('sys:config:department:update')")
    public ResponseResult moveDept(@RequestBody List<DepartmentMoveQO> departmentMoveQOList,
                                   @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return departmentService.moveDept(departmentMoveQOList, loginAdminDTO);
    }

    @ApiOperation("导入")
    @PostMapping("import")
    @PreAuthorize("hasAnyAuthority('sys:config:department:import')")
    public ResponseResult importDept(@RequestParam("file") MultipartFile file,
                                     @ApiIgnore Authentication authentication) throws IOException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return departmentService.importDept(file, loginAdminDTO);
    }

    @ApiOperation("获取部门全路径")
    @PostMapping("full")
    public ResponseResult getFullDept(@RequestParam @NotNull(message = "部门ID不能为空") Integer deptId) {
        return ResponseResult.success(departmentService.getFullDept(deptId));
    }

}
