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.EnteralNutritionBo;
import org.dromara.web.domain.bo.EnteralNutritionSaveBo;
import org.dromara.web.domain.bo.SettlementBo;
import org.dromara.web.domain.vo.EnteralNutritionQueryVo;
import org.dromara.web.domain.vo.EnteralNutritionVo;
import org.dromara.web.service.IEnteralNutritionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 肠内营养
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/nutrition")
public class EnteralNutritionController extends BaseController {

    private final IEnteralNutritionService enteralNutritionService;

    /**
     * 查询肠内营养列表
     */
    @SaCheckPermission("system:nutrition:list")
    @GetMapping("/list")
    public TableDataInfo<EnteralNutritionQueryVo> list(SettlementBo bo, PageQuery pageQuery) {
        return enteralNutritionService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询营养评估列表
     */
    @SaCheckPermission("system:evaluation:list")
    @GetMapping("/listHistory")
    public TableDataInfo<EnteralNutritionQueryVo> listHistory(SettlementBo bo, PageQuery pageQuery) {
        return enteralNutritionService.listHistory(bo, pageQuery);
    }

    /**
     * 查询营养评估详情
     */
    @SaCheckPermission("system:evaluation:list")
    @GetMapping("/detail")
    public R<EnteralNutritionQueryVo> detail(@RequestParam Long id) {
        return enteralNutritionService.detail(id);
    }

    /**
     * 导出肠内营养列表
     */
    @SaCheckPermission("system:nutrition:export")
    @Log(title = "肠内营养", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EnteralNutritionBo bo, HttpServletResponse response) {
        List<EnteralNutritionVo> list = enteralNutritionService.queryList(bo);
        ExcelUtil.exportExcel(list, "肠内营养", EnteralNutritionVo.class, response);
    }

    /**
     * 获取肠内营养详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:nutrition:query")
    @GetMapping("/{id}")
    public R<EnteralNutritionVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(enteralNutritionService.queryById(id));
    }

    /**
     * 新增肠内营养
     */
    @SaCheckPermission("system:nutrition:add")
    @Log(title = "肠内营养", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EnteralNutritionSaveBo bo) {
        return toAjax(enteralNutritionService.insertByBo(bo));
    }

    /**
     * 修改肠内营养
     */
    @SaCheckPermission("system:nutrition:edit")
    @Log(title = "肠内营养", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EnteralNutritionSaveBo bo) {
        return toAjax(enteralNutritionService.updateByBo(bo));
    }

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