private static bool CheckConnectionRedis() { bool isConnected = Connection.IsConnected; if (!isConnected) { logger.Error(Utils.A("CheckConnectionRedis", "Not connection!", AppConfig.REDIS_HOST, AppConfig.REDIS_PORT, AppConfig.REDIS_PASS)); } return(isConnected); }
/// <summary> /// Insert SMS to database /// </summary> /// <param name="storedName"></param> /// <param name="keyValues"></param> /// <returns></returns> public async Task <ReturnModel> InsertSmsAsync(string storedName, object[] keyValues) { if (logger.IsDebugEnabled) { watch.Restart(); watch.Start(); } ReturnModel returnModel = new ReturnModel(); try { OracleDynamicParameter parameters = new OracleDynamicParameter(); parameters.Add("p_refcursor", OracleDbType.RefCursor, ParameterDirection.Output, null); parameters.Add("p_account_id", OracleDbType.Long, ParameterDirection.Input, keyValues[0]); parameters.Add("p_sender_id", OracleDbType.Long, ParameterDirection.Input, keyValues[1]); parameters.Add("p_sms_type", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[2]); parameters.Add("p_sms_content", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[3]); parameters.Add("p_sms_count", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[4]); parameters.Add("p_phone", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[5]); parameters.Add("p_telco", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[6]); parameters.Add("p_schedule_time", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[7]); parameters.Add("p_order_name", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[8]); parameters.Add("p_campaign_id", OracleDbType.Long, ParameterDirection.Input, keyValues[9]); parameters.Add("p_is_schedule", OracleDbType.Long, ParameterDirection.Input, keyValues[10]); parameters.Add("p_client_sms_id", OracleDbType.Long, ParameterDirection.Input, keyValues[11]); parameters.Add("p_via", OracleDbType.Varchar2, ParameterDirection.Input, keyValues[12]); parameters.Add("p_is_encrypted", OracleDbType.Int16, ParameterDirection.Input, keyValues[13]); parameters.Add("p_err_code", OracleDbType.Varchar2, ParameterDirection.Output, null, 10); parameters.Add("p_err_message", OracleDbType.Varchar2, ParameterDirection.Output, null, 255); using (IDbConnection dbConnection = new OracleConnection(AppConfig.ORACLE_CONNECTION)) { await dbConnection.ExecuteAsync(storedName, param : parameters, commandType : CommandType.StoredProcedure); returnModel.code = parameters.oracleParameters[parameters.oracleParameters.Count - 2]?.Value.ToString(); returnModel.message = parameters.oracleParameters[parameters.oracleParameters.Count - 1]?.Value.ToString(); } } catch (Exception ex) { returnModel.code = AppConst.SYS_ERR_EXCEPTION.ToString(); returnModel.message = ex.ToString(); logger.Error(Utils.A("InsertSMSAsync", storedName, ex)); } if (logger.IsDebugEnabled) { watch.Stop(); logger.Info(Utils.A("InsertSMSAsync.:Stopwatch", storedName, watch.ElapsedMilliseconds)); } return(returnModel); }
/// <summary> /// /// </summary> /// <param name="storedName"></param> /// <param name="keyValues"></param> /// <returns></returns> public async Task <ReturnModel> UpdateQuota(string storedName, long account_id, string type, long count) { if (logger.IsDebugEnabled) { watch.Restart(); watch.Start(); } ReturnModel returnModel = new ReturnModel(); try { OracleDynamicParameter parameters = new OracleDynamicParameter(); parameters.Add("p_refcursor", OracleDbType.RefCursor, ParameterDirection.Output, null); parameters.Add("p_accountid", OracleDbType.Long, ParameterDirection.Input, account_id); parameters.Add("p_servicename", OracleDbType.Varchar2, ParameterDirection.Input, type); parameters.Add("p_quota_use", OracleDbType.Long, ParameterDirection.Input, count); parameters.Add("p_err_code", OracleDbType.Varchar2, ParameterDirection.Output, null, 10); parameters.Add("p_err_message", OracleDbType.Varchar2, ParameterDirection.Output, null, 255); using (IDbConnection dbConnection = new OracleConnection(AppConfig.ORACLE_CONNECTION)) { await dbConnection.ExecuteAsync(storedName, param : parameters, commandType : CommandType.StoredProcedure); returnModel.code = parameters.oracleParameters[parameters.oracleParameters.Count - 2]?.Value.ToString(); returnModel.message = parameters.oracleParameters[parameters.oracleParameters.Count - 1]?.Value.ToString(); } } catch (Exception ex) { returnModel.code = AppConst.SYS_ERR_EXCEPTION.ToString(); returnModel.message = ex.ToString(); logger.Error(Utils.A("UpdateQuota", storedName, ex)); } if (logger.IsDebugEnabled) { watch.Stop(); logger.Info(Utils.A("UpdateQuota.:Stopwatch", storedName, watch.ElapsedMilliseconds)); } return(returnModel); }
public async Task <IActionResult> SendSMSAsync([FromBody] SmsModel sms) { try { logger.Info(Utils.A("SendSMS", JsonConvert.SerializeObject(sms))); #region Check validate input if (String.IsNullOrEmpty(sms.brandname) || String.IsNullOrEmpty(sms.phone) || String.IsNullOrEmpty(sms.message) || String.IsNullOrEmpty(sms.username) || String.IsNullOrEmpty(sms.password)) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "2", message = "Input invalid" }))); } #endregion #region Check username & password AccountModel account = JsonConvert.DeserializeObject <AccountModel>(RedisHelper.Get(String.Format("ACCOUNTS:{0}", sms.username))); if (!account.PASSWORD.Equals(sms.password)) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "3", message = "Username or password invalid" }))); } #endregion #region Check account active if (account.IS_ACTIVE != 1) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "4", message = "Account not active" }))); } #endregion #region Check mobile number sms.phone = Utils.AddPhone84(sms.phone); sms.telco = Utils.GetTelco(sms.phone); if (String.IsNullOrEmpty(sms.telco)) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "5", message = "Mobile number invalid" }))); } #endregion #region Check brandname by account object[] kvAccountSender = new object[] { account.ACCOUNT_ID, sms.brandname, AppConst.SMS_TYPE_CSKH }; IDictionary <string, object> resultAccountSender = (new DapperHelper()).GetAccountSender(AppConfig.API_GET_ACCOUNT_SENDER, kvAccountSender); string json = JsonConvert.SerializeObject(resultAccountSender[AppConst.DATA], Formatting.Indented); IList <SenderModel> lstSender = JsonConvert.DeserializeObject <IList <SenderModel> >(json); if (lstSender.Count == 0) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "8", message = "Brand name is not active by account" }))); } #endregion #region Check exists mapping object[] kvMapping = new object[] { account.ACCOUNT_ID, lstSender[0].sender_id, sms.telco, AppConst.SMS_TYPE_CSKH }; IDictionary <string, object> resultMapping = (new DapperHelper()).GetMappingByTelco(AppConfig.API_GET_PARTNER_SENDER_BY_TELCO, kvMapping); string jsonMap = JsonConvert.SerializeObject(resultMapping[AppConst.DATA], Formatting.Indented); IList <SenderModel> lstMapp = JsonConvert.DeserializeObject <IList <SenderModel> >(jsonMap); if (lstMapp.Count == 0) { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "9", message = "Telco " + sms.telco + " is not active" }))); } #endregion sms.message = Utils.RemoveVietnameseSigns(sms.message); sms.sms_count = Utils.GetCountSms(sms.message); DateTime currentDate = DateTime.Now; sms.sendtime = currentDate.ToString(AppConst.DATE_FORMAT_TEMPLATE_4); ReturnModel result = new ReturnModel(); //check quota object[] keyValues = new object[] { account.ACCOUNT_ID, AppConst.SMS_TYPE_CSKH }; IDictionary <string, object> result1 = (new DapperHelper()).GetQuotaByAccount(AppConfig.API_GET_QUOTA_REMAIN, keyValues); json = JsonConvert.SerializeObject(result1[AppConst.DATA], Formatting.Indented); IList <AccountCimastModel> quota = JsonConvert.DeserializeObject <IList <AccountCimastModel> >(json); bool checkPayment = false; if (quota[0].PAYMENT_TYPE == 1 && quota.Count > 0) { AccountCimastModel quotaAccount = quota[0]; var quota_remain = quotaAccount.VOL != null ? quotaAccount.VOL : 0; if (quota_remain == 0 || sms.sms_count > quota_remain) { checkPayment = true; return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "7", message = "The account is not enough quota." }))); } // trừ quỹ tin await(new DapperHelper()).UpdateQuota(AppConfig.API_UPDATE_QUOTA, account.ACCOUNT_ID, AppConst.SMS_TYPE_CSKH, sms.sms_count); } if (!checkPayment || quota[0].PAYMENT_TYPE == 2) { string sendTime = RedisHelper.Get(string.Format("PARTNER_CHECK_LOOP:{0},{1},{2},{3},{4}", account.ACCOUNT_ID, lstSender[0].sender_id, sms.phone, sms.message, sms.telco)); if ((!string.IsNullOrEmpty(sendTime) && currentDate.AddMinutes(-Convert.ToDouble(AppConst.TIME_CHECK_LOOP)) > Convert.ToDateTime(sendTime)) || string.IsNullOrEmpty(sendTime)) { // Insert sms object[] values = new object[] { account.ACCOUNT_ID, Convert.ToInt64(lstSender[0].sender_id), AppConst.SMS_TYPE_CSKH, sms.message, sms.sms_count, sms.phone, sms.telco, sms.sendtime, String.Empty, String.Empty, 0, sms.id, AppConst.VIA_SMS_API }; result = await(new DapperHelper()).InsertSmsAsync(AppConfig.API_POST_SMS, values); if (result.code != "0") { //cộng trả lại int count = 0 - sms.sms_count; result = await(new DapperHelper()).UpdateQuota(AppConfig.API_UPDATE_QUOTA, account.ACCOUNT_ID, AppConst.SMS_TYPE_CSKH, count); if (result.code != "0") { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "1", message = "System exception." }))); } } RedisHelper.Set(string.Format("PARTNER_CHECK_LOOP:{0},{1},{2},{3},{4}", account.ACCOUNT_ID, lstSender[0].sender_id, sms.phone, sms.message, sms.telco), currentDate.ToString()); } else { if (account.IS_SEND_SMS_LOOP == 1) { // Insert sms object[] values = new object[] { account.ACCOUNT_ID, lstSender[0].sender_id, AppConst.SMS_TYPE_CSKH, sms.message, String.Empty, sms.sms_count, sms.phone, sms.telco, currentDate.AddMinutes(Convert.ToDouble(AppConst.TIME_CHECK_LOOP)).ToString(AppConst.DATE_FORMAT_TEMPLATE_4), String.Empty, String.Empty, 0, sms.id, AppConst.VIA_SMS_API }; result = await(new DapperHelper()).InsertSmsAsync(AppConfig.API_POST_SMS, values); if (result.code != "0") { //cộng trả lại int count = 0 - sms.sms_count; result = await(new DapperHelper()).UpdateQuota(AppConfig.API_UPDATE_QUOTA, account.ACCOUNT_ID, AppConst.SMS_TYPE_CSKH, count); if (result.code != "0") { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "1", message = "System exception." }))); } } RedisHelper.Set(string.Format("PARTNER_CHECK_LOOP:{0},{1},{2},{3},{4}", account.ACCOUNT_ID, lstSender[0].sender_id, sms.phone, sms.message, sms.telco), currentDate.ToString()); } else { return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "6", message = "Phone send sms is loop" }))); } } } return(Ok(JsonConvert.SerializeObject(result))); } catch (Exception ex) { logger.Error(Utils.A("SendSMS", JsonConvert.SerializeObject(sms), ex)); return(Ok(JsonConvert.SerializeObject(new ReturnModel() { phone = sms.phone, code = "1", message = "System exception" }))); } }