huminmin
4 天以前 4f74481c9091b217bf63e3eb8fb014ea39022644
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;
@@ -62,6 +68,7 @@
@RequiredArgsConstructor
@Slf4j
public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService {
    private final AccountIncomeService accountIncomeService;
    private final SalesLedgerMapper salesLedgerMapper;
@@ -78,6 +85,8 @@
    private final InvoiceLedgerMapper invoiceLedgerMapper;
    private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper;
    private final IApproveProcessService approveProcessService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
@@ -120,7 +129,8 @@
        // 3.查询上传文件
        LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
        salesLedgerFileWrapper.eq(CommonFile::getCommonId, salesLedger.getId());
        salesLedgerFileWrapper.eq(CommonFile::getCommonId, salesLedger.getId())
                .eq(CommonFile::getType, FileNameType.SALE.getValue());
        List<CommonFile> salesLedgerFiles = commonFileMapper.selectList(salesLedgerFileWrapper);
        // 4. 转换 DTO
@@ -357,7 +367,27 @@
        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);
    }
@@ -376,13 +406,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. 处理子表数据
@@ -402,10 +457,55 @@
            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));
                //6是销售台账
                approveProcessVO.setApproveType(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;
    }
    // 文件迁移方法
@@ -452,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)