using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 SqlOperate;
using Base;
using nspEmail.Lables;
using nspBase.Core;
using System.Web.SessionState;

public partial class Admin_Email_EmailNotice : Base.BasePage
{
    public string w = "0";
    public static int strSuccess = 0;
    public static int strFailure = 0;
    public static int strCount;//发送邮件数量
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("Basic", "D1");
        BindData();
        lblMessage.Visible = false;
        if (Session["work"] == null)
        {
            Session["work"] = w;
        }
        else
        {
            w = Session["work"].ToString();
        }
        switch (w)
        {
            case "0":
                {
                    lblMessage.Visible = false;
                    table1.Visible = false;
                    break;
                }
            case "1":
                {
                    tableEmail.Visible = false;
                    table1.Visible = true;
                    lblMessage.Text = "";
                    Page.RegisterStartupScript("", "<script>window.setTimeout('location.href=location.href',2000);</script>");
                    break;
                }
            case "2":
                {
                    tableEmail.Visible = true;
                    table1.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "发送完成";
                    lblCount.Visible = true;
                    lblCount.Text = "共发送" + strCount.ToString() + "份邮件  成功发送" + strSuccess.ToString() + "份邮件  失败" + strFailure.ToString() + "份邮件  <a href=\"../Email/EmailStatistics.aspx\"> 查看详细</a>";
                    Session["work"] = "0";
                    break;
                }
        }
    }
    private void BindData()
    {  
        string strTypeName = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + lblType.Text + "']", "type");
        if (Lb_TypeName.Text.Trim() == strTypeName)
            lblTime.Text = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_TypeName.Text.Trim() + "']", "value");
        else
        {
            string[][] strList =BindList(Lb_TypeName.Text.Trim(), lblType.Text.Trim(),"");
            Base.XMLProcess.Insert("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag", "mode",strList);
        }
        string strTypeName1 = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + Lb_BusType.Text + "']", "type");
        if (Lb_Business.Text.Trim() == strTypeName1)
            LB_BusTime.Text = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_Business.Text.Trim() + "']", "value");
        else {
            string[][] strList = BindList(Lb_Business.Text.Trim(), Lb_BusType.Text.Trim(), "");
            Base.XMLProcess.Insert("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag", "mode", strList);
        }
        string strTypeName2 = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + Lb_NewsID.Text + "']", "type");
        if (Lb_NewsName.Text.Trim() == strTypeName2)
            Lb_NewsTime.Text = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_NewsName.Text.Trim() + "']", "value");
        else {
            string[][] strList = BindList(Lb_NewsName.Text.Trim(), Lb_NewsID.Text.Trim(), "");
            Base.XMLProcess.Insert("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag", "mode", strList);
        }
    }
    private string[][] BindList(string strTypeName, string strType, string strDataTime)
    {
        string[][] strList = new string[][] {
                                               new string[] { "type", strTypeName },
                                               new string[] { "name", strType }, 
                                               new string[] { "value",strDataTime }
                                            };
        return strList;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtdataTime.Text.Trim() != "")
        {
            bool strNum = Common.isNum(txtdataTime.Text.Trim());
            if (!strNum)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "日期必须为整形!";
                return;
            }
            lock (this)
            {
                if (Session["work"].ToString() != "1")
                {
                    Session["work"] = "1";
                    table1.Visible = true;
                    tableEmail.Visible = false;
                }
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(dowork));
                thread.Start();
            }
            Page.RegisterStartupScript("", "<script>location.href=location.href;</script>");
        }
    }
    private void dowork()
    {
        try
        {
            string sql1 = "select UserID,LoginName,Email,datediff(day,getdate(),EndDate) as EndTime from U_UserInfo where datediff(day,getdate(),EndDate)<=" + txtdataTime.Text.Trim() + " and datediff(day,getdate(),EndDate)>0";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteSqlQuery(sql1);
            string strEmail1 = "";
            if (dt.Rows.Count != 0)
            {
                strCount = dt.Rows.Count;
                string strName = "";
                string strEmail = "";
                string strBody = "";
                int k = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int strUserId = Int32.Parse(dt.Rows[i]["UserID"].ToString());
                    strName = dt.Rows[i]["LoginName"].ToString();
                    strEmail = dt.Rows[i]["Email"].ToString();
                    string  strdateTime=dt.Rows[i]["EndTime"].ToString();
                    strBody = EmailTempinc.EmailSend(strName, Int32.Parse(strdateTime));
                    strEmail1 = clsEmail.SendMail("邮件通知", strEmail, strBody);
                    if (strEmail1 == "发送成功")
                        k = k + 1;
                    EmailStatistics es = new EmailStatistics();
                    es.UserId = strUserId;
                    es.State = strEmail1;
                    es.SubscriptionType = "会员";
                    es.AddDate = DateTime.Now;
                    es.Extend = strdateTime;
                    new EmailStatistics().Insert(es);
                }
                Session["work"] = "2";
                strSuccess = k;
                strFailure = strCount - strSuccess;
            }           
            string strTypeName = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + lblType.Text.Trim() + "']", "type");
            if (Lb_TypeName.Text.Trim().ToString() == strTypeName)
                Base.XMLProcess.Update("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_TypeName.Text.Trim() + "']", "value", DateTime.Now.ToString());           
            dt.Clear();
            dt.Dispose();
        }
        catch
        {

        }
    }
    protected void btnBusiness_Click(object sender, EventArgs e)
    {
        if (txt_Business.Text.Trim() != "")
        {
            bool isNum = Common.isNum(txt_Business.Text.Trim());
            if (!isNum)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "日期必须为整形!";
                return;
            }
            lock (this)
            {
                if (Session["work"].ToString() != "1")
                {
                    Session["work"] = "1";
                    table1.Visible = true;
                    tableEmail.Visible = false;
                }
                System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(BusinessSend));
                thread1.Start();
            }
            Page.RegisterStartupScript("", "<script>location.href=location.href;</script>");
        }
    }
    private void BusinessSend()
    {
        string sql2 = "select a.Email,a.UserID,a.ID as TypeID,c.LoginName  from U_SubscriptionEmail as a inner join U_UserInfo c on a.UserID=c.UserID where a.UserID in (select UserID from U_SubscriptionProduct where datediff(day,AddDate,getdate())<=" + txt_Business.Text.Trim() + ")";
        string strMessage = new EmailTempinc().GetTemplate("us_SubscriptionProduct", sql2);
        Session["work"] = "2";
        strCount = EmailTempinc.strCount;
        strSuccess = EmailTempinc.strSuccess;
        strFailure = EmailTempinc.strFailure;
        if (strMessage != "")
        {
            string strTypeName = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + Lb_BusType.Text.Trim() + "']", "type");
            if (Lb_Business.Text.ToString() == strTypeName)
                Base.XMLProcess.Update("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_Business.Text.Trim() + "']", "value", DateTime.Now.ToString());            
        }
    }
    protected void btnSendNews_Click(object sender, EventArgs e)
    {
        if (txt_NewsTime.Text.Trim() != "")
        {
            bool isNum = Common.isNum(txt_NewsTime.Text.Trim());
            if (!isNum)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "日期必须为整形!";
                return;
            }
            lock (this)
            {
                if (Session["work"].ToString() != "1")
                {
                    Session["work"] = "1";
                    table1.Visible = true;
                    tableEmail.Visible = false;
                }
                System.Threading.Thread thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(NewsSend));
                thread2.Start();
            }
            Page.RegisterStartupScript("", "<script>location.href=location.href;</script>");
        }
    }
    private void NewsSend()
    {
        string sql2 = "select top 10 a.Type as TypeID,a.UserID,b.Email as Email,c.LoginName  from U_SubscriptionNews a inner join U_SubscriptionEmail b on a.UserID=b.UserID inner join U_UserInfo c on b.UserID=c.UserID where c.UserID in (select UserID from U_SubscriptionNews where datediff(day,AddDate,getdate())<=" + txt_NewsTime.Text.Trim() + ")";
        string strMessage = new EmailTempinc().GetTemplate("us_SubScriptionNews", sql2);
        Session["work"] = "2";
        strCount = EmailTempinc.strCount;
        strSuccess = EmailTempinc.strSuccess;
        strFailure = EmailTempinc.strFailure;
        if (strMessage != "")
        {
            string strData = DateTime.Now.ToString();
            string strTypeName = Base.XMLProcess.Read("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@name='" + Lb_NewsID.Text.Trim() + "']", "type");
            if (Lb_NewsName.Text.ToString() == strTypeName)
                Base.XMLProcess.Update("/Admin/Email/EmailTemplate/EmailSend.xml", "/Item/flag/mode[@type='" + Lb_NewsName.Text.Trim() + "']", "value", strData);
        }
    }
}
