package com.qingyun.web.controller.api.contract;

import com.qingyun.common.core.validate.EditGroup;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.qingyun.idempotent.annotation.RepeatSubmit;
import com.qingyun.common.annotation.Log;
import com.qingyun.common.core.controller.BaseController;
import com.qingyun.common.core.domain.PageQuery;
import com.qingyun.common.core.domain.R;
import com.qingyun.common.core.validate.AddGroup;
import com.qingyun.common.enums.BusinessType;
import com.qingyun.service.domain.vo.ContractModelConfigVo;
import com.qingyun.service.domain.bo.ContractModelConfigBo;
import com.qingyun.service.service.IContractModelConfigService;
import com.qingyun.mybatisplus.page.TableDataInfo;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 合同模型配置
 *
 * @author jianlu
 * @date 2025-07-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/service/modelConfig")
public class ContractModelConfigController extends BaseController {

    private final IContractModelConfigService contractModelConfigService;

    /**
     * 查询模型配置列表 "service:modelConfig:list"
     */
    @SaCheckPermission("service:modelConfig:list")
    @GetMapping("/list")
    public TableDataInfo<ContractModelConfigVo> list(ContractModelConfigBo bo, PageQuery pageQuery) {
        return contractModelConfigService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取模型配置详细信息 "service:modelConfig:query"
     *
     * @param id 主键
     */
    @SaCheckPermission("service:modelConfig:query")
    @GetMapping("/{id}")
    public R<ContractModelConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(contractModelConfigService.queryById(id));
    }

    /**
     * 新增模型配置 "service:modelConfig:add"
     */
    @SaCheckPermission("service:modelConfig:add")
    @Log(title = "模型配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContractModelConfigBo bo) {
        return toAjax(contractModelConfigService.insertByBo(bo));
    }

    /**
     * 删除模型配置 "service:modelConfig:remove"
     */
    @SaCheckPermission("service:modelConfig:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        if (Arrays.asList(ids).contains(1L)) {
            return R.fail("默认配置，不能删除");
        }
        return toAjax(contractModelConfigService.deleteWithValidByIds(ids, true));
    }

    /**
     * 修改模型配置 "service:modelConfig:edit"
      * @param bo 模型配置业务对象
      * @return 操作结果
     */
    @SaCheckPermission("service:modelConfig:edit")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContractModelConfigBo bo) {
        return toAjax(contractModelConfigService.updateByBo(bo));
    }
}
