package com.yfd.platform.modules.algorithm.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.algorithm.domain.AlgorithmClass;
import com.yfd.platform.modules.algorithm.domain.AlgorithmParamsRequest;
import com.yfd.platform.modules.algorithm.domain.TreeNode;
import com.yfd.platform.modules.algorithm.service.IAlgorithmClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *   算法分类
 *  前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/algorithm/algorithm-class")
@Api(value = "AlgorithmClassController", tags = "算法分类")
public class AlgorithmClassController {

    @Resource
    private IAlgorithmClassService algorithmClassService;

    @GetMapping("/getAlgorithmClassList")
    @ApiOperation("查询算法分类")
    public ResponseResult getAlgorithmClassList(String algorithmClassName) {
        LambdaQueryWrapper<AlgorithmClass> queryWrapper = new LambdaQueryWrapper<AlgorithmClass>();
        queryWrapper.like(StrUtil.isNotBlank(algorithmClassName), AlgorithmClass::getAlgorithmClassName,
                algorithmClassName).orderByAsc(AlgorithmClass::getAlgorithmClassCode);
        List<AlgorithmClass> list = algorithmClassService.list(queryWrapper);
        return ResponseResult.successData(list);
    }

    @GetMapping("/getAlgorithmClassTree")
    @ApiOperation("获取算法分类树")
    public ResponseResult getAlgorithmClassTree(String stationCode,String algorithmClassName) {
        List<TreeNode> treeNodes = algorithmClassService.getAlgorithmComponentTree(stationCode,algorithmClassName);
        return ResponseResult.successData(treeNodes);
    }

    @PostMapping("/addAlgorithmClass")
    @ApiOperation("新增算法分类")
    public ResponseResult addAlgorithmClass(@RequestBody AlgorithmClass algorithmClass) {
        boolean isOK = algorithmClassService.addAlgorithmClass(algorithmClass);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    @PostMapping("/addAlgorithmClassAndParams")
    @ApiOperation("保存算法分类和算法参数")
    public ResponseResult addAlgorithmClassAndParams(@RequestBody AlgorithmParamsRequest algorithmParamsRequest) {
        boolean isOK = algorithmClassService.addAlgorithmClassAndParams(algorithmParamsRequest);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    @PostMapping("/updateAlgorithmClass")
    @ApiOperation("修改算法分类")
    public ResponseResult updateAlgorithmClass(@RequestBody AlgorithmClass algorithmClass) {
        boolean isOK = algorithmClassService.updateAlgorithmClass(algorithmClass);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

    @PostMapping("/deleteAlgorithmClass")
    @ApiOperation("删除算法分类")
    public ResponseResult deleteAlgorithmClass(String id) {
        if (StrUtil.isBlank(id)) {
            return ResponseResult.error("算法分类id为空");
        }
        boolean isOK = algorithmClassService.deleteAlgorithmClass(id);
        if (isOK) {
            return ResponseResult.success();
        } else {
            return ResponseResult.error();
        }
    }

}
