private int AddNewstoDB(NewsModels newsModels, string dateTimeStr, string jsonStr, string cid, string cName, string type)
{
bool flag = false;
string errorMessage = string.Empty;
int num = 0;
int num2 = 0;
int num3 = 1;
foreach (NewsModel model in newsModels.news)
{
model.title = HttpHelper.GetHtmlTxt(model.title).Replace("br", ",");
if (string.IsNullOrEmpty(model.title))
{
base.BeginInvoke(this.logInvoke, new object[] { $"取消当前新闻插入:{"标题信息为空"}" });
flag = true;
errorMessage = "标题信息为空";
}
else
{
string str2;
string str3;
string str4;
int num4;
if ((cid.Equals("20") || cid.Equals("21")) || cid.Equals("22"))
{
if (string.IsNullOrEmpty(model.url))
{
goto Label_042F;
}
if (string.IsNullOrEmpty(model.photo))
{
model.photo = "http://es.whu.edu.cn/hbjc/image/noImage.png";
}
model.content = $"<img src="{model.photo}" title="{model.url}" />";
num3 = 3;
}
else
{
this.ParseImage(model);
}
if (string.IsNullOrEmpty(model.content))
{
if (string.IsNullOrEmpty(model.url))
{
goto Label_042F;
}
model.content = $"<head> <meta http-equiv="refresh" content="0;{model.url}"> </head>";
}
if (type.Contains("Normal"))
{
str2 = "select count(*) from articleInfo where sid =@sid and cid=@cid";
str3 = "UPDATE [articleInfo] SET [articleTitle] = @articleTitle, [articleContent] = @articleContent, [articleType] = @articleType, [cid] = @cid, [articleTime] = @articleTime, [articleAuthor] = @articleAuthor, [uid] = @uid, [articleSource]= @articleSource, [sid] = @sid WHERE sid =@sid and cid=@cid";
str4 = "INSERT INTO [articleInfo] ([articleTitle],[articleContent],[articleType],[cid],[articleTime],[articleAuthor],[uid],[articleSource],[sid]) VALUES(@articleTitle,@articleContent,@articleType,@cid,@articleTime,@articleAuthor,@uid,@articleSource,@sid)";
}
else
{
str2 = "select count(*) from subArticle where sid =@sid and cid=@cid";
str3 = "UPDATE [subArticle] SET [articleTitle] = @articleTitle, [articleContent] = @articleContent, [articleType] = @articleType, [cid] = @cid, [articleTime] = @articleTime, [articleAuthor] = @articleAuthor, [uid] = @uid, [articleSource]= @articleSource, [sid] = @sid WHERE sid =@sid and cid=@cid";
str4 = "INSERT INTO [subArticle] ([articleTitle],[articleContent],[articleType],[cid],[articleTime],[articleAuthor],[uid],[articleSource],[sid]) VALUES(@articleTitle,@articleContent,@articleType,@cid,@articleTime,@articleAuthor,@uid,@articleSource,@sid)";
}
object obj2 = SqlHelper.ExecuteScalar(str2, CommandType.Text, new SqlParameter[] { new SqlParameter("@cid", cid), new SqlParameter("@sid", model.id) });
try
{
num4 = Convert.ToInt32(obj2);
}
catch (Exception)
{
num4 = 1;
}
if (num4 > 0)
{
try
{
SqlHelper.ExecuteNonQuery(str3, CommandType.Text, new SqlParameter[] { new SqlParameter("@articleTitle", model.title), new SqlParameter("@articleContent", model.content), new SqlParameter("@articleType", num3), new SqlParameter("@cid", cid), new SqlParameter("@articleTime", Convert.ToDateTime(model.date)), new SqlParameter("@articleAuthor", model.author), new SqlParameter("@uid", 0x3e8), new SqlParameter("@articleSource", model.from), new SqlParameter("@sid", model.id) });
num2++;
goto Label_042F;
}
catch (Exception exception)
{
base.BeginInvoke(this.logInvoke, new object[] { $"更新数据库出现异常:{exception.Message}" });
this.WriteException(exception);
this.WriteErrorNewsJsonFile(exception.Message, dateTimeStr, jsonStr, cid, cName);
goto Label_042F;
}
}
try
{
SqlHelper.ExecuteNonQuery(str4, CommandType.Text, new SqlParameter[] { new SqlParameter("@articleTitle", model.title), new SqlParameter("@articleContent", model.content), new SqlParameter("@articleType", num3), new SqlParameter("@cid", cid), new SqlParameter("@articleTime", Convert.ToDateTime(model.date)), new SqlParameter("@articleAuthor", model.author), new SqlParameter("@uid", 0x3e8), new SqlParameter("@articleSource", model.from), new SqlParameter("@sid", model.id) });
num++;
}
catch (Exception exception2)
{
base.BeginInvoke(this.logInvoke, new object[] { $"插入数据库出现异常:{exception2.Message}" });
this.WriteException(exception2);
this.WriteErrorNewsJsonFile(exception2.Message, dateTimeStr, jsonStr, cid, cName);
}
Label_042F:;
}
}
if (flag)
{
this.WriteErrorNewsJsonFile(errorMessage, dateTimeStr, jsonStr, cid, cName);
}
base.BeginInvoke(this.logInvoke, new object[] { $"[ {cid}.{cName} ] 新增 {num} 条 , 更新 {num2} 条" });
return num;
}
网友评论