package com.taais.biz.controller;

import java.util.List;

import com.taais.biz.service.impl.AlgorithmModelServiceImpl;
import com.taais.system.domain.vo.SysOssVo;
import com.taais.system.service.ISysOssService;
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.AlgorithmModelVo;
import com.taais.biz.domain.bo.AlgorithmModelBo;
import com.taais.biz.service.IAlgorithmModelService;

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

/**
 * 算法模型配置Controller
 *
 * @author 0
 * 2024-05-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ag/model")
public class AlgorithmModelController extends BaseController {
    @Resource
    private AlgorithmModelServiceImpl algorithmModelService;

    /**
     * 查询算法模型配置列表
     */
    @SaCheckPermission("ag:model:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmModelVo>> list(AlgorithmModelBo algorithmModelBo) {
        return CommonResult.success(algorithmModelService.selectPage(algorithmModelBo));
    }

    /**
     * 导出算法模型配置列表
     */
    @SaCheckPermission("ag:model:export")
    @Log(title = "算法模型配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmModelBo algorithmModelBo) {
        List<AlgorithmModelVo> list = algorithmModelService.selectList(algorithmModelBo);
        ExcelUtil.exportExcel(list, "算法模型配置", AlgorithmModelVo.class, response);
    }

    /**
     * 获取算法模型配置详细信息
     */
    @SaCheckPermission("ag:model:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AlgorithmModelVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(algorithmModelService.selectById(id));
    }

    @GetMapping(value = "algoList")
    public CommonResult<List<AlgorithmModelVo>> getAlgorithmRelatedModelInfo(Long id) {
        return CommonResult.success(algorithmModelService.getModelByAlgorithmId(id));
    }

    @Resource
    private ISysOssService sysOssService;

    /**
     * 新增算法模型配置
     */
    @SaCheckPermission("ag:model:add")
    @Log(title = "算法模型配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AlgorithmModelBo algorithmModelBo) {
        String modelAddress = algorithmModelBo.getModelAddress();
        SysOssVo vo = sysOssService.getById(Long.parseLong(modelAddress));
        algorithmModelBo.setModelAddress("/profile" + vo.getUrl().split("/profile")[1]);
        boolean inserted = algorithmModelService.insert(algorithmModelBo);
        if (!inserted) {
            return CommonResult.fail("新增算法模型配置记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改算法模型配置
     */
    @SaCheckPermission("ag:model:edit")
    @Log(title = "算法模型配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AlgorithmModelBo algorithmModelBo) {
        Boolean updated = algorithmModelService.update(algorithmModelBo);
        if (!updated) {
            return CommonResult.fail("修改算法模型配置记录失败!");
        }
        return CommonResult.success();
    }

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