using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class WebXabara_clearData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        sqlServer sql = new sqlServer();
        admin a = new admin();
        javasrcipt js = new javasrcipt();

        a.check("|news|");

        if (!IsPostBack)
        {
            string strSql;
            DataTable dt;

            //清除与分类表格有关的自身数据记录===========================================
            strSql = "select distinct(treeTid) from NewsXabara";
            dt = sql.getDataTable(strSql);
            for (int t = 0; t < dt.Rows.Count; t++)
            {
                string newsID = dt.Rows[t]["treeTid"].ToString();
                string strSqlImg = "select * from NewsTree where Tid=" + newsID; //与分类表查询
                if (sql.getDataTable(strSqlImg).Rows.Count < 1)
                {
                    sql.del("NewsXabara", "treeTid", newsID, "", true, "");  //删除数据库记录
                }
            }
            dt.Dispose();

            //清除与图片库无关联文件及记录==================================================
            strSql = "select Fid,fileID,fileName from upFileName where fileTable='news' order by Fid ASC";
            dt = sql.getDataTable(strSql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strSqlImg = "select * from NewsXabara where upNameID='" + dt.Rows[i]["fileID"].ToString().Trim() + "'"; //与数据关联表查询 

                if (sql.getDataTable(strSqlImg).Rows.Count < 1)
                {
                    sql.del("upFileName", "Fid", dt.Rows[i]["Fid"].ToString(), "", true, ""); //删除数据库记录
                }
            }
            dt.Dispose();

            Session.Add("upFileID", string.Empty); //清除关联ID为空

            js.div(false,"数据清理", "text", "清理数据完成！", 300, 0, "../admin.aspx", "top");

            sql.log(a.uid, "admin", "新闻系统数据清理");
        }        
    }
}
