| | |
| | | |
| | | @Override |
| | | public int addOrUpdateRegistration(TicketRegistrationDto ticketRegistrationDto) throws IOException { |
| | | // 1. 查询采购台账记录 |
| | | PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(ticketRegistrationDto.getPurchaseLedgerId()); |
| | | if (purchaseLedger == null) { |
| | | // 处理采购台账不存在的情况,例如抛出异常或返回错误 |
| | | throw new IllegalArgumentException("采购台账记录不存在,ID: " + ticketRegistrationDto.getPurchaseLedgerId()); |
| | | } |
| | | |
| | | // 2. 查询是否已存在票据登记记录 |
| | | TicketRegistration existingRecord = ticketRegistrationMapper.selectOne( |
| | | new LambdaQueryWrapper<TicketRegistration>() |
| | | .eq(TicketRegistration::getPurchaseLedgerId, ticketRegistrationDto.getPurchaseLedgerId()) |
| | | ); |
| | | |
| | | // 3. 创建或更新票据登记实体 |
| | | TicketRegistration ticketRegistration = new TicketRegistration(); |
| | | BeanUtils.copyProperties(ticketRegistrationDto, ticketRegistration); |
| | | if (existingRecord != null){ |
| | | ticketRegistration.setId(existingRecord.getId()); |
| | | } |
| | | ticketRegistration.setPurchaseContractNumber(purchaseLedger.getPurchaseContractNumber()); |
| | | ticketRegistration.setTenantId(purchaseLedger.getTenantId()); |
| | | ticketRegistration.setContractAmount(purchaseLedger.getContractAmount()); |
| | | // 处理子表数据 |
| | | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productData = ticketRegistrationDto.getProductData(); |
| | | if (productData != null && !productData.isEmpty()) { |
| | | if (CollectionUtils.isNotEmpty(productData)) { |
| | | handleSalesLedgerProducts(purchaseLedger.getId(), productData, 2); |
| | | } |
| | | |
| | | // 执行插入或更新操作 |
| | | int i; |
| | | if (ticketRegistrationDto.getId() == null) { |
| | | i = ticketRegistrationMapper.insert(ticketRegistration); |
| | | } else { |
| | | i = ticketRegistrationMapper.updateById(ticketRegistration); |
| | | } |
| | | // 5. 执行插入或更新操作 |
| | | int rowsAffected = existingRecord != null |
| | | ? ticketRegistrationMapper.updateById(ticketRegistration) |
| | | : ticketRegistrationMapper.insert(ticketRegistration); |
| | | |
| | | // 迁移临时文件到正式目录 |
| | | if (ticketRegistrationDto.getTempFileIds() != null && !ticketRegistrationDto.getTempFileIds().isEmpty()) { |
| | | migrateTempFilesToFormal(ticketRegistration.getId(), ticketRegistrationDto.getTempFileIds()); |
| | | } |
| | | return i; |
| | | return rowsAffected; |
| | | } |
| | | |
| | | |
| | |
| | | // 批量更新(需要 MyBatis 提供批量更新方法) |
| | | if (!updateList.isEmpty()) { |
| | | updateList.forEach(product -> { |
| | | product.setFutureTickets(product.getQuantity().subtract(new BigDecimal(product.getTicketsNum())).longValue()); |
| | | product.setFutureTicketsAmount(product.getTaxExclusiveTotalPrice().subtract(product.getTicketsAmount())); |
| | | // 非空校验,任一字段为空则抛出异常 |
| | | if (product.getQuantity() == null) { |
| | | throw new BaseException("数量不能为空"); |
| | | } |
| | | if (product.getTicketsNum() == null) { |
| | | throw new BaseException("已开票数量不能为空"); |
| | | } |
| | | if (product.getTaxExclusiveTotalPrice() == null) { |
| | | throw new BaseException("不含税总价不能为空"); |
| | | } |
| | | if (product.getTicketsAmount() == null) { |
| | | throw new BaseException("本次来票金额(元)不能为空"); |
| | | } |
| | | |
| | | // 计算 futureTickets(直接使用 BigDecimal 计算,避免精度丢失) |
| | | product.setFutureTickets( |
| | | product.getQuantity() |
| | | .subtract(BigDecimal.valueOf(product.getTicketsNum())) |
| | | .longValueExact() // 使用 exact 方法确保无小数部分 |
| | | ); |
| | | |
| | | // 计算 futureTicketsAmount |
| | | product.setFutureTicketsAmount( |
| | | product.getTaxExclusiveTotalPrice() |
| | | .subtract(product.getTicketsAmount()) |
| | | ); |
| | | product.setType(type); |
| | | salesLedgerProductMapper.updateById(product); |
| | | }); |