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

import ink.wgink.annotation.CheckRequestBodyAnnotation;
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.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.position.pojo.vos.PositionVO;
import ink.wgink.service.position.service.IPositionService;
import ink.wgink.pojo.vos.IdsVO;
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.Arrays;
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_SYSTEM_PREFIX + "职位管理接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/position")
public class PositionController extends DefaultBaseController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation(value = "职位新增", notes = "职位新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult savePosition(@RequestBody PositionVO positionVO) {
        positionService.save(positionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "职位删除", notes = "通过id列表批量删除职位接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "职位ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult removePosition(@PathVariable("ids") String ids) {
        positionService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "职位修改", notes = "职位修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "职位ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{positionId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("positionId") String positionId, @RequestBody PositionVO positionVO) {
        positionService.update(positionId, positionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "职位列表", notes = "职位列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionParentId", value = "职位上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listallbyparentid/{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/{positionId}")
    public PositionDTO get(@PathVariable("positionId") String positionId) {
        Map<String, Object> params = getParams();
        params.put("positionId", positionId);
        return positionService.get(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("listztree")
    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 = "通过ID列表获取职位列表", notes = "通过ID列表获取用户职位接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/position-ids")
    public List<PositionDTO> listByIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return positionService.listByPositionIds(idsVO.getIds());
    }

}
