/**
 * 20241029 ©WANGKANG 1686617586@qq.com
 */

package com.taais.biz.controller;

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

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.ResponseEntity;
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.TargetDetectionVo;
import com.taais.biz.domain.bo.TargetDetectionBo;
import com.taais.biz.service.ITargetDetectionService;

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

/**
 * 目标检测Controller
 *
 * @author wangkang
 * 2024-09-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/TargetDetection")
public class TargetDetectionController extends BaseController {
    @Resource
    private ITargetDetectionService targetDetectionService;

    /**
     * 查询目标检测列表
     */
    @SaCheckPermission("demo:TargetDetection:list")
    @GetMapping("/list")
    public CommonResult<PageResult<TargetDetectionVo>> list(TargetDetectionBo targetDetectionBo) {
        return CommonResult.success(targetDetectionService.selectPage(targetDetectionBo));
    }

    /**
     * 导出目标检测列表
     */
    @SaCheckPermission("demo:TargetDetection:export")
    @Log(title = "目标检测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TargetDetectionBo targetDetectionBo) {
        List<TargetDetectionVo> list = targetDetectionService.selectList(targetDetectionBo);
        ExcelUtil.exportExcel(list, "目标检测", TargetDetectionVo.class, response);
    }

    /**
     * 获取目标检测详细信息
     */
    @SaCheckPermission("demo:TargetDetection:query")
    @GetMapping(value = "/{id}")
    public CommonResult<TargetDetectionVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(targetDetectionService.selectById(id));
    }

    /**
     * 新增目标检测
     */
    @SaCheckPermission("demo:TargetDetection:add")
    @Log(title = "目标检测", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody TargetDetectionBo targetDetectionBo) {
        boolean inserted = targetDetectionService.insert(targetDetectionBo);
        if (!inserted) {
            return CommonResult.fail("新增目标检测记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改目标检测
     */
    @SaCheckPermission("demo:TargetDetection:edit")
    @Log(title = "目标检测", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody TargetDetectionBo targetDetectionBo) {
        Boolean updated = targetDetectionService.update(targetDetectionBo);
        if (!updated) {
            return CommonResult.fail("修改目标检测记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除目标检测
     */
    @SaCheckPermission("demo:TargetDetection:remove")
    @Log(title = "目标检测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = targetDetectionService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除目标检测记录失败!");
        }
        return CommonResult.success();
    }

    @GetMapping("/start/{id}")
    public CommonResult start(@PathVariable("id") Long id) {
        return targetDetectionService.start(id);
    }

    @GetMapping("/stop/{id}")
    public CommonResult stop(@PathVariable("id") Long id) {
        return targetDetectionService.stop(id);
    }

        @GetMapping("/zip/{id}")
    public ResponseEntity<org.springframework.core.io.Resource> zipFolder(@PathVariable("id") Long id) throws IOException {
        /**
         * 传VideoImage的ID
         */
        return targetDetectionService.zipImages(id);
    }
    @GetMapping("/log/{id}")
    public CommonResult getLog(@PathVariable("id") Long id) {
        return targetDetectionService.getLog(id);
    }

    @GetMapping("/model/{id}")
    public CommonResult getModelList(@PathVariable("id") Long id) {
        return targetDetectionService.getModelList(id);
    }
}
