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.EnteralNutritionTemplateBo;
import org.dromara.web.domain.vo.EnteralNutritionTemplateVo;
import org.dromara.web.service.IEnteralNutritionTemplateService;
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 Huanyi
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/parameter/basicPublicTemplate/enteralNutritionTemplate")
public class EnteralNutritionTemplateController extends BaseController {

    private final IEnteralNutritionTemplateService enteralNutritionTemplateService;

    /**
     * 查询肠内处方模板列表
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<EnteralNutritionTemplateVo> list(EnteralNutritionTemplateBo bo, PageQuery pageQuery) {
        return enteralNutritionTemplateService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:list")
    @GetMapping("/listAll")
    public TableDataInfo<EnteralNutritionTemplateVo> listAll() {
        return enteralNutritionTemplateService.listAll();
    }

    /**
     * 导出肠内处方模板列表
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:export")
    @Log(title = "肠内处方模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EnteralNutritionTemplateBo bo, HttpServletResponse response) {
        List<EnteralNutritionTemplateVo> list = enteralNutritionTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "肠内处方模板", EnteralNutritionTemplateVo.class, response);
    }

    /**
     * 获取肠内处方模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:query")
    @GetMapping("/{id}")
    public R<EnteralNutritionTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(enteralNutritionTemplateService.queryById(id));
    }

    /**
     * 新增肠内处方模板
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:add")
    @Log(title = "肠内处方模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EnteralNutritionTemplateBo bo) {
        return toAjax(enteralNutritionTemplateService.insertByBo(bo));
    }

    /**
     * 修改肠内处方模板
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:edit")
    @Log(title = "肠内处方模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EnteralNutritionTemplateBo bo) {
        return toAjax(enteralNutritionTemplateService.updateByBo(bo));
    }

    /**
     * 删除肠内处方模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("basicPublicTemplate:enteralNutritionTemplate:remove")
    @Log(title = "肠内处方模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(enteralNutritionTemplateService.deleteWithValidByIds(List.of(ids), true));
    }
}
