package com.taais.biz.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.taais.biz.domain.vo.AlgorithmDataSetVo;
import com.taais.biz.domain.bo.AlgorithmDataSetBo;
import com.taais.biz.service.IAlgorithmDataSetService;

import com.taais.common.core.core.page.PageResult;

/**
 * 算法数据集合Controller
 *
 * @author km
 * 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/dataSet")
public class AlgorithmDataSetController extends BaseController {
    @Resource
    private IAlgorithmDataSetService algorithmDataSetService;

    /**
     * 查询算法数据集合列表
     */
    @SaCheckPermission("task:dataSet:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmDataSetVo>> list(AlgorithmDataSetBo algorithmDataSetBo) {
        return CommonResult.success(algorithmDataSetService.selectPage(algorithmDataSetBo));
    }

    /**
     * 导出算法数据集合列表
     */
    @SaCheckPermission("task:dataSet:export")
    @Log(title = "算法数据集合", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmDataSetBo algorithmDataSetBo) {
        List<AlgorithmDataSetVo> list = algorithmDataSetService.selectList(algorithmDataSetBo);
        ExcelUtil.exportExcel(list, "算法数据集合", AlgorithmDataSetVo.class, response);
    }

    /**
     * 获取算法数据集合详细信息
     */
    @SaCheckPermission("task:dataSet:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AlgorithmDataSetVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(algorithmDataSetService.selectById(id));
    }

    /**
     * 新增算法数据集合
     */
    @SaCheckPermission("task:dataSet:add")
    @Log(title = "算法数据集合", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AlgorithmDataSetBo algorithmDataSetBo) {
        boolean inserted = algorithmDataSetService.insert(algorithmDataSetBo);
        if (!inserted) {
            return CommonResult.fail("新增算法数据集合记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改算法数据集合
     */
    @SaCheckPermission("task:dataSet:edit")
    @Log(title = "算法数据集合", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AlgorithmDataSetBo algorithmDataSetBo) {
        Boolean updated = algorithmDataSetService.update(algorithmDataSetBo);
        if (!updated) {
            return CommonResult.fail("修改算法数据集合记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除算法数据集合
     */
    @SaCheckPermission("task:dataSet:remove")
    @Log(title = "算法数据集合", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = algorithmDataSetService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除算法数据集合记录失败!");
        }
        return CommonResult.success();
    }
}
