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.DailyMealRecipeBo;
import org.dromara.web.domain.vo.DailyMealRecipeVo;
import org.dromara.web.service.IDailyMealRecipeService;
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-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/dailyMealRecipe")
public class DailyMealRecipeController extends BaseController {

    private final IDailyMealRecipeService dailyMealRecipeService;

    /**
     * 查询日常膳食食谱明细列表
     */
    @SaCheckPermission("patients:dailyMealRecipe:list")
    @GetMapping("/list")
    public TableDataInfo<DailyMealRecipeVo> list(DailyMealRecipeBo bo, PageQuery pageQuery) {
        return dailyMealRecipeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出日常膳食食谱明细列表
     */
    @SaCheckPermission("patients:dailyMealRecipe:export")
    @Log(title = "日常膳食食谱明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DailyMealRecipeBo bo, HttpServletResponse response) {
        List<DailyMealRecipeVo> list = dailyMealRecipeService.queryList(bo);
        ExcelUtil.exportExcel(list, "日常膳食食谱明细", DailyMealRecipeVo.class, response);
    }

    /**
     * 获取日常膳食食谱明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("patients:dailyMealRecipe:query")
    @GetMapping("/{id}")
    public R<DailyMealRecipeVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(dailyMealRecipeService.queryById(id));
    }

    /**
     * 新增日常膳食食谱明细
     */
    @SaCheckPermission("patients:dailyMealRecipe:add")
    @Log(title = "日常膳食食谱明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DailyMealRecipeBo bo) {
        return toAjax(dailyMealRecipeService.insertByBo(bo));
    }

    /**
     * 修改日常膳食食谱明细
     */
    @SaCheckPermission("patients:dailyMealRecipe:edit")
    @Log(title = "日常膳食食谱明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DailyMealRecipeBo bo) {
        return toAjax(dailyMealRecipeService.updateByBo(bo));
    }

    /**
     * 删除日常膳食食谱明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("patients:dailyMealRecipe:remove")
    @Log(title = "日常膳食食谱明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dailyMealRecipeService.deleteWithValidByIds(List.of(ids), true));
    }
}
