package ink.wgink.service.department.controller.resources;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.department.DepartmentContainUserDTO;
import ink.wgink.pojo.dtos.department.DepartmentDTO;
import ink.wgink.pojo.dtos.department.DepartmentUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.service.department.service.IDepartmentUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DepartmentUserController
 * @Description: 组织部门用户
 * @Author: wanggeng
 * @Date: 2021/1/28 4:19 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_RESOURCE_PREFIX + "组织部门用户")
@RestController
@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/department/user")
public class DepartmentUserResourceController extends DefaultBaseController {

    @Autowired
    private IDepartmentUserService departmentUserService;

    @ApiOperation(value = "组织部门用户列表", notes = "组织部门用户列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<DepartmentUserDTO> list() {
        Map<String, Object> params = requestParams();
        return departmentUserService.list(params);
    }

    @ApiOperation(value = "组织部门用户列表", notes = "通过组织部门ID获取组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/{departmentId}")
    public List<DepartmentUserDTO> list(@PathVariable("departmentId") String departmentId) {
        Map<String, Object> params = requestParams();
        return departmentUserService.list(departmentId, params);
    }

    @ApiOperation(value = "组织部门用户列表", notes = "通过部门ID列表获取组织部门用户列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/department-ids")
    public List<DepartmentUserDTO> listByDepartmentIds(@RequestBody IdsVO idsVO) {
        return departmentUserService.list(idsVO.getIds());
    }

    @ApiOperation(value = "组织部门用户列表", notes = "通过用户ID列表获取组织部门用户列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/user-ids")
    public List<DepartmentUserDTO> listByUserIds(@RequestBody IdsVO idsVO) {
        return departmentUserService.listByUserIds(idsVO.getIds());
    }

    @ApiOperation(value = "组织部门用户ID列表", notes = "通过部门ID组织部门用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user-id/{departmentId}")
    public List<String> listUserId(@PathVariable("departmentId") String departmentId) {
        return departmentUserService.listUserId(departmentId);
    }

    @ApiOperation(value = "组织部门用户ID列表", notes = "通过组织部门ID列表获取组织部门用户ID列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user-id/department-ids")
    public List<String> listUserIdByDepartmentIds(@RequestBody IdsVO idsVO) {
        return departmentUserService.listUserId(idsVO.getIds());
    }

    @ApiOperation(value = "组织部门用户ID列表", notes = "通过组织部门ID与用户ID列表获取组织部门用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user-id/department-id/{departmentId}/user-ids")
    public List<String> listUserIdByDepartmentIdAndUserIds(@PathVariable("departmentId") String departmentId, @RequestBody IdsVO idsVO) {
        return departmentUserService.listUserId(departmentId, idsVO.getIds());
    }

    @ApiOperation(value = "组织部门用户ID列表", notes = "通过组织部门ID列表与用户ID列表获取用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user-id/department-ids/user-ids")
    public List<String> listUserIdByDepartmentIdsAndUserIds(@RequestBody IdsVO idsVO) {
        return departmentUserService.listUserId(idsVO.getIds(), idsVO.getIds2());
    }

    @ApiOperation(value = "组织部门用户分页列表", notes = "组织部门用户分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage/{departmentId}")
    public SuccessResultList<List<DepartmentUserDTO>> listPage(@PathVariable("departmentId") String departmentId, ListPage page) {
        return departmentUserService.listPage(departmentId, page);
    }


    @ApiOperation(value = "自己的部门列表", notes = "自己的部门列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-department/self")
    public List<DepartmentDTO> listDepartmentBySelf() {
        return departmentUserService.listDepartmentBySelf();
    }

    @ApiOperation(value = "组织部门用户ID列表", notes = "组织部门用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-department/user-id/{userId}")
    public List<DepartmentDTO> listDepartmentByUserId(@PathVariable("userId") String userId) {
        return departmentUserService.listDepartmentByUserId(userId);
    }

    @ApiOperation(value = "用户列表", notes = "通过部门ID获取用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "组织部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user/department-id/{departmentId}")
    public List<UserDTO> listUserByDepartmentId(@PathVariable("departmentId") String departmentId) {
        return departmentUserService.listUserByDepartmentId(departmentId);
    }

    @ApiOperation(value = "用户分页列表", notes = "通过部门ID获取用户分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "组织部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/department-id/{departmentId}")
    public SuccessResultList<List<UserDTO>> listPageUserByDepartmentId(@PathVariable("departmentId") String departmentId, ListPage page) {
        return departmentUserService.listPageUserByDepartmentId(departmentId, page);
    }

    @ApiOperation(value = "部门ID列表", notes = "通过用户ID获取部门ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-department-id/user-id/{userId}")
    public List<String> listDepartmentIdByUserId(@PathVariable("userId") String userId) {
        return departmentUserService.listDepartmentIdByUserId(userId);
    }

    @ApiOperation(value = "部门ID列表", notes = "通过用户ID列表获取部门ID列表")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-department-id/user-ids")
    public List<String> listDepartmentIdByUserIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return departmentUserService.listDepartmentIdByUserIds(idsVO.getIds());
    }

    @ApiOperation(value = "通过用户ID同部门用户ID列表", notes = "通过用户ID同部门用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-save-department-user-id/user-id/{userId}")
    public List<String> listSameDepartmentUserIdByUserId(@PathVariable("userId") String userId) {
        return departmentUserService.listSameDepartmentUserIdByUserId(userId);
    }

    @ApiOperation(value = "通过用户ID列表同部门用户ID列表", notes = "通过用户ID列表同部门用户ID列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-save-department-user-ids/user-ids")
    public List<String> listSameDepartmentUserIdByUserIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return departmentUserService.listSameDepartmentUserIdByUserIds(idsVO.getIds());
    }

    @ApiOperation(value = "组织部门用户列表", notes = "组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-user/exclude-department-id/{departmentId}")
    public List<UserDTO> listUserByExcludeDepartmentId(@PathVariable("departmentId") String departmentId) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("excludeDepartmentId", departmentId);
        return departmentUserService.listUser(requestParams);
    }

    @ApiOperation(value = "不在组织部门的用户列表", notes = "不在组织部门的用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/exclude-department-id/{departmentId}")
    public SuccessResultList<List<UserDTO>> listPageUserByExcludeDepartmentId(@PathVariable("departmentId") String departmentId, ListPage page) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("excludeDepartmentId", departmentId);
        page.setParams(requestParams);
        return departmentUserService.listPageUser(page);
    }

    @ApiOperation(value = "获取组织详情（包含本级用户列表与下级部门列表）", notes = "获取组织详情（包含本级用户列表与下级部门列表）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-department-contain-user-and-sub/department-id/{departmentId}")
    public DepartmentContainUserDTO getDepartmentContainUserAndSubByDepartmentId(@PathVariable("departmentId") String departmentId) {
        return departmentUserService.getDepartmentContainUserAndSubByDepartmentId(departmentId);
    }

}
