package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.ProductInOutRecordBo;
import org.dromara.web.domain.vo.ProductInOutRecordVo;
import org.dromara.web.service.IProductInOutRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品出入库记录
 *
 * @author Lion Li
 * @date 2025-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock/inOutRecord")
public class ProductInOutRecordController extends BaseController {

    private final IProductInOutRecordService productInOutRecordService;

    /**
     * 查询食材库存
     */
    @SaCheckPermission("stock:inOutRecord:list")
    @GetMapping("/inOutStockList")
    public TableDataInfo<ProductInOutRecordVo> stockList(ProductInOutRecordBo bo, PageQuery pageQuery) {
        return productInOutRecordService.queryStockBalancePage(bo, pageQuery);
    }

    /**
     * 查询产品出入库记录列表
     */
    @SaCheckPermission("stock:inOutRecord:list")
    @GetMapping("/list")
    public TableDataInfo<ProductInOutRecordVo> list(ProductInOutRecordBo bo, PageQuery pageQuery) {
        return productInOutRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品出入库记录列表
     */
    @SaCheckPermission("stock:inOutRecord:export")
    @Log(title = "产品出入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductInOutRecordBo bo, HttpServletResponse response) {
        List<ProductInOutRecordVo> list = productInOutRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品出入库记录", ProductInOutRecordVo.class, response);
    }

    /**
     * 获取产品出入库记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("stock:inOutRecord:query")
    @GetMapping("/{id}")
    public R<ProductInOutRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(productInOutRecordService.queryById(id));
    }

    /**
     * 新增产品出入库记录
     */
    @SaCheckPermission("stock:inOutRecord:add")
    @Log(title = "产品出入库记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<ProductInOutRecordBo> boList) {
        return toAjax(productInOutRecordService.insertByBo(boList));
    }

    /**
     * 修改产品出入库记录
     */
    @SaCheckPermission("stock:inOutRecord:edit")
    @Log(title = "产品出入库记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductInOutRecordBo bo) {
        return toAjax(productInOutRecordService.updateByBo(bo));
    }

    /**
     * 删除产品出入库记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("stock:inOutRecord:remove")
    @Log(title = "产品出入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(productInOutRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
