package org.dromara.web.controller;

import java.util.List;

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.BatchSuppliesManageBo;
import org.dromara.web.domain.bo.SuppliesManageBo;
import org.dromara.web.domain.vo.SuppliesManageVo;
import org.dromara.web.service.ISuppliesManageService;
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 Lion Li
 * @date 2025-07-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/suppliesManage")
public class SuppliesManageController extends BaseController {

    private final ISuppliesManageService suppliesManageService;

    /**
     * 查询耗材管理列表
     */
    @SaCheckPermission("warehouse:suppliesManage:list")
    @GetMapping("/list")
    public TableDataInfo<SuppliesManageVo> list(SuppliesManageBo bo, PageQuery pageQuery) {
        return suppliesManageService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("warehouse:suppliesManage:list")
    @GetMapping("/listAll")
    public TableDataInfo<SuppliesManageVo> listAll() {
        return suppliesManageService.listAll();
    }

    /**
     * 导出耗材管理列表
     */
    @SaCheckPermission("warehouse:suppliesManage:export")
    @Log(title = "耗材管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SuppliesManageBo bo, HttpServletResponse response) {
        List<SuppliesManageVo> list = suppliesManageService.queryList(bo);
        ExcelUtil.exportExcel(list, "耗材管理", SuppliesManageVo.class, response);
    }

    /*下载模板*/
    @SaCheckPermission("warehouse:suppliesManage:export")
    @RequestMapping(value = "/downLoadTemplate", method = RequestMethod.GET)
    public void downLoadTemplate(HttpServletResponse response) {
        ExcelUtil.downLoadTemplate("suppliesManage.xlsx", "耗材管理模版", response);
    }

    /**
     * 获取耗材管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:suppliesManage:query")
    @GetMapping("/{id}")
    public R<SuppliesManageVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(suppliesManageService.queryById(id));
    }

    /**
     * 新增耗材管理
     */
    @SaCheckPermission("warehouse:suppliesManage:add")
    @Log(title = "耗材管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SuppliesManageBo bo) {
        return toAjax(suppliesManageService.insertByBo(bo));
    }

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

    /**
     * 批量修改耗材管理中的许可证和保质期到期提醒
     */
    @Log(title = "耗材管理")
    @RepeatSubmit()
    @PostMapping("/batchUpdate")
    public R<Void> batchUpdate(@Validated(EditGroup.class) @RequestBody BatchSuppliesManageBo bo) {
        return toAjax(suppliesManageService.batchUpdateSuppliesManage(bo));
    }

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

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