chenrui
2 天以前 ab01a0f611c0adb97662bc8f548ca4a911ec8045
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -2,15 +2,21 @@
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.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.*;
@@ -69,7 +75,7 @@
    private final InvoiceLedgerMapper invoiceLedgerMapper;
    @Autowired
    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
    private SysDeptMapper sysDeptMapper;
    @Value("${file.upload-dir}")
    private String uploadDir;
@@ -98,6 +104,9 @@
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        for (SalesLedgerProduct product : products) {
            product.setOriginalNoInvoiceNum(product.getNoInvoiceNum());
            // 提供临时未开票数,未开票金额供前段计算
            product.setTempnoInvoiceAmount(product.getNoInvoiceAmount());
            product.setTempNoInvoiceNum(product.getNoInvoiceNum());
        }
        // 3.查询上传文件
@@ -119,7 +128,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);
@@ -233,6 +242,11 @@
        }
        Collections.reverse(monthlyAmounts);
        return monthlyAmounts;
    }
    @Override
    public IPage<SalesLedger> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) {
        return salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto);
    }
    // 内部类用于存储聚合结果
@@ -447,7 +461,7 @@
        if (!insertList.isEmpty()) {
            for (SalesLedgerProduct salesLedgerProduct : insertList) {
                salesLedgerProduct.setType(type);
                salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity().intValue());
                salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
                salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
                salesLedgerProductMapper.insert(salesLedgerProduct);
            }
@@ -489,11 +503,19 @@
                throw new RuntimeException("获取合同编号生成锁失败:超时");
            }
            // 2. 查询当天已存在的序列号(与原逻辑一致)
            // 2. 查询当天/公司已存在的序列号(与原逻辑一致)
            Integer 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";