| | |
| | | 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.basic.mapper.CustomerMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | | import com.ruoyi.common.enums.FileNameType; |
| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.production.mapper.SalesLedgerProductionAccountingMapper; |
| | | import com.ruoyi.production.mapper.SalesLedgerSchedulingMapper; |
| | | import com.ruoyi.production.mapper.SalesLedgerWorkMapper; |
| | | import com.ruoyi.production.pojo.SalesLedgerProductionAccounting; |
| | | import com.ruoyi.production.pojo.SalesLedgerScheduling; |
| | | import com.ruoyi.production.pojo.SalesLedgerWork; |
| | | import com.ruoyi.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.sales.dto.MonthlyAmountDto; |
| | |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService { |
| | | private final AccountIncomeService accountIncomeService; |
| | | |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | | |
| | |
| | | private final InvoiceLedgerMapper invoiceLedgerMapper; |
| | | |
| | | private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper; |
| | | |
| | | private final SalesLedgerWorkMapper salesLedgerWorkMapper; |
| | | |
| | | private final SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; |
| | | |
| | | private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | |
| | | private final InvoiceRegistrationMapper invoiceRegistrationMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | |
| | | |
| | | // 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 |
| | |
| | | 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<InvoiceRegistrationProduct> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.in(InvoiceRegistrationProduct::getSalesLedgerId, idList); |
| | | List<InvoiceRegistrationProduct> invoiceRegistrationProducts = invoiceRegistrationProductMapper.selectList(wrapper); |
| | | List<Integer> invoiceLedgerIds = new ArrayList<>(); |
| | | if(CollectionUtils.isNotEmpty(invoiceRegistrationProducts)){ |
| | | LambdaQueryWrapper<InvoiceLedger> wrapperOne = new LambdaQueryWrapper<>(); |
| | | wrapperOne.in(InvoiceLedger::getInvoiceRegistrationProductId, invoiceRegistrationProducts.stream().map(InvoiceRegistrationProduct::getId).collect(Collectors.toList())); |
| | | List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(wrapperOne); |
| | | if(CollectionUtils.isNotEmpty(invoiceLedgers)){ |
| | | invoiceLedgerIds = invoiceLedgers.stream().map(InvoiceLedger::getId).collect(Collectors.toList()); |
| | | } |
| | | invoiceLedgerMapper.delete(wrapperOne); |
| | | } |
| | | invoiceRegistrationProductMapper.delete(wrapper); |
| | | LambdaQueryWrapper<InvoiceRegistration> wrapperTwo = new LambdaQueryWrapper<>(); |
| | | wrapperTwo.in(InvoiceRegistration::getSalesLedgerId, idList); |
| | | invoiceRegistrationMapper.delete(wrapperTwo); |
| | | |
| | | if(CollectionUtils.isNotEmpty(invoiceLedgerIds)){ |
| | | LambdaQueryWrapper<ReceiptPayment> wrapperTree = new LambdaQueryWrapper<>(); |
| | | wrapperTree.in(ReceiptPayment::getInvoiceLedgerId, invoiceLedgerIds); |
| | | receiptPaymentMapper.delete(wrapperTree); |
| | | } |
| | | |
| | | |
| | | // 删除生产管控数据 |
| | | // 删除生产订单数据 |
| | | LambdaQueryWrapper<SalesLedgerScheduling> in = new LambdaQueryWrapper<SalesLedgerScheduling>() |
| | | .in(SalesLedgerScheduling::getSalesLedgerId, idList); |
| | | salesLedgerSchedulingMapper.delete(in); |
| | | // 删除生产派工数据 |
| | | LambdaQueryWrapper<SalesLedgerWork> workOrderWrapper = new LambdaQueryWrapper<>(); |
| | | workOrderWrapper.in(SalesLedgerWork::getSalesLedgerId, idList); |
| | | salesLedgerWorkMapper.delete(workOrderWrapper); |
| | | // 删除生产报工数据 |
| | | LambdaQueryWrapper<SalesLedgerProductionAccounting> reportWrapper = new LambdaQueryWrapper<>(); |
| | | reportWrapper.in(SalesLedgerProductionAccounting::getSalesLedgerId, idList); |
| | | salesLedgerProductionAccountingMapper.delete(reportWrapper); |
| | | // 2. 再删除主表数据 |
| | | return salesLedgerMapper.deleteBatchIds(idList); |
| | | } |
| | |
| | | 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 (!org.springframework.util.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. 处理子表数据 |
| | |
| | | |
| | | 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) |