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.NutritionEducationTemplateBo;
import org.dromara.web.domain.vo.NutritionEducationTemplateVo;
import org.dromara.web.service.INutritionEducationTemplateService;
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-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/basicPublicTemplate/educationTemplate")
public class NutritionEducationTemplateController extends BaseController {

    private final INutritionEducationTemplateService nutritionEducationTemplateService;

    /**
     * 查询营养宣教模板列表
     */
    @SaCheckPermission("basicPublicTemplate:educationTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<NutritionEducationTemplateVo> list(NutritionEducationTemplateBo bo, PageQuery pageQuery) {
        return nutritionEducationTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出营养宣教模板列表
     */
    @SaCheckPermission("basicPublicTemplate:educationTemplate:export")
    @Log(title = "营养宣教模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NutritionEducationTemplateBo bo, HttpServletResponse response) {
        List<NutritionEducationTemplateVo> list = nutritionEducationTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "营养宣教模板", NutritionEducationTemplateVo.class, response);
    }

    /**
     * 获取营养宣教模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("basicPublicTemplate:educationTemplate:query")
    @GetMapping("/{id}")
    public R<NutritionEducationTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(nutritionEducationTemplateService.queryById(id));
    }

    /**
     * 新增营养宣教模板
     */
    @SaCheckPermission("basicPublicTemplate:educationTemplate:add")
    @Log(title = "营养宣教模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NutritionEducationTemplateBo bo) {
        return toAjax(nutritionEducationTemplateService.insertByBo(bo));
    }

    /**
     * 修改营养宣教模板
     */
    @SaCheckPermission("basicPublicTemplate:educationTemplate:edit")
    @Log(title = "营养宣教模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NutritionEducationTemplateBo bo) {
        return toAjax(nutritionEducationTemplateService.updateByBo(bo));
    }

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