/// <summary>
        ///
        /// </summary>
        private void UpdatePushMessage(PushMessageMode Status)
        {
            string uid = this.RequestQueryString("uid").DecryptDES();

            List <IDataParameter> para = null;

            string strSql = string.Empty, message = string.Empty;

            Hashtable ht = new Hashtable();

            DataTable dt = null;

            try
            {
                this.WriteLog(string.Format("修改推播訊息({0})", Status.GetPushMessageCH()));

                bool send_now = this.sendMode1.Checked;

                DateTime send_time = DateTime.Parse(string.Format("{0} {1}:{2}:00", this.txtSendDate.Text, this.ddlHour.SelectedValue, this.ddlMinute.SelectedValue));

                strSql = this.Update.PushMessage(this.UID, "3", "0", send_now, send_now ? string.Empty : send_time.ToString("yyyy/MM/dd HH:mm:ss"), (int)Status, this.SessionMgr.UserInfos.UserID, ref para);

                #region SQL Debug

                this.WriteLog(Mode.LogMode.DEBUG, strSql);

                this.WriteLog(para.ToLog());

                #endregion

                int result = this.DBConnTransac.GeneralSqlCmd.ExecuteNonQuery(strSql, para);

                if (result.Equals(0))
                {
                    this.Rollback();

                    this.MessageBox(Mode.LogMode.ERROR, message = string.Format("修改{0}失敗", Status.GetPushMessageCH()));

                    this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                    return;
                }
                string[] push_list = this.chBoxCustList.Checked && !String.IsNullOrEmpty(this.HiddenPushListName.Value) ? this.HiddenPushListName.Value.Split('|') : null;

                string json = string.Empty;

                if (push_list != null)
                {
                    strSql = this.Select.UserDeviceToken(push_list, 0, string.Empty, ref para);

                    #region SQL Debug

                    this.WriteLog(Mode.LogMode.DEBUG, strSql);

                    this.WriteLog(para.ToLog());

                    #endregion

                    dt = this.DBConnTransac.GeneralSqlCmd.ExecuteToDataTable(strSql, para);

                    StringBuilder sb = new StringBuilder();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        StringBuilder sb_item = new StringBuilder();

                        sb_item.Append(string.Format("{0}<", i == 0 ? " " : " ,"));

                        for (int i2 = 0; i2 < dt.Columns.Count; i2++)
                        {
                            string column_name = dt.Columns[i2].ColumnName;

                            sb_item.Append(string.Format("{0}\"{1}\" : \"{2}\""
                                                         , i2 == 0 ? " " : " ,"
                                                         , column_name.ToLower()
                                                         , dt.Rows[i][column_name].ToString().EncryptBase64()
                                                         ));
                        }
                        sb_item.Append(">");

                        sb.AppendLine(sb_item.ToString().Replace("<", "{").Replace(">", "}"));
                    }
                    json = "{ \"Changingtec\": { \"sendList\": [" + sb.ToString() + "] }}";
                }
                string old_file_name = this.ViewState["PushImageName"].ToString();

                bool upload_image = !String.IsNullOrEmpty(this.HiddenPushImageName.Value) && !this.HiddenPushImageName.Value.Equals(old_file_name);

                if (upload_image && !String.IsNullOrEmpty(this.HiddenPushImageBody.Value))
                {
                    string file_body = this.HiddenPushImageBody.Value;

                    string publicURIPrefix = this.HiddenPublicURIPrefix.Value;

                    if (!publicURIPrefix.EndsWith("/") && !publicURIPrefix.EndsWith(@"\"))
                    {
                        publicURIPrefix += "/";
                    }

                    string connect_test_url = publicURIPrefix + "ConnectTest";

                    string http_result = connect_test_url.WebRequest(UtilityExtensions.RequestMode.GET, null);

                    if (!http_result.Equals("OK"))
                    {
                        throw new Exception(string.Format("File Server 連線失敗::\r\n{0}", http_result));
                    }

                    string upload_file_url = publicURIPrefix + "UploadFile";

                    string parameter = string.Format("opUser={0}&opPassword={1}&b64FileContent={2}"
                                                     , Utility.WebConfig.OP_USER
                                                     , Utility.WebConfig.OP_PASS
                                                     , file_body);

                    http_result = upload_file_url.WebRequest(UtilityExtensions.RequestMode.POST, parameter);

                    JSONResonse json_result = JsonConvert.DeserializeObject <JSONResonse>(http_result);

                    if (!json_result.ReturnCode.Equals(0))
                    {
                        throw new Exception(string.Format("檔案上傳失敗::\r\n{0}", json_result.Message.DecryptBase64()));
                    }

                    this.ViewState["PushImageName"] = this.HiddenPushImageName.Value = json_result.Result;

                    if (!String.IsNullOrEmpty(old_file_name))
                    {
                        string delete_file_url = publicURIPrefix + string.Format("{0}?opUser={1}&opPassword={2}&fileHandle={3}"
                                                                                 , "DeleteFile"
                                                                                 , Utility.WebConfig.OP_USER
                                                                                 , Utility.WebConfig.OP_PASS
                                                                                 , old_file_name.DecryptBase64());

                        http_result = delete_file_url.WebRequest(UtilityExtensions.RequestMode.GET, null);

                        this.WriteLog(string.Format("DeleteFile:{0}", http_result));
                    }
                }
                ht.Add("msg_id", this.UID);
                ht.Add("msg_push_title", this.txtPushTitle.Text.Trim());
                ht.Add("msg_push_content", this.txtPushBody.Text.Trim());
                ht.Add("msg_push_hyperlink", this.txtPushHyperlink.Text.Trim());
                ht.Add("msg_push_image", this.HiddenPushImageName.Value.DecryptBase64());
                ht.Add("msg_push_is_private", "0");
                ht.Add("msg_push_os_type", String.IsNullOrEmpty(this.ddlOS.SelectedValue) ? "0" : this.ddlOS.SelectedValue);
                ht.Add("msg_push_app_identifier", this.HiddenAppName.Value.Trim());
                ht.Add("msg_push_recp_list", this.chBoxCustList.Checked ? string.Format("{0}|{1}", this.HiddenPushListName.Value.EncryptBase64(), this.HiddenPushListBody.Value) : string.Empty);
                ht.Add("msg_push_recp_bind_info", json);
                ht.Add("msg_push_type_identifier", String.IsNullOrEmpty(this.ddlType.SelectedValue) ? "0" : this.ddlType.SelectedItem.Text.Split('-')[0]);

                strSql = this.Update.PushMessageDetail(ht, ref para);

                #region SQL Debug

                this.WriteLog(Mode.LogMode.DEBUG, strSql);

                this.WriteLog(para.ToLog());

                #endregion

                result = this.DBConnTransac.GeneralSqlCmd.ExecuteNonQuery(strSql, para);

                if (result.Equals(0))
                {
                    this.Rollback();

                    this.MessageBox(Mode.LogMode.ERROR, message = string.Format("修改{0}細節資料失敗", Status.GetPushMessageCH()));

                    this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                    return;
                }
                this.DBConnTransac.GeneralSqlCmd.Transaction.Commit();

                this.MessageBox(Mode.LogMode.INFO, message = string.Format("修改{0}成功", Status.GetPushMessageCH()));

                message += string.Format(",訊息編號:{0}", this.ViewState["IDF"].ToString());

                this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                this.LoadData();
            }
            catch (System.Exception ex)
            {
                this.Rollback();

                this.MessageBox(ex);

                this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.SystemError), this.PageName, ex.Message);
            }
            finally
            {
                dt = null;

                ht = null;

                para = null;

                this.CloseConnTransac();

                this.WriteLog(string.Format("修改推播訊息結束({0})", Status.GetPushMessageCH()));
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            List <IDataParameter> para = null;

            string strSql = string.Empty, message = string.Empty;

            DataTable dt = null;

            try
            {
                string body_fromat = "{"
                                     + " \"data_content\":\"[data_content]\""
                                     + " ,\"is_private\": \"[is_private]\""
                                     + " ,\"data_link\": \"[data_link]\""
                                     + " ,\"data_image\": \"[data_image]\""
                                     + " ,\"msg_id\": \"[msg_id]\""
                                     + " }";

                this.WriteLog(string.Format("開始核准推播訊息"));

                strSql = this.Update.PushMessageStatus(this.UID, "3", ref para);

                #region SQL Debug

                this.WriteLog(Mode.LogMode.DEBUG, strSql);

                this.WriteLog(para.ToLog());

                #endregion

                int result = this.DBConnTransac.GeneralSqlCmd.ExecuteNonQuery(strSql, para);

                if (result.Equals(0))
                {
                    this.Rollback();

                    this.MessageBox(Mode.LogMode.ERROR, message = string.Format("核准推播訊息失敗,推播申請單號:{0}", this.lblID.Text.HtmlEncode()));

                    this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                    return;
                }
                strSql = this.Select.PushMessageApproved(this.UID, null, null, null, ref para);

                #region SQL Debug

                this.WriteLog(Mode.LogMode.DEBUG, strSql);

                this.WriteLog(para.ToLog());

                #endregion

                dt = this.DBConnTransac.GeneralSqlCmd.ExecuteToDataTable(strSql, para);

                string msg_push_image = dt.Rows[0]["msg_push_image"].ToString().Trim();

                if (!String.IsNullOrEmpty(msg_push_image))
                {
                    string publicURIPrefix = this.GetSystemSetting("PublicURIPrefix");

                    if (!publicURIPrefix.EndsWith("/") && !publicURIPrefix.EndsWith(@"\"))
                    {
                        publicURIPrefix += "/";
                    }

                    string connect_test_url = publicURIPrefix + "ConnectTest";

                    string http_result = connect_test_url.WebRequest(UtilityExtensions.RequestMode.GET, null);

                    if (!http_result.Equals("OK"))
                    {
                        throw new Exception(string.Format("File Server 連線失敗::\r\n{0}", http_result));
                    }

                    string upload_file_url = publicURIPrefix + "GetFileLink";

                    string parameter = string.Format("opUser={0}&opPassword={1}&fileHandle={2}"
                                                     , Utility.WebConfig.OP_USER
                                                     , Utility.WebConfig.OP_PASS
                                                     , msg_push_image);

                    http_result = upload_file_url.WebRequest(UtilityExtensions.RequestMode.POST, parameter);

                    JSONResonse json_result = JsonConvert.DeserializeObject <JSONResonse>(http_result);

                    if (!json_result.ReturnCode.Equals(0))
                    {
                        throw new Exception(string.Format("取得檔案連結失敗::\r\n{0}", json_result.Message.DecryptBase64()));
                    }

                    msg_push_image = json_result.Result.DecryptBase64();
                }
                string body = body_fromat.Replace("[data_content]", dt.Rows[0]["msg_push_content"].ToString().Trim());
                body = body.Replace("[is_private]", dt.Rows[0]["msg_push_is_private"].ToString().Trim());
                body = body.Replace("[data_link]", dt.Rows[0]["msg_push_hyperlink"].ToString().Trim().Replace("/", "\\/"));
                body = body.Replace("[data_image]", msg_push_image);
                body = body.Replace("[msg_id]", dt.Rows[0]["msg_identifier"].ToString().Trim());

                strSql = this.Update.PushMessagePushBody(this.UID, body, ref para);

                #region SQL Debug

                this.WriteLog(Mode.LogMode.DEBUG, strSql);

                this.WriteLog(para.ToLog());

                #endregion

                result = this.DBConnTransac.GeneralSqlCmd.ExecuteNonQuery(strSql, para);

                if (result.Equals(0))
                {
                    this.Rollback();

                    this.MessageBox(Mode.LogMode.ERROR, message = string.Format("Push Body 寫入失敗,推播申請單號:{0}", this.lblID.Text.HtmlEncode()));

                    this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                    return;
                }
                int push_total = 0;

                #region OS & APP

                int msg_push_os_type = Convert.ToInt16(this.ViewState["msg_push_os_type"].ToString().Trim());

                if (msg_push_os_type > 0)
                {
                    string msg_push_app_identifier = this.ViewState["msg_push_app_identifier"].ToString().Trim();

                    strSql = this.Select.UserDeviceToken(msg_push_os_type, msg_push_app_identifier, this.UID, ref para);

                    #region SQL Debug

                    this.WriteLog(Mode.LogMode.DEBUG, strSql);

                    this.WriteLog(para.ToLog());

                    #endregion

                    dt = this.DBConnTransac.GeneralSqlCmd.ExecuteToDataTable(strSql, para);

                    DataTable dt_push_send = new DataTable();
                    dt_push_send.Columns.Add("msg_id", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_time", typeof(DateTime));
                    dt_push_send.Columns.Add("msg_push_send_recp", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_app_identifier", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_recp_device", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_processor", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_status", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_user_operation", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_log", typeof(string));

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string push_device_token_id = dt.Rows[i]["push_device_token_id"].ToString().Trim();

                        string push_device_token_value = dt.Rows[i]["push_device_token_value"].ToString().Trim();

                        string push_device_token_app_identifier = dt.Rows[i]["push_device_token_app_identifier"].ToString().Trim();

                        string msg_push_send_id = dt.Rows[i]["msg_push_send_id"].ToString().Trim();

                        bool check = String.IsNullOrEmpty(msg_push_send_id);

                        if (!check)
                        {
                            this.WriteLog(string.Format("Token ID 有重覆 ({0})", push_device_token_id)); continue;
                        }
                        DataRow dr_push_send = dt_push_send.NewRow();
                        dr_push_send["msg_id"]                       = Convert.ToInt32(this.UID);
                        dr_push_send["msg_push_send_time"]           = DateTime.Now;
                        dr_push_send["msg_push_send_recp"]           = push_device_token_id;
                        dr_push_send["msg_push_send_app_identifier"] = push_device_token_app_identifier;
                        dr_push_send["msg_push_send_recp_device"]    = push_device_token_value;
                        dr_push_send["msg_push_send_processor"]      = DBNull.Value;
                        dr_push_send["msg_push_send_status"]         = 1;
                        dr_push_send["msg_push_send_user_operation"] = 0;
                        dr_push_send["msg_push_send_log"]            = DBNull.Value;
                        dt_push_send.Rows.Add(dr_push_send);
                    }
                    push_total = dt_push_send.Rows.Count;

                    bool bulk_copy_result = ((DBLibDAO.SqlTransac) this.DBConnTransac.GeneralSqlCmd).InsertBulkCopy("cg_message_push_send", dt_push_send);

                    if (!bulk_copy_result)
                    {
                        this.Rollback();

                        this.MessageBox(Mode.LogMode.ERROR, message = string.Format("1.新增推播名單失敗,推播申請單號:{0},OS Type:{1}", this.UID, msg_push_os_type));

                        this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                        return;
                    }
                }
                #endregion

                #region Push List

                if (!String.IsNullOrEmpty(this.HiddenPushListName.Value))
                {
                    string[] push_list = this.HiddenPushListName.Value.Split('|');

                    strSql = this.Select.UserDeviceToken(push_list, msg_push_os_type, this.UID, ref para);

                    #region SQL Debug

                    this.WriteLog(Mode.LogMode.DEBUG, strSql);

                    this.WriteLog(para.ToLog());

                    #endregion

                    dt = this.DBConnTransac.GeneralSqlCmd.ExecuteToDataTable(strSql, para);

                    DataTable dt_push_send = new DataTable();
                    dt_push_send.Columns.Add("msg_id", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_time", typeof(DateTime));
                    dt_push_send.Columns.Add("msg_push_send_recp", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_app_identifier", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_recp_device", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_processor", typeof(string));
                    dt_push_send.Columns.Add("msg_push_send_status", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_user_operation", typeof(int));
                    dt_push_send.Columns.Add("msg_push_send_log", typeof(string));

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string push_device_token_id = dt.Rows[i]["push_device_token_id"].ToString().Trim();

                        string push_device_token_value = dt.Rows[i]["push_device_token_value"].ToString().Trim();

                        string push_device_token_app_identifier = dt.Rows[i]["push_device_token_app_identifier"].ToString().Trim();

                        string msg_push_send_id = dt.Rows[i]["msg_push_send_id"].ToString().Trim();

                        DataRow dr_push_send = dt_push_send.NewRow();
                        dr_push_send["msg_id"]                       = Convert.ToInt32(this.UID);
                        dr_push_send["msg_push_send_time"]           = DateTime.Now;
                        dr_push_send["msg_push_send_recp"]           = push_device_token_id;
                        dr_push_send["msg_push_send_app_identifier"] = push_device_token_app_identifier;
                        dr_push_send["msg_push_send_recp_device"]    = push_device_token_value;
                        dr_push_send["msg_push_send_processor"]      = DBNull.Value;
                        dr_push_send["msg_push_send_status"]         = 1;
                        dr_push_send["msg_push_send_user_operation"] = 0;
                        dr_push_send["msg_push_send_log"]            = DBNull.Value;
                        dt_push_send.Rows.Add(dr_push_send);
                    }
                    push_total += dt_push_send.Rows.Count;

                    bool bulk_copy_result = ((DBLibDAO.SqlTransac) this.DBConnTransac.GeneralSqlCmd).InsertBulkCopy("cg_message_push_send", dt_push_send);

                    if (!bulk_copy_result)
                    {
                        this.Rollback();

                        this.MessageBox(Mode.LogMode.ERROR, message = string.Format("2.新增推播名單失敗,推播申請單號:{0},OS Type:{1}", this.UID, msg_push_os_type));

                        this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                        return;
                    }
                }
                #endregion

                this.DBConnTransac.GeneralSqlCmd.Transaction.Commit();

                this.MessageBox(Mode.LogMode.INFO, message = string.Format("推播申請單號 {0},核准推播完成\r\n共排入{1}個裝置待發送", this.lblID.Text.HtmlEncode(), push_total.ToString()));

                this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.UpdateFail), this.PageName, message);

                this.LoadData();
            }
            catch (System.Exception ex)
            {
                this.Rollback();

                this.MessageBox(ex);

                this.MyMonitor.LogMonitor(this, MonitorActionMode.Update, this.SessionMgr.UserInfos.UserID, CaptionMode.Get(CaptionLib.Mode.SystemError), this.PageName, ex.Message);
            }
            finally
            {
                dt = null;

                para = null;

                this.CloseConnTransac();

                this.WriteLog(string.Format("退回推播訊息結束"));
            }
        }