maven
3 天以前 366b67926809852c1090f14c5a24fe00109a81b5
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -2,15 +2,24 @@
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.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.*;
@@ -68,8 +77,10 @@
    private final InvoiceLedgerMapper invoiceLedgerMapper;
    private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
    @Autowired
    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
    private SysDeptMapper sysDeptMapper;
    @Value("${file.upload-dir}")
    private String uploadDir;
@@ -82,13 +93,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());
@@ -99,7 +107,16 @@
        // 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<>();
@@ -120,7 +137,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);
@@ -187,53 +204,82 @@
    }
    @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);
        }
        return monthlyAmounts;
        // 反转列表,使时间顺序从早到晚
        Collections.reverse(result);
        return result;
    }
    @Override
    public IPage<SalesLedger> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) {
        return salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto);
    }
    // 内部类用于存储聚合结果
@@ -294,15 +340,23 @@
        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);
    }
@@ -412,7 +466,8 @@
                fileRecord.setName(originalFilename);
                fileRecord.setUrl(formalFilePath.toString());
                fileRecord.setCreateTime(LocalDateTime.now());
                fileRecord.setType("1");
                //销售
                fileRecord.setType(FileNameType.SALE.getValue());
                commonFileMapper.insert(fileRecord);
                // 删除临时文件记录
@@ -448,7 +503,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);
            }
@@ -490,11 +545,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";