package com.taais.biz.controller;

import java.util.List;

import com.taais.biz.domain.bo.AlgorithmConfigBo;
import com.taais.biz.domain.bo.AlgorithmModelBo;
import com.taais.biz.domain.bo.DataBo;
import com.taais.biz.domain.dto.task.CreateTaskDto;
import com.taais.biz.domain.vo.AlgorithmConfigVo;
import com.taais.biz.domain.vo.AlgorithmModelVo;
import com.taais.biz.domain.vo.DataVo;
import com.taais.biz.service.IAlgorithmConfigService;
import com.taais.biz.service.IAlgorithmModelService;
import com.taais.biz.service.IDataService;
import com.taais.common.core.utils.StringUtils;
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.AlgorithmTaskVo;
import com.taais.biz.domain.bo.AlgorithmTaskBo;
import com.taais.biz.service.IAlgorithmTaskService;

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

/**
 * 算法任务Controller
 *
 * @author 0
 * 2024-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task/task")
public class AlgorithmTaskController extends BaseController {
    @Resource
    private IAlgorithmTaskService algorithmTaskService;
    @Resource
    private DataController dataController;
    @Resource
    private IAlgorithmConfigService algorithmConfigService;
    @Resource
    private IAlgorithmModelService modelService;

    /**
     * 查询算法任务列表
     */
    @SaCheckPermission("task:task:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmTaskVo>> list(AlgorithmTaskBo algorithmTaskBo) {
        return CommonResult.success(algorithmTaskService.selectPage(algorithmTaskBo));
    }

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

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

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

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

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

    /**
     * 新增算法任务
     */
    @SaCheckPermission("task:task:add")
    @Log(title = "算法任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/create")
    public CommonResult<Object> create(@RequestBody CreateTaskDto taskDto) {
        String string = algorithmTaskService.create(taskDto);
        if (StringUtils.isNotEmpty(string)) {
            try {
                return CommonResult.success(Long.parseLong(string));
            } catch (NumberFormatException e) {
                return CommonResult.fail("新增算法任务记录失败！失败圆晕："+string);
            }
        }
        return CommonResult.success();
    }

    @Log(title = "算法定时任务", businessType = BusinessType.INSERT)
    @GetMapping("/taskRun")
    public CommonResult<Void> taskRun() {
        algorithmTaskService.taskRun();
        return CommonResult.success();
    }

    @SaCheckPermission("task:task:add")
    @PostMapping("/getImage")
    public CommonResult<PageResult<DataVo>> getImage(@Validated @RequestBody DataBo data) {
        return dataController.list(data);
    }

    @SaCheckPermission("task:task:add")
    @PostMapping("/getAlgorithm/{type}/{subsystem}")
    public CommonResult<List<AlgorithmConfigVo>> getAlgorithm(@PathVariable String type, @PathVariable String subsystem) {
        AlgorithmConfigBo algorithmConfigBo = new AlgorithmConfigBo();
        algorithmConfigBo.setType(type);
        algorithmConfigBo.setSubsystem(subsystem);
        return CommonResult.success(algorithmConfigService.selectList(algorithmConfigBo));
    }

    @SaCheckPermission("task:task:add")
    @PostMapping("/getModel/{algorithmId}")
    public CommonResult<List<AlgorithmModelVo>> getModel(@PathVariable Long algorithmId) {
        AlgorithmModelBo algorithmModel = new AlgorithmModelBo();
        algorithmModel.setAlgorithmId(algorithmId);
        return CommonResult.success(modelService.selectList(algorithmModel));
    }

    @GetMapping("/getAlgorithmOption")
    public CommonResult<List> getAlgorithmOption() {
        return CommonResult.success(algorithmConfigService.getOption());
    }
}
