package org.dromara.web.controller;

import java.math.BigDecimal;
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.NutritionSettingBo;
import org.dromara.web.domain.bo.RecalculateBo;
import org.dromara.web.domain.vo.NutritionSettingVo;
import org.dromara.web.service.INutritionSettingService;
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-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patients/nutritionSetting")
public class NutritionSettingController extends BaseController {

    private final INutritionSettingService nutritionSettingService;

    /**
     * 查询营养设定列表
     */
    @SaCheckPermission("patients:nutritionSetting:list")
    @GetMapping("/list")
    public TableDataInfo<NutritionSettingVo> list(NutritionSettingBo bo, PageQuery pageQuery) {
        return nutritionSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出营养设定列表
     */
    @SaCheckPermission("patients:nutritionSetting:export")
    @Log(title = "营养设定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NutritionSettingBo bo, HttpServletResponse response) {
        List<NutritionSettingVo> list = nutritionSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "营养设定", NutritionSettingVo.class, response);
    }

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

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

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

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

    /**
     * 计算
     */
    @PostMapping("/recalculate")
    public Map<String, String> recalculate(@Validated(EditGroup.class) @RequestBody RecalculateBo bo) {
        return nutritionSettingService.calculateEnergyRequirements(bo.getSettingType(),bo.getGender(), bo.getAge(), bo.getHeight().doubleValue(), bo.getWeight().doubleValue(), bo.getActivity(), bo.getStressType(),bo.getBurnArea());
    }
}
