package com.cm.common.plugin.oauth.controller.apis.position;

import com.alibaba.fastjson.JSONArray;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.oauth.service.position.IPositionService;
import com.cm.common.result.ErrorResult;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: PositionController
 * @Description: 职位
 * @Author: WangGeng
 * @Date: 2019/8/30 5:33 下午
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "职位管理")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/position")
public class PositionController extends AbstractController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation(value = "用户职位列表", notes = "用户职位列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listuserposition/{userId}")
    public JSONArray listUserPosition(@PathVariable("userId") String userId) throws AccessTokenException, SearchException {
        Map<String, Object> params = getParams();
        params.put("userId", userId);
        return positionService.listUserPosition(params);
    }

    @ApiOperation(value = "zTree列表", notes = "zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "上级ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztreeposition")
    public JSONArray listZTreePosition() throws SearchException {
        Map<String, Object> params = requestParams();
        String positionParentId = "0";
        if (!StringUtils.isBlank(params.get("id") == null ? null : params.get("id").toString())) {
            positionParentId = params.get("id").toString();
        }
        return positionService.listZTreePosition(positionParentId);
    }

}
