package net.ferrum.web.controller.business;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import net.ferrum.common.annotation.RepeatSubmit;
import net.ferrum.common.annotation.Log;
import net.ferrum.common.core.controller.BaseController;
import net.ferrum.common.core.domain.PageQuery;
import net.ferrum.common.core.domain.R;
import net.ferrum.common.core.validate.AddGroup;
import net.ferrum.common.core.validate.EditGroup;
import net.ferrum.common.enums.BusinessType;
import net.ferrum.common.utils.poi.ExcelUtil;
import net.ferrum.business.domain.vo.QsBatchVo;
import net.ferrum.business.domain.bo.QsBatchBo;
import net.ferrum.business.service.IQsBatchService;
import net.ferrum.common.core.page.TableDataInfo;

/**
 * 批次管理
 *
 * @author tiezx
 * @date 2025-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/batch")
public class QsBatchController extends BaseController {

    private final IQsBatchService iQsBatchService;

    /**
     * 查询批次管理列表
     */
    @SaCheckPermission("business:batch:list")
    @GetMapping("/list")
    public TableDataInfo<QsBatchVo> list(QsBatchBo bo, PageQuery pageQuery) {
        return iQsBatchService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出批次管理列表
     */
    @SaCheckPermission("business:batch:export")
    @Log(title = "批次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QsBatchBo bo, HttpServletResponse response) {
        List<QsBatchVo> list = iQsBatchService.queryList(bo);
        ExcelUtil.exportExcel(list, "批次管理", QsBatchVo.class, response);
    }

    /**
     * 获取批次管理详细信息
     *
     * @param batchId 主键
     */
    @SaCheckPermission("business:batch:query")
    @GetMapping("/{batchId}")
    public R<QsBatchVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long batchId) {
        return R.ok(iQsBatchService.queryById(batchId));
    }

    /**
     * 新增批次管理
     */
    @SaCheckPermission("business:batch:add")
    @Log(title = "批次管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QsBatchBo bo) {
        return toAjax(iQsBatchService.insertByBo(bo));
    }

    /**
     * 修改批次管理
     */
    @SaCheckPermission("business:batch:edit")
    @Log(title = "批次管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QsBatchBo bo) {
        return toAjax(iQsBatchService.updateByBo(bo));
    }

    /**
     * 删除批次管理
     *
     * @param batchIds 主键串
     */
    @SaCheckPermission("business:batch:remove")
    @Log(title = "批次管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] batchIds) {
        return toAjax(iQsBatchService.deleteWithValidByIds(Arrays.asList(batchIds), true));
    }
}
