zss
7 天以前 1f53841c3046579d366f061761241127c63da86a
src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -6,6 +6,13 @@
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.AccountExpenseService;
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.ProductMapper;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.mapper.SupplierManageMapper;
@@ -14,6 +21,7 @@
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;
@@ -41,6 +49,7 @@
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;
@@ -50,6 +59,7 @@
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;
@@ -65,8 +75,9 @@
@RequiredArgsConstructor
@Slf4j
public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService {
    private final AccountExpenseService accountExpenseService;
    private final PurchaseLedgerMapper purchaseLedgerMapper;
    private final IApproveProcessService approveProcessService;
    private final SalesLedgerMapper salesLedgerMapper;
    private final SalesLedgerProductMapper salesLedgerProductMapper;
@@ -104,6 +115,7 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrEditPurchase(PurchaseLedgerDto purchaseLedgerDto) throws IOException {
        SalesLedger salesLedger = salesLedgerMapper.selectById(purchaseLedgerDto.getSalesLedgerId());
@@ -124,12 +136,41 @@
        purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId());
        purchaseLedger.setRecorderName(sysUser.getNickName());
        purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
        // 2. 处理账户收入
        AccountExpense accountExpense = new AccountExpense();
        accountExpense.setExpenseDate(purchaseLedger.getEntryDate());
        accountExpense.setExpenseType("0");
        accountExpense.setSupplierName(purchaseLedger.getSupplierName());
        accountExpense.setExpenseMoney(purchaseLedger.getContractAmount());
        accountExpense.setExpenseDescribed("采购合同:" + purchaseLedger.getPurchaseContractNumber());
        accountExpense.setExpenseMethod("0");
        accountExpense.setInvoiceNumber(purchaseLedger.getPurchaseContractNumber());
        accountExpense.setInputTime(new Date());
        accountExpense.setInputUser(loginUser.getNickName());
        // 3. 新增或更新主表
        if (purchaseLedger.getId() == null) {
            purchaseLedgerMapper.insert(purchaseLedger);
//            accountIncomeService.save(accountIncome);
            accountExpenseService.save(accountExpense);
        } else {
            purchaseLedgerMapper.updateById(purchaseLedger);
            PurchaseLedger purchaseLedgerDB = purchaseLedgerMapper.selectById(purchaseLedger.getId());
            List<AccountExpense> accountExpenseDBs = accountExpenseService.getByInvoiceNumberList(purchaseLedger.getPurchaseContractNumber());
            if (!CollectionUtils.isEmpty(accountExpenseDBs)) {
                accountExpenseDBs.forEach(accountExpenseDB ->{
                    accountExpenseDB.setExpenseDate(purchaseLedgerDB.getEntryDate());
                    accountExpenseDB.setExpenseType("0");
                    accountExpenseDB.setSupplierName(purchaseLedgerDB.getSupplierName());
                    accountExpenseDB.setExpenseMoney(purchaseLedgerDB.getContractAmount());
                    accountExpenseDB.setExpenseDescribed("采购合同:" + purchaseLedgerDB.getPurchaseContractNumber());
                    accountExpenseDB.setExpenseMethod("0");
                    accountExpenseDB.setInvoiceNumber(purchaseLedgerDB.getPurchaseContractNumber());
                    accountExpenseService.updateById(accountExpenseDB);
                });
            }
        }
        // 4. 处理子表数据
@@ -141,6 +182,22 @@
        // 5. 迁移临时文件到正式目录
        if (purchaseLedgerDto.getTempFileIds() != null && !purchaseLedgerDto.getTempFileIds().isEmpty()) {
            migrateTempFilesToFormal(purchaseLedger.getId(), purchaseLedgerDto.getTempFileIds());
        }
        //6.采购需要审批
        //新增审批数据approve_process
        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
        approveProcessVO.setApproveDeptId(purchaseLedger.getTenantId());
        approveProcessVO.setApproveReason(purchaseLedger.getPurchaseContractNumber());//审批理由是采购合同号用这个来关联
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        approveProcessVO.setApproveTime(LocalDate.now().format(dateFormat));
        approveProcessVO.setApproveType(5);//5是采购申请
        approveProcessVO.setApproveUser(purchaseLedger.getRecorderId());//录入人=申请人
        approveProcessVO.setApproveUserIds(purchaseLedgerDto.getApproveUserIds());//审批人
        try {
            approveProcessService.addApprove(approveProcessVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
@@ -196,6 +253,7 @@
        List<SalesLedgerProduct> updateList = partitionedProducts.get(true);
        List<SalesLedgerProduct> insertList = partitionedProducts.get(false);
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(salesLedgerId);
        // 执行更新操作
        if (!updateList.isEmpty()) {
@@ -208,6 +266,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);
            }
        }
@@ -267,12 +329,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)
@@ -314,6 +379,19 @@
        LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productRecordLambdaQueryWrapper.in(ProductRecord::getPurchaseLedgerId,ids);
        productRecordMapper.delete(productRecordLambdaQueryWrapper);
        // 删除关联的审批表
        List<String> strings = purchaseLedgerMapper.selectBatchIds(Arrays.asList(ids)).stream().map(PurchaseLedger::getPurchaseContractNumber).distinct().collect(Collectors.toList());
        List<ApproveProcess> list = approveProcessService.list(Wrappers.<ApproveProcess>lambdaQuery()
                .eq(ApproveProcess::getApproveType, 5)
                .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);
        }
        // 批量删除采购台账
        return purchaseLedgerMapper.deleteBatchIds(Arrays.asList(ids));
    }
@@ -372,6 +450,7 @@
        }
        // 创建并填充DTO
        PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
        resultDto.setSalesLedgerId(purchaseLedger.getSalesLedgerId());
        resultDto.setSalesContractNoId(purchaseLedger.getSalesLedgerId());
        resultDto.setSalesContractNo(purchaseLedger.getSalesContractNo());
        resultDto.setSupplierName(purchaseLedger.getSupplierName());
@@ -414,6 +493,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) {
@@ -466,6 +546,32 @@
        return purchaseNo + String.format("%03d", sequence);
    }
    @Override
    public PurchaseLedgerDto getPurchaseByCode(PurchaseLedgerDto purchaseLedgerDto) {
        // 1. 查询主表
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>()
                .eq(PurchaseLedger::getPurchaseContractNumber, purchaseLedgerDto.getPurchaseContractNumber())
                .last("LIMIT 1"));
        if (purchaseLedger == null) {
            throw new BaseException("采购台账不存在");
        }
        // 2. 查询子表
        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                .eq(SalesLedgerProduct::getType, 2);
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        // 4. 转换 DTO
        PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
        BeanUtils.copyProperties(purchaseLedger, resultDto);
        if (!products.isEmpty()) {
            resultDto.setHasChildren(true);
            resultDto.setProductData(products);
        }
        return resultDto;
    }
    /**
     * 下划线命名转驼峰命名
     */