Foxtable(狐表)用户栏目专家坐堂 → 求代码优化思路,谢谢!


  共有177人关注过本帖树形打印复制链接

主题:求代码优化思路,谢谢!

帅哥哟,离线,有人找我吗?
listfor
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:182 积分:1658 威望:0 精华:0 注册:2017/11/6 17:23:00
  发帖心情 Post By:2025/2/17 20:31:00 [显示全部帖子]

If r IsNot Nothing Then
    Forms("SL_销售退货选择").Show()
    
    If PublicJson IsNot Nothing Then
        Try
            ' 提前获取表格引用减少重复访问
            Dim tableName As String = eForm.Name & "_Table1"
            Dim targetTable As Table = Tables(tableName)
            
            Dim jsonArray As JArray = CType(PublicJson, JArray)
            If jsonArray.Count = 0 Then Exit Try

            ' 预定义字段映射关系(可根据实际情况扩展)
            Dim fieldMappings As New Dictionary(Of String, String) From {
                {"产品编号", "产品编号"},
                {"产品名称", "产品名称"},
                {'...其他字段...', "..."} ' 实际使用时需补全
            }

            ' 批量操作前准备
            targetTable.BeginUpdate()
            
            For Each item As JToken In jsonArray
                Dim salesOrderGuid As String = CStr(item("Guid_销售订单"))
                
                ' 检查重复行
                If targetTable.FindRow($"Guid_销售订单='{salesOrderGuid}'") <> -1 Then Continue For
                
                ' 添加新行
                Dim newRow As Row = targetTable.AddNew()
                
                ' 关键字段赋值
                newRow("编号") = r("编号")
                newRow("销售退货单明细ID") = Guid.NewGuid().ToString()
                
                ' 使用循环处理相同逻辑的字段赋值
                For Each mapping In fieldMappings
                    newRow(mapping.Key) = CStr(item(mapping.Value))
                Next
                
                ' 特殊处理字段
                newRow("数量") = CStr(item("实际出库数量"))
                '...其他特殊字段处理...
            Next

            ' 提交修改
            targetTable.EndUpdate()
            targetTable.Save()
            targetTable.AutoSizeCols()

        Catch ex As Exception
            ' 添加异常处理
            MessageBox.Show($"数据加载失败:{ex.Message}")
            ' 记录日志
            ' LogError(ex)
        End Try
    End If
End If

 回到顶部