package cn.cslg.pas.controller;


import cn.cslg.pas.common.core.annotation.Permission;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.vo.SearchSourceDataVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.CustomAnalysisItem;
import cn.cslg.pas.service.CustomAnalysisItemService;

import cn.cslg.pas.service.CustomAnalysisItemSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 自定义分析 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-23
 */
@Tag(name = "自定义分析管理")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/custom/analysis/item")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CustomAnalysisItemController {
    private final CustomAnalysisItemService customAnalysisItemService;
    private final CustomAnalysisItemSourceService customAnalysisItemSourceService;

    @GetMapping("tree")
    @Operation(summary = "分析项列表")
    public String getItemTreeList(Integer projectId) {
        return Response.success(customAnalysisItemService.getTreeList(projectId));
    }

    @checkAuth(FunId = "/workspace/folder/analyticSystem/chartAnalysis/moreMenu/saveAs")
    @Permission(roles = {2})
    @PostMapping("add")
    @Operation(summary = "新增分析项")
    public String add(@RequestBody CustomAnalysisItem customAnalysisItem) {
        return customAnalysisItemService.add(customAnalysisItem);
    }

    @GetMapping("{uid}")
    @Operation(summary = "根据UID获取分析项")
    public String getByUid(@PathVariable String uid) {
        return Response.success(customAnalysisItemService.getItemByUid(uid));
    }

    @checkAuth(FunId = "/workspace/folder/analyticSystem/chartAnalysis/moreMenu/save")
    @Permission(roles = {2})
    @PostMapping("edit")
    @Operation(summary = "编辑分析项")
    public String edit(@RequestBody CustomAnalysisItem customAnalysisItem) {
        return customAnalysisItemService.edit(customAnalysisItem);
    }

    @checkAuth(FunId = "/workspace/folder/analyticSystem/chartAnalysis/groupModify")
    @Permission(roles = {2})
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除分析项")
    public String delete(@PathVariable Integer id) {
        return customAnalysisItemService.delete(id);
    }

    @PostMapping("source")
    @Operation(summary = "获取分析项目统计")
    public String getSourceData(@RequestBody SearchSourceDataVO searchSourceDataVO) {
        return customAnalysisItemSourceService.getSourceData2(searchSourceDataVO);
    }
}

