分表分页查询

\rg3-pf\RG3.DO.Core\DbContext.Execute$DoShred.cs
\rg3-pf\RG3.DO.Core\ShredDbTableProvider.cs
\rg3-pf\RG3.BO.Qu\Services\ListService.cs
\rg3-pf\RG3.BO.Qu\Services\PageService.cs
\rg3-pf\RG3.BO.Qu\Services\DetailService.cs
\rg3-pf\RG3.DO.Core\DbSqlConvertProvider.cs (不同数据库兼容待进一步开发)

  if (shredConfig.ShredMethod == ShredMethod.Snowflake)
                    {
                        //求模方式
                        var snowflake = RegexUtil.GetSnowflakeNumValue(parameters[shredConfig.ShredPrimary].ToString());
                        string tableSuffixTemp = "0";
                        string dbSuffixTemp = "0";
                        try
                        {
                            var dataDateS = GidSnowflakeUtil.Analyze(Convert.ToInt64(snowflake.SnowflakeId)).DataDate;
                            var dataDate = Convert.ToDateTime(dataDateS);

                            if (shredConfig.ShredType== ShredType.Db|| shredConfig.ShredType == ShredType.DbTable)
                            {
                                tableSuffixTemp = dataDate.ToString(shredConfig.ShredTableFormat) ?? "0";
                            }
                            if (shredConfig.ShredType == ShredType.Table || shredConfig.ShredType == ShredType.DbTable)
                            {
                                dbSuffixTemp = dataDate.ToString(shredConfig.ShredDbFormat) ?? "0";
                            }
                        }
                        catch (Exception e)
                        {
                            tableSuffixTemp = "0";
                            dbSuffixTemp = "0";
                        }

                        if (shredConfig.ShredType == ShredType.Db || shredConfig.ShredType == ShredType.DbTable)
                        {
                            lstGroupDb.Add(dbSuffixTemp);
                            parameters["dbSuffixTemp"] = dbSuffixTemp;
                        }
                        if (shredConfig.ShredType == ShredType.Table || shredConfig.ShredType == ShredType.DbTable)
                        {

                            lstGroupTable.Add(tableSuffixTemp);
                            parameters["tableSuffixTemp"] = tableSuffixTemp;
                        }

                    }
                    else if (shredConfig.ShredMethod == ShredMethod.Date)
                    {
                        var dataDate = Convert.ToDateTime(parameters[shredConfig.ShredColumn]);

                        if (shredConfig.ShredType == ShredType.Db || shredConfig.ShredType == ShredType.DbTable)
                        {
                            string dbSuffixTemp = dataDate.ToString(shredConfig.ShredDbFormat);
                            lstGroupDb.Add(dbSuffixTemp);
                            parameters["dbSuffixTemp"] = dbSuffixTemp;
                        }
                        if (shredConfig.ShredType == ShredType.Table || shredConfig.ShredType == ShredType.DbTable)
                        {
                            string tableSuffixTemp = dataDate.ToString(shredConfig.ShredTableFormat);

                            lstGroupTable.Add(tableSuffixTemp);
                            parameters["tableSuffixTemp"] = tableSuffixTemp;
                        }
                    }
                    else if (shredConfig.ShredMethod == ShredMethod.Mod)
                    {
                        //求模方式

                        string tableSuffixTemp = "0";
                        string dbSuffixTemp = "0";
                        try
                        {
                            var snowflake = RegexUtil.GetSnowflakeNumValue(parameters[shredConfig.ShredPrimary].ToString());
                            if (shredConfig.ShredType == ShredType.Db || shredConfig.ShredType == ShredType.DbTable)
                            {
                                dbSuffixTemp = (Convert.ToInt64(snowflake.UsedId) % Convert.ToInt32(shredConfig.ShredModDbNum)).ToString();
                            }
                            if (shredConfig.ShredType == ShredType.Table || shredConfig.ShredType == ShredType.DbTable)
                            {
                                tableSuffixTemp = (Convert.ToInt64(snowflake.UsedId) % Convert.ToInt32(shredConfig.ShredModTableNum)).ToString();
                            }
                        }
                        catch (Exception e)
                        {
                            tableSuffixTemp = "0";
                            dbSuffixTemp = "0";
                        }

                        if (shredConfig.ShredType == ShredType.Db || shredConfig.ShredType == ShredType.DbTable)
                        {
                            lstGroupDb.Add(dbSuffixTemp);
                            parameters["dbSuffixTemp"] = dbSuffixTemp;
                        }
                        if (shredConfig.ShredType == ShredType.Table || shredConfig.ShredType == ShredType.DbTable)
                        {
                            lstGroupTable.Add(tableSuffixTemp);
                            parameters["tableSuffixTemp"] = tableSuffixTemp;
                        }
                    }

                    if (parameters.ContainsKey("tableSuffixTemp"))
                    {
                        //把数据添加到组中
                        string groupTableKey = parameters["tableSuffixTemp"].ToString();

                        if (!lstGroupTableV.ContainsKey(groupTableKey))
                        {
                            lstGroupTableV.Add(groupTableKey, new List<Dictionary<string, object>>());

                            lstTableSuffixs.Add(groupTableKey);
                        }
                        lstGroupTableV[groupTableKey].Add(parameters);
                    }
                    if (parameters.ContainsKey("dbSuffixTemp"))
                    {
                        //把数据添加到组中
                        string groupDbKey = parameters["dbSuffixTemp"].ToString();

                        if (!lstGroupDbV.ContainsKey(groupDbKey))
                        {
                            lstGroupDbV.Add(groupDbKey, new List<Dictionary<string, object>>());

                            lstDbSuffixs.Add(groupDbKey);
                        }
                        lstGroupDbV[groupDbKey].Add(parameters);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"异常索引【{i}】");
                    throw e;
                }
            }
            #endregion 设置组信息 start




            #region 获取建库语句
            var tableMapper = _mapperProvider.GetMapper(pfParams, shredConfig.ShredTableMKey);
            #endregion


            #region 如果不存在建库表

            //foreach (var suffix in lstTableSuffixs.Distinct())
            //{
            //    int result = _dbInvoker.ExecuteInvoker(pfParams, connectionO, tableMapper.Sql.Replace("@suffix", suffix), new { });

            //    Console.WriteLine($"建表结果【{result}】【{suffix}】");
            //}

            #endregion


            #region 获取建表脚本
            //var tableMapper = _mapperProvider.GetMapper(pfParams, shredConfig.ShredTableMKey);
            #endregion


            #region 如果不存在创建表

            foreach (var suffix in lstTableSuffixs.Distinct())
            {
                int result = _dbInvoker.ExecuteInvoker(pfParams, connectionO, tableMapper.Sql.Replace("@suffix", suffix), new { });

                Console.WriteLine($"建表结果【{result}】【{suffix}】");
            }

            #endregion

