package org.dromara.web.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.dromara.web.domain.bo.EnteralNutritionConsumableBo;
import org.dromara.web.domain.vo.EnteralNutritionConsumableVo;
import org.dromara.web.service.IEnteralNutritionConsumableService;
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.common.mybatis.core.page.TableDataInfo;

/**
 * 肠内营养耗材
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/nutritionConsumable")
public class EnteralNutritionConsumableController extends BaseController {

    private final IEnteralNutritionConsumableService enteralNutritionConsumableService;

    /**
     * 查询肠内营养耗材列表
     */
    @SaCheckPermission("system:nutritionConsumable:list")
    @GetMapping("/list")
    public TableDataInfo<EnteralNutritionConsumableVo> list(EnteralNutritionConsumableBo bo, PageQuery pageQuery) {
        return enteralNutritionConsumableService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出肠内营养耗材列表
     */
    @SaCheckPermission("system:nutritionConsumable:export")
    @Log(title = "肠内营养耗材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EnteralNutritionConsumableBo bo, HttpServletResponse response) {
        List<EnteralNutritionConsumableVo> list = enteralNutritionConsumableService.queryList(bo);
        ExcelUtil.exportExcel(list, "肠内营养耗材", EnteralNutritionConsumableVo.class, response);
    }

    /**
     * 获取肠内营养耗材详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:nutritionConsumable:query")
    @GetMapping("/{id}")
    public R<EnteralNutritionConsumableVo> getInfo(@NotNull(message = "主键不能为空")
                                                   @PathVariable Long id) {
        return R.ok(enteralNutritionConsumableService.queryById(id));
    }

    /**
     * 新增肠内营养耗材
     */
    @SaCheckPermission("system:nutritionConsumable:add")
    @Log(title = "肠内营养耗材", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EnteralNutritionConsumableBo bo) {
        return toAjax(enteralNutritionConsumableService.insertByBo(bo));
    }

    /**
     * 修改肠内营养耗材
     */
    @SaCheckPermission("system:nutritionConsumable:edit")
    @Log(title = "肠内营养耗材", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EnteralNutritionConsumableBo bo) {
        return toAjax(enteralNutritionConsumableService.updateByBo(bo));
    }

    /**
     * 删除肠内营养耗材
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:nutritionConsumable:remove")
    @Log(title = "肠内营养耗材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(enteralNutritionConsumableService.deleteWithValidByIds(List.of(ids), true));
    }
}
