package org.dromara.web.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.domain.bo.NutritionEducationBo;
import org.dromara.web.domain.vo.NutritionEducationVo;
import org.dromara.web.service.INutritionEducationService;
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-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/nutritionEducation")
public class NutritionEducationController extends BaseController {

    private final INutritionEducationService nutritionEducationService;

    /**
     * 查询营养宣教列表
     */
    @SaCheckPermission("patients:nutritionEducation:list")
    @GetMapping("/list")
    public TableDataInfo<NutritionEducationVo> list(NutritionEducationBo bo, PageQuery pageQuery) {
        return nutritionEducationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出营养宣教列表
     */
    @SaCheckPermission("patients:nutritionEducation:export")
    @Log(title = "营养宣教", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NutritionEducationBo bo, HttpServletResponse response) {
        List<NutritionEducationVo> list = nutritionEducationService.queryList(bo);
        ExcelUtil.exportExcel(list, "营养宣教", NutritionEducationVo.class, response);
    }

    /**
     * 查询模板列表
     */
    @GetMapping("/queryTemplateList")
    public Map<String, Object> queryTemplateList() {
        return nutritionEducationService.queryTemplateList();
    }

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

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

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

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