分表分页配置

  "shredConfig": {
    "connName": "ConnOpLog",
    //原始库名  用于告知第三方处理库 以及查询时候逻辑处理
    "dbName": null,
    //原始表名 用于告知第三方处理库  以及查询时候逻辑处理
    "tableName": "bo_op_log",
    // 存储如  mkey 或者 mapperKey  ,  数据在 db.provider.shreding.json
    "shredKey": null,
    //切片列   如时间列、自增列、数值列
    "shredColumn": "create_date",
    //主键列   自增列、雪花算法列
    "shredPrimary": "log_id",
    "shredPrimaryField": "logId",
    //切片类型  Date  Snowflake Mod  Unknown 不切片
    "shredMethod": "Snowflake",
    // 分表 时间格式
    "shredTableFormat": "yyyyMMdd",
    // 分库 时间格式
    "shredDbFormat": "yyyyMM",
    // 分表  完整的表名
    "shredTableDatabaseFormat": "@tableName_@shredTableFormat",
    //切片数   求模切片
    "shredModTableNum": null,
    //切片数   求模切片
    "shredModDbNum": null,
    //切片规则 表   year  month  day   mod(求模)
    //"shredTableRule": null,
    //切片规则 库   year  month  day   mod(求模)
    //"shredDbRule": null,
    //切片类型   db-table     分库-分表  DbTable  Db  Table Unknown 不切片
    "shredType": "Table"
  },
  "selectSql": {
    "order": "create_date desc"
  },
文档更新时间: 2023-12-02 09:35   作者:admin