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.ProductStockBo;
import org.dromara.web.domain.vo.ProductStockVo;
import org.dromara.web.service.IProductStockService;
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/productStock")
public class ProductStockController extends BaseController {

    private final IProductStockService productStockService;

    /**
     * 查询产品库存列表
     */
    @SaCheckPermission("stock:productStock:list")
    @GetMapping("/list")
    public TableDataInfo<ProductStockVo> list(ProductStockBo bo, PageQuery pageQuery) {
        return productStockService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品库存列表
     */
    @SaCheckPermission("stock:productStock:export")
    @Log(title = "产品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductStockBo bo, HttpServletResponse response) {
        List<ProductStockVo> list = productStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品库存", ProductStockVo.class, response);
    }

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

    /**
     * 新增产品库存
     */
    @SaCheckPermission("stock:productStock:add")
    @Log(title = "产品库存", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductStockBo bo) {
        return toAjax(productStockService.insertByBo(bo));
    }

    /**
     * 修改产品库存
     */
    @SaCheckPermission("stock:productStock:edit")
    @Log(title = "产品库存", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductStockBo bo) {
        return toAjax(productStockService.updateByBo(bo));
    }

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