| | |
| | | package com.ruoyi.sales.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | 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.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import com.ruoyi.sales.mapper.SalesLedgerContractRelMapper; |
| | | import com.ruoyi.sales.pojo.SalesLedgerContractRel; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | /** |
| | | * 销售台账Service业务层处理 |
| | |
| | | private final FileUtil fileUtil; |
| | | private final AccountInvoiceApplicationMapper accountInvoiceApplicationMapper; |
| | | private final AccountSalesCollectionMapper accountSalesCollectionMapper; |
| | | private final SalesLedgerContractRelMapper salesLedgerContractRelMapper; |
| | | |
| | | @Autowired |
| | | private SysDeptMapper sysDeptMapper; |
| | |
| | | SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId()); |
| | | if (salesLedger == null) { |
| | | throw new BaseException("台账不存在"); |
| | | } |
| | | |
| | | // 查询关联的合同 |
| | | List<SalesLedgerContractRel> rels = salesLedgerContractRelMapper.selectList( |
| | | new LambdaQueryWrapper<SalesLedgerContractRel>().eq(SalesLedgerContractRel::getSalesLedgerId, salesLedger.getId()) |
| | | ); |
| | | if (!CollectionUtils.isEmpty(rels)) { |
| | | List<Long> contractIds = rels.stream().map(SalesLedgerContractRel::getContractId).collect(Collectors.toList()); |
| | | salesLedger.setAssociatedContractIds(contractIds); |
| | | } |
| | | |
| | | // 2. 查询子表 |
| | |
| | | salesLedger.setContractAmount(salesLedgerProductImportDtos.stream() |
| | | .map(SalesLedgerProductImportDto::getTaxInclusiveTotalPrice) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | salesLedger.setNetContractAmount(salesLedger.getContractAmount()); |
| | | salesLedgerMapper.insert(salesLedger); |
| | | |
| | | |
| | |
| | | // 删除发货台账记录 |
| | | List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>() |
| | | .in(ShippingInfo::getSalesLedgerId, idList)); |
| | | if (CollectionUtils.isNotEmpty(shippingInfos)) { |
| | | if (CollUtil.isNotEmpty(shippingInfos)) { |
| | | shippingInfoServiceImpl.delete(shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList())); |
| | | } |
| | | // 删除附件表 |
| | |
| | | // 删除生产管控数据 |
| | | //查询生产报工id |
| | | ArrayList<Long> mainIdList = productionProductMainService.listMain(idList); |
| | | if (CollectionUtils.isNotEmpty(mainIdList)) { |
| | | if (CollUtil.isNotEmpty(mainIdList)) { |
| | | mainIdList.stream().forEach(mainId -> { |
| | | productionProductMainService.removeProductMain(mainId); |
| | | }); |
| | |
| | | salesLedgerMapper.updateById(salesLedger); |
| | | } |
| | | |
| | | // 处理关联合同数据 |
| | | salesLedgerContractRelMapper.delete( |
| | | new LambdaQueryWrapper<SalesLedgerContractRel>().eq(SalesLedgerContractRel::getSalesLedgerId, salesLedger.getId()) |
| | | ); |
| | | if (!CollectionUtils.isEmpty(salesLedgerDto.getAssociatedContractIds())) { |
| | | for (Long contractId : salesLedgerDto.getAssociatedContractIds()) { |
| | | SalesLedgerContractRel rel = new SalesLedgerContractRel(); |
| | | rel.setSalesLedgerId(salesLedger.getId()); |
| | | rel.setContractId(contractId); |
| | | salesLedgerContractRelMapper.insert(rel); |
| | | } |
| | | } |
| | | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = salesLedgerDto.getProductData(); |
| | |
| | | ); |
| | | } |
| | | |
| | | // 5. 保存销售台账附件 |
| | | // 5. 兜底:确保实际合同金额有初始值等于合同金额 |
| | | if (salesLedger.getNetContractAmount() == null) { |
| | | SalesLedger dbLedger = salesLedgerMapper.selectById(salesLedger.getId()); |
| | | if (dbLedger != null && dbLedger.getNetContractAmount() == null && dbLedger.getContractAmount() != null) { |
| | | SalesLedger updateEntity = new SalesLedger(); |
| | | updateEntity.setId(salesLedger.getId()); |
| | | updateEntity.setNetContractAmount(dbLedger.getContractAmount()); |
| | | salesLedgerMapper.updateById(updateEntity); |
| | | } |
| | | } |
| | | |
| | | // 6. 保存销售台账附件 |
| | | fileUtil.saveStorageAttachment(ApplicationTypeEnum.FILE, RecordTypeEnum.SALES_LEDGER, salesLedger.getId(), salesLedgerDto.getStorageBlobDTOs()); |
| | | |
| | | return 1; |
| | |
| | | amountField.setAccessible(true); |
| | | amountField.set(entity, totalAmount); |
| | | |
| | | // 实际合同金额初始值等于合同金额 |
| | | try { |
| | | Field netAmountField = mainEntityClass.getDeclaredField("netContractAmount"); |
| | | netAmountField.setAccessible(true); |
| | | netAmountField.set(entity, totalAmount); |
| | | } catch (NoSuchFieldException ignored) { |
| | | // 子类可能没有该字段,忽略 |
| | | } |
| | | |
| | | mainMapper.updateById(entity); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("动态更新主表金额失败", e); |