using System;
using System.Collections.Generic;
using System.Text;
using Fm.Tags;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using Fm.Bussiness;
namespace Fm.Tags
{
    public class HtmlWrite:System.Web.UI.Page
    {
        /// <summary>
        /// 静态文档写入
        /// </summary>
        /// <param name="row"></param>
        public void WriteContent(int docid)
        {
            Temp_Content cont = new Temp_Content();//文章数据调用类
            ATL.IO.IOStream stream = new ATL.IO.IOStream();//文件读取类
            Tags_sql sql = new Tags_sql();
            public_select ps = new public_select();//公用数据库操作类
            //获取文章信息
            DataView dw = sql.GetContentView("docid=" + docid + "") as DataView;
            DataView row = ps.Getps("Fm_column", "cl_id,cl_path,cl_temp_cont,cl_content", "cl_id=" + int.Parse(dw[0]["colid"].ToString()) + "");
            int cid = int.Parse(row[0]["cl_id"].ToString());
            
            //循环生成
            foreach (DataRow dr in dw.Table.Rows)
            {
                //判断文档是否允许生成静态
                if (dr["html"].ToString().ToLower() == "true")
                {

                    //获取文档发布时间
                    DateTime time = DateTime.Parse(dr["writertime"].ToString());
                    //获取文档生成路径
                    string path = "/" + row[0]["cl_path"].ToString() + row[0]["cl_content"].ToString();
                    //替换路径中自定义的变量
                    path = path.Replace("{Y}", time.Year.ToString());
                    path = path.Replace("{M}", time.Month.ToString());
                    path = path.Replace("{D}", time.Day.ToString());
                    path = path.Replace("{aid}", dr["docid"].ToString());
                    //string path = Server.MapPath("../" + ur);
                    //获取存放路径(如果没有其存放目录则创建存放目录)
                    string folder = path.Substring(0, path.LastIndexOf("/"));
                    folder = Server.MapPath(folder);

                    // Response.Write(path+" "+folder+" " );
                    if (!System.IO.Directory.Exists(folder))
                    {
                        stream.CreateFolder(folder);
                    }
                    //删除原有存放的静态路径
                    ps.Queryps("delete from Fm_model_htm where docid=" + docid + "");
                    //正则验证是否插入了分页符
                    Regex r = new Regex(@"(#p#)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    int number = r.Matches(dr["content"].ToString()).Count;
                    //如果有分页符则执行
                    if (number > 0)
                    {
                        number = number + 1;
                        //判断文档是否写入成功，如果成功则把路径填入数据库
                        string kzname = Path.GetExtension(path);
                        string filename = path.Replace(kzname, "");
                        for (int i = 2; i <= number; i++)
                        {
                            string newpath = filename + "_" + i + kzname;
                            stream.WriteFile(Server.MapPath(newpath), GetContent(cont,docid,i,path));
                            //保存新的文档存放路径
                            InsertHtml(ps,docid,newpath,i);
                        }
                    }
                    //写入第一页文档
                    InsertHtml(ps,docid,path,1);
                    stream.WriteFile(Server.MapPath(path), GetContent(cont,docid,1,path));

                }
            }
        }
        void InsertHtml(public_select ps,int docid,string path,int page)
        {
            StringBuilder sqlstr = new StringBuilder();
            sqlstr.Append("insert into Fm_model_htm(docid,path,page) values(");
            sqlstr.Append(docid);
            sqlstr.Append(",'");
            sqlstr.Append(path);
            sqlstr.Append("',");
            sqlstr.Append(page);
            sqlstr.Append(")");
            //保存新的文档存放路径
            ps.Queryps(sqlstr.ToString());
        }
        //获取内容页内容
        string GetContent(Temp_Content cont,int docid,int page,string path)
        {
            return cont.Get_Content(docid, Server.MapPath("../templates/"), page, path);
        }
    }
}
