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.EnteralNutritionBo;
import org.dromara.web.domain.bo.HospitalMealPlanBo;
import org.dromara.web.domain.vo.*;
import org.dromara.web.service.IHospitalMealPlanService;
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-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/hospitalMealPlan")
public class HospitalMealPlanController extends BaseController {

    private final IHospitalMealPlanService hospitalMealPlanService;

    /**
     * 查询院内膳食主列表
     */
    @SaCheckPermission("patients:hospitalMealPlan:list")
    @GetMapping("/list")
    public TableDataInfo<HospitalMealPlanVo> list(HospitalMealPlanBo bo, PageQuery pageQuery) {
        return hospitalMealPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出院内膳食主列表
     */
    @SaCheckPermission("patients:hospitalMealPlan:export")
    @Log(title = "院内膳食主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HospitalMealPlanBo bo, HttpServletResponse response) {
        List<HospitalMealPlanVo> list = hospitalMealPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "院内膳食主", HospitalMealPlanVo.class, response);
    }

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

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

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

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

    /**
     * 查询膳食处方信息列表
     */
    @SaCheckPermission("patients:hospitalMealPlan:list")
    @GetMapping("/makeRecipeList")
    public TableDataInfo<HospitalMealPlanQueryVo> queryMakeRecipeListList(EnteralNutritionBo bo, PageQuery pageQuery) {
        return hospitalMealPlanService.queryHospitalMealPlanList(bo, pageQuery);
    }
    @SaCheckPermission("patients:hospitalMealPlan:query")
    @GetMapping("/getBySettlementId")
    public R<List<HospitalMealRecipeVo>> getBySettlementId(@RequestParam Long settlementId) {
        return R.ok(hospitalMealPlanService.queryBySettlementId(settlementId));
    }
    @SaCheckPermission("patients:hospitalMealPlan:query")
    @GetMapping("/queryById")
    public R<List<HospitalMealRecipeVo>> queryById(@RequestParam String ids) {
        return hospitalMealPlanService.queryById(ids);
    }

    /*手动配置*/
    @SaCheckPermission("patients:hospitalMealPlan:edit")
    @GetMapping("/manualConfig")
    public R manualConfig(@RequestParam String ids) {
        return hospitalMealPlanService.manualConfig(ids);
    }

    /*执行操作*/
    @SaCheckPermission("patients:hospitalMealPlan:edit")
    @GetMapping("/execute")
    public R execute(@RequestParam String ids) {
        return hospitalMealPlanService.execute(ids);
    }

    /*打印标签*/
    @SaCheckPermission("patients:hospitalMealPlan:edit")
    @GetMapping("/tagPrint")
    public R tagPrint(@RequestParam String ids) {
        return hospitalMealPlanService.tagPrint(ids);
    }
}
