From c4aec3aa2d6c7c91b67d32ef625fe9e1f457177f Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 24 六月 2025 17:34:23 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 207 +++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 188 insertions(+), 19 deletions(-)
diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 988410d..a31835b 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -2,27 +2,32 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.pojo.Customer;
+import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.other.mapper.TempFileMapper;
import com.ruoyi.other.pojo.TempFile;
+import com.ruoyi.project.system.domain.SysDept;
+import com.ruoyi.project.system.mapper.SysDeptMapper;
+import com.ruoyi.sales.dto.MonthlyAmountDto;
import com.ruoyi.sales.dto.SalesLedgerDto;
-import com.ruoyi.sales.mapper.CommonFileMapper;
-import com.ruoyi.sales.mapper.SalesLedgerMapper;
-import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
-import com.ruoyi.sales.pojo.CommonFile;
-import com.ruoyi.sales.pojo.SalesLedger;
-import com.ruoyi.sales.pojo.SalesLedgerProduct;
+import com.ruoyi.sales.mapper.*;
+import com.ruoyi.sales.pojo.*;
import com.ruoyi.sales.service.ISalesLedgerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
@@ -32,9 +37,13 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
-import java.nio.file.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -62,6 +71,13 @@
private final TempFileMapper tempFileMapper;
+ private final ReceiptPaymentMapper receiptPaymentMapper;
+
+ private final InvoiceLedgerMapper invoiceLedgerMapper;
+
+ @Autowired
+ private SysDeptMapper sysDeptMapper;
+
@Value("${file.upload-dir}")
private String uploadDir;
@@ -73,13 +89,10 @@
@Override
public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
- LambdaQueryWrapper<SalesLedger> queryWrapper = new LambdaQueryWrapper<>();
- if (StringUtils.isNotBlank(salesLedgerDto.getCustomerName())) {
- queryWrapper.eq(SalesLedger::getCustomerName, salesLedgerDto.getCustomerName());
- }
- return salesLedgerMapper.selectList(queryWrapper);
+ return salesLedgerMapper.selectSalesLedgerList(salesLedgerDto);
}
+ @Override
public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto) {
// 1. 鏌ヨ涓昏〃
SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId());
@@ -91,6 +104,14 @@
LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId());
List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
+ for (SalesLedgerProduct product : products) {
+ product.setOriginalNoInvoiceNum(product.getNoInvoiceNum());
+ // 鎻愪緵涓存椂鏈紑绁ㄦ暟锛屾湭寮�绁ㄩ噾棰濅緵鍓嶆璁$畻
+ product.setTempnoInvoiceAmount(product.getNoInvoiceAmount());
+ product.setTempNoInvoiceNum(product.getNoInvoiceNum());
+ product.setRegister(SecurityUtils.getLoginUser().getUser().getNickName());
+ product.setRegisterDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ }
// 3.鏌ヨ涓婁紶鏂囦欢
LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
@@ -111,7 +132,7 @@
@Override
public List<Map<String, Object>> getSalesNo() {
LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.lambdaQuery();
- queryWrapper.select(SalesLedger::getId, SalesLedger::getSalesContractNo);
+ queryWrapper.select(SalesLedger::getId, SalesLedger::getSalesContractNo, SalesLedger::getProjectName);
// 鑾峰彇鍘熷鏌ヨ缁撴灉
List<Map<String, Object>> result = salesLedgerMapper.selectMaps(queryWrapper);
@@ -122,6 +143,144 @@
entry -> underlineToCamel(entry.getKey()),
Map.Entry::getValue))
).collect(Collectors.toList());
+ }
+
+ @Override
+ public BigDecimal getContractAmount() {
+ LocalDate now = LocalDate.now();
+ YearMonth currentMonth = YearMonth.from(now);
+
+ // 鍒涘缓LambdaQueryWrapper
+ LambdaQueryWrapper<SalesLedger> queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.ge(SalesLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay()) // 澶т簬绛変簬鏈湀绗竴澶�
+ .lt(SalesLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 灏忎簬涓嬫湀绗竴澶�
+
+ // 鎵ц鏌ヨ骞惰绠楁�诲拰
+ List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(queryWrapper);
+
+ BigDecimal totalContractAmount = salesLedgers.stream()
+ .map(SalesLedger::getContractAmount)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ return totalContractAmount;
+ }
+
+ @Override
+ public List getTopFiveList() {
+ // 鏌ヨ鍘熷鏁版嵁
+ LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.lambdaQuery();
+ queryWrapper.select(SalesLedger::getCustomerId,
+ SalesLedger::getCustomerName,
+ SalesLedger::getContractAmount)
+ .orderByDesc(SalesLedger::getContractAmount);
+ List<SalesLedger> records = salesLedgerMapper.selectList(queryWrapper);
+
+ // 鎸夊鎴稩D鍒嗙粍骞惰仛鍚堥噾棰�
+ Map<Long, GroupedCustomer> groupedMap = new LinkedHashMap<>(); // 浣跨敤LinkedHashMap淇濇寔鎺掑簭
+ for (SalesLedger record : records) {
+ groupedMap.computeIfAbsent(record.getCustomerId(),
+ k -> new GroupedCustomer(record.getCustomerId(), record.getCustomerName()))
+ .addAmount(record.getContractAmount());
+ }
+
+ // 杞崲涓虹粨鏋滃垪琛ㄥ苟鍙栧墠5
+ return groupedMap.values().stream()
+ .sorted(Comparator.comparing(GroupedCustomer::getTotalAmount).reversed())
+ .limit(5)
+ .map(customer -> {
+ Map<String, Object> result = new HashMap<>();
+ result.put("customerId", customer.getCustomerId());
+ result.put("customerName", customer.getCustomerName());
+ result.put("totalAmount", customer.getTotalAmount());
+ return result;
+ })
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public List<MonthlyAmountDto> getAmountHalfYear() {
+ LocalDate now = LocalDate.now();
+ YearMonth currentMonth = YearMonth.from(now);
+
+ List<MonthlyAmountDto> monthlyAmounts = new ArrayList<>();
+
+ for (int i = 0; i < 6; i++) {
+ YearMonth targetMonth = currentMonth.minusMonths(i);
+ LocalDate firstDayOfMonth = targetMonth.atDay(1);
+ LocalDate firstDayOfNextMonth = targetMonth.plusMonths(1).atDay(1);
+
+ LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
+ LocalDateTime startOfNextMonth = firstDayOfNextMonth.atStartOfDay();
+
+ LambdaQueryWrapper<ReceiptPayment> receiptPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
+ receiptPaymentLambdaQueryWrapper.ge(ReceiptPayment::getCreateTime, startOfMonth)
+ .lt(ReceiptPayment::getCreateTime, startOfNextMonth);
+
+ LambdaQueryWrapper<InvoiceLedger> invoiceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
+ invoiceLedgerLambdaQueryWrapper.ge(InvoiceLedger::getCreateTime, startOfMonth)
+ .lt(InvoiceLedger::getCreateTime, startOfNextMonth);
+
+ // 鑾峰彇鍥炴閲戦
+ List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.selectList(receiptPaymentLambdaQueryWrapper);
+ //寮�绁ㄩ噾棰�
+ List<InvoiceLedger> invoiceLedgerList = invoiceLedgerMapper.selectList(invoiceLedgerLambdaQueryWrapper);
+
+ // 浣跨敤 Stream 姹傚拰
+ BigDecimal invoiceAmount = invoiceLedgerList.stream()
+ .map(InvoiceLedger::getInvoiceTotal)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ BigDecimal receiptAmount = receiptPaymentList.stream()
+ .map(ReceiptPayment::getReceiptPaymentAmount)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ MonthlyAmountDto monthlyAmount = new MonthlyAmountDto();
+ monthlyAmount.setMonth(targetMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
+ monthlyAmount.setInvoiceAmount(invoiceAmount);
+ monthlyAmount.setReceiptAmount(receiptAmount);
+
+ monthlyAmounts.add(monthlyAmount);
+ }
+ Collections.reverse(monthlyAmounts);
+ return monthlyAmounts;
+ }
+
+ @Override
+ public IPage<SalesLedger> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) {
+ return salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto);
+ }
+
+ // 鍐呴儴绫荤敤浜庡瓨鍌ㄨ仛鍚堢粨鏋�
+ private static class GroupedCustomer {
+ private final Long customerId;
+ private final String customerName;
+ private BigDecimal totalAmount = BigDecimal.ZERO;
+
+ public GroupedCustomer(Long customerId, String customerName) {
+ this.customerId = customerId;
+ this.customerName = customerName;
+ }
+
+ public void addAmount(BigDecimal amount) {
+ if (amount != null) {
+ this.totalAmount = this.totalAmount.add(amount);
+ }
+ }
+
+ public Long getCustomerId() {
+ return customerId;
+ }
+
+ public String getCustomerName() {
+ return customerName;
+ }
+
+ public BigDecimal getTotalAmount() {
+ return totalAmount;
+ }
}
/**
@@ -192,7 +351,7 @@
// 4. 澶勭悊瀛愯〃鏁版嵁
List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
if (productList != null && !productList.isEmpty()) {
- handleSalesLedgerProducts(salesLedger.getId(), productList,salesLedgerDto.getType());
+ handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType());
updateMainContractAmount(
salesLedger.getId(),
productList,
@@ -206,7 +365,6 @@
if (salesLedgerDto.getTempFileIds() != null && !salesLedgerDto.getTempFileIds().isEmpty()) {
migrateTempFilesToFormal(salesLedger.getId(), salesLedgerDto.getTempFileIds());
}
-
return 1;
} catch (IOException e) {
throw new BaseException("鏂囦欢杩佺Щ澶辫触: " + e.getMessage());
@@ -271,7 +429,8 @@
fileRecord.setName(originalFilename);
fileRecord.setUrl(formalFilePath.toString());
fileRecord.setCreateTime(LocalDateTime.now());
- fileRecord.setType("1");
+ //閿�鍞�
+ fileRecord.setType(FileNameType.SALE.getValue());
commonFileMapper.insert(fileRecord);
// 鍒犻櫎涓存椂鏂囦欢璁板綍
@@ -287,7 +446,7 @@
}
- private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products,Integer type) {
+ private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
// 鎸塈D鍒嗙粍锛屽尯鍒嗘柊澧炲拰鏇存柊鐨勮褰�
Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream()
.peek(p -> p.setSalesLedgerId(salesLedgerId))
@@ -307,6 +466,8 @@
if (!insertList.isEmpty()) {
for (SalesLedgerProduct salesLedgerProduct : insertList) {
salesLedgerProduct.setType(type);
+ salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
+ salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
salesLedgerProductMapper.insert(salesLedgerProduct);
}
}
@@ -347,11 +508,19 @@
throw new RuntimeException("鑾峰彇鍚堝悓缂栧彿鐢熸垚閿佸け璐ワ細瓒呮椂");
}
- // 2. 鏌ヨ褰撳ぉ宸插瓨鍦ㄧ殑搴忓垪鍙凤紙涓庡師閫昏緫涓�鑷达級
+ // 2. 鏌ヨ褰撳ぉ/鍏徃宸插瓨鍦ㄧ殑搴忓垪鍙凤紙涓庡師閫昏緫涓�鑷达級
+ Long tenantId = SecurityUtils.getLoginUser().getTenantId();
+ if (null != tenantId) {
+ //鑾峰彇鍏徃缂栧彿
+ SysDept sysDept = sysDeptMapper.selectDeptById(tenantId.longValue());
+ if (!ObjectUtils.isEmpty(sysDept)) {
+ datePart = (StringUtils.isEmpty(sysDept.getDeptNick()) ? "" : sysDept.getDeptNick()) + datePart;
+ }
+ }
List<Integer> existingSequences = salesLedgerMapper.selectSequencesByDate(datePart);
int nextSequence = findFirstMissingSequence(existingSequences);
- return datePart + String.format("%02d", nextSequence);
+ return datePart + String.format("%03d", nextSequence);
} finally {
// 3. 閲婃斁閿侊紙浣跨敤Lua鑴氭湰淇濊瘉鍘熷瓙鎬э紝閬垮厤璇垹鍏朵粬绾跨▼鐨勯攣锛�
String luaScript = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end";
--
Gitblit v1.9.3