package ink.wgink.service.group.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.group.GroupUserDTO;
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.group.service.IGroupUserService;
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: GroupUserController
 * @Description: 用户组用户
 * @Author: wanggeng
 * @Date: 2021/2/16 11:31 上午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "组用户")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/group/user")
public class GroupUserController extends DefaultBaseController {

    @Autowired
    private IGroupUserService groupUserService;

    @ApiOperation(value = "保存组用户列表", notes = "保存组用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save/{groupId}")
    public synchronized SuccessResult save(@PathVariable("groupId") String groupId,
                                           @RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("用户ID列表不能为空");
        }
        groupUserService.save(groupId, 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/{groupId}")
    public synchronized SuccessResult update(@PathVariable("groupId") String groupId,
                                             @RequestBody SaveAndDeleteIdsVO saveAndDeleteIdsVO) {
        groupUserService.update(groupId, saveAndDeleteIdsVO.getSaveIds(), saveAndDeleteIdsVO.getDeleteIds());
        return new SuccessResult();
    }

    @ApiOperation(value = "组用户删除", notes = "组用户删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", 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/{groupId}/{ids}")
    public synchronized SuccessResult remove(@PathVariable("groupId") String groupId,
                                             @PathVariable("ids") String ids) {
        groupUserService.delete(groupId, Arrays.asList(ids.split("_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "组用户列表", notes = "组用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组ID", paramType = "path", dataType = "String"),
            @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", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage/{groupId}")
    public SuccessResultList<List<GroupUserDTO>> listPage(@PathVariable("groupId") String groupId, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return groupUserService.listPage(groupId, page);
    }

    @ApiOperation(value = "组用户ID列表", notes = "组用户ID列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "用户组ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listuserid/{groupId}")
    public List<String> listUserId(@PathVariable("groupId") String groupId) {
        return groupUserService.listUserId(groupId);
    }

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

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

    @ApiOperation(value = "组用户列表", notes = "组用户列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-user/group-id/{groupId}")
    public SuccessResultList<List<UserDTO>> listPageUserByGroupId(@PathVariable("groupId") String groupId, ListPage page) {
        Map<String, Object> requestParams = requestParams();
        requestParams.put("groupId", groupId);
        page.setParams(requestParams);
        return groupUserService.listPageUser(page);
    }

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

}
