namespace WDFROG.BLL
{
public delegate void CreateHtmlHandler(string fileName,string msg,int percent );
public class News
{
private const string C_ERR_MSG = "You have no rights!";
private const string C_CHK_NAME = "NewsManage";
private static string C_DIR = ConfigurationManager.AppSettings["NewsHtml"];
public event CreateHtmlHandler CompleteAFile;
public static int Create(string AdminName,NewsInfo news)
{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
news.HtmlFile =FileOperator.GetRandFileName(".html");
int ret= NewsDAL.Create(news);
CreateHtmlFile(news);
return ret;
}
public static int DeleteByIdList(string AdminName,string IdList)
{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
DataTable dt = SelectHtmlFileByIdList(IdList).Tables[0];
string fileName = "";
foreach (DataRow dr in dt.Rows)
{
if (!(dr[0] is DBNull))
{
fileName = dr[0].ToString();
if(string.IsNullOrEmpty(fileName))
{
fileName = FileOperator.MapPath(C_DIR + fileName);
File.Delete(fileName);
}
}
}
return NewsDAL.DeleteByIdList(IdList);
}
public static int DeleteByNewsID(string AdminName, int NewsID)
{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
string fileName = SelectHtmlFile(NewsID);
if (!string.IsNullOrEmpty(fileName))
File.Delete(HttpContext.Current.Server.MapPath(C_DIR + fileName));
return NewsDAL.DeleteByNewsID(NewsID);
}
public static DataSet QueryPage(int ClassID, string Title, int PageSize, int PageIndex)
{
return NewsDAL.QueryPage(ClassID,Title,PageSize,PageIndex);
}
public static int QueryPageCount(int ClassID, string Title)
{
return NewsDAL.QueryPageCount( ClassID, Title);
}
public static DataSet QueryTopNews(int ClassID, int Num, bool OrderType)
{
return NewsDAL.QueryTopNews(ClassID, Num, OrderType);
}
public static string SelectHtmlFile(int NewsID)
{
return NewsDAL.SelectHtmlFile(NewsID);
}
public static DataSet SelectHtmlFileByIdList(string IdList)
{
return NewsDAL.SelectHtmlFileByIdList(IdList);
}
public static int Update(string AdminName,NewsInfo news)
{
IPermissionChecker chk = PermissionCheckerFactory.GetChecker(AdminName, C_CHK_NAME);
chk.Check(C_ERR_MSG);
int ret= NewsDAL.Update(news);
CreateHtmlFile(news);
return ret;
}
public static DataSet QueryPageWithContent(int ClassID, string Title, int PageSize, int PageIndex)
{
return NewsDAL.QueryPageWithContent( ClassID, Title, PageSize, PageIndex);
}
//这里
public void CreateHtmlFile(int startPage, int endPage,int pageSize,int ClassID)
{
int total = QueryPageCount(ClassID, string.Empty);
total=(total % pageSize)==0 ? total / pageSize : (total /pageSize) +1;
if (total < endPage)
endPage = total;
string fileName = "";
string HtmlFile = "";
int rTotal = (endPage - startPage + 1) * pageSize; //总记录数
for (int i = startPage; i <= endPage; i++)
{
DataTable dt = QueryPageWithContent(ClassID, string.Empty, pageSize, i).Tables[0];
int curR = (i-startPage) * pageSize;
foreach (DataRow dr in dt.Rows)
{
if (!(dr["HtmlFile"] is DBNull))
{
HtmlFile = dr["HtmlFile"].ToString();
if (string.IsNullOrEmpty(HtmlFile))
{
HtmlFile = FileOperator.GetRandFileName(".html");
}
fileName = XMLData.GetFileNameByID(TemplateNode.News, (int)dr["TemplateID"]);
string tempContent = FileOperator.LoadFileWithCache(fileName);
tempContent = tempContent.Replace("{$NewsTitle$}", dr["title"].ToString());
tempContent = tempContent.Replace("{$NewsContent$}", dr["content"].ToString());
tempContent = tempContent.Replace("{$NewsAddTime$}", ((DateTime)dr["AddTime"]).ToString("yyyy-MM-dd"));
curR++;
FileOperator.SaveFile(FileOperator.MapPath(C_DIR + HtmlFile), tempContent);
if (CompleteAFile != null)
CompleteAFile(HtmlFile, string.Empty,(curR * 100)/rTotal);
}
}
}
}
private static void CreateHtmlFile(NewsInfo news)
{
string fileName = XMLData.GetFileNameByID(TemplateNode.News, news.TemplateID);
string tempContent = FileOperator.LoadFileWithCache(fileName);
tempContent = tempContent.Replace("{$NewsTitle$}", news.Title);
tempContent = tempContent.Replace("{$NewsContent$}", news.Content);
tempContent=tempContent.Replace("{$NewsAddTime$}",news.AddTime.ToString("yyyy-MM-dd"));
FileOperator.SaveFile(HttpContext.Current.Server.MapPath(C_DIR + news.HtmlFile),tempContent);
}
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void NavigateButton1_Click(object sender, EventArgs e)
{
WDFROG.BLL.News.DeleteByIdList(Admin.AdminName, "1,2,3,4,5,6");
}
protected void Button1_Click(object sender, EventArgs e)
{
WDFROG.BLL.News objNews = new News();
objNews.CompleteAFile += new CreateHtmlHandler(CreateAFile);
Response.Buffer = false;
Response.Write("<div style="width: 100px; height: 25px;background-color:yellow;" Id="cp">_</div>" + Environment.NewLine);
objNews.CreateHtmlFile(1, 100, 30, -1);
Response.Write("<script>cp.style.visibility='hidden'</script>");
}
protected void CreateAFile(string fileName, string msg, int percent)
{
this.Response.Write(string.Format("<script> cp.innerHTML='Complete {0}%'; </script>", percent ));
Response.Flush();
}
}