C#后台利用正则表达式查找匹配字符

时间:2022-04-14 20:18:59

/// <summary>
        /// 发送短信 系统固化短信
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="mobile"></param>
        /// <param name="subType"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public static Result SendMsgBySysInit(string memberId, string mobile, string subType, int groupId)
        {
            using (CRMEntities db = new CRMEntities())
            {
                var qTemp = db.TM_Act_CommunicationTemplet.Where(o => o.IsSysInit == true && o.Type == "SMS" && o.SubType == subType && o.DataGroupID == groupId).FirstOrDefault();
                string sendMsg = "";
                int templateId = 0;
                if (qTemp != null)
                {
                    sendMsg = qTemp.BasicContent;
                    templateId = qTemp.TempletID;
                }
                Regex reg = new Regex(@"{[a-zA-Z \u4e00-\u9fa5]+}");
                MatchCollection mc = reg.Matches(sendMsg);
                List<Dictionary<string, string>> lstDict = new List<Dictionary<string, string>>();
                for (int i = 0; i < mc.Count; i++)
                {
                    string strRgx = mc[i].Value.ToString().TrimStart('{').TrimEnd('}');
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    var qAlias = db.TD_SYS_FieldAlias.Where(o => o.FieldDesc == strRgx).FirstOrDefault();
                    if (qAlias != null)
                    {
                        dict["name"] = qAlias.FieldAlias;
                        if (qAlias.ControlType == "date")
                        {
                            DateTime qSqlRet = db.Database.SqlQuery<DateTime>(string.Format("select {2} from {0} where memberId='{1}'", qAlias.TableName, memberId, qAlias.FieldName)).FirstOrDefault();
                            dict["value"] = qSqlRet.ToShortDateString();
                        }
                        else
                        {
                            string qSql = db.Database.SqlQuery<string>(string.Format("select Convert(varchar,{2}) from {0} where memberId='{1}'", qAlias.TableName, memberId, qAlias.FieldName)).FirstOrDefault();
                            dict["value"] = qSql;
                        }
                        lstDict.Add(dict);
                    }
                }
                if (lstDict != null && lstDict.Count > 0)
                {
                    Result ret = SendSms(memberId, mobile, templateId, lstDict, sendMsg);
                    return ret;
                }
                else
                    return new Result(false, "参数不正确,不能发送短信!");
            }
        }

internal static Result SendSms(string memberId, string mobile, int templateId, object para, string message)
        {
            string defaultUser = "sys";
            try
            {
                using (CRMEntities db = new CRMEntities())
                {
                    StringBuilder sb = new StringBuilder();
                    List<Dictionary<string, string>> lstPara = (List<Dictionary<string, string>>)para;
                    sb.Append("{");
                    for (int i = 0; i < lstPara.Count; i++)
                    {
                        Dictionary<string, string> dict = lstPara[i];
                        sb.AppendFormat("\"{0\"}:\"{1}\"", dict["name"], dict["value"]);
                        if (i < lstPara.Count - 1)
                            sb.AppendFormat(",");
                    }
                    sb.Append("}");
                    var ent = new TM_Sys_SMSSendingQueue
                    {
                        Mobile = mobile,
                        Message = message,
                        MemberID = memberId,
                        AddedDate = DateTime.Now,
                        AddedUser = defaultUser,
                        Remark = "sys",
                        MsgPara = sb.ToString(),//JsonHelper.Serialize(para),
                        TempletID = templateId,
                        IsSent = false,
                    };
                    db.TM_Sys_SMSSendingQueue.Add(ent);
                    db.SaveChanges();

return new Result(true, "发送成功!");
                }
            }
            catch (Exception ex)
            {
                return new Result(false, ex.Message);
            }
        }