| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.mapper.CustomerMapper; |
| | | import com.ruoyi.basic.pojo.Customer; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | |
| | | @Slf4j |
| | | public class SalesLedgerServiceImpl extends ServiceImpl<SalesLedgerMapper, SalesLedger> implements ISalesLedgerService { |
| | | |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | | |
| | | private final CustomerMapper customerMapper; |
| | | private final CustomerMapper customerMapper; |
| | | |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | private final SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | private final SalesLedgerFileMapper salesLedgerFileMapper; |
| | | |
| | |
| | | productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId()); |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper); |
| | | |
| | | // 3. 转换 DTO |
| | | // 3.查询上传文件 |
| | | LambdaQueryWrapper<SalesLedgerFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>(); |
| | | salesLedgerFileWrapper.eq(SalesLedgerFile::getLedgerId, salesLedger.getId()); |
| | | List<SalesLedgerFile> salesLedgerFiles = salesLedgerFileMapper.selectList(salesLedgerFileWrapper); |
| | | |
| | | // 4. 转换 DTO |
| | | SalesLedgerDto resultDto = new SalesLedgerDto(); |
| | | BeanUtils.copyProperties(salesLedger, resultDto); |
| | | if (!products.isEmpty()) { |
| | | resultDto.setHasChildren(true); |
| | | resultDto.setProductData(products); |
| | | resultDto.setSalesLedgerFiles(salesLedgerFiles); |
| | | } |
| | | return resultDto; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getSalesNo() { |
| | | LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.lambdaQuery(); |
| | | queryWrapper.select(SalesLedger::getId, SalesLedger::getSalesContractNo); |
| | | |
| | | // 获取原始查询结果 |
| | | List<Map<String, Object>> result = salesLedgerMapper.selectMaps(queryWrapper); |
| | | |
| | | // 将下划线命名转换为驼峰命名 |
| | | return result.stream().map(map -> map.entrySet().stream() |
| | | .collect(Collectors.toMap( |
| | | entry -> underlineToCamel(entry.getKey()), |
| | | Map.Entry::getValue)) |
| | | ).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 下划线命名转驼峰命名 |
| | | */ |
| | | private String underlineToCamel(String param) { |
| | | if (param == null || "".equals(param.trim())) { |
| | | return ""; |
| | | } |
| | | int len = param.length(); |
| | | StringBuilder sb = new StringBuilder(len); |
| | | for (int i = 0; i < len; i++) { |
| | | char c = param.charAt(i); |
| | | if (c == '_') { |
| | | if (++i < len) { |
| | | sb.append(Character.toUpperCase(param.charAt(i))); |
| | | } |
| | | } else { |
| | | sb.append(Character.toLowerCase(c)); |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = salesLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList); |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList,salesLedgerDto.getType()); |
| | | updateMainContractAmount( |
| | | salesLedger.getId(), |
| | | productList, |
| | |
| | | // 查询临时文件记录 |
| | | TempFile tempFile = tempFileMapper.selectById(tempFileId); |
| | | if (tempFile == null) { |
| | | throw new FileNotFoundException("临时文件不存在: " + tempFileId); |
| | | log.warn("临时文件不存在,跳过处理: {}", tempFileId); |
| | | continue; |
| | | } |
| | | |
| | | // 构建正式文件名(包含业务ID和时间戳,避免冲突) |
| | |
| | | // 更新文件记录(关联到业务ID) |
| | | SalesLedgerFile fileRecord = new SalesLedgerFile(); |
| | | fileRecord.setLedgerId(businessId); |
| | | fileRecord.setFileName(originalFilename); |
| | | fileRecord.setFilePath(formalFilePath.toString()); |
| | | fileRecord.setName(originalFilename); |
| | | fileRecord.setUrl(formalFilePath.toString()); |
| | | fileRecord.setCreateTime(LocalDateTime.now()); |
| | | salesLedgerFileMapper.insert(fileRecord); |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products) { |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products,Integer type) { |
| | | // 按ID分组,区分新增和更新的记录 |
| | | Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream() |
| | | .peek(p -> p.setSalesLedgerId(salesLedgerId)) |
| | |
| | | // 执行更新操作 |
| | | if (!updateList.isEmpty()) { |
| | | for (SalesLedgerProduct product : updateList) { |
| | | product.setType(type); |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | } |
| | | // 执行插入操作 |
| | | if (!insertList.isEmpty()) { |
| | | for (SalesLedgerProduct salesLedgerProduct : insertList) { |
| | | salesLedgerProduct.setType(type); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | } |
| | | } |