From 79764df45449041d0d276b8cb28e9c8148de34da Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 30 三月 2026 11:23:17 +0800
Subject: [PATCH] yys 新增配置文件

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |  209 ++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 168 insertions(+), 41 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 b6c43ed..f647856 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -8,6 +8,12 @@
 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.approve.pojo.ApproveProcess;
+import com.ruoyi.approve.service.IApproveProcessService;
+import com.ruoyi.approve.vo.ApproveProcessVO;
 import com.ruoyi.basic.mapper.CustomerMapper;
 import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.common.enums.FileNameType;
@@ -20,6 +26,8 @@
 import com.ruoyi.production.pojo.SalesLedgerScheduling;
 import com.ruoyi.project.system.domain.SysDept;
 import com.ruoyi.project.system.mapper.SysDeptMapper;
+import com.ruoyi.purchase.dto.PurchaseLedgerDto;
+import com.ruoyi.purchase.pojo.PurchaseLedger;
 import com.ruoyi.sales.dto.MonthlyAmountDto;
 import com.ruoyi.sales.dto.SalesLedgerDto;
 import com.ruoyi.sales.mapper.*;
@@ -33,6 +41,7 @@
 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.format.annotation.DateTimeFormat;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -43,6 +52,7 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
+import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.YearMonth;
@@ -62,8 +72,11 @@
 @RequiredArgsConstructor
 @Slf4j
 public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService {
+    private final AccountIncomeService accountIncomeService;
 
     private final SalesLedgerMapper salesLedgerMapper;
+
+    private final IApproveProcessService approveProcessService;
 
     private final CustomerMapper customerMapper;
 
@@ -204,53 +217,77 @@
     }
 
     @Override
-    public List<MonthlyAmountDto> getAmountHalfYear() {
+    public List<MonthlyAmountDto> getAmountHalfYear(Integer type) {
         LocalDate now = LocalDate.now();
-        YearMonth currentMonth = YearMonth.from(now);
+        LocalDateTime currentDateTime = LocalDateTime.now();
 
-        List<MonthlyAmountDto> monthlyAmounts = new ArrayList<>();
+        // 鏍规嵁 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++) {
-            YearMonth targetMonth = currentMonth.minusMonths(i);
-            LocalDate firstDayOfMonth = targetMonth.atDay(1);
-            LocalDate firstDayOfNextMonth = targetMonth.plusMonths(1).atDay(1);
+            // 璁$畻褰撳墠鏃堕棿娈电殑璧峰鍜岀粨鏉熸椂闂�
+            LocalDateTime endTime = currentDateTime.minusDays(i * daysPerPeriod);
+            LocalDateTime startTime = endTime.minusDays(daysPerPeriod);
 
-            LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
-            LocalDateTime startOfNextMonth = firstDayOfNextMonth.atStartOfDay();
+            // 鏌ヨ鍥炴閲戦
+            LambdaQueryWrapper<ReceiptPayment> receiptPaymentQuery = new LambdaQueryWrapper<>();
+            receiptPaymentQuery
+                    .ge(ReceiptPayment::getCreateTime, startTime)
+                    .lt(ReceiptPayment::getCreateTime, endTime);
+            List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(receiptPaymentQuery);
 
-            LambdaQueryWrapper<ReceiptPayment> receiptPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
-            receiptPaymentLambdaQueryWrapper.ge(ReceiptPayment::getCreateTime, startOfMonth)
-                    .lt(ReceiptPayment::getCreateTime, startOfNextMonth);
+            // 鏌ヨ寮�绁ㄩ噾棰�
+            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerQuery = new LambdaQueryWrapper<>();
+            invoiceLedgerQuery
+                    .ge(InvoiceLedger::getCreateTime, startTime)
+                    .lt(InvoiceLedger::getCreateTime, endTime);
+            List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(invoiceLedgerQuery);
 
-            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()
+            // 璁$畻鍥炴鎬婚
+            BigDecimal receiptAmount = receiptPayments.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);
+            // 璁$畻寮�绁ㄦ�婚
+            BigDecimal invoiceAmount = invoiceLedgers.stream()
+                    .map(InvoiceLedger::getInvoiceTotal)
+                    .filter(Objects::nonNull)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-            monthlyAmounts.add(monthlyAmount);
+            // 鏋勯�犺繑鍥炵殑 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(monthlyAmounts);
-        return monthlyAmounts;
+
+        // 鍙嶈浆鍒楄〃锛屼娇鏃堕棿椤哄簭浠庢棭鍒版櫄
+        Collections.reverse(result);
+        return result;
     }
 
     @Override
@@ -333,7 +370,21 @@
         LambdaQueryWrapper<SalesLedgerScheduling> in = new LambdaQueryWrapper<SalesLedgerScheduling>()
                 .in(SalesLedgerScheduling::getSalesLedgerId, idList);
         salesLedgerSchedulingMapper.delete(in);
