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

import java.util.Arrays;

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.core.validate.EditGroup;
import com.qingyun.common.enums.BusinessType;
import com.qingyun.service.domain.vo.ContractRuleTemplateVo;
import com.qingyun.service.domain.bo.ContractRuleTemplateBo;
import com.qingyun.service.service.IContractRuleTemplateService;
import com.qingyun.mybatisplus.page.TableDataInfo;

import javax.validation.constraints.*;

/**
 * 规则要素模板
 *
 * @author jianlu
 * @date 2025-07-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/service/ruleTemplate")
public class ContractRuleTemplateController extends BaseController {

    private final IContractRuleTemplateService contractRuleTemplateService;

    /**
     * 查询规则要素模板列表 "service:ruleTemplate:list"
     */
    @SaCheckPermission("service:ruleTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<ContractRuleTemplateVo> list(ContractRuleTemplateBo bo, PageQuery pageQuery) {
        return contractRuleTemplateService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取规则要素模板详细信息 "service:ruleTemplate:query"
     *
     * @param id 主键
     */
    @SaCheckPermission("service:ruleTemplate:query")
    @GetMapping("/{id}")
    public R<ContractRuleTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(contractRuleTemplateService.queryById(id));
    }

    /**
     * 新增规则要素模板 "service:ruleTemplate:add"
     */
    @SaCheckPermission("service:ruleTemplate:add")
    @Log(title = "规则要素模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContractRuleTemplateBo bo) {
        return toAjax(contractRuleTemplateService.insertByBo(bo));
    }

    /**
     * 修改规则要素模板 "service:ruleTemplate:edit"
     */
    @SaCheckPermission("service:ruleTemplate:edit")
    @Log(title = "规则要素模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContractRuleTemplateBo bo) {
        return toAjax(contractRuleTemplateService.updateByBo(bo));
    }

    /**
     * 删除规则要素模板 "service:ruleTemplate:remove"
     *
     * @param ids 主键串
     */
    @SaCheckPermission("service:ruleTemplate:remove")
    @Log(title = "规则要素模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(contractRuleTemplateService.deleteWithValidByIds(Arrays.asList(ids), Boolean.TRUE));
    }
}
