package org.dromara.biz.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.biz.domain.vo.BizCsformVo;
import org.dromara.biz.domain.bo.BizCsformBo;
import org.dromara.biz.service.IBizCsformService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 测试单
 *
 * @author Blue
 * @date 2025-05-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/csform")
public class BizCsformController extends BaseController {

    private final IBizCsformService bizCsformService;

    /**
     * 查询测试单列表
     */
    @SaCheckPermission("biz:csform:list")
    @GetMapping("/list")
    public TableDataInfo<BizCsformVo> list(BizCsformBo bo, PageQuery pageQuery) {
        return bizCsformService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出测试单列表
     */
    @SaCheckPermission("biz:csform:export")
    @Log(title = "测试单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizCsformBo bo, HttpServletResponse response) {
        List<BizCsformVo> list = bizCsformService.queryList(bo);
        ExcelUtil.exportExcel(list, "测试单", BizCsformVo.class, response);
    }

    /**
     * 获取测试单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:csform:query")
    @GetMapping("/{id}")
    public R<BizCsformVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bizCsformService.queryById(id));
    }

    /**
     * 新增测试单
     */
    @SaCheckPermission("biz:csform:add")
    @Log(title = "测试单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizCsformBo bo) {
        return toAjax(bizCsformService.insertByBo(bo));
    }

    /**
     * 修改测试单
     */
    @SaCheckPermission("biz:csform:edit")
    @Log(title = "测试单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizCsformBo bo) {
        return toAjax(bizCsformService.updateByBo(bo));
    }

    /**
     * 删除测试单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("biz:csform:remove")
    @Log(title = "测试单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bizCsformService.deleteWithValidByIds(List.of(ids), true));
    }
}
