|
@@ -0,0 +1,284 @@
|
|
|
+package org.jeecg.modules.sparePartsConsumables.inventoryManagement.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.common.aspect.annotation.AutoLog;
|
|
|
+import org.jeecg.common.system.base.controller.JeecgController;
|
|
|
+import org.jeecg.common.system.query.QueryGenerator;
|
|
|
+import org.jeecg.modules.sparePartsConsumables.inventoryManagement.entity.InventoryAlert;
|
|
|
+import org.jeecg.modules.sparePartsConsumables.inventoryManagement.service.IInventoryAlertService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 库存预警
|
|
|
+ * @Author: jeecg-boot
|
|
|
+ * @Date: 2025-05-22
|
|
|
+ * @Version: V1.0
|
|
|
+ */
|
|
|
+@Api(tags = "库存预警")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/sparePartsConsumables/inventoryAlert")
|
|
|
+@Slf4j
|
|
|
+public class InventoryAlertController extends JeecgController<InventoryAlert, IInventoryAlertService> {
|
|
|
+ @Autowired
|
|
|
+ private IInventoryAlertService inventoryAlertService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页列表查询
|
|
|
+ *
|
|
|
+ * @param inventoryAlert
|
|
|
+ * @param pageNo
|
|
|
+ * @param pageSize
|
|
|
+ * @param req
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ //@AutoLog(value = "库存预警-分页列表查询")
|
|
|
+ @ApiOperation(value = "库存预警-分页列表查询", notes = "库存预警-分页列表查询")
|
|
|
+ @GetMapping(value = "/list")
|
|
|
+ public Result<IPage<InventoryAlert>> queryPageList(InventoryAlert inventoryAlert,
|
|
|
+ @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
|
|
|
+ @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
|
|
|
+ HttpServletRequest req) {
|
|
|
+ QueryWrapper<InventoryAlert> queryWrapper = QueryGenerator.initQueryWrapper(inventoryAlert, req.getParameterMap());
|
|
|
+ Page<InventoryAlert> page = new Page<InventoryAlert>(pageNo, pageSize);
|
|
|
+ IPage<InventoryAlert> pageList = inventoryAlertService.page(page, queryWrapper);
|
|
|
+ return Result.OK(pageList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加
|
|
|
+ *
|
|
|
+ * @param inventoryAlert
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @AutoLog(value = "库存预警-添加")
|
|
|
+ @ApiOperation(value = "库存预警-添加", notes = "库存预警-添加")
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:add")
|
|
|
+ @PostMapping(value = "/add")
|
|
|
+ public Result<String> add(@RequestBody InventoryAlert inventoryAlert) {
|
|
|
+ inventoryAlertService.save(inventoryAlert);
|
|
|
+ return Result.OK("添加成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑
|
|
|
+ *
|
|
|
+ * @param inventoryAlert
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @AutoLog(value = "库存预警-编辑")
|
|
|
+ @ApiOperation(value = "库存预警-编辑", notes = "库存预警-编辑")
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:edit")
|
|
|
+ @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
|
|
|
+ public Result<String> edit(@RequestBody InventoryAlert inventoryAlert) {
|
|
|
+ inventoryAlertService.updateById(inventoryAlert);
|
|
|
+ return Result.OK("编辑成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过id删除
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @AutoLog(value = "库存预警-通过id删除")
|
|
|
+ @ApiOperation(value = "库存预警-通过id删除", notes = "库存预警-通过id删除")
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:delete")
|
|
|
+ @DeleteMapping(value = "/delete")
|
|
|
+ public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
|
|
|
+ inventoryAlertService.removeById(id);
|
|
|
+ return Result.OK("删除成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除
|
|
|
+ *
|
|
|
+ * @param ids
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @AutoLog(value = "库存预警-批量删除")
|
|
|
+ @ApiOperation(value = "库存预警-批量删除", notes = "库存预警-批量删除")
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:deleteBatch")
|
|
|
+ @DeleteMapping(value = "/deleteBatch")
|
|
|
+ public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
|
|
|
+ this.inventoryAlertService.removeByIds(Arrays.asList(ids.split(",")));
|
|
|
+ return Result.OK("批量删除成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过id查询
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ //@AutoLog(value = "库存预警-通过id查询")
|
|
|
+ @ApiOperation(value = "库存预警-通过id查询", notes = "库存预警-通过id查询")
|
|
|
+ @GetMapping(value = "/queryById")
|
|
|
+ public Result<InventoryAlert> queryById(@RequestParam(name = "id", required = true) String id) {
|
|
|
+ InventoryAlert inventoryAlert = inventoryAlertService.getById(id);
|
|
|
+ if (inventoryAlert == null) {
|
|
|
+ return Result.error("未找到对应数据");
|
|
|
+ }
|
|
|
+ return Result.OK(inventoryAlert);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出excel
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param inventoryAlert
|
|
|
+ */
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:exportXls")
|
|
|
+ @RequestMapping(value = "/exportXls")
|
|
|
+ public ModelAndView exportXls(HttpServletRequest request, InventoryAlert inventoryAlert) {
|
|
|
+ return super.exportXls(request, inventoryAlert, InventoryAlert.class, "库存预警");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过excel导入数据
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequiresPermissions("sparePartsConsumables:sbsmzq_inventory_alert:importExcel")
|
|
|
+ @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
|
|
+ public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ return super.importExcel(request, response, InventoryAlert.class);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页列表查询
|
|
|
+ *
|
|
|
+ * @param inventoryAlert
|
|
|
+ * @param pageNo
|
|
|
+ * @param pageSize
|
|
|
+ * @param req
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "超期预警-分页列表查询", notes = "超期预警-分页列表查询")
|
|
|
+ @GetMapping(value = "/overdueWarningList")
|
|
|
+ public Result<IPage<InventoryAlert>> queryOverduePageList(InventoryAlert inventoryAlert,
|
|
|
+ @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
|
|
|
+ @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
|
|
|
+ HttpServletRequest req) {
|
|
|
+ QueryWrapper<InventoryAlert> queryWrapper = QueryGenerator.initQueryWrapper(inventoryAlert, req.getParameterMap());
|
|
|
+
|
|
|
+ // 查询全部匹配数据(不分页)
|
|
|
+ List<InventoryAlert> allRecords = inventoryAlertService.list(queryWrapper);
|
|
|
+ List<InventoryAlert> filteredList = new ArrayList<>();
|
|
|
+
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+
|
|
|
+ for (InventoryAlert item : allRecords) {
|
|
|
+ if (item.getStorageDate() == null || item.getInventoryDays() == null || item.getWarningValue() == null)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ LocalDate storageDate = item.getStorageDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ long usedDays = ChronoUnit.DAYS.between(storageDate, now); // 已经使用的天数
|
|
|
+ long remainingDays = item.getInventoryDays() - usedDays;
|
|
|
+ // 筛选出剩余天数大于等于 0 且小于等于预警值的记录
|
|
|
+ if (remainingDays >= 0 && remainingDays <= item.getWarningValue()) {
|
|
|
+ filteredList.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 手动分页
|
|
|
+ int total = filteredList.size();
|
|
|
+ int start = (pageNo - 1) * pageSize;
|
|
|
+ int end = Math.min(start + pageSize, total);
|
|
|
+ List<InventoryAlert> paginatedList = (start < total) ? filteredList.subList(start, end) : Collections.emptyList();
|
|
|
+
|
|
|
+ Page<InventoryAlert> resultPage = new Page<>(pageNo, pageSize);
|
|
|
+ resultPage.setTotal(total);
|
|
|
+ resultPage.setRecords(paginatedList);
|
|
|
+
|
|
|
+ return Result.OK(resultPage);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页列表查询
|
|
|
+ *
|
|
|
+ * @param inventoryAlert
|
|
|
+ * @param pageNo
|
|
|
+ * @param pageSize
|
|
|
+ * @param req
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "过期预警-分页列表查询", notes = "过期预警-分页列表查询")
|
|
|
+ @GetMapping(value = "/expiredWarningList")
|
|
|
+ public Result<IPage<InventoryAlert>> queryExpiredPageList(InventoryAlert inventoryAlert,
|
|
|
+ @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
|
|
|
+ @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
|
|
|
+ HttpServletRequest req) {
|
|
|
+ QueryWrapper<InventoryAlert> queryWrapper = QueryGenerator.initQueryWrapper(inventoryAlert, req.getParameterMap());
|
|
|
+
|
|
|
+ // 不使用分页,先查出所有数据
|
|
|
+ List<InventoryAlert> allRecords = inventoryAlertService.list(queryWrapper);
|
|
|
+
|
|
|
+ List<InventoryAlert> filteredList = new ArrayList<>();
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+
|
|
|
+ for (InventoryAlert item : allRecords) {
|
|
|
+ if (item.getExpirationDate() == null) continue;
|
|
|
+
|
|
|
+ // 每条记录单独获取 warningValue,默认为 Integer.MAX_VALUE
|
|
|
+ int itemWarningValue = item.getWarningValue() != null ? item.getWarningValue() : Integer.MAX_VALUE;
|
|
|
+
|
|
|
+ LocalDate expirationDate = item.getExpirationDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ long diff = ChronoUnit.DAYS.between(now, expirationDate);
|
|
|
+ // 只保留 diff <= warningValue 的记录
|
|
|
+ if (diff <= itemWarningValue) {
|
|
|
+ String msg;
|
|
|
+ if (diff > 0) {
|
|
|
+ msg = "还有" + diff + "天过期";
|
|
|
+ } else if (diff == 0) {
|
|
|
+ msg = "今天过期";
|
|
|
+ } else {
|
|
|
+ msg = "已过期" + Math.abs(diff) + "天";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!msg.equals(item.getCurrentQuantity())) {
|
|
|
+ item.setCurrentQuantity(msg);
|
|
|
+ inventoryAlertService.updateById(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ filteredList.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 手动分页
|
|
|
+ int total = filteredList.size();
|
|
|
+ int start = (pageNo - 1) * pageSize;
|
|
|
+ int end = Math.min(start + pageSize, total);
|
|
|
+ List<InventoryAlert> paginatedList = (start < end) ? filteredList.subList(start, end) : Collections.emptyList();
|
|
|
+
|
|
|
+ Page<InventoryAlert> resultPage = new Page<>(pageNo, pageSize);
|
|
|
+ resultPage.setTotal(total);
|
|
|
+ resultPage.setRecords(paginatedList);
|
|
|
+
|
|
|
+ return Result.OK(resultPage);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|