package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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.system.domain.vo.SysFoodIngredientVo;
import org.dromara.system.domain.bo.SysFoodIngredientBo;
import org.dromara.system.service.ISysFoodIngredientService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 食材管理
 *
 * @author Lion Li
 * @date 2025-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/foodIngredient")
public class SysFoodIngredientController extends BaseController {

    private final ISysFoodIngredientService sysFoodIngredientService;

    /**
     * 查询食材管理列表
     */
    @SaCheckPermission("system:foodIngredient:list")
    @GetMapping("/list")
    public TableDataInfo<SysFoodIngredientVo> list(SysFoodIngredientBo bo, PageQuery pageQuery) {
        return sysFoodIngredientService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出食材管理列表
     */
    @SaCheckPermission("system:foodIngredient:export")
    @Log(title = "食材管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysFoodIngredientBo bo, HttpServletResponse response) {
        List<SysFoodIngredientVo> list = sysFoodIngredientService.queryList(bo);
        ExcelUtil.exportExcel(list, "食材管理", SysFoodIngredientVo.class, response);
    }

    /**
     * 获取食材管理详细信息
     *
     * @param foodIngredientId 主键
     */
    @SaCheckPermission("system:foodIngredient:query")
    @GetMapping("/{foodIngredientId}")
    public R<SysFoodIngredientVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long foodIngredientId) {
        return R.ok(sysFoodIngredientService.queryById(foodIngredientId));
    }

    /**
     * 新增食材管理
     */
    @SaCheckPermission("system:foodIngredient:add")
    @Log(title = "食材管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<String> add(@Validated(AddGroup.class) @RequestBody SysFoodIngredientBo bo) {
        sysFoodIngredientService.insertByBo(bo);
        return R.ok("成功!",String.valueOf(bo.getFoodIngredientId()));
    }

    /**
     * 修改食材管理
     */
    @SaCheckPermission("system:foodIngredient:edit")
    @Log(title = "食材管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysFoodIngredientBo bo) {
        return toAjax(sysFoodIngredientService.updateByBo(bo));
    }

    /**
     * 删除食材管理
     *
     * @param foodIngredientIds 主键串
     */
    @SaCheckPermission("system:foodIngredient:remove")
    @Log(title = "食材管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{foodIngredientIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] foodIngredientIds) {
        return toAjax(sysFoodIngredientService.deleteWithValidByIds(List.of(foodIngredientIds), true));
    }

    @SaCheckPermission("system:foodIngredient:downLoadTemplate")
    @RequestMapping(value = "/downLoadTemplate", method = RequestMethod.GET)
    public void downLoadTemplate(HttpServletResponse response) {
        ExcelUtil.downLoadTemplate("foodIngredient.xlsx", "疾病_部位标签模版", response);
    }

    @SaCheckPermission("system:foodIngredient:import")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public R<?> importExcel(MultipartFile file) throws Exception {
        return sysFoodIngredientService.importExcel(file);
    }
}
