package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.web.domain.bo.DailyMealPlanBo;
import org.dromara.web.domain.vo.DailyMealPlanVo;
import org.dromara.web.service.IDailyMealPlanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 院内膳食主
 *
 * @author Lion Li
 * @date 2025-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/dailyMealPlan")
public class DailyMealPlanController extends BaseController {

    private final IDailyMealPlanService dailyMealPlanService;

    /**
     * 查询日常膳食主列表
     */
    @SaCheckPermission("patients:dailyMealPlan:list")
    @GetMapping("/list")
    public TableDataInfo<DailyMealPlanVo> list(DailyMealPlanBo bo, PageQuery pageQuery) {
        return dailyMealPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出日常膳食主列表
     */
    @SaCheckPermission("patients:dailyMealPlan:export")
    @Log(title = "日常膳食主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DailyMealPlanBo bo, HttpServletResponse response) {
        List<DailyMealPlanVo> list = dailyMealPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "日常膳食主", DailyMealPlanVo.class, response);
    }

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

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

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

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