-        // 2. 鍐嶅垹闄や富琛ㄦ暟鎹�
+
+        // 2. 鍒犻櫎鍏宠仈鐨勫鎵硅〃
+        List<String> strings = salesLedgerMapper.selectBatchIds(idList).stream().map(SalesLedger::getSalesContractNo).distinct().collect(Collectors.toList());
+        List<ApproveProcess> list = approveProcessService.list(Wrappers.<ApproveProcess>lambdaQuery()
+                .eq(ApproveProcess::getApproveType, 6)
+                .in(ApproveProcess::getApproveReason, strings));
+        if (CollectionUtils.isNotEmpty(list)){
+            List<String> approveIds = list.stream().map(ApproveProcess::getApproveId).distinct().collect(Collectors.toList());
+            Long[] ides = approveIds.stream()
+                    .filter(s -> s != null && !s.isEmpty()) // 杩囨护绌哄瓧绗︿覆
+                    .map(Long::valueOf)
+                    .toArray(Long[]::new);
+            approveProcessService.delApprove(ides);
+        }
+        // 3. 鍐嶅垹闄や富琛ㄦ暟鎹�
         return salesLedgerMapper.deleteBatchIds(idList);
     }
 
@@ -352,13 +403,38 @@
             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());
+                List<AccountIncome> accountIncomeDBs = accountIncomeService.getByInvoiceNumberList(salesLedger.getSalesContractNo());
+                if (!CollectionUtils.isEmpty(accountIncomeDBs)) {
+                    accountIncomeDBs.forEach(accountIncomeDB ->{
+                        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. 澶勭悊瀛愯〃鏁版嵁
@@ -378,10 +454,58 @@
             if (salesLedgerDto.getTempFileIds() != null && !salesLedgerDto.getTempFileIds().isEmpty()) {
                 migrateTempFilesToFormal(salesLedger.getId(), salesLedgerDto.getTempFileIds());
             }
+
+            //6.閿�鍞鍗曟寜绱ф�ョ▼搴﹀垎绫伙紝濡傛櫘閫氳鍗曚互鍙婄揣鎬ヨ鍗曪紱鏅�氳鍗曢渶娴佽浆鑷充笟鍔″崟鎹繘琛屽鎵癸紝绱ф�ヨ鍗曟棤闇�瀹℃壒銆�
+            if (salesLedger.getSalesType().equals("鏅��")){
+                //鏂板瀹℃壒鏁版嵁approve_process
+                ApproveProcessVO approveProcessVO = new ApproveProcessVO();
+                approveProcessVO.setApproveDeptId(salesLedger.getTenantId());
+                approveProcessVO.setApproveReason(salesLedger.getSalesContractNo());//瀹℃壒鐞嗙敱鏄攢鍞悎鍚屽彿鐢ㄨ繖涓潵鍏宠仈
+                DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+                approveProcessVO.setApproveTime(LocalDate.now().format(dateFormat));
+                approveProcessVO.setApproveType(6);//6鏄攢鍞彴璐�
+                approveProcessVO.setApproveUser(Long.parseLong(salesLedger.getEntryPerson()));//褰曞叆浜�=鐢宠浜�
+                approveProcessVO.setApproveUserIds(salesLedgerDto.getApproveUserIds());//瀹℃壒浜�
+                try {
+                    approveProcessService.addApprove(approveProcessVO);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }else {
+                //绱ф�ラ粯璁ら�氳繃
+                salesLedger.setApprovalStatus(2);
+                salesLedgerMapper.updateById(salesLedger);
+            }
             return 1;
         } catch (IOException e) {
             throw new BaseException("鏂囦欢杩佺Щ澶辫触: " + e.getMessage());
         }
+    }
+
+    @Override
+    public SalesLedgerDto getSalesByCode(SalesLedgerDto salesLedgerDto) {
+        // 1. 鏌ヨ涓昏〃
+        SalesLedger salesLedger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>()
+                .eq(SalesLedger::getSalesContractNo, salesLedgerDto.getSalesContractNo())
+                .last("LIMIT 1"));
+        if (salesLedger == null) {
+            throw new BaseException("閿�鍞彴璐︿笉瀛樺湪");
+        }
+
+        // 2. 鏌ヨ瀛愯〃
+        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
+        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId())
+                .eq(SalesLedgerProduct::getType, 1);
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
+
+        // 4. 杞崲 DTO
+        SalesLedgerDto resultDto = new SalesLedgerDto();
+        BeanUtils.copyProperties(salesLedger, resultDto);
+        if (!products.isEmpty()) {
+            resultDto.setHasChildren(true);
+            resultDto.setProductData(products);
+        }
+        return resultDto;
     }
 
     // 鏂囦欢杩佺Щ鏂规硶
@@ -428,12 +552,15 @@
 
             try {
                 // 鎵ц鏂囦欢杩佺Щ锛堜娇鐢ㄥ師瀛愭搷浣滅‘淇濆畨鍏ㄦ�э級
-                Files.move(
-                        Paths.get(tempFile.getTempPath()),
-                        formalFilePath,
-                        StandardCopyOption.REPLACE_EXISTING,
-                        StandardCopyOption.ATOMIC_MOVE
-                );
+//                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锛�

--
Gitblit v1.9.3