package org.dromara.web.controller;

import java.util.List;
import java.util.stream.Collectors;

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

/**
 * 营养产品信息
 *
 * @author h
 * @date 2025-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/nutrition")
public class ProductNutritionController extends BaseController {

    private final IProductNutritionService productNutritionService;

    /**
     * 查询营养产品信息列表
     */
    @SaCheckPermission("warehouse:nutrition:list")
    @GetMapping("/list")
    public TableDataInfo<ProductNutritionVo> list(ProductNutritionBo bo, PageQuery pageQuery) {
        return productNutritionService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("warehouse:nutrition:list")
    @GetMapping("/listAll")
    public TableDataInfo<ProductNutritionVo> listAll() {
        return productNutritionService.listAll();
    }

    /**
     * 导出营养产品信息列表
     */
    @SaCheckPermission("warehouse:nutrition:export")
    @Log(title = "营养产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductNutritionBo bo, HttpServletResponse response) {
        List<ProductNutritionVo> list = productNutritionService.queryList(bo);
        ExcelUtil.exportExcel(list, "营养产品信息", ProductNutritionVo.class, response);
    }

    /*下载模板*/
    @SaCheckPermission("warehouse:nutrition:export")
    @RequestMapping(value = "/downLoadTemplate", method = RequestMethod.GET)
    public void downLoadTemplate(HttpServletResponse response) {
        ExcelUtil.downLoadTemplate("nutritionProduct.xlsx", "营养产品模版", response);
    }

    /*导入*/
    @SaCheckPermission("warehouse:nutrition:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public R<?> importExcel(MultipartFile file) throws Exception {
        return productNutritionService.importExcel(file);
    }

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

    /**
     * 新增营养产品信息
     */
    @SaCheckPermission("warehouse:nutrition:add")
    @Log(title = "营养产品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductNutritionBo bo) {
        return toAjax(productNutritionService.insertByBo(bo));
    }

    /**
     * 修改营养产品信息
     */
    @SaCheckPermission("warehouse:nutrition:edit")
    @Log(title = "营养产品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductNutritionBo bo) {
        return toAjax(productNutritionService.updateByBo(bo));
    }

    /**
     * 批量修改营养产品中的许可证和保质期到期提醒
     */
    @Log(title = "营养产品信息")
    @RepeatSubmit()
    @PostMapping("/batchUpdate")
    public R<Void> batchUpdate(@Validated(EditGroup.class) @RequestBody BatchProductNuyritionBo bo) {
        return toAjax(productNutritionService.batchUpdateNutrition(bo));
    }

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