package com.taais.biz.controller;

import java.util.List;

import com.taais.biz.domain.dto.task.CreateTargetIdentificationTaskDto;
import com.taais.biz.service.ITargetIdentificationSubtaskService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
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.TargetIdentificationTaskVo;
import com.taais.biz.domain.bo.TargetIdentificationTaskBo;
import com.taais.biz.service.ITargetIdentificationTaskService;

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

/**
 * 目标识别任务Controller
 *
 * @author km
 * 2024-08-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/identification/identificationTask")
public class TargetIdentificationTaskController extends BaseController {
    @Resource
    private ITargetIdentificationTaskService targetIdentificationTaskService;

    /**
     * 查询目标识别任务列表
     */
    @SaCheckPermission("identification:identificationTask:list")
    @GetMapping("/list")
    public CommonResult<PageResult<TargetIdentificationTaskVo>> list(TargetIdentificationTaskBo targetIdentificationTaskBo) {
        return CommonResult.success(targetIdentificationTaskService.selectPage(targetIdentificationTaskBo));
    }

    /**
     * 导出目标识别任务列表
     */
    @SaCheckPermission("identification:identificationTask:export")
    @Log(title = "目标识别任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TargetIdentificationTaskBo targetIdentificationTaskBo) {
        List<TargetIdentificationTaskVo> list = targetIdentificationTaskService.selectList(targetIdentificationTaskBo);
        ExcelUtil.exportExcel(list, "目标识别任务", TargetIdentificationTaskVo.class, response);
    }

    /**
     * 获取目标识别任务详细信息
     */
    @SaCheckPermission("identification:identificationTask:query")
    @GetMapping(value = "/{id}")
    public CommonResult<TargetIdentificationTaskVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(targetIdentificationTaskService.selectById(id));
    }

    /**
     * 新增目标识别任务
     */
    @SaCheckPermission("identification:identificationTask:add")
    @Log(title = "目标识别任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody TargetIdentificationTaskBo targetIdentificationTaskBo) {
        boolean inserted = targetIdentificationTaskService.insert(targetIdentificationTaskBo);
        if (!inserted) {
            return CommonResult.fail("新增目标识别任务记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改目标识别任务
     */
    @SaCheckPermission("identification:identificationTask:edit")
    @Log(title = "目标识别任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody TargetIdentificationTaskBo targetIdentificationTaskBo) {
        Boolean updated = targetIdentificationTaskService.update(targetIdentificationTaskBo);
        if (!updated) {
            return CommonResult.fail("修改目标识别任务记录失败!");
        }
        return CommonResult.success();
    }

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

    @PostMapping("/create")
    public CommonResult<String> createTask(@RequestBody CreateTargetIdentificationTaskDto taskDto) {
        System.out.println("received task" + taskDto);
        String errorMsg = targetIdentificationTaskService.createTask(taskDto);

        if (StringUtils.isNotEmpty(errorMsg)) {
            return CommonResult.fail(errorMsg);
        } else {
            return CommonResult.success();
        }
    }

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