| | |
| | | package com.ruoyi.sales.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | |
| | | 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.common.utils.StringUtils; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | | import com.ruoyi.other.pojo.TempFile; |
| | | import com.ruoyi.production.mapper.SalesLedgerSchedulingMapper; |
| | | import com.ruoyi.production.pojo.SalesLedgerScheduling; |
| | | import com.ruoyi.production.mapper.*; |
| | | import com.ruoyi.production.pojo.*; |
| | | import com.ruoyi.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.quality.mapper.QualityInspectMapper; |
| | | import com.ruoyi.sales.dto.MonthlyAmountDto; |
| | | import com.ruoyi.sales.dto.SalesLedgerDto; |
| | | import com.ruoyi.sales.mapper.*; |
| | |
| | | private final InvoiceLedgerMapper invoiceLedgerMapper; |
| | | |
| | | private final SalesLedgerSchedulingMapper salesLedgerSchedulingMapper; |
| | | |
| | | private final SalesLedgerWorkMapper salesLedgerWorkMapper; |
| | | |
| | | private final SalesLedgerProductionAccountingMapper salesLedgerProductionAccountingMapper; |
| | | |
| | | private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | |
| | | private final InvoiceRegistrationMapper invoiceRegistrationMapper; |
| | | |
| | | private final ProductOrderMapper productOrderMapper; |
| | | |
| | | private final ProcessRouteMapper processRouteMapper; |
| | | |
| | | private final ProcessRouteItemMapper processRouteItemMapper; |
| | | |
| | | private final ProductProcessRouteItemMapper productProcessRouteItemMapper; |
| | | |
| | | private final ProductWorkOrderMapper productWorkOrderMapper; |
| | | |
| | | private final ProductionProductMainMapper productionProductMainMapper; |
| | | |
| | | private final ProductionProductOutputMapper productionProductOutputMapper; |
| | | |
| | | private final ProductionProductInputMapper productionProductInputMapper; |
| | | |
| | | private final QualityInspectMapper qualityInspectMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | |
| | | if (CollectionUtils.isEmpty(idList)) { |
| | | return 0; |
| | | } |
| | | // 生产订单有待排产数据,台账不可删除 |
| | | LambdaQueryWrapper<SalesLedgerScheduling> salesLedgerSchedulingLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerScheduling>() |
| | | .in(SalesLedgerScheduling::getSalesLedgerId, idList); |
| | | if (salesLedgerSchedulingMapper.selectCount(salesLedgerSchedulingLambdaQueryWrapper) > 0) { |
| | | throw new BaseException("有排产数据,不可删除"); |
| | | // 删除销售管理数据 |
| | | LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.in(SalesLedgerProduct::getSalesLedgerId, idList) |
| | | .select(SalesLedgerProduct::getId); |
| | | |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(queryWrapper); |
| | | List<Long> productIds = products.stream() |
| | | .map(SalesLedgerProduct::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | //批量查询productOrder |
| | | List<ProductOrder> productOrders = productOrderMapper.selectList( |
| | | new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getProductModelId, productIds) |
| | | ); |
| | | |
| | | if (!org.springframework.util.CollectionUtils.isEmpty(productOrders)) { |
| | | List<Long> orderIds = productOrders.stream() |
| | | .map(ProductOrder::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 批量查询processRouteItems |
| | | List<ProductProcessRouteItem> allRouteItems = productProcessRouteItemMapper.selectList( |
| | | new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getRouteId, orderIds) |
| | | ); |
| | | |
| | | if (!CollectionUtils.isEmpty(allRouteItems)) { |
| | | // 获取要删除的工序项ID |
| | | List<Long> routeItemIds = allRouteItems.stream() |
| | | .map(ProductProcessRouteItem::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的工单ID |
| | | List<ProductWorkOrder> workOrders = productWorkOrderMapper.selectList( |
| | | new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds) |
| | | ); |
| | | if (!CollectionUtils.isEmpty(workOrders)) { |
| | | List<Long> workOrderIds = workOrders.stream() |
| | | .map(ProductWorkOrder::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 查询关联的生产主表ID |
| | | List<ProductionProductMain> productMains = productionProductMainMapper.selectList( |
| | | new LambdaQueryWrapper<ProductionProductMain>() |
| | | .in(ProductionProductMain::getWorkOrderId, workOrderIds) |
| | | ); |
| | | List<Long> productMainIds = productMains.stream() |
| | | .map(ProductionProductMain::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 删除产出表、投入表数据 |
| | | if (!CollectionUtils.isEmpty(productMainIds)) { |
| | | productionProductOutputMapper.deleteByProductMainIds(productMainIds); |
| | | productionProductInputMapper.deleteByProductMainIds(productMainIds); |
| | | qualityInspectMapper.deleteByProductMainIds(productMainIds); |
| | | } |
| | | |
| | | // 删除生产主表数据 |
| | | productionProductMainMapper.deleteByWorkOrderIds(workOrderIds); |
| | | |
| | | // 删除工单数据 |
| | | productWorkOrderMapper.delete(new LambdaQueryWrapper<ProductWorkOrder>() |
| | | .in(ProductWorkOrder::getProductProcessRouteItemId, routeItemIds)); |
| | | } |
| | | } |
| | | // 批量删除processRouteItem |
| | | productProcessRouteItemMapper.delete(new LambdaQueryWrapper<ProductProcessRouteItem>() |
| | | .in(ProductProcessRouteItem::getRouteId, orderIds)); |
| | | |
| | | // 批量删除productOrder |
| | | productOrderMapper.delete(new LambdaQueryWrapper<ProductOrder>() |
| | | .in(ProductOrder::getProductModelId, productIds)); |
| | | } |
| | | // 1. 先删除子表数据 |
| | | LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>(); |
| | | productWrapper.in(SalesLedgerProduct::getSalesLedgerId, idList); |
| | | salesLedgerProductMapper.delete(productWrapper); |
| | | |
| | | // 批量删除产品子表 |
| | | if (!productIds.isEmpty()) { |
| | | salesLedgerProductMapper.deleteBatchIds(productIds); |
| | | } |
| | | |
| | | 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); |
| | | } |
| | |
| | | salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity()); |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | |
| | | ProductOrder productOrder = new ProductOrder(); |
| | | productOrder.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId()); |
| | | productOrder.setProductModelId(salesLedgerProduct.getId()); |
| | | productOrder.setNpsNo("SC" + String.format("%08d", salesLedgerProduct.getId())); |
| | | productOrderMapper.insert(productOrder); |
| | | |
| | | ProcessRoute processRoute = processRouteMapper.selectOne(new QueryWrapper<ProcessRoute>().lambda().eq(ProcessRoute::getProductModelId, salesLedgerProduct.getProductModelId())); |
| | | if (processRoute != null) { |
| | | List<ProcessRouteItem> processRouteItems = processRouteItemMapper.selectList(new QueryWrapper<ProcessRouteItem>().lambda().eq(ProcessRouteItem::getRouteId, processRoute.getId())); |
| | | // 生成当前日期的前缀:年月日 |
| | | String datePrefix = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | int dragSort = 1; |
| | | for (ProcessRouteItem processRouteItem : processRouteItems) { |
| | | ProductProcessRouteItem productProcessRouteItem = new ProductProcessRouteItem(); |
| | | productProcessRouteItem.setProductModelId(processRouteItem.getProductModelId()); |
| | | productProcessRouteItem.setProcessId(processRouteItem.getProcessId()); |
| | | productProcessRouteItem.setRouteId(productOrder.getId()); |
| | | productProcessRouteItem.setDragSort(dragSort); |
| | | int insert = productProcessRouteItemMapper.insert(productProcessRouteItem); |
| | | if (insert > 0) { |
| | | // 查询今日已存在的最大工单号 |
| | | QueryWrapper<ProductWorkOrder> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.likeRight("work_order_no", datePrefix) |
| | | .orderByDesc("work_order_no") |
| | | .last("LIMIT 1"); |
| | | |
| | | ProductWorkOrder lastWorkOrder = productWorkOrderMapper.selectOne(queryWrapper); |
| | | |
| | | int sequenceNumber = 1; // 默认序号 |
| | | if (lastWorkOrder != null && lastWorkOrder.getWorkOrderNo() != null) { |
| | | String lastNo = lastWorkOrder.getWorkOrderNo().toString(); |
| | | if (lastNo.startsWith(datePrefix)) { |
| | | String seqStr = lastNo.substring(datePrefix.length()); |
| | | try { |
| | | sequenceNumber = Integer.parseInt(seqStr) + 1; |
| | | } catch (NumberFormatException e) { |
| | | sequenceNumber = 1; |
| | | } |
| | | } |
| | | } |
| | | // 生成完整的工单号 |
| | | String workOrderNoStr = String.format("%s%03d", datePrefix, sequenceNumber); |
| | | ProductWorkOrder productWorkOrder = new ProductWorkOrder(); |
| | | productWorkOrder.setProductProcessRouteItemId(productProcessRouteItem.getId()); |
| | | productWorkOrder.setProductOrderId(productOrder.getId()); |
| | | productWorkOrder.setPlanQuantity(salesLedgerProduct.getQuantity()); |
| | | productWorkOrder.setWorkOrderNo(workOrderNoStr); |
| | | productWorkOrder.setStatus(1); |
| | | productWorkOrderMapper.insert(productWorkOrder); |
| | | } |
| | | dragSort++; |
| | | } |
| | | productOrder.setRouteId(processRoute.getId()); |
| | | productOrderMapper.updateById(productOrder); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | return datePart + String.format("%03d", nextSequence); |
| | | } finally { |
| | | // 3. 释放锁(使用Lua脚本保证原子性,避免误删其他线程的锁) |
| | | // 3. 释放锁 |
| | | String luaScript = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end"; |
| | | redisTemplate.execute( |
| | | new DefaultRedisScript<>(luaScript, Long.class), |
| | | Collections.singletonList(lockKey), |
| | | lockValue // 只有持有相同值的线程才能删除锁 |
| | | lockValue |
| | | ); |
| | | } |
| | | } |
| | |
| | | if (sequences.isEmpty()) { |
| | | return 1; |
| | | } |
| | | // 排序后查找第一个缺失的正整数(与原逻辑一致) |
| | | // 排序后查找第一个缺失的正整数 |
| | | sequences.sort(Integer::compareTo); |
| | | int next = 1; |
| | | for (int seq : sequences) { |
| | |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // 构造主表更新对象(支持任意主表类型) |
| | | // 构造主表更新对象 |
| | | try { |
| | | S entity = mainEntityClass.getDeclaredConstructor().newInstance(); |
| | | Field idField = mainEntityClass.getDeclaredField("id"); |
| | | idField.setAccessible(true); |
| | | idField.set(entity, mainId); |
| | | |
| | | // 设置 contractAmount 字段,注意这里假设字段名为 "contractAmount" |
| | | Field amountField = mainEntityClass.getDeclaredField("contractAmount"); |
| | | amountField.setAccessible(true); |
| | | amountField.set(entity, totalAmount); |