package com.taais.biz.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.biz.domain.bo.AlgorithmBizProcessBo;
import com.taais.biz.domain.dto.task.TestResultDto;
import com.taais.biz.domain.dto.task.TranResultDto;
import com.taais.biz.domain.vo.AlgorithmBizProcessVo;
import com.taais.biz.service.IAlgorithmBizProcessService;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.core.core.page.PageResult;
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 jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 算法业务处理Controller
 *
 * @author 0
 * 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/bizProcess")
public class AlgorithmBizProcessController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(AlgorithmBizProcessController.class);
    @Resource
    private IAlgorithmBizProcessService algorithmBizProcessService;

    /**
     * 查询算法业务处理列表
     */
    @SaCheckPermission("task:bizProcess:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmBizProcessVo>> list(AlgorithmBizProcessBo algorithmBizProcessBo) {
        return CommonResult.success(algorithmBizProcessService.selectPage(algorithmBizProcessBo));
    }

    /**
     * 导出算法业务处理列表
     */
    @SaCheckPermission("task:bizProcess:export")
    @Log(title = "算法业务处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmBizProcessBo algorithmBizProcessBo) {
        List<AlgorithmBizProcessVo> list = algorithmBizProcessService.selectList(algorithmBizProcessBo);
        ExcelUtil.exportExcel(list, "算法业务处理", AlgorithmBizProcessVo.class, response);
    }

    /**
     * 获取算法业务处理详细信息
     */
    @SaCheckPermission("task:bizProcess:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AlgorithmBizProcessVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(algorithmBizProcessService.selectById(id));
    }

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

    /**
     * 修改算法业务处理
     */
    @SaCheckPermission("task:bizProcess:edit")
    @Log(title = "算法业务处理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AlgorithmBizProcessBo algorithmBizProcessBo) {
        Boolean updated = algorithmBizProcessService.update(algorithmBizProcessBo);
        if (!updated) {
            return CommonResult.fail("修改算法业务处理记录失败!");
        }
        return CommonResult.success();
    }

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

    @GetMapping("/getProcessBySubtaskId/{subtaskId}")
    public CommonResult<List<AlgorithmBizProcessVo>> getProcessBySubtaskId(@PathVariable Long subtaskId) {
        return CommonResult.success(algorithmBizProcessService.getProcessBySubtaskId(subtaskId));
    }

    @GetMapping("/getTrainResult/{subtaskId}")
    public CommonResult<TranResultDto> getTranResult(@PathVariable Long subtaskId) {
        return CommonResult.success(algorithmBizProcessService.getTranResult(subtaskId));
    }
    @GetMapping("/getVerifyResult/{subtaskId}")
    public CommonResult<TranResultDto> getVerifyResult(@PathVariable Long subtaskId) {
        return CommonResult.success(algorithmBizProcessService.getTranResult(subtaskId));
    }
    @GetMapping("/getTestResult/{subtaskId}")
    public CommonResult<TestResultDto> getTestResult(@PathVariable Long subtaskId) {
        TestResultDto testResult = null;
        try {
            testResult = algorithmBizProcessService.getTestResult(subtaskId);
        } catch (IOException e) {
            // 日志
            log.error("getTestResult 异常",e);
            return CommonResult.fail("系统异常，请联系管理员");
        }
        return CommonResult.success(testResult);
    }
}
