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

    @Autowired
    private IPositionUserService positionUserService;

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

    @ApiOperation(value = "职位人员删除", notes = "职位人员删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", 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/{positionId}/{ids}")
    public synchronized SuccessResult remove(@PathVariable("positionId") String positionId,
                                             @PathVariable("ids") String ids) {
        positionUserService.delete(positionId, Arrays.asList(ids.split("_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "组人员列表", notes = "组人员列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", 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/{positionId}")
    public SuccessResultList<List<PositionUserDTO>> listPage(@PathVariable("positionId") String positionId, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return positionUserService.listPage(positionId, page);
    }

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

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

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

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

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

}
