package com.cmvr.web.controller.evaluation;

import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;

import com.cmvr.evaluation.model.dto.AeIndicatorCreateDTO;
import com.cmvr.evaluation.model.dto.AeIndicatorUpdateDTO;
import com.cmvr.evaluation.service.AeIndicatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "评价管理--指标库")
@RestController
@RequestMapping("/evaluate/indicator")
@RequiredArgsConstructor
public class AeIndicatorController extends BaseController {

    private final AeIndicatorService indicatorService;

    @ApiOperation("根据父id查询指标")
    @GetMapping("/getIndicatorByParentId")
    public AjaxResult getIndicatorByParentId(@RequestParam Long parentId, @RequestParam Integer type) {
        if (parentId == null || type == null) {
            return AjaxResult.error("参数不能为空");
        }
        return success(indicatorService.selectIndicatorByParentId(parentId, type));
    }

    @ApiOperation("新增指标")
    @PostMapping("/indicator/insert")
    public AjaxResult insertIndicator(@Valid @RequestBody AeIndicatorCreateDTO createDTO) {
        return success(indicatorService.saveIndicator(createDTO));
    }

    @ApiOperation("根据ID级联删除：删除当前指标及其所有后代指标（子/孙级）")
    @DeleteMapping("/{id}")
    public AjaxResult removeIndicator(@PathVariable Long id) {
        if (id == null) {
            return AjaxResult.error("id不能为空");
        }
        return toAjax(indicatorService.removeIndicator(id));
    }

    @ApiOperation("修改指标")
    @PutMapping
    public AjaxResult updateIndicator(@Valid @RequestBody AeIndicatorUpdateDTO updateDTO) {
        return toAjax(indicatorService.updateIndicator(updateDTO));
    }

    @ApiOperation("获取指定parentId下指定level层级的指标")
    @GetMapping("/getChildrenByParentIdWithLevelLimit")
    public AjaxResult getChildrenByParentIdWithLevelLimit(@RequestParam Long parentId, @RequestParam Integer maxLevel, @RequestParam Integer type) {
        return success(indicatorService.getChildrenByParentIdWithLevelLimit(parentId, maxLevel, type));
    }
}
