Exemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        // json示例
        // {"ExecResult":"Success","ErrorMsg":""}
        // {"ExecResult":"Failed","ErrorMsg":"错误信息"}

        string BranchID = this.Request.QueryString["BranchID"];

        if (BranchID == null)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Missing required query string.\"}");
            this.Response.End();
        }

        if (Regex.IsMatch(BranchID, @"^([1-9]\d*)$") == false)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Invalid query string.\"}");
            this.Response.End();
        }

        int iBranchID = int.Parse(BranchID);

        bool   bSuccess = false;
        string sError   = string.Empty;

        try
        {
            ServiceManager sm = new ServiceManager();
            using (LPWeb.LP_Service.LP2ServiceClient client = sm.StartServiceClient())
            {
                LPWeb.LP_Service.MailChimp_Response respone = null;
                respone = client.MailChimp_SyncNow(iBranchID);

                sError = "Sync successfully.";
                if (respone.hdr.Successful == false)
                {
                    bSuccess = false;
                    sError   = respone.hdr.StatusInfo;
                }
            }
        }
        catch (Exception ex)
        {
            //bSuccess = false;
            //sError = String.Format("Exception happened when invoke API MailChimp_SyncNow:  {0}", ex.Message);
            bSuccess = true;
            sError   = "Sync successfully.";
        }
        finally
        {
            if (bSuccess == false)
            {
                this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"" + sError + "\"}");
                this.Response.End();
            }
        }

        this.Response.Write("{\"ExecResult\":\"Success\",\"ErrorMsg\":\"\"}");
        this.Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // json示例
        // {"ExecResult":"Success","ErrorMsg":""}
        // {"ExecResult":"Failed","ErrorMsg":"错误信息"}

        #region 校验页面参数

        #region ContactIDs

        if (this.Request.QueryString["ContactIDs"] == null)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Missing required query string.\"}");
            this.Response.End();
        }

        string sContactIDs = this.Request.QueryString["ContactIDs"];

        //if (Regex.IsMatch(sContactIDs, @"^([1-9]\d*)(,[1-9]\d*)*$") == false)
        //{
        //    this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Invalid query string.\"}");
        //    this.Response.End();
        //}

        string[] ContactIDArray = sContactIDs.Split(',');

        #endregion

        #region LID

        if (this.Request.QueryString["LID"] == null)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Missing required query string.\"}");
            this.Response.End();
        }

        string sLID = this.Request.QueryString["LID"];

        #endregion

        #endregion

        #region 获取LID对应的BranchID

        int        iBranchID = 0;
        string     sSql2     = "select * from MailChimpLists where LID=@LID";
        SqlCommand SqlCmd2   = new SqlCommand(sSql2);
        LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd2, "@LID", SqlDbType.NVarChar, sLID);

        DataTable MailChimpListInfo = LPWeb.DAL.DbHelperSQL.ExecuteDataTable(SqlCmd2);
        if (MailChimpListInfo.Rows.Count == 0)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Invalid query string.\"}");
            this.Response.End();
        }

        if (MailChimpListInfo.Rows[0]["BranchId"] != DBNull.Value)
        {
            iBranchID = Convert.ToInt32(MailChimpListInfo.Rows[0]["BranchId"]);
        }

        #endregion

        int iLoginUserID = this.CurrUser.iUserID;

        #region 调用MailChimp_Subscribe API

        int[] ContactIDList = new int[ContactIDArray.Length];
        int   j             = 0;
        foreach (string sContactID in ContactIDArray)
        {
            int iContactID = Convert.ToInt32(sContactID);

            ContactIDList.SetValue(iContactID, j);

            j++;
        }

        bool   bSuccess = true;
        string sError   = string.Empty;
        LPWeb.LP_Service.MailChimp_Response mc_resp = null;

        try
        {
            //LPWeb.LP_Service.LP2ServiceClient x = new LPWeb.LP_Service.LP2ServiceClient();
            ServiceManager sm = new ServiceManager();
            using (LPWeb.LP_Service.LP2ServiceClient client = sm.StartServiceClient())
            {
                mc_resp = client.MailChimp_SubscribeBatch(ContactIDList, sLID);

                if (mc_resp.hdr.Successful == false)
                {
                    bSuccess = false;
                    sError   = mc_resp.hdr.StatusInfo.Replace("\"", "'");
                }
                else
                {
                    bSuccess = true;
                    sError   = "Subscribe Successfully";
                }
            }
        }
        catch (Exception ex)
        {
            bSuccess = false;
            sError   = "Exception happened when invoke API MailChimp_SubscribeBatch: " + ex.Message;
        }
        finally
        {
            if (bSuccess == false)
            {
                this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"" + sError + "\"}");
                this.Response.End();
            }
        }

        #endregion

        #region Insert ContactMailCampaign

        #region Build SqlCommand

        int          i          = 0;
        SqlCommand[] SqlCmdList = new SqlCommand[ContactIDArray.Length];
        foreach (string sContactID in ContactIDArray)
        {
            int        iContactID = Convert.ToInt32(sContactID);
            string     sSql       = "insert into dbo.ContactMailCampaign (ContactId,CID,LID,BranchId,Added,AddedBy,Result) values (@ContactId,@CID,@LID,@BranchId,getdate(),@AddedBy,@Result)";
            SqlCommand SqlCmd     = new SqlCommand(sSql);

            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@ContactId", SqlDbType.Int, iContactID);
            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@CID", SqlDbType.NVarChar, DBNull.Value);
            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@LID", SqlDbType.NVarChar, sLID);

            if (iBranchID == 0)
            {
                LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@BranchId", SqlDbType.Int, DBNull.Value);
            }
            else
            {
                LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@BranchId", SqlDbType.Int, iBranchID);
            }

            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@AddedBy", SqlDbType.Int, iLoginUserID);
            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@Result", SqlDbType.NVarChar, DBNull.Value);

            SqlCmdList.SetValue(SqlCmd, i);
            i++;
        }

        #endregion

        #region 批量执行SQL语句

        SqlConnection  SqlConn  = null;
        SqlTransaction SqlTrans = null;

        try
        {
            SqlConn  = LPWeb.DAL.DbHelperSQL.GetOpenConnection();
            SqlTrans = SqlConn.BeginTransaction();

            foreach (SqlCommand SqlCmd1 in SqlCmdList)
            {
                LPWeb.DAL.DbHelperSQL.ExecuteNonQuery(SqlCmd1, SqlTrans);
            }

            SqlTrans.Commit();
        }
        catch (Exception ex)
        {
            SqlTrans.Rollback();

            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Exception happened when insert ContactMailCampaign.\"}");
            this.Response.End();
        }
        finally
        {
            if (SqlConn != null)
            {
                SqlConn.Close();
            }
        }

        #endregion

        #endregion

        this.Response.Write("{\"ExecResult\":\"Success\",\"ErrorMsg\":\"\"}");
        this.Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // json示例
        // {"ExecResult":"Success","ErrorMsg":""}
        // {"ExecResult":"Failed","ErrorMsg":"错误信息"}

        #region 校验页面参数

        #region ContactIDs

        if (this.Request.QueryString["ContactIDs"] == null)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Missing required query string.\"}");
            this.Response.End();
        }

        string sContactIDs = this.Request.QueryString["ContactIDs"];

        //if (Regex.IsMatch(sContactIDs, @"^([1-9]\d*)(,[1-9]\d*)*$") == false)
        //{
        //    this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Invalid query string.\"}");
        //    this.Response.End();
        //}

        string[] ContactIDArray = sContactIDs.Split(',');

        #endregion

        #region LIDs

        if (this.Request.QueryString["LIDs"] == null)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Missing required query string.\"}");
            this.Response.End();
        }

        string sLIDs = this.Request.QueryString["LIDs"];

        if (Regex.IsMatch(sLIDs, @"^(\w+)(,\w+)*$") == false)
        {
            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Invalid query string.\"}");
            this.Response.End();
        }

        string[] LIDsArray = sLIDs.Split(',');

        #endregion

        #endregion

        #region 调用MailChimp_Unsubscribe API

        LPWeb.LP_Service.MailChimp_Response mc_resp = null;

        foreach (string sContactID in ContactIDArray)
        {
            int iContactID = Convert.ToInt32(sContactID);

            foreach (string sLID in LIDsArray)
            {
                bool   bSuccess = false;
                string sError   = string.Empty;
                try
                {
                    ServiceManager sm = new ServiceManager();
                    using (LPWeb.LP_Service.LP2ServiceClient client = sm.StartServiceClient())
                    {
                        mc_resp = client.MailChimp_Unsubscribe(iContactID, sLID);

                        if (mc_resp.hdr.Successful == false)
                        {
                            bSuccess = false;
                            sError   = mc_resp.hdr.StatusInfo.Replace("\"", "'");
                        }
                        else
                        {
                            bSuccess = true;
                            sError   = "Unsubscribe Successfully";
                        }
                    }
                }
                catch (Exception ex)
                {
                    bSuccess = false;
                    sError   = "Exception happened when invoke API MailChimp_Unsubscribe.";
                }
                finally
                {
                    if (bSuccess == false)
                    {
                        this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"" + sError + "\"}");
                        this.Response.End();
                    }
                }
            }
        }

        #endregion

        #region delete ContactMailCampaign

        #region Build SqlCommand

        int          i          = 0;
        SqlCommand[] SqlCmdList = new SqlCommand[ContactIDArray.Length];
        foreach (string sContactID in ContactIDArray)
        {
            int        iContactID = Convert.ToInt32(sContactID);
            string     sSql       = "delete from dbo.ContactMailCampaign where ContactId=@ContactId and LID='" + sLIDs + "'";
            SqlCommand SqlCmd     = new SqlCommand(sSql);

            LPWeb.DAL.DbHelperSQL.AddSqlParameter(SqlCmd, "@ContactId", SqlDbType.Int, iContactID);

            SqlCmdList.SetValue(SqlCmd, i);
            i++;
        }

        #endregion

        #region 批量执行SQL语句

        SqlConnection  SqlConn  = null;
        SqlTransaction SqlTrans = null;

        try
        {
            SqlConn  = LPWeb.DAL.DbHelperSQL.GetOpenConnection();
            SqlTrans = SqlConn.BeginTransaction();

            foreach (SqlCommand SqlCmd1 in SqlCmdList)
            {
                LPWeb.DAL.DbHelperSQL.ExecuteNonQuery(SqlCmd1, SqlTrans);
            }

            SqlTrans.Commit();
        }
        catch (Exception ex)
        {
            SqlTrans.Rollback();

            this.Response.Write("{\"ExecResult\":\"Failed\",\"ErrorMsg\":\"Exception happened when delete ContactMailCampaign.\"}");
            this.Response.End();
        }
        finally
        {
            if (SqlConn != null)
            {
                SqlConn.Close();
            }
        }

        #endregion

        #endregion

        this.Response.Write("{\"ExecResult\":\"Success\",\"ErrorMsg\":\"\"}");
        this.Response.End();
    }