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.NutritionDiagnosisBo;
import org.dromara.web.domain.vo.NutritionDiagnosisVo;
import org.dromara.web.service.INutritionDiagnosisService;
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-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/nutritionDiagnosis")
public class NutritionDiagnosisController extends BaseController {

    private final INutritionDiagnosisService nutritionDiagnosisService;

    /**
     * 查询营养诊断列表
     */
    @SaCheckPermission("patients:nutritionDiagnosis:list")
    @GetMapping("/list")
    public TableDataInfo<NutritionDiagnosisVo> list(NutritionDiagnosisBo bo, PageQuery pageQuery) {
        return nutritionDiagnosisService.queryPageList(bo, pageQuery);
    }

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

    /**
     * 导出营养诊断列表
     */
    @SaCheckPermission("patients:nutritionDiagnosis:export")
    @Log(title = "营养诊断", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NutritionDiagnosisBo bo, HttpServletResponse response) {
        List<NutritionDiagnosisVo> list = nutritionDiagnosisService.queryList(bo);
        ExcelUtil.exportExcel(list, "营养诊断", NutritionDiagnosisVo.class, response);
    }

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

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

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

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