From 8c627df4abacbe7397d0c1831f1a71c62ebe15b2 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期三, 15 十月 2025 10:45:33 +0800
Subject: [PATCH] yys 1.随机排产
---
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 153 insertions(+), 22 deletions(-)
diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
index c92da97..a77c7d4 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -5,19 +5,24 @@
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.entity.CoalInfo;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.business.dto.ProductionMasterDto;
import com.ruoyi.business.entity.*;
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.service.ProductionMasterService;
+import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@@ -45,15 +50,38 @@
private final PendingInventoryMapper pendingInventoryMapper;
+ private final ProductionSchedulingMapper productionSchedulingMapper;
+
@Override
public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
// 1. 鏋勫缓涓昏〃鏌ヨ鏉′欢
LambdaQueryWrapper<ProductionMaster> masterQueryWrapper = new LambdaQueryWrapper<>();
+ String keyword = productionMasterDto.getSearchAll();
+ if (StringUtils.isNotBlank(keyword)) {
+ // 鏌ヨ鐓ょ鍚嶇О涓ā绯婂尮閰嶇殑coalId鍒楄〃
+ List<Long> matchedCoalIds = coalInfoMapper.selectList(
+ new LambdaQueryWrapper<CoalInfo>().like(CoalInfo::getCoal, keyword)
+ ).stream()
+ .map(CoalInfo::getId)
+ .toList();
+ // 缁勮鏌ヨ鏉′欢锛氱叅绉岻D鍦ㄥ尮閰嶇殑鍒楄〃涓�
+ // 濡傛灉 matchedCoalIds 涓虹┖锛岀洿鎺ヨ繑鍥� 0 鏉℃暟鎹紙鏋勯�犱竴涓笉鍙兘鎴愮珛鐨勬潯浠讹級
+ if (matchedCoalIds.isEmpty()) {
+ masterQueryWrapper.apply("1 = 0"); // 寮哄埗杩斿洖绌虹粨鏋�
+ }
+ // 濡傛灉鏈夊尮閰嶇殑 coalId锛屽垯鎸� coalId 鏌ヨ
+ else {
+ String ids = matchedCoalIds.stream()
+ .map(String::valueOf)
+ .collect(Collectors.joining(","));
-
-
-
+ masterQueryWrapper.apply(
+ "{0} = ANY(string_to_array(coal_id, ','))",
+ ids
+ );
+ }
+ }
// 2. 鎵ц涓昏〃鍒嗛〉鏌ヨ
IPage<ProductionMaster> entityPage = productionMasterMapper.selectPage(page, masterQueryWrapper);
@@ -148,7 +176,7 @@
batchInsertInventories(masterId, dto.getProductionInventoryList());
// 鎻掑叆寰呭叆搴撴暟鎹�
- insertPendingInventory(dto.getProductionList());
+// insertPendingInventory(dto.getProductionList());
return 1;
}
@@ -238,6 +266,7 @@
BeanUtils.copyProperties(p, copy);
copy.setId(null);
copy.setProductionMasterId(masterId);
+ copy.setStatus(1);
productionMapper.insert(copy);
}
}
@@ -258,18 +287,68 @@
/**
* 灏嗗姞宸ヤ骇鐢熺殑浜у搧璁板綍鍒板緟鍏ュ簱琛�
*/
- private void insertPendingInventory(List<Production> list) {
+ public void insertPendingInventory(List<Production> list) {
+ LocalDate currentDate = LocalDate.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ String formattedDate = currentDate.format(formatter);
+ final int SCALE = 2;
+ final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;
+ final BigDecimal TAX_RATE = new BigDecimal("1.13");
+
for (Production p : list) {
PendingInventory pending = new PendingInventory();
pending.setCoalId(p.getCoalId());
pending.setInventoryQuantity(p.getProductionQuantity());
- pending.setSupplierName("鐢熶骇鍔犲伐鍏ュ簱");
- pending.setTotalPriceIncludingTax(p.getTotalCost());
- pending.setPriceIncludingTax(p.getPurchasePrice());
+ pending.setUnit("鍚�");
+ pending.setSupplierName(formattedDate + " - " + "鐢熶骇鍔犲伐鍏ュ簱");
+
+ // 闈炵┖澶勭悊
+ BigDecimal totalCost = p.getTotalCost() == null ? BigDecimal.ZERO : p.getTotalCost();
+ BigDecimal productionQuantity = p.getProductionQuantity() == null ? BigDecimal.ZERO : p.getProductionQuantity();
+
+ // 鍚◣鎬讳环
+ BigDecimal totalPriceIncludingTax = totalCost.multiply(productionQuantity)
+ .setScale(SCALE, ROUNDING_MODE);
+ pending.setTotalPriceIncludingTax(totalPriceIncludingTax);
+
+ // 鍚◣鍗曚环
+ pending.setPriceIncludingTax(totalCost.setScale(SCALE, ROUNDING_MODE));
+
+ // 涓嶅惈绋庡崟浠凤紙鐩存帴淇濈暀2浣嶅皬鏁帮級
+ BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, SCALE, ROUNDING_MODE);
+ pending.setPriceExcludingTax(priceExcludingTax);
+
+ // 涓嶅惈绋庢�讳环
+ BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity)
+ .setScale(SCALE, ROUNDING_MODE);
+ pending.setTotalPriceExcludingTax(totalPriceExcludingTax);
+
pending.setRegistrantId(p.getProducerId());
pending.setRegistrationDate(LocalDate.now());
+
+ pending.setCode(generateCode(p.getType()));
+ pending.setType(p.getType());
pendingInventoryMapper.insert(pending);
}
+ }
+
+ private String generateCode(Integer type) {
+ LocalDate now = LocalDate.now();
+ String year = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+ String s = "";
+ //鐢熸垚缂栫爜
+ switch (type){
+ case 1:
+ s = "CP";
+ break;
+ case 2:
+ s = "YL";
+ break;
+ }
+ // 鏌ヨ褰撳ぉ寰呭叆搴撴柊澧炴潯鏁�
+ Long count = pendingInventoryMapper.selectCount(new LambdaQueryWrapper<PendingInventory>()
+ .apply("date_trunc('day', create_time) = date_trunc('day', now())"));
+ return s + year + String.format("%03d", count + 1);
}
@Override
@@ -312,12 +391,21 @@
.collect(Collectors.toList());
productionInventoryMapper.deleteBatchIds(inventoryIds);
}
+ List<Production> productions = productionMapper.selectList(new LambdaQueryWrapper<Production>()
+ .in(Production::getProductionMasterId, idList));
+
+ // 鍒犻櫎鐢熶骇鎶ュ伐璁板綍
+ productionSchedulingMapper.delete(
+ new LambdaQueryWrapper<ProductionScheduling>()
+ .in(ProductionScheduling::getProductionId, productions.stream().map(Production::getId).collect(Collectors.toList()))
+ );
// 鍒犻櫎鐢熶骇鏄庣粏
productionMapper.delete(
new LambdaQueryWrapper<Production>()
.in(Production::getProductionMasterId, idList)
);
+
// 5. 鍒犻櫎涓昏褰�
return productionMasterMapper.deleteBatchIds(idList);
@@ -349,20 +437,20 @@
}
// 鎵归噺鏇存柊瀹樻柟搴撳瓨
- for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
- OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
- if (official == null) {
- throw new BaseException("瀹樻柟搴撳瓨涓嶅瓨鍦紝ID: " + entry.getKey());
- }
-
- // 浣跨敤绾跨▼瀹夊叏鐨凚igDecimal鎿嶄綔
- official.setInventoryQuantity(
- Optional.ofNullable(official.getInventoryQuantity())
- .orElse(BigDecimal.ZERO)
- .add(entry.getValue())
- );
- officialInventoryMapper.updateById(official);
- }
+// for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
+// OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
+// if (official == null) {
+// throw new BaseException("瀹樻柟搴撳瓨涓嶅瓨鍦紝ID: " + entry.getKey());
+// }
+//
+// // 浣跨敤绾跨▼瀹夊叏鐨凚igDecimal鎿嶄綔
+// official.setInventoryQuantity(
+// Optional.ofNullable(official.getInventoryQuantity())
+// .orElse(BigDecimal.ZERO)
+// .add(entry.getValue())
+// );
+// officialInventoryMapper.updateById(official);
+// }
// 鎵归噺鍒犻櫎鐢熶骇搴撳瓨
if (!productionIdsToDelete.isEmpty()) {
@@ -371,4 +459,47 @@
return productionIdsToDelete.size();
}
+
+ @Override
+ public Object randomScheduling(LoginUser loginUser) {
+ List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(null);
+ if(CollectionUtils.isEmpty(officialInventories)){
+ throw new BaseException("娌℃湁鍙皟搴︾殑搴撳瓨");
+ }
+ Random random = new Random();
+ // 鍦ㄥ畼鏂瑰簱瀛樹腑闅忔満閫夋嫨涓�涓�
+ OfficialInventory officialInventory = officialInventories.get(random.nextInt(officialInventories.size()));
+ List<ProductionInventory> productionInventories = new ArrayList<>();
+ ProductionInventory productionInventory = new ProductionInventory();
+ productionInventory.setOfficialId(officialInventory.getId());
+ productionInventory.setCoalId(officialInventory.getCoalId());
+ productionInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().intValue());
+ productionInventory.setUsedQuantity(random.nextInt(officialInventory.getInventoryQuantity().intValue()) + 1);
+ productionInventories.add(productionInventory);
+ List<Production> productions = new ArrayList<>();
+ Production production = new Production();
+ production.setCoalId(officialInventory.getCoalId());
+ // 1-500闅忔満鐢熸垚
+ production.setEnergyConsumptionCost(BigDecimal.valueOf(random.nextInt(500) + 1));
+ production.setEquipmentDepreciation(BigDecimal.valueOf(random.nextInt(500) + 1));
+ production.setLaborCost(BigDecimal.valueOf(random.nextInt(500) + 1));
+ production.setProductionQuantity(BigDecimal.valueOf(random.nextInt(500) + 1));
+ production.setPurchasePrice(BigDecimal.valueOf(random.nextInt(500) + 1));
+
+ // 姹傚拰
+ BigDecimal add = production.getEquipmentDepreciation()
+ .add(production.getEnergyConsumptionCost())
+ .add(production.getLaborCost())
+ .add(production.getPurchasePrice())
+ .add(production.getProductionQuantity());
+ production.setTotalCost(add);
+ production.setType(2);
+ production.setProducerId(loginUser.getUserId());
+ production.setProducer(loginUser.getUsername());
+ productions.add(production);
+ ProductionMasterDto productionMasterDto = new ProductionMasterDto();
+ productionMasterDto.setProductionList(productions);
+ productionMasterDto.setProductionInventoryList(productionInventories);
+ return addOrEditPM(productionMasterDto);
+ }
}
--
Gitblit v1.9.3