|
@@ -1,6 +1,7 @@
|
|
|
using System;
|
|
|
using System.Data;
|
|
|
using System.Linq;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
using System.Threading;
|
|
|
using Library;
|
|
|
using LitJson;
|
|
@@ -40,8 +41,11 @@ namespace MySystem
|
|
|
{
|
|
|
string Title = source.Title; //说明
|
|
|
string TableName = source.TableName; //数据表
|
|
|
+ TableName = MatchExpressionVal(TableName);
|
|
|
string FieldList = source.FieldList; //原始数据字段
|
|
|
string FlagField = source.TargetFieldList; //原始标记字段
|
|
|
+ int StartId = source.QueryId;
|
|
|
+ string Month = source.Month;
|
|
|
string QueryField = "";
|
|
|
JsonData fieldJson = JsonMapper.ToObject(FieldList);
|
|
|
for (int i = 0; i < fieldJson.Count; i++)
|
|
@@ -50,7 +54,19 @@ namespace MySystem
|
|
|
string source_name = item["source_name"].ToString();
|
|
|
if(source_name.StartsWith("v("))
|
|
|
{
|
|
|
- QueryField += source_name.Replace("v(", "").Replace(")", "") + ",";
|
|
|
+ if(source_name.Contains(" from "))
|
|
|
+ {
|
|
|
+ string sqlString = source_name.Replace("v(", "").Replace(")", "");
|
|
|
+ Match m = Regex.Match(sqlString, "\\$\\{.*?\\}");
|
|
|
+ if(m.Success)
|
|
|
+ {
|
|
|
+ QueryField += m.Value.Replace("${", "").Replace("}", "") + ",";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ QueryField += source_name.Replace("v(", "").Replace(")", "") + ",";
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -59,9 +75,8 @@ namespace MySystem
|
|
|
}
|
|
|
|
|
|
//构造抓取数据
|
|
|
- int StartId = source.QueryId;
|
|
|
string sql = "select " + QueryField.TrimEnd(',') + " from " + TableName + " where 1=1";
|
|
|
- var condiList = db.SourceCondition.Where(m => m.SourceId == source.Id).ToList();
|
|
|
+ var condiList = db.SourceCondition.Where(m => m.SourceId == source.Id && m.Status == 1).ToList();
|
|
|
foreach(var condi in condiList)
|
|
|
{
|
|
|
int QueryCondition = condi.QueryCondition;
|
|
@@ -122,6 +137,7 @@ namespace MySystem
|
|
|
}
|
|
|
|
|
|
//创建原始数据表
|
|
|
+ if(TableName.Contains(".")) TableName = TableName.Split('.')[1];
|
|
|
var sourceEdit = db.SourceData.FirstOrDefault(m => m.Id == source.Id && m.Version == 0);
|
|
|
if(sourceEdit != null)
|
|
|
{
|
|
@@ -140,8 +156,8 @@ namespace MySystem
|
|
|
}
|
|
|
string insertSql = "";
|
|
|
sql = sql.Replace("${QueryId}$", StartId.ToString());
|
|
|
- sql += " order by Id limit 10";
|
|
|
- DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.SourceSqlConnStr);
|
|
|
+ sql += " order by id limit 10";
|
|
|
+ DataTable dt = CustomerSqlConn.dtable(sql, AppConfig.Base.JavaStatSqlConnStr);
|
|
|
foreach(DataRow dr in dt.Rows)
|
|
|
{
|
|
|
string values = "";
|
|
@@ -154,7 +170,36 @@ namespace MySystem
|
|
|
{
|
|
|
if(source_name.StartsWith("v("))
|
|
|
{
|
|
|
- values += source_name.Replace("v(", "").Replace(")", "") + ",";
|
|
|
+ if(source_name.Contains(" from "))
|
|
|
+ {
|
|
|
+ string sqlString = source_name.Replace("v(", "").Replace(")", "").Trim('\'');
|
|
|
+ Match m = Regex.Match(sqlString, "\\$\\{.*?\\}");
|
|
|
+ if(m.Success)
|
|
|
+ {
|
|
|
+ string sourceVal = m.Value.Replace("${", "").Replace("}", "");
|
|
|
+ sqlString = sqlString.Replace(m.Value, dr[sourceVal].ToString());
|
|
|
+ DataTable dataTable = CustomerSqlConn.dtable(sqlString, AppConfig.Base.JavaStatSqlConnStr);
|
|
|
+ if(dataTable.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ if(field_type == "int" || field_type == "numeric")
|
|
|
+ {
|
|
|
+ values += "" + dataTable.Rows[0][0].ToString() + ",";
|
|
|
+ }
|
|
|
+ else if(field_type == "datetime")
|
|
|
+ {
|
|
|
+ values += "'" + DateTime.Parse(dataTable.Rows[0][0].ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "',";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ values += "'" + dataTable.Rows[0][0].ToString() + "',";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ values += source_name.Replace("v(", "").Replace(")", "") + ",";
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -174,13 +219,22 @@ namespace MySystem
|
|
|
}
|
|
|
}
|
|
|
insertSql += "insert into u_" + TableName + " (" + TargetQueryField.TrimEnd(',') + ") values (" + values.TrimEnd(',') + ");";
|
|
|
- StartId = int.Parse(dr["Id"].ToString());
|
|
|
- CustomerSqlConn.op("update " + TableName + " set " + FlagField + "=1 where Id=" + StartId, AppConfig.Base.RmSourceSqlConnStr);
|
|
|
+ StartId = int.Parse(dr["id"].ToString());
|
|
|
+ // CustomerSqlConn.op("update " + TableName + " set " + FlagField + "=1 where Id=" + StartId, AppConfig.Base.RmSourceSqlConnStr);
|
|
|
}
|
|
|
var edit = db.SourceData.FirstOrDefault(m => m.Id == source.Id);
|
|
|
if(edit != null)
|
|
|
{
|
|
|
- edit.QueryId = StartId;
|
|
|
+ if(Month != DateTime.Now.ToString("yyyyMM"))
|
|
|
+ {
|
|
|
+ edit.Month = DateTime.Now.ToString("yyyyMM");
|
|
|
+ edit.QueryId = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ edit.QueryId = StartId;
|
|
|
+ if(edit.Month != Month) edit.Month = Month;
|
|
|
+ }
|
|
|
db.SaveChanges();
|
|
|
}
|
|
|
if(!string.IsNullOrEmpty(insertSql))
|
|
@@ -242,5 +296,56 @@ namespace MySystem
|
|
|
sql += ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='" + Title + "';";
|
|
|
CustomerSqlConn.op(sql, AppConfig.Base.SqlConnStr);
|
|
|
}
|
|
|
+
|
|
|
+ public string GetExpressionVal(string str)
|
|
|
+ {
|
|
|
+ if(str == "#{now}#") return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(str == "#{today}#") return DateTime.Now.ToString("yyyy-MM-dd");
|
|
|
+ if(str == "#{this_month}#") return DateTime.Now.ToString("yyyy-MM");
|
|
|
+ if(str.StartsWith("#{now") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(str.StartsWith("#{now") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
|
|
|
+ if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
|
|
|
+ if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) return DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
|
|
|
+ if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) return DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
|
|
|
+ if(str.StartsWith("#{") && str.EndsWith("DAY}#")) return DateTime.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[0]).AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(str.StartsWith("#{") && str.EndsWith("MONTH}#")) return DateTime.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[0]).AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ if(str.StartsWith("#{split") && str.EndsWith("}#"))
|
|
|
+ {
|
|
|
+ string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
|
|
|
+ string text = data[1];
|
|
|
+ string splitTag = data[2];
|
|
|
+ string index = data[3];
|
|
|
+ return text.Split(new string[]{ splitTag }, StringSplitOptions.None)[int.Parse(index)];
|
|
|
+ }
|
|
|
+ else if(str.StartsWith("#{") && str.EndsWith("}#"))
|
|
|
+ {
|
|
|
+ string[] data = str.Replace("#{", "").Replace("}#", "").Split(',');
|
|
|
+ string tag = data[0];
|
|
|
+ string format = data[data.Length - 1];
|
|
|
+ if(tag == "now" && format.StartsWith("yyyy"))
|
|
|
+ {
|
|
|
+ if(data.Length == 2)
|
|
|
+ {
|
|
|
+ return DateTime.Now.ToString(format);
|
|
|
+ }
|
|
|
+ else if(data.Length == 4)
|
|
|
+ {
|
|
|
+ if(data[2] == "DAY") return DateTime.Now.AddDays(int.Parse(data[1])).ToString(format);
|
|
|
+ if(data[2] == "MONTH") return DateTime.Now.AddMonths(int.Parse(data[1])).ToString(format);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ public string MatchExpressionVal(string str)
|
|
|
+ {
|
|
|
+ MatchCollection mc = Regex.Matches(str, "#\\{.*?\\}#");
|
|
|
+ foreach(Match m in mc)
|
|
|
+ {
|
|
|
+ str = str.Replace(m.Value, GetExpressionVal(m.Value));
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
}
|
|
|
}
|