package com.taais.biz.controller;

import java.util.List;

import com.taais.biz.domain.bo.AlgorithmTaskConfigurationParam;
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.AlgorithmTaskConfigurationVo;
import com.taais.biz.domain.bo.AlgorithmTaskConfigurationBo;
import com.taais.biz.service.IAlgorithmTaskConfigurationService;

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

/**
 * 算法任务Controller
 *
 * @author 0
 * 2024-08-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/taskConfiguration")
public class AlgorithmTaskConfigurationController extends BaseController {
    @Resource
    private IAlgorithmTaskConfigurationService algorithmTaskConfigurationService;

    /**
     * 查询算法任务列表
     */
    @SaCheckPermission("task:taskConfiguration:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmTaskConfigurationVo>> list(AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
        return CommonResult.success(algorithmTaskConfigurationService.selectPage(algorithmTaskConfigurationBo));
    }

    /**
     * 导出算法任务列表
     */
    @SaCheckPermission("task:taskConfiguration:export")
    @Log(title = "算法任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
        List<AlgorithmTaskConfigurationVo> list = algorithmTaskConfigurationService.selectList(algorithmTaskConfigurationBo);
        ExcelUtil.exportExcel(list, "算法任务", AlgorithmTaskConfigurationVo.class, response);
    }

    /**
     * 获取算法任务详细信息
     */
    @SaCheckPermission("task:taskConfiguration:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AlgorithmTaskConfigurationVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(algorithmTaskConfigurationService.selectById(id));
    }

    /**
     * 新增算法任务
     */
    @SaCheckPermission("task:taskConfiguration:add")
    @Log(title = "算法任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
        boolean inserted = algorithmTaskConfigurationService.insert(algorithmTaskConfigurationBo);
        if (!inserted) {
            return CommonResult.fail("新增算法任务记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改算法任务
     */
    @SaCheckPermission("task:taskConfiguration:edit")
    @Log(title = "算法任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AlgorithmTaskConfigurationBo algorithmTaskConfigurationBo) {
        Boolean updated = algorithmTaskConfigurationService.update(algorithmTaskConfigurationBo);
        if (!updated) {
            return CommonResult.fail("修改算法任务记录失败!");
        }
        return CommonResult.success();
    }

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

    @PostMapping("/importModel")
    public CommonResult<Boolean> insertExistedModel(@RequestBody AlgorithmTaskConfigurationParam param) {
        System.out.println("assert-model: " + param.toString());
        boolean res = algorithmTaskConfigurationService.insertExistedModel(param);
        return CommonResult.success(res);
    }
}
