package com.ruoyi.ccdi.project.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ccdi.project.domain.dto.ModelParamQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.ModelParamSaveDTO;
import com.ruoyi.ccdi.project.domain.dto.ModelParamAllQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.ModelParamSaveAllDTO;
import com.ruoyi.ccdi.project.domain.vo.ModelListVO;
import com.ruoyi.ccdi.project.domain.vo.ModelParamVO;
import com.ruoyi.ccdi.project.domain.vo.ModelParamAllVO;
import com.ruoyi.ccdi.project.service.CcdiProjectAccessService;
import com.ruoyi.ccdi.project.service.ICcdiModelParamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 模型参数配置Controller
 */
@Tag(name = "模型参数配置")
@RestController
@RequestMapping("/ccdi/modelParam")
public class CcdiModelParamController extends BaseController {

    @Resource
    private ICcdiModelParamService modelParamService;

    @Resource
    private CcdiProjectAccessService projectAccessService;

    /**
     * 查询模型列表
     */
    @Operation(summary = "查询模型列表")
    @GetMapping("/modelList")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult listModels(@RequestParam(required = false) Long projectId) {
        assertCanReadProjectParam(projectId);
        List<ModelListVO> list = modelParamService.selectModelList(projectId);
        return success(list);
    }

    /**
     * 查询模型参数列表
     */
    @Operation(summary = "查询模型参数列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult list(@Validated ModelParamQueryDTO queryDTO) {
        assertCanReadProjectParam(queryDTO.getProjectId());
        List<ModelParamVO> list = modelParamService.selectParamList(queryDTO);
        return success(list);
    }

    /**
     * 保存模型参数(只更新阈值)
     */
    @Operation(summary = "保存模型参数")
    @Log(title = "模型参数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult save(@Validated @RequestBody ModelParamSaveDTO saveDTO) {
        assertCanOperateProjectParam(saveDTO.getProjectId());
        modelParamService.saveParams(saveDTO);
        return success("保存成功");
    }

    /**
     * 查询所有模型及其参数(按模型分组)
     */
    @Operation(summary = "查询所有模型及其参数")
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('ccdi:project:query')")
    public AjaxResult listAll(@Validated ModelParamAllQueryDTO queryDTO) {
        assertCanReadProjectParam(queryDTO.getProjectId());
        ModelParamAllVO result = modelParamService.selectAllParams(queryDTO.getProjectId());
        return success(result);
    }

    /**
     * 批量保存所有模型的参数修改
     */
    @Operation(summary = "批量保存所有模型参数")
    @Log(title = "模型参数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/saveAll")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult saveAll(@Validated @RequestBody ModelParamSaveAllDTO saveAllDTO) {
        assertCanOperateProjectParam(saveAllDTO.getProjectId());
        modelParamService.saveAllParams(saveAllDTO);
        return success("保存成功");
    }

    private void assertCanReadProjectParam(Long projectId) {
        if (projectId == null || projectId <= 0) {
            return;
        }
        projectAccessService.assertCanRead(projectId);
    }

    private void assertCanOperateProjectParam(Long projectId) {
        if (projectId == null || projectId <= 0) {
            return;
        }
        projectAccessService.assertCanOperate(projectId);
    }
}
