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

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.DepartmentUserDTO;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.pojo.vos.SaveAndDeleteIdsVO;
import ink.wgink.service.department.pojo.vos.DepartmentUserSortVO;
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.Arrays;
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_SYSTEM_PREFIX + "组织部门用户")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/department/user")
public class DepartmentUserController extends DefaultBaseController {

    @Autowired
    private IDepartmentUserService departmentUserService;

    @ApiOperation(value = "保存组织部门用户列表", notes = "保存组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save/{departmentId}")
    public synchronized SuccessResult save(@PathVariable("departmentId") String departmentId,
                                           @RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("用户ID列表不能为空");
        }
        departmentUserService.save(departmentId, idsVO.getIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "更新组织部门用户列表", notes = "更新组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{departmentId}")
    public synchronized SuccessResult update(@PathVariable("departmentId") String departmentId,
                                             @RequestBody SaveAndDeleteIdsVO saveAndDeleteIdsVO) {
        departmentUserService.update(departmentId, saveAndDeleteIdsVO.getSaveIds(), saveAndDeleteIdsVO.getDeleteIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "更新组织部门用户排序", notes = "更新组织部门用户排序接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update-sort/{departmentId}")
    public SuccessResult updateSort(@PathVariable("departmentId") String departmentId,
                                    @RequestBody DepartmentUserSortVO departmentUserSortVO) {
        departmentUserService.updateSort(departmentId, departmentUserSortVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "组织部门用户删除", notes = "组织部门用户删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
            @ApiImplicitParam(name = "ids", value = "人员ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{departmentId}/{ids}")
    public synchronized SuccessResult remove(@PathVariable("departmentId") String departmentId,
                                             @PathVariable("ids") String ids) {
        departmentUserService.delete(departmentId, null, Arrays.asList(ids.split("_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "组织部门用户列表", notes = "组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query")
    })
    @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 = "组织部门用户列表接口")
    @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) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return departmentUserService.listPage(departmentId, page);
    }

    @ApiOperation(value = "组织部门用户列表", notes = "组织部门用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query")
    })
    @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 = "组织部门用户ID列表", notes = "组织部门用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listuserid/{departmentId}")
    public List<String> listUserId(@PathVariable("departmentId") String departmentId) {
        return departmentUserService.listUserId(departmentId);
    }

    @ApiOperation(value = "组织部门用户列表", notes = "组织部门用户列表接口")
    @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) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("departmentId", 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("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/department-id/{departmentId}")
    public SuccessResultList<List<UserDTO>> listPageUserByDepartmentId(@PathVariable("departmentId") String departmentId, ListPage page) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("departmentId", 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("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);
    }

}
