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.AlgorithmSubtaskVo;
import com.taais.biz.domain.bo.AlgorithmSubtaskBo;
import com.taais.biz.service.IAlgorithmSubtaskService;

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

/**
 * 算法子任务Controller
 *
 * @author Allen
 * 2024-06-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/subtask")
public class AlgorithmSubtaskController extends BaseController {
    @Resource
    private IAlgorithmSubtaskService algorithmSubtaskService;

    /**
     * 查询算法子任务列表
     */
    @SaCheckPermission("task:subtask:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmSubtaskVo>> list(AlgorithmSubtaskBo algorithmSubtaskBo) {
        return CommonResult.success(algorithmSubtaskService.selectPage(algorithmSubtaskBo));
    }

    /**
     * 导出算法子任务列表
     */
    @SaCheckPermission("task:subtask:export")
    @Log(title = "算法子任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmSubtaskBo algorithmSubtaskBo) {
        List<AlgorithmSubtaskVo> list = algorithmSubtaskService.selectList(algorithmSubtaskBo);
        ExcelUtil.exportExcel(list, "算法子任务", AlgorithmSubtaskVo.class, response);
    }

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

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

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

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

    @GetMapping("/getSubtaskByTaskId/{taskId}")
    public CommonResult<List<AlgorithmSubtaskVo>> getSubtaskByTaskId(@PathVariable Long taskId) {
        return CommonResult.success(algorithmSubtaskService.getSubtaskByTaskId(taskId));
    }
}
