| | |
| | | import io.micrometer.core.annotation.Timed; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.modules.cms.entity.CuttingInventory; |
| | | import org.jeecg.modules.cms.entity.CuttingReceive; |
| | | import org.jeecg.modules.cms.entity.CuttingReceiveDetail; |
| | | import org.jeecg.modules.cms.entity.RatedLife; |
| | | import org.jeecg.modules.cms.mapper.CuttingReceiveMapper; |
| | | import org.jeecg.modules.cms.service.ICuttingInventoryService; |
| | | import org.jeecg.modules.cms.service.ICuttingReceiveDetailService; |
| | | import org.jeecg.modules.cms.service.ICuttingReceiveService; |
| | | import org.jeecg.modules.cms.service.IRatedLifeService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | private ICuttingInventoryService cuttingInventoryService; |
| | | @Autowired |
| | | private ICuttingReceiveDetailService cuttingReceiveDetailService; |
| | | @Autowired |
| | | private IRatedLifeService ratedLifeService; |
| | | @Override |
| | | public IPage<Map<String, Object>> getInventoryToolList(Integer pageNo, Integer pageSize, Map<String, Object> params) { |
| | | IPage<Map> pageData = new Page<Map>(pageNo, pageSize); |
| | |
| | | return Result.error("提交失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public synchronized Result<?> handleBack(String orderId) { |
| | | long startTime = System.currentTimeMillis(); |
| | | try { |
| | | // 1. 更新领用单状态为已归还 |
| | | CuttingReceive cuttingReceive = this.getById(orderId); |
| | | if (cuttingReceive == null) { |
| | | return Result.error("未找到对应的领用单"); |
| | | } |
| | | |
| | | // 检查领用单状态,只允许状态为"已领用"的领用单进行归还操作 |
| | | if (!"2".equals(cuttingReceive.getOrderStatus())) { |
| | | return Result.error("只有状态为已领用的领用单才能执行归还操作"); |
| | | } |
| | | |
| | | cuttingReceive.setOrderStatus("3"); // 设置为已归还状态 |
| | | boolean updateResult = this.updateById(cuttingReceive); |
| | | if (!updateResult) { |
| | | return Result.error("更新领用单状态失败,可能已被其他用户处理"); |
| | | } |
| | | |
| | | // 2. 获取领用明细 |
| | | List<CuttingReceiveDetail> detailList = cuttingReceiveDetailService.lambdaQuery() |
| | | .eq(CuttingReceiveDetail::getOrderId, orderId) |
| | | .list(); |
| | | |
| | | // 3. 收集所有需要更新状态的库存ID |
| | | List<String> inventoryIds = new ArrayList<>(); |
| | | for (CuttingReceiveDetail detail : detailList) { |
| | | if (detail.getInventoryId() != null && !detail.getInventoryId().isEmpty()) { |
| | | inventoryIds.add(detail.getInventoryId()); |
| | | } |
| | | } |
| | | |
| | | // 4. 批量更新库存状态为"正常" |
| | | if (!inventoryIds.isEmpty()) { |
| | | cuttingInventoryService.updateStatus(inventoryIds, "正常"); |
| | | } |
| | | // 5. 寿命扣减 |
| | | for (CuttingReceiveDetail detail : detailList) { |
| | | if (detail.getInventoryId() != null && !detail.getInventoryId().isEmpty()) { |
| | | CuttingInventory inventory = cuttingInventoryService.getById(detail.getInventoryId()); |
| | | //FIXME:ratedLife空指针 |
| | | RatedLife ratedLife = ratedLifeService.lambdaQuery() //额定寿命 |
| | | .eq(RatedLife::getCuttingId, inventory.getCuttingId()) |
| | | .eq(RatedLife::getWorkpieceMaterial, inventory.getWorkpieceMaterial()) |
| | | .one(); |
| | | |
| | | BigDecimal currentLife = inventory.getCurrentLife().divide(BigDecimal.valueOf(100),java.math.RoundingMode.HALF_UP);//当前寿命百分比 |
| | | Integer useLife = detail.getUsedLife();//使用寿命 |
| | | //计算公式: (ratedLife * currentLife - useLife) * 100 |
| | | BigDecimal newLife = ratedLife.getRatedLife() |
| | | .multiply(currentLife) |
| | | .subtract(BigDecimal.valueOf(useLife)) |
| | | .multiply(BigDecimal.valueOf(100)); |
| | | // 更新库存寿命 |
| | | inventory.setCurrentLife(newLife); |
| | | cuttingInventoryService.updateById(inventory); |
| | | } |
| | | } |
| | | long endTime = System.currentTimeMillis(); |
| | | log.info("刀具归还处理完成,耗时: {} ms", (endTime - startTime)); |
| | | return Result.ok("归还成功"); |
| | | |
| | | } catch (Exception e) { |
| | | long endTime = System.currentTimeMillis(); |
| | | log.error("处理归还失败,orderId: " + orderId + ",耗时: " + (endTime - startTime) + " ms", e); |
| | | return Result.error("归还失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | } |