package ink.wgink.service.position.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.ZTreeDTO;
import ink.wgink.pojo.dtos.position.PositionDTO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.service.position.service.IPositionService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: PositionController
 * @Description: 职位
 * @Author: admin
 * @Date: 2019-04-11 22:41:20
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_RESOURCE_PREFIX + "职位管理接口")
@RestController
@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/position")
public class PositionResourceController extends DefaultBaseController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation(value = "职位列表", notes = "职位列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionParentId", value = "职位上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all")
    public List<PositionDTO> listAll() {
        return positionService.listAllByParentId("0");
    }

    @ApiOperation(value = "职位列表", notes = "职位列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionParentId", value = "职位上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all/parentid/{positionParentId}")
    public List<PositionDTO> listAllByParentId(@PathVariable("positionParentId") String positionParentId) {
        return positionService.listAllByParentId(positionParentId);
    }

    @ApiOperation(value = "职位详情", notes = "职位详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "职位ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get")
    public PositionDTO get() {
        Map<String, Object> params = requestParams();
        return positionService.get(params);
    }

    @ApiOperation(value = "职位详情", notes = "职位详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "职位ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/id/{positionId}")
    public PositionDTO getById(@PathVariable("positionId") String positionId) {
        return positionService.get(positionId);
    }

    @ApiOperation(value = "zTree列表", notes = "zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-ztree")
    public List<ZTreeDTO> listZTree() {
        Map<String, Object> params = requestParams();
        String positionParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            positionParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("positionParentId", positionParentId);
        return positionService.listZTree(params);
    }

    @ApiOperation(value = "分页职位列表", notes = "分页职位列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", 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"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<PositionDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        String positionParentId = "0";
        if (!StringUtils.isBlank(params.get("parentId") == null ? null : params.get("parentId").toString())) {
            positionParentId = params.get("parentId").toString();
        }
        params.put("positionParentId", positionParentId);
        page.setParams(params);
        return positionService.listPage(page);
    }

    @ApiOperation(value = "取职位列表", notes = "获取用户职位接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list")
    public List<PositionDTO> list() {
        Map<String, Object> params = requestParams();
        return positionService.list(params);
    }

    @ApiOperation(value = "通过ID列表获取职位列表", notes = "通过ID列表获取用户职位接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/position-ids")
    public List<PositionDTO> listByIds(@RequestBody IdsVO idsVO) {
        return positionService.listByPositionIds(idsVO.getIds());
    }

    @ApiOperation(value = "取职位列表", notes = "用户职位接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-po")
    public List<PositionPO> listPO() {
        Map<String, Object> params = requestParams();
        return positionService.listPO(params);
    }

    @ApiOperation(value = "通过ID列表获取职位列表", notes = "通过ID列表获取用户职位接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-po/ids")
    public List<PositionPO> listPOByIds(@RequestBody IdsVO idsVO) {
        return positionService.listPO(idsVO.getIds());
    }

    @ApiOperation(value = "统计", notes = "统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    public SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        Integer count = positionService.count(params);
        return new SuccessResultData<>(count);
    }


    @ApiOperation(value = "通过ID列表获取职位列表", notes = "通过ID列表获取用户职位接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "职位ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-parent/position-id/{positionId}")
    public PositionDTO getParentByPositionId(@PathVariable("positionId") String positionId) {
        return positionService.getParentByPositionId(positionId);
    }

    @ApiOperation(value = "通过职位ID列表获取上级职位列表", notes = "通过职位ID列表获取职位列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-parent/position-ids")
    public List<PositionDTO> listParentByPositionIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("ids不能为空");
        }
        return positionService.listParentByPositionIds(idsVO.getIds());
    }

    @ApiOperation(value = "通过上级职位ID列表获取职位列表", notes = "通过上级职位ID列表获取职位列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/position-parent-ids")
    public List<PositionDTO> listByPositionParentIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("ids不能为空");
        }
        return positionService.listByPositionParentIds(idsVO.getIds());
    }

}
