using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Email.Subscription;
using System.Text;
using UserInfo;

/// <summary>
/// EM_ProcessLable 的摘要说明
/// </summary>
namespace nspEmail.Lables
{
    public class EM_ProcessLable
    {
        public EM_ProcessLable()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 获取订阅商业资讯
        /// </summary>
        /// <param name="typeID">行业资讯ID</param>
        /// <returns></returns>
        public static string EM_Newslist(string typeID)
        {
            //获取路径
            string strUrl = Base.BasePage.Config.WebURL + "News/Detail-";
            string strExtension = Base.BasePage.Config.Rewrite;
            DataTable dt = EmailSQLDAL.GetDataTable("select top 12 * from W_Consultative where TypeID=" + typeID + " order by AddDate desc");
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(@"<table width='100%' align='center' style='border:1px solid #FA9150; padding:21px;'>");
            strBuilder.Append(@"<tr><td align='left'>今日要闻</td></tr>");
            strBuilder.Append(@"<tr><td height='37' align='center'>");
            strBuilder.Append(@"<a href='" + strUrl + "" + dt.Rows[0]["ID"] + "" + strExtension + "'><font style='font-family:黑体;font-size:17px;color:black'>" + dt.Rows[0]["Title"] + "</font></a></td></tr>");
            strBuilder.Append(@"<tr><td align='center'><a href='" + strUrl + "" + dt.Rows[1]["ID"] + "" + strExtension + "'><font style='font-family:黑体;font-size:14px;color:black'>" + dt.Rows[1]["Title"] + "</font></a></td></tr>");
            strBuilder.Append(@"<tr><td align='center'><a href='" + strUrl + "" + dt.Rows[2]["ID"] + "" + strExtension + "'><font style='font-family:黑体;font-size:14px;color:black'>" + dt.Rows[2]["Title"] + "</font></a></td></tr>");
            //strBuilder.Append(@"<table width='100%' style='border:1px solid #FA9150; padding:14px;'>");
            for (int i = 3; i < dt.Rows.Count; i++)
            {
                strBuilder.Append(@"<tr><td align='left'>·<a href='" + strUrl + "" + dt.Rows[i]["ID"] + "" + strExtension + "'><font style='font-size:12px;color:black'>" + dt.Rows[i]["Title"] + "</font></a></td></tr>");
            }
            strBuilder.Append(@"</table>");
            string strLable = strBuilder.ToString();
            return strLable;
        }
        ///<summary>
        ///处理网站供求统计信息
        ///</summary>
        ///<returns></returns>
        public static string EM_ProductInfo()
        {
            StringBuilder strBuilder = new StringBuilder();
            DataTable dt = Solely.Other.GetCount();
            if (dt.Rows.Count > 0)
            {
                strBuilder.Append("<table width='100%'><tr><th>");
                strBuilder.Append("统计</th><th>总数</th><th>已审</th><th>未审</th><th>推荐</th><th>查看</th></tr>");
                for (int i = 0; i < 3; i++)
                {
                    if (i == 0)
                    {
                        strBuilder.Append(@"<tr><td>供应信息</td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/SupplyManage.aspx' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t1"].ToString());
                        strBuilder.Append(@"</a></td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/SupplyManage.aspx?Verify=1' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t2"].ToString());
                        strBuilder.Append(@"</a></td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/SupplyManage.aspx?Verify=3' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t3"].ToString());
                        strBuilder.Append(@"</a></td><td>");
                        strBuilder.Append(dt.Rows[i]["t4"].ToString());
                        strBuilder.Append(@"</td><td>");
                        strBuilder.Append(@"<a href='" + Base.BasePage.Config.WebURL + "Member/Trade/SupplyManage.aspx");
                        strBuilder.Append(@"' Target='_bank'>");
                        strBuilder.Append(@"查看</a><td>");
                    }
                    if (i == 1)
                    {
                        strBuilder.Append(@"<tr><td>求购信息</td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/PurchaseManage.aspx' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t1"].ToString());
                        strBuilder.Append(@"</a></td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/PurchaseManage.aspx?verify=1' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t2"].ToString());
                        strBuilder.Append(@"</a></td><td><a href='");
                        strBuilder.Append(Base.BasePage.Config.WebURL);
                        strBuilder.Append(@"Member/Trade/PurchaseManage.aspx?Verify=3' target='_blank'>");
                        strBuilder.Append(dt.Rows[i]["t3"].ToString());
                        strBuilder.Append(@"</a></td><td>");
                        strBuilder.Append(dt.Rows[i]["t4"].ToString());
                        strBuilder.Append(@"</td><td>");
                        strBuilder.Append(@"<a href='{PB_WebURL}Member/Trade/PurchaseManage.aspx");
                        strBuilder.Append(@"' Target='_bank'>");
                        strBuilder.Append(@"查看</a><td>");
                    }
                    strBuilder.Append(@"</tr>");

                }
                strBuilder.Append(@"</table> ");
            }
            return strBuilder.ToString();
        }
        /// <summary>
        /// 获取订阅供求信息
        /// </summary>
        /// <returns></returns>
        public static string EM_Productlist(string strId)
        {
            DataTable dt = new SubscriptionProduct().GetDataTable("where UserID="+strId);
            StringBuilder strBuilder = new StringBuilder();            
            //strBuilder.Append(@"<div>");
            string strSupplyKey = "";
            string strPurchaseKey = "";
            string strKey = "";
            string strKey2 = "";
            DataTable dt1 = new DataTable();   
            strBuilder.Append(@"<table width='700px' align='center'><tr><td height='27' background='http://img.china.alibaba.com/news/upload/5000441/aaa/bar_1187770552226.gif' bgcolor='#FA9150'>");
            strBuilder.Append(@"<table width='100%' border='0' cellspacing='0' cellpadding='0'");
            strBuilder.Append(@"<tr><td align='left' width='100%' style='font-size:12px;'><strong>已订阅内容</strong></td></tr></table></td>");
            strBuilder.Append(@"</tr><tr><td style='border:1px solid #FA9150; padding:14px;'>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {                
                if (dt.Rows[i]["Type"].ToString().IndexOf("供应")>=0)
                {
                    strBuilder.Append("<a href=\"");
                    strBuilder.Append(Base.BasePage.Config.WebURL);
                    strBuilder.Append(@"search/SearchSupply.aspx?keyword="+dt.Rows[i]["Keyword"].ToString()+"\">");
                    strBuilder.Append("&nbsp;" +dt.Rows[i]["Keyword"].ToString() + "(供应)</a>&nbsp;&nbsp;");
                    string key=dt.Rows[i]["Keyword"].ToString();
                    if (strSupplyKey.Length <= 2)
                    {
                        strKey += key;
                        strSupplyKey += "a.[Key] like '%" + key + "%' ";
                        if (dt.Rows[i]["IndType"] != null)
                        {
                            string str = dt.Rows[i]["IndType"].ToString().TrimEnd(new char[] { ',' });
                            strSupplyKey += "and patindex('%,'+rtrim( '" + str + "')+',%',','+ a.IndType+',')>0 ";
                        }
                        if (dt.Rows[i]["Validity"].ToString() != "0")
                            strSupplyKey += "and (datediff(day,a.AddDate,getdate())<'" + dt.Rows[i]["Validity"] + "')";
                    }
                    else
                    {
                        strKey +="、"+ key;
                        strSupplyKey += "or a.[Key] like '%" + key + "%' ";
                        if (dt.Rows[i]["IndType"] != null)
                        {
                            string str = dt.Rows[i]["IndType"].ToString().TrimEnd(new char[] { ',' });
                            strSupplyKey += "and patindex('%,'+rtrim( '" + str + "')+',%',','+ a.IndType+',')>0 ";
                        }
                        if (dt.Rows[i]["Validity"].ToString() != "0")
                            strSupplyKey += "and (datediff(day,a.AddDate,getdate())<'" + dt.Rows[i]["Validity"] + "')";
                    }

                }
                if (dt.Rows[i]["Type"].ToString().IndexOf("求购") >= 0)
                {
                    strBuilder.Append("<a href=\"");
                    strBuilder.Append(Base.BasePage.Config.WebURL);
                    strBuilder.Append(@"search/SearchPurchase.aspx?keyword=" + dt.Rows[i]["Keyword"].ToString() + "\">");
                    strBuilder.Append("&nbsp;" + dt.Rows[i]["Keyword"].ToString() + "(求购)</a>&nbsp;&nbsp;");
                    string key = dt.Rows[i]["Keyword"].ToString();
                    if (strPurchaseKey.Length <= 2)
                    {
                        strKey2 += key;
                        strPurchaseKey += "a.[Key] like '%" + key + "%' ";
                        if (dt.Rows[i]["IndType"] != null)
                        {
                            string str = dt.Rows[i]["IndType"].ToString().TrimEnd(new char[] { ',' });
                            strPurchaseKey += "and patindex('%,'+rtrim( '" + str + "')+',%',','+ a.IndType+',')>0 ";
                        }
                        if (dt.Rows[i]["Validity"].ToString() != "0")
                            strPurchaseKey += "and (datediff(day,a.AddDate,getdate())<'" + dt.Rows[i]["Validity"] + "')";
                    }
                    else
                    {
                        strKey2 += "、"+key;
                        strPurchaseKey += "or a.[Key] like '%" + key + "%' ";
                        if (dt.Rows[i]["IndType"] != null)
                        {
                            string str = dt.Rows[i]["IndType"].ToString().TrimEnd(new char[] { ',' });
                            strPurchaseKey += "and patindex('%,'+rtrim( '" + str + "')+',%',','+ a.IndType+',')>0 ";
                        }
                        if (dt.Rows[i]["Validity"].ToString() != "0")
                            strPurchaseKey += "and (datediff(day,a.AddDate,getdate())<'" + dt.Rows[i]["Validity"] + "')";
                    }
                }
                if (i == dt.Rows.Count - 1)
                {
                    strBuilder.Append(@"</td></tr>");
                    strBuilder.Append(@"<table width='700px' align='center' style='border:1px solid #FA9150; padding:14px;'>");
                    if (dt.Rows[i]["Type"].ToString().IndexOf("供应") >= 0)
                    {
                        string strSelect = "top 12 a.ID,a.Name,a.AddDate,b.Province,b.City";
                        string strWhere =" where "+ strPurchaseKey;
                        string strTable = "C_Supply as a inner join U_UserInfo b on a.UserID=b.UserID ";
                        string strOrder = " order by a.AddDate desc ";
                        dt1 = EmailSQLDAL.GetDataTable(strSelect, strWhere, strTable, strOrder);
                        if (dt1.Rows.Count != 0)
                        {
                            int strCount = 0;
                            if (dt1.Rows.Count <= 12)
                                strCount = dt1.Rows.Count;
                            else
                                strCount = 12;
                            strBuilder.Append(@"<tr><td colspan='2' height='27' width='100%' align='center' background='http://img.china.alibaba.com/news/upload/5000441/aaa/bar_1187770552226.gif' bgcolor='#FA9150'>");
                            strBuilder.Append(strKey);
                            strBuilder.Append(@"最新供应信息&nbsp;(共有符合条件的供应信息" + dt1.Rows.Count + "条，以下是最新的" + strCount + "条)");
                            strBuilder.Append(@"</td></tr>");
                            strBuilder.Append(@"<tr style='border:1px solid #FA9150; padding:14px;'>");
                            for (int j = 0; j < dt1.Rows.Count; j++)
                            {
                                strBuilder.Append(@"<td><a href='");
                                strBuilder.Append(Base.BasePage.Config.WebURL);
                                strBuilder.Append(@"Supply/Detail.aspx?id=");
                                strBuilder.Append(dt1.Rows[j]["ID"]);
                                strBuilder.Append(@"' target='_bank'>");
                                strBuilder.Append(dt1.Rows[j]["Name"]);
                                strBuilder.Append(@"</a></br>");
                                strBuilder.Append(dt1.Rows[j]["AddDate"]);
                                strBuilder.Append(@"&nbsp;&nbsp;");
                                if (dt1.Rows[j]["Province"].ToString() != "" || dt1.Rows[j]["Province"] != null)
                                    strBuilder.Append(dt1.Rows[j]["Province"]);
                                if (dt1.Rows[j]["City"].ToString() != "" || dt1.Rows[j]["City"] != null)
                                    strBuilder.Append(dt1.Rows[j]["City"]);
                                strBuilder.Append(@"</td>");

                            }
                            strBuilder.Append(@"</tr>");
                        }
                        dt1.Clear();
                        dt1.Dispose();
                    }
                    if (dt.Rows[i]["Type"].ToString().IndexOf("求购") >= 0)
                    {
                        string strSelect = "top 12 a.ID,a.Name,a.AddDate,b.Province,b.City";
                        string strWhere = "where "+strPurchaseKey;
                        string strTable = "C_Purchase as a inner join U_UserInfo b on a.UserID=b.UserID ";
                        string strOrder = " order by a.AddDate desc ";
                        dt1 = EmailSQLDAL.GetDataTable(strSelect, strWhere, strTable, strOrder);
                        if (dt1.Rows.Count != 0)
                        {
                            int strCount = 0;
                            if (dt1.Rows.Count <= 12)
                                strCount = dt1.Rows.Count;
                            else
                                strCount = 12;
                            strBuilder.Append(@"<tr><td colspan='2' height='27' width='100%' align='center' background='http://img.china.alibaba.com/news/upload/5000441/aaa/bar_1187770552226.gif' bgcolor='#FA9150'>");
                            strBuilder.Append(strKey2);
                            strBuilder.Append(@"最新求购信息&nbsp;(共有符合条件的求购信息" + dt1.Rows.Count + "条，以下是最新的" + strCount + "条)");
                            strBuilder.Append(@"</td></tr>");
                            strBuilder.Append(@"<tr style='border:1px solid #FA9150; padding:14px;'>");
                            for (int j = 0; j < dt1.Rows.Count; j++)
                            {
                                strBuilder.Append(@"<td><a href='");
                                strBuilder.Append(Base.BasePage.Config.WebURL);
                                strBuilder.Append(@"Purchase/Detail.aspx?id=");
                                strBuilder.Append(dt1.Rows[j]["ID"]);
                                strBuilder.Append(@"' target='_bank'>");
                                strBuilder.Append(dt1.Rows[j]["Name"]);
                                strBuilder.Append(@"</a></br>");
                                strBuilder.Append(dt1.Rows[j]["AddDate"]);
                                strBuilder.Append(@"&nbsp;&nbsp;");
                                if (dt1.Rows[j]["Province"].ToString() != "" || dt1.Rows[j]["Province"] != null)
                                    strBuilder.Append(dt1.Rows[j]["Province"]);
                                if (dt1.Rows[j]["City"].ToString() != "" || dt1.Rows[j]["City"] != null)
                                    strBuilder.Append(dt1.Rows[j]["City"]);
                                strBuilder.Append(@"</td>");
                            }
                            strBuilder.Append(@"</tr>");
                        }
                        dt1.Clear();
                        dt1.Dispose();
                    }
                }
            } 
            strBuilder.Append(@"</table>");
            return strBuilder.ToString();
        }

        public static string EM_Floot(string strId)
        {
            string strUserName = "";
            IUserInfo Ruser = new RUserInfo();
            TUserInfo Tuser = new TUserInfo();
            Tuser=Ruser.GateItem(Int32.Parse(strId));
            strUserName = Tuser.LoginName;
            return strUserName;
        }
    }
}
