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.system.config.RedisUtil;
import org.dromara.web.domain.bo.FoodIngredientStockBo;
import org.dromara.web.domain.vo.FoodIngredientStockVo;
import org.dromara.web.domain.vo.FoodStockBalanceVo;
import org.dromara.web.service.IFoodIngredientStockService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 食材出入库
 *
 * @author Lion Li
 * @date 2025-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stock/ingredientStock")
public class FoodIngredientStockController extends BaseController {

    private final IFoodIngredientStockService foodIngredientStockService;

    /**
     * 查询食材出入库列表
     */
    @SaCheckPermission("stock:foodIngredientInOut:list")
    @GetMapping("/list")
    public TableDataInfo<FoodIngredientStockVo> list(FoodIngredientStockBo bo, PageQuery pageQuery) {
        return foodIngredientStockService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出食材出入库列表
     */
    @SaCheckPermission("stock:foodIngredientInOut:export")
    @Log(title = "食材出入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FoodIngredientStockBo bo, HttpServletResponse response) {
        List<FoodIngredientStockVo> list = foodIngredientStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "食材出入库", FoodIngredientStockVo.class, response);
    }

    /**
     * 获取食材出入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("stock:foodIngredientInOut:query")
    @GetMapping("/{id}")
    public R<FoodIngredientStockVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(foodIngredientStockService.queryById(id));
    }

    /**
     * 新增食材出入库
     */
    @SaCheckPermission("stock:foodIngredientInOut:add")
    @Log(title = "食材出入库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody List<FoodIngredientStockBo> boList) {
        return toAjax(foodIngredientStockService.insertByBo(boList));
    }

    /**
     * 修改食材出入库
     */
    @SaCheckPermission("stock:foodIngredientInOut:edit")
    @Log(title = "食材出入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FoodIngredientStockBo bo) {
        return toAjax(foodIngredientStockService.updateByBo(bo));
    }

    /**
     * 删除食材出入库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("stock:foodIngredientInOut:remove")
    @Log(title = "食材出入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(foodIngredientStockService.deleteWithValidByIds(List.of(ids), true));
    }
    /**获取入库批次号
     */
    @GetMapping("/getBatchNumber")
    public R<String> getBatchNumber(@RequestParam String prefix) {
        return R.ok(RedisUtil.generatePatientNumber(prefix));
    }

    /**
     * 查询食材库存
     */
    @SaCheckPermission("stock:foodIngredientInOut:list")
    @GetMapping("/stockList")
    public TableDataInfo<FoodStockBalanceVo> stockList(FoodIngredientStockBo bo, PageQuery pageQuery) {
        return foodIngredientStockService.queryStockBalancePage(bo, pageQuery);
    }

    /**
     * 食材库存导出
     */
    @SaCheckPermission("stock:foodIngredientInOut:export")
    @Log(title = "食材库存导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportStock")
    public void exportStock(FoodIngredientStockBo bo, HttpServletResponse response) {
        TableDataInfo<FoodStockBalanceVo> tableDataInfo = foodIngredientStockService.queryStockBalance(bo);
        ExcelUtil.exportExcel(tableDataInfo.getRows(), "食材出入库", FoodStockBalanceVo.class, response);
    }

}
