liyong
23 小时以前 d9aac64d061758c77af5ecccce740df43167024e
src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -12,7 +12,9 @@
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.pojo.SupplierManage;
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.security.LoginUser;
@@ -28,10 +30,9 @@
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.purchase.pojo.TicketRegistration;
import com.ruoyi.purchase.service.IPurchaseLedgerService;
import com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.mapper.*;
import com.ruoyi.sales.pojo.CommonFile;
import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.RequiredArgsConstructor;
@@ -39,7 +40,9 @@
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
@@ -49,8 +52,10 @@
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -85,6 +90,10 @@
    private final ProductRecordMapper productRecordMapper;
    private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
    private final StringRedisTemplate redisTemplate;
    @Value("${file.upload-dir}")
    private String uploadDir;
@@ -98,6 +107,7 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException {
        SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId());
@@ -111,7 +121,7 @@
        BeanUtils.copyProperties(purchaseLedgerDto, purchaseLedger);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(ObjectUtils.isNotEmpty(loginUser) && null != loginUser.getTenantId()) {
            purchaseLedger.setTenantId(loginUser.getTenantId().longValue());
            purchaseLedger.setTenantId(loginUser.getTenantId());
        }
        purchaseLedger.setSalesContractNo(ObjectUtils.isNotEmpty(salesLedger) ? salesLedger.getSalesContractNo() : null);
        purchaseLedger.setSupplierName(supplierManage.getSupplierName());
@@ -190,6 +200,7 @@
        List<SalesLedgerProduct> updateList = partitionedProducts.get(true);
        List<SalesLedgerProduct> insertList = partitionedProducts.get(false);
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(salesLedgerId);
        // 执行更新操作
        if (!updateList.isEmpty()) {
@@ -202,6 +213,10 @@
        if (!insertList.isEmpty()) {
            for (SalesLedgerProduct salesLedgerProduct : insertList) {
                salesLedgerProduct.setType(type);
                Date entryDate = purchaseLedger.getEntryDate();
                LocalDateTime localDateTime = entryDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                salesLedgerProduct.setRegisterDate(localDateTime);
                salesLedgerProductMapper.insert(salesLedgerProduct);
            }
        }
@@ -275,7 +290,7 @@
                fileRecord.setName(originalFilename);
                fileRecord.setUrl(formalFilePath.toString());
                fileRecord.setCreateTime(LocalDateTime.now());
                fileRecord.setType("2");
                fileRecord.setType(FileNameType.PURCHASE.getValue());
                commonFileMapper.insert(fileRecord);
                // 删除临时文件记录
@@ -298,7 +313,7 @@
        // 批量删除关联的采购台账产品
        LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SalesLedgerProduct::getSalesLedgerId, ids)
                .eq(SalesLedgerProduct::getType, "2");
                .eq(SalesLedgerProduct::getType, 2);
        salesLedgerProductMapper.delete(queryWrapper);
        // 批量删除关联的采购台账的来票登记
        LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -366,6 +381,7 @@
        }
        // 创建并填充DTO
        PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
        resultDto.setSalesLedgerId(purchaseLedger.getSalesLedgerId());
        resultDto.setSalesContractNoId(purchaseLedger.getSalesLedgerId());
        resultDto.setSalesContractNo(purchaseLedger.getSalesContractNo());
        resultDto.setSupplierName(purchaseLedger.getSupplierName());
@@ -408,6 +424,7 @@
    public PurchaseLedgerDto getPurchaseNoById(Long id) {
        PurchaseLedgerDto purchaseLedgerDto = new PurchaseLedgerDto();
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
        BeanUtils.copyProperties(purchaseLedger, purchaseLedgerDto);
//        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectOne(new LambdaQueryWrapper<TicketRegistration>().eq(TicketRegistration::getPurchaseLedgerId, id));
//        if (ticketRegistration != null) {
@@ -419,12 +436,45 @@
    }
    @Override
    public IPage<PurchaseLedger> selectPurchaseLedgerListPage(IPage ipage, PurchaseLedger purchaseLedger) {
        LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(purchaseLedger.getPurchaseContractNumber())) {
            queryWrapper.like(PurchaseLedger::getPurchaseContractNumber, purchaseLedger.getPurchaseContractNumber());
    public IPage<PurchaseLedgerDto> selectPurchaseLedgerListPage(IPage ipage, PurchaseLedgerDto purchaseLedger) {
        IPage<PurchaseLedgerDto> purchaseLedgerDtoIPage = purchaseLedgerMapper.selectPurchaseLedgerListPage(ipage, purchaseLedger);
        purchaseLedgerDtoIPage.getRecords().forEach(purchaseLedgerDto -> {
            List<CommonFile> commonFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>().eq(CommonFile::getCommonId, purchaseLedgerDto.getId()).eq(CommonFile::getType, FileNameType.PURCHASELEDGER.getValue()));
            purchaseLedgerDto.setSalesLedgerFiles(commonFiles);
        });
        return purchaseLedgerDtoIPage;
    }
    @Override
    public List<InvoiceRegistrationProduct> getProductBySalesNo(Long id) {
        List<InvoiceRegistrationProduct> invoiceRegistrationProducts = invoiceRegistrationProductMapper.selectList(new LambdaQueryWrapper<InvoiceRegistrationProduct>()
                        .select(InvoiceRegistrationProduct::getId, InvoiceRegistrationProduct::getProductCategory, InvoiceRegistrationProduct::getSpecificationModel,
                                InvoiceRegistrationProduct::getUnit, InvoiceRegistrationProduct::getQuantity)
                .eq(InvoiceRegistrationProduct::getSalesLedgerId, id));
        if (invoiceRegistrationProducts.isEmpty()) {
            return new ArrayList<>();
        }
        return purchaseLedgerMapper.selectPage(ipage, queryWrapper);
        return invoiceRegistrationProducts;
    }
    @Override
    public String getPurchaseNo() {
        // 生成日期前缀(例如:CG20250405)
        String purchaseNo = "CG" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 构建 Redis Key(按天分隔)
        String redisKey = "purchase_no:" + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
        // 获取当前序号并递增(原子操作)
        Long sequence = redisTemplate.opsForValue().increment(redisKey);
        // 设置过期时间(次日失效,防止冗余数据)
        if (sequence == 1) {
            redisTemplate.expire(redisKey, 1, TimeUnit.DAYS);
        }
        // 返回完整编号(如 CG202504050001)
        return purchaseNo + String.format("%03d", sequence);
    }
    /**