package com.taais.biz.controller;

import java.util.List;

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.AlgorithmConfigVo;
import com.taais.biz.domain.bo.AlgorithmConfigBo;
import com.taais.biz.service.IAlgorithmConfigService;

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

/**
 * 算法配置Controller
 *
 * @author 0
 * 2024-05-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ag/config")
public class AlgorithmConfigController extends BaseController {
    @Resource
    private IAlgorithmConfigService algorithmConfigService;

    /**
     * 查询算法配置列表
     */
    @SaCheckPermission("ag:config:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AlgorithmConfigVo>> list(AlgorithmConfigBo algorithmConfigBo) {
        return CommonResult.success(algorithmConfigService.selectPage(algorithmConfigBo));
    }

    /**
     * 导出算法配置列表
     */
    @SaCheckPermission("ag:config:export")
    @Log(title = "算法配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlgorithmConfigBo algorithmConfigBo) {
        List<AlgorithmConfigVo> list = algorithmConfigService.selectList(algorithmConfigBo);
        ExcelUtil.exportExcel(list, "算法配置", AlgorithmConfigVo.class, response);
    }

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

    /**
     * 新增算法配置
     */
    @SaCheckPermission("ag:config:add")
    @Log(title = "算法配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AlgorithmConfigBo algorithmConfigBo) {
        boolean inserted = algorithmConfigService.insert(algorithmConfigBo);
        if (!inserted) {
            return CommonResult.fail("新增算法配置记录失败！");
        }
        return CommonResult.success();
    }

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

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

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