From 0e4272399c014a3c6987ef4bd7d70f6ad90610ce Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期三, 15 十月 2025 18:02:20 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 569 ++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 503 insertions(+), 66 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 a1f884d..0cbe9d1 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1,28 +1,58 @@
package com.ruoyi.sales.service.impl;
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.account.pojo.AccountExpense;
+import com.ruoyi.account.pojo.AccountIncome;
+import com.ruoyi.account.service.AccountIncomeService;
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.production.mapper.SalesLedgerSchedulingMapper;
+import com.ruoyi.production.pojo.SalesLedgerScheduling;
+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.SalesLedgerMapper;
-import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
-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.AllArgsConstructor;
+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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.math.BigDecimal;
+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;
+import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -32,14 +62,32 @@
* @date 2025-05-08
*/
@Service
-@AllArgsConstructor
+@RequiredArgsConstructor
+@Slf4j
public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService {
+ private final AccountIncomeService accountIncomeService;
- private SalesLedgerMapper salesLedgerMapper;
+ private final SalesLedgerMapper salesLedgerMapper;
- private CustomerMapper customerMapper;
+ private final CustomerMapper customerMapper;
- private SalesLedgerProductMapper salesLedgerProductMapper;
+ private final SalesLedgerProductMapper salesLedgerProductMapper;
+
+ private final CommonFileMapper commonFileMapper;
+
+ private final TempFileMapper tempFileMapper;
+
+ private final ReceiptPaymentMapper receiptPaymentMapper;
+
+ private final InvoiceLedgerMapper invoiceLedgerMapper;
+
+ private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
+
+ @Autowired
+ private SysDeptMapper sysDeptMapper;
+
+ @Value("${file.upload-dir}")
+ private String uploadDir;
private static final String LOCK_PREFIX = "contract_no_lock:";
private static final long LOCK_WAIT_TIMEOUT = 10; // 閿佺瓑寰呰秴鏃舵椂闂达紙绉掞級
@@ -48,33 +96,246 @@
private final RedisTemplate<String, String> redisTemplate;
@Override
- public List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger) {
- return salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
- }
-
- public List<SalesLedgerDto> getSalesLedgerWithProducts() {
- List<SalesLedger> ledgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
- List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<>());
-
- Map<Long, List<SalesLedgerProduct>> productMap = products.stream()
- .collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId));
-
- return ledgers.stream().map(ledger -> {
- SalesLedgerDto dto = new SalesLedgerDto();
- org.springframework.beans.BeanUtils.copyProperties(ledger, dto);
-
- List<SalesLedgerProduct> ledgerProducts = productMap.getOrDefault(ledger.getId(), Collections.emptyList());
- if (!ledgerProducts.isEmpty()) {
- dto.setHasChildren(true);
- dto.setProductData(ledgerProducts);
- }
- return dto;
- }).collect(Collectors.toList());
+ public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
+ return salesLedgerMapper.selectSalesLedgerList(salesLedgerDto);
}
@Override
- public SalesLedger selectSalesLedgerById(Long id) {
- return salesLedgerMapper.selectById(id);
+ public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto) {
+ // 1. 鏌ヨ涓昏〃
+ SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId());
+ if (salesLedger == null) {
+ throw new BaseException("鍙拌处涓嶅瓨鍦�");
+ }
+
+ // 2. 鏌ヨ瀛愯〃
+ LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
+ productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId());
+ productWrapper.eq(SalesLedgerProduct::getType, 1);
+ 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());
+ }
+
+ // 3.鏌ヨ涓婁紶鏂囦欢
+ LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
+ salesLedgerFileWrapper.eq(CommonFile::getCommonId, salesLedger.getId());
+ List<CommonFile> salesLedgerFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
+
+ // 4. 杞崲 DTO
+ SalesLedgerDto resultDto = new SalesLedgerDto();
+ BeanUtils.copyProperties(salesLedger, resultDto);
+ if (!products.isEmpty()) {
+ resultDto.setHasChildren(true);
+ resultDto.setProductData(products);
+ resultDto.setSalesLedgerFiles(salesLedgerFiles);
+ }
+ return resultDto;
+ }
+
+ @Override
+ public List<Map<String, Object>> getSalesNo() {
+ LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.lambdaQuery();
+ queryWrapper.select(SalesLedger::getId, SalesLedger::getSalesContractNo, SalesLedger::getProjectName);
+
+ // 鑾峰彇鍘熷鏌ヨ缁撴灉
+ List<Map<String, Object>> result = salesLedgerMapper.selectMaps(queryWrapper);
+
+ // 灏嗕笅鍒掔嚎鍛藉悕杞崲涓洪┘宄板懡鍚�
+ return result.stream().map(map -> map.entrySet().stream()
+ .collect(Collectors.toMap(
+ 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(Integer type) {
+ LocalDate now = LocalDate.now();
+ LocalDateTime currentDateTime = LocalDateTime.now();
+
+ // 鏍规嵁 type 纭畾鏌ヨ鐨勬椂闂撮棿闅旓紙澶╂暟锛夊拰鎬绘煡璇㈠ぉ鏁�
+ int daysPerPeriod;
+ int totalDays;
+ switch (type) {
+ case 1:
+ daysPerPeriod = 5; // 姣�5澶╂煡涓�娆�
+ totalDays = 30; // 6娆� 脳 5澶� = 30澶�
+ break;
+ case 2:
+ daysPerPeriod = 15; // 姣�15澶╂煡涓�娆�
+ totalDays = 90; // 6娆� 脳 15澶� = 90澶�
+ break;
+ case 3:
+ daysPerPeriod = 30; // 姣�30澶╂煡涓�娆�
+ totalDays = 180; // 6娆� 脳 30澶� = 180澶�
+ break;
+ default:
+ throw new IllegalArgumentException("Invalid type value: " + type);
+ }
+
+ List<MonthlyAmountDto> result = new ArrayList<>();
+
+ // 寰幆6娆★紝姣忔鏌ヨ涓�涓椂闂存鐨勬暟鎹�
+ for (int i = 0; i < 6; i++) {
+ // 璁$畻褰撳墠鏃堕棿娈电殑璧峰鍜岀粨鏉熸椂闂�
+ LocalDateTime endTime = currentDateTime.minusDays(i * daysPerPeriod);
+ LocalDateTime startTime = endTime.minusDays(daysPerPeriod);
+
+ // 鏌ヨ鍥炴閲戦
+ LambdaQueryWrapper<ReceiptPayment> receiptPaymentQuery = new LambdaQueryWrapper<>();
+ receiptPaymentQuery
+ .ge(ReceiptPayment::getCreateTime, startTime)
+ .lt(ReceiptPayment::getCreateTime, endTime);
+ List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(receiptPaymentQuery);
+
+ // 鏌ヨ寮�绁ㄩ噾棰�
+ LambdaQueryWrapper<InvoiceLedger> invoiceLedgerQuery = new LambdaQueryWrapper<>();
+ invoiceLedgerQuery
+ .ge(InvoiceLedger::getCreateTime, startTime)
+ .lt(InvoiceLedger::getCreateTime, endTime);
+ List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(invoiceLedgerQuery);
+
+ // 璁$畻鍥炴鎬婚
+ BigDecimal receiptAmount = receiptPayments.stream()
+ .map(ReceiptPayment::getReceiptPaymentAmount)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ // 璁$畻寮�绁ㄦ�婚
+ BigDecimal invoiceAmount = invoiceLedgers.stream()
+ .map(InvoiceLedger::getInvoiceTotal)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ // 鏋勯�犺繑鍥炵殑 DTO
+ MonthlyAmountDto dto = new MonthlyAmountDto();
+ dto.setMonth(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " ~ " +
+ endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ dto.setReceiptAmount(receiptAmount);
+ dto.setInvoiceAmount(invoiceAmount);
+
+ result.add(dto);
+ }
+
+ // 鍙嶈浆鍒楄〃锛屼娇鏃堕棿椤哄簭浠庢棭鍒版櫄
+ Collections.reverse(result);
+ return result;
+ }
+
+ @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;
+ }
+ }
+
+ /**
+ * 涓嬪垝绾垮懡鍚嶈浆椹煎嘲鍛藉悕
+ */
+ private String underlineToCamel(String param) {
+ if (param == null || "".equals(param.trim())) {
+ return "";
+ }
+ int len = param.length();
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ char c = param.charAt(i);
+ if (c == '_') {
+ if (++i < len) {
+ sb.append(Character.toUpperCase(param.charAt(i)));
+ }
+ } else {
+ sb.append(Character.toLowerCase(c));
+ }
+ }
+ return sb.toString();
}
@Override
@@ -83,51 +344,176 @@
List<Long> idList = Arrays.stream(ids)
.filter(Objects::nonNull)
.collect(Collectors.toList());
-
if (CollectionUtils.isEmpty(idList)) {
return 0;
+ }
+ // 鐢熶骇璁㈠崟鏈夊緟鎺掍骇鏁版嵁锛屽彴璐︿笉鍙垹闄�
+ LambdaQueryWrapper<SalesLedgerScheduling> salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerScheduling>()
+ .in(SalesLedgerScheduling::getSalesLedgerId, idList);
+ if (salesLedgerSchedulingMapper.selectCount(salesLedgerSchedulingLambdaQueryWrapper) > 0) {
+ throw new BaseException("鏈夋帓浜ф暟鎹紝涓嶅彲鍒犻櫎");
}
// 1. 鍏堝垹闄ゅ瓙琛ㄦ暟鎹�
LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
productWrapper.in(SalesLedgerProduct::getSalesLedgerId, idList);
salesLedgerProductMapper.delete(productWrapper);
-
+ // 鍒犻櫎鐢熶骇璁㈠崟鏁版嵁
+ LambdaQueryWrapper<SalesLedgerScheduling> in = new LambdaQueryWrapper<SalesLedgerScheduling>()
+ .in(SalesLedgerScheduling::getSalesLedgerId, idList);
+ salesLedgerSchedulingMapper.delete(in);
// 2. 鍐嶅垹闄や富琛ㄦ暟鎹�
return salesLedgerMapper.deleteBatchIds(idList);
}
+ @Override
@Transactional(rollbackFor = Exception.class)
public int addOrUpdateSalesLedger(SalesLedgerDto salesLedgerDto) {
- // 1. 鏍¢獙瀹㈡埛淇℃伅
- Customer customer = customerMapper.selectById(salesLedgerDto.getCustomerId());
- if (customer == null) {
- throw new BaseException("瀹㈡埛涓嶅瓨鍦�");
+ try {
+ // 1. 鏍¢獙瀹㈡埛淇℃伅
+ Customer customer = customerMapper.selectById(salesLedgerDto.getCustomerId());
+ if (customer == null) {
+ throw new BaseException("瀹㈡埛涓嶅瓨鍦�");
+ }
+
+ // 2. DTO杞珽ntity
+ SalesLedger salesLedger = convertToEntity(salesLedgerDto);
+ salesLedger.setCustomerName(customer.getCustomerName());
+ salesLedger.setTenantId(customer.getTenantId());
+
+ AccountIncome accountIncome = new AccountIncome();
+ accountIncome.setIncomeDate(salesLedger.getEntryDate());
+ accountIncome.setIncomeType("0");
+ accountIncome.setCustomerName(customer.getCustomerName());
+ accountIncome.setIncomeMoney(salesLedger.getContractAmount());
+ accountIncome.setIncomeMethod("0");
+ accountIncome.setInputTime(new Date());
+ accountIncome.setInputUser(salesLedger.getEntryPerson());
+ // 3. 鏂板鎴栨洿鏂颁富琛�
+ if (salesLedger.getId() == null) {
+ String contractNo = generateSalesContractNo();
+ salesLedger.setSalesContractNo(contractNo);
+ salesLedgerMapper.insert(salesLedger);
+ accountIncome.setIncomeDescribed("閿�鍞悎鍚岋細" + salesLedger.getSalesContractNo());
+ accountIncome.setInvoiceNumber(salesLedger.getSalesContractNo());
+ accountIncomeService.save(accountIncome);
+ } else {
+ salesLedgerMapper.updateById(salesLedger);
+ SalesLedger salesLedgerDB = salesLedgerMapper.selectById(salesLedger.getId());
+ AccountIncome accountIncomeDB = accountIncomeService.getByInvoiceNumber(salesLedger.getSalesContractNo());
+ if (ObjectUtils.isEmpty(accountIncomeDB)) {
+ throw new BaseException("鏀跺叆绠$悊鏃犺閿�鍞悎鍚岀殑鏀跺叆璁板綍");
+ }
+ accountIncomeDB.setCustomerName(salesLedgerDB.getCustomerName());
+ accountIncomeDB.setIncomeMoney(salesLedgerDB.getContractAmount());
+ accountIncomeDB.setIncomeDescribed("閿�鍞悎鍚岋細" + salesLedgerDB.getSalesContractNo());
+ accountIncomeDB.setInvoiceNumber(salesLedgerDB.getSalesContractNo());
+ accountIncomeDB.setInputTime(new Date());
+ accountIncomeDB.setInputUser(salesLedgerDB.getEntryPerson());
+ accountIncomeService.updateById(accountIncomeDB);
+ }
+
+ // 4. 澶勭悊瀛愯〃鏁版嵁
+ List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
+ if (productList != null && !productList.isEmpty()) {
+ handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType());
+ updateMainContractAmount(
+ salesLedger.getId(),
+ productList,
+ SalesLedgerProduct::getTaxInclusiveTotalPrice,
+ salesLedgerMapper,
+ SalesLedger.class
+ );
+ }
+
+ // 5. 杩佺Щ涓存椂鏂囦欢鍒版寮忕洰褰�
+ if (salesLedgerDto.getTempFileIds() != null && !salesLedgerDto.getTempFileIds().isEmpty()) {
+ migrateTempFilesToFormal(salesLedger.getId(), salesLedgerDto.getTempFileIds());
+ }
+ return 1;
+ } catch (IOException e) {
+ throw new BaseException("鏂囦欢杩佺Щ澶辫触: " + e.getMessage());
}
-
- // 2. DTO杞珽ntity
- SalesLedger salesLedger = convertToEntity(salesLedgerDto);
- salesLedger.setCustomerName(customer.getCustomerName());
- salesLedger.setTenantId(customer.getTenantId());
-
- // 3. 鏂板鎴栨洿鏂颁富琛�
- if (salesLedger.getId() == null) {
- // 鐢熸垚鍚堝悓缂栧彿
- String contractNo = generateSalesContractNo();
- salesLedger.setSalesContractNo(contractNo);
- salesLedgerMapper.insert(salesLedger);
- } else {
- salesLedgerMapper.updateById(salesLedger);
- }
-
- // 4. 澶勭悊瀛愯〃鏁版嵁
- if (salesLedgerDto.getProductData() != null && !salesLedgerDto.getProductData().isEmpty()) {
- handleSalesLedgerProducts(salesLedger.getId(), salesLedgerDto.getProductData());
- }
-
- return 1; // 鎿嶄綔鎴愬姛杩斿洖1
}
- private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products) {
+ // 鏂囦欢杩佺Щ鏂规硶
+
+ /**
+ * 灏嗕复鏃舵枃浠惰縼绉诲埌姝e紡鐩綍
+ *
+ * @param businessId 涓氬姟ID锛堥攢鍞彴璐D锛�
+ * @param tempFileIds 涓存椂鏂囦欢ID鍒楄〃
+ * @throws IOException 鏂囦欢鎿嶄綔寮傚父
+ */
+ private void migrateTempFilesToFormal(Long businessId, List<String> tempFileIds) throws IOException {
+ if (CollectionUtils.isEmpty(tempFileIds)) {
+ return;
+ }
+
+ // 鏋勫缓姝e紡鐩綍璺緞锛堟寜涓氬姟绫诲瀷鍜屾棩鏈熷垎缁勶級
+ String formalDir = uploadDir + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
+
+ Path formalDirPath = Paths.get(formalDir);
+
+ // 纭繚姝e紡鐩綍瀛樺湪锛堥�掑綊鍒涘缓锛�
+ if (!Files.exists(formalDirPath)) {
+ Files.createDirectories(formalDirPath);
+ }
+
+ for (String tempFileId : tempFileIds) {
+ // 鏌ヨ涓存椂鏂囦欢璁板綍
+ TempFile tempFile = tempFileMapper.selectById(tempFileId);
+ if (tempFile == null) {
+ log.warn("涓存椂鏂囦欢涓嶅瓨鍦紝璺宠繃澶勭悊: {}", tempFileId);
+ continue;
+ }
+
+ // 鏋勫缓姝e紡鏂囦欢鍚嶏紙鍖呭惈涓氬姟ID鍜屾椂闂存埑锛岄伩鍏嶅啿绐侊級
+ String originalFilename = tempFile.getOriginalName();
+ String fileExtension = FilenameUtils.getExtension(originalFilename);
+ String formalFilename = businessId + "_" +
+ System.currentTimeMillis() + "_" +
+ UUID.randomUUID().toString().substring(0, 8) +
+ (StringUtils.hasText(fileExtension) ? "." + fileExtension : "");
+
+ Path formalFilePath = formalDirPath.resolve(formalFilename);
+
+ try {
+ // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級
+// Files.move(
+// Paths.get(tempFile.getTempPath()),
+// formalFilePath,
+// StandardCopyOption.REPLACE_EXISTING,
+// StandardCopyOption.ATOMIC_MOVE
+// );
+ // 鍘熷瓙绉诲姩澶辫触锛屼娇鐢ㄥ鍒�+鍒犻櫎
+ Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING);
+ Files.deleteIfExists(Paths.get(tempFile.getTempPath()));
+ log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath);
+
+ // 鏇存柊鏂囦欢璁板綍锛堝叧鑱斿埌涓氬姟ID锛�
+ CommonFile fileRecord = new CommonFile();
+ fileRecord.setCommonId(businessId);
+ fileRecord.setName(originalFilename);
+ fileRecord.setUrl(formalFilePath.toString());
+ fileRecord.setCreateTime(LocalDateTime.now());
+ //閿�鍞�
+ fileRecord.setType(FileNameType.SALE.getValue());
+ commonFileMapper.insert(fileRecord);
+
+ // 鍒犻櫎涓存椂鏂囦欢璁板綍
+ tempFileMapper.deleteById(tempFile);
+
+ log.info("鏂囦欢杩佺Щ鎴愬姛: {} -> {}", tempFile.getTempPath(), formalFilePath);
+ } catch (IOException e) {
+ log.error("鏂囦欢杩佺Щ澶辫触: {}", tempFile.getTempPath(), e);
+ // 鍙�夋嫨鍥炴粴浜嬪姟鎴栬褰曞け璐ユ枃浠�
+ throw new IOException("鏂囦欢杩佺Щ寮傚父", e);
+ }
+ }
+ }
+
+
+ private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
// 鎸塈D鍒嗙粍锛屽尯鍒嗘柊澧炲拰鏇存柊鐨勮褰�
Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream()
.peek(p -> p.setSalesLedgerId(salesLedgerId))
@@ -138,11 +524,19 @@
// 鎵ц鏇存柊鎿嶄綔
if (!updateList.isEmpty()) {
- salesLedgerProductMapper.updateBatchSomeColumn(updateList);
+ for (SalesLedgerProduct product : updateList) {
+ product.setType(type);
+ salesLedgerProductMapper.updateById(product);
+ }
}
// 鎵ц鎻掑叆鎿嶄綔
if (!insertList.isEmpty()) {
- salesLedgerProductMapper.insertBatchSomeColumn(insertList);
+ for (SalesLedgerProduct salesLedgerProduct : insertList) {
+ salesLedgerProduct.setType(type);
+ salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
+ salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
+ salesLedgerProductMapper.insert(salesLedgerProduct);
+ }
}
}
@@ -181,11 +575,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";
@@ -213,4 +615,39 @@
}
return next;
}
+
+ public <T, S> void updateMainContractAmount(
+ Long mainId,
+ List<T> subList,
+ Function<T, BigDecimal> amountGetter,
+ BaseMapper<S> mainMapper,
+ Class<S> mainEntityClass) {
+
+ if (mainId == null || subList == null || subList.isEmpty()) {
+ return;
+ }
+
+ // 璁$畻瀛愯〃閲戦鎬诲拰
+ BigDecimal totalAmount = subList.stream()
+ .map(amountGetter)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+ // 鏋勯�犱富琛ㄦ洿鏂板璞★紙鏀寔浠绘剰涓昏〃绫诲瀷锛�
+ try {
+ S entity = mainEntityClass.getDeclaredConstructor().newInstance();
+ Field idField = mainEntityClass.getDeclaredField("id");
+ idField.setAccessible(true);
+ idField.set(entity, mainId);
+
+ // 璁剧疆 contractAmount 瀛楁锛屾敞鎰忚繖閲屽亣璁惧瓧娈靛悕涓� "contractAmount"
+ Field amountField = mainEntityClass.getDeclaredField("contractAmount");
+ amountField.setAccessible(true);
+ amountField.set(entity, totalAmount);
+
+ mainMapper.updateById(entity);
+ } catch (Exception e) {
+ throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);
+ }
+ }
}
--
Gitblit v1.9.3