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.AlgorithmSubtaskDetailVo;
import com.taais.biz.domain.bo.AlgorithmSubtaskDetailBo;
import com.taais.biz.service.IAlgorithmSubtaskDetailService;

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

/**
 * 算法子任务详情Controller
 *
 * @author 0
 * 2024-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/subtaskDetail")
public class AlgorithmSubtaskDetailController extends BaseController {
    @Resource
    private IAlgorithmSubtaskDetailService algorithmSubtaskDetailService;

    /**
     * 查询算法子任务详情列表
     */
    @SaCheckPermission("task:subtaskDetail:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmSubtaskDetailVo>> list(AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        return CommonResult.success(algorithmSubtaskDetailService.selectPage(algorithmSubtaskDetailBo));
    }

    /**
     * 导出算法子任务详情列表
     */
    @SaCheckPermission("task:subtaskDetail:export")
    @Log(title = "算法子任务详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmSubtaskDetailBo algorithmSubtaskDetailBo) {
        List<AlgorithmSubtaskDetailVo> list = algorithmSubtaskDetailService.selectList(algorithmSubtaskDetailBo);
        ExcelUtil.exportExcel(list, "算法子任务详情", AlgorithmSubtaskDetailVo.class, response);
    }

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

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

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

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