Exemple #1
0
        /// <summary>
        /// 替换@{day(0)}、@{month(0)}、@{years(0)},@{last_day()}
        /// </summary>
        /// <param name="inStr"></param>
        /// <param name="nowDt"></param>
        /// <returns></returns>
        public static string ReplaceDataTime(string inStr, DateTime nowDt, string loginKey = null)
        {
            if (!string.IsNullOrEmpty(loginKey))
            {
                GlobalUser gu = Global.GetUser(loginKey);
                if (gu != null)
                {
                    inStr = inStr.Replace("@{DISTRICT_ID}", gu.DistrictId.ToString());
                    inStr = inStr.Replace("@{DISTRICT_CODE}", gu.DistrictCode.ToString());
                    inStr = inStr.Replace("@{USER_ID}", gu.UserId.ToString());
                    inStr = inStr.Replace("@{REGION}", gu.Region);
                    inStr = inStr.Replace("@{ALL_ROLE}", gu.GetRoleAllStr());
                    inStr = inStr.Replace("@{ALL_REGION}", gu.GetRegionLeveStr());
                    inStr = inStr.Replace("@{NOW_LEVEL_ID}", gu.LevelId.ToString());
                }
            }

            inStr = inStr.Replace("@{day}", "@{day(0)}");
            inStr = inStr.Replace("@{month}", "@{month(0)}");
            inStr = inStr.Replace("@{years}", "@{year(0)}");
            inStr = inStr.Replace("@{years", "@{year");
            inStr = inStr.Replace("@{last_day}", "@{last_day(0)}");

            var sql      = inStr;
            int nowPlace = 0;

            {
                int s = sql.IndexOf("@{day(");
                nowPlace = s;
                if (s > -1)
                {
                    int e = sql.IndexOf(")}", s);
                    while (e > s && s > -1)
                    {
                        s = s + 6;
                        int per = 0;
                        if (e > s)
                        {
                            per = Convert.ToInt32(sql.Substring(s, e - s));
                        }
                        sql = sql.Replace("@{day(" + per + ")}", nowDt.AddDays(per).ToString("yyyyMMdd"));
                        if (per == 0)
                        {
                            sql = sql.Replace("@{day()}", nowDt.AddDays(per).ToString("yyyyMMdd"));
                        }

                        s = sql.IndexOf("@{day(");
                        if (nowPlace == s)
                        {
                            return("");
                        }
                        nowPlace = s;
                        if (s > -1)
                        {
                            e = sql.IndexOf(")}", s);
                        }
                    }
                }
            }

            {
                int s = sql.IndexOf("@{month(");
                nowPlace = s;
                if (s > -1)
                {
                    int e = sql.IndexOf(")}", s);
                    while (e > s && s > -1)
                    {
                        s = s + 8;
                        int per = 0;
                        if (e > s)
                        {
                            per = Convert.ToInt32(sql.Substring(s, e - s));
                        }
                        sql = sql.Replace("@{month(" + per + ")}", nowDt.AddMonths(per).ToString("yyyyMM"));
                        if (per == 0)
                        {
                            sql = sql.Replace("@{month()}", nowDt.AddMonths(per).ToString("yyyyMM"));
                        }

                        s = sql.IndexOf("@{month(");
                        if (nowPlace == s)
                        {
                            return("");
                        }
                        nowPlace = s;
                        if (s > -1)
                        {
                            e = sql.IndexOf(")}", s);
                        }
                    }
                }
            }
            {
                int s = sql.IndexOf("@{year(");
                nowPlace = s;
                if (s > -1)
                {
                    int e = sql.IndexOf(")}", s);
                    while (e > s && s > -1)
                    {
                        s = s + 7;
                        int per = 0;
                        if (e > s)
                        {
                            per = Convert.ToInt32(sql.Substring(s, e - s));
                        }
                        sql = sql.Replace("@{year(" + per + ")}", nowDt.AddYears(per).ToString("yyyy"));
                        if (per == 0)
                        {
                            sql = sql.Replace("@{year()}", nowDt.AddYears(per).ToString("yyyy"));
                        }

                        s = sql.IndexOf("@{year(");
                        if (nowPlace == s)
                        {
                            return("");
                        }
                        nowPlace = s;
                        if (s > -1)
                        {
                            e = sql.IndexOf(")}", s);
                        }
                    }
                }
            }

            {
                int s = sql.IndexOf("@{last_day(");
                nowPlace = s;
                if (s > -1)
                {
                    int e = sql.IndexOf(")}", s);
                    while (e > s && s > -1)
                    {
                        s = s + 11;
                        int per = 0;
                        if (e > s)
                        {
                            per = Convert.ToInt32(sql.Substring(s, e - s));
                        }

                        DateTime temp = new DateTime(nowDt.Year, nowDt.Month, 1);
                        var      tmpV = temp.AddMonths(per + 1).AddDays(-1).ToString("yyyyMMdd");

                        sql = sql.Replace("@{last_day(" + per + ")}", tmpV);
                        if (per == 0)
                        {
                            sql = sql.Replace("@{last_day()}", tmpV);
                        }

                        s = sql.IndexOf("@{last_day(");
                        if (nowPlace == s)
                        {
                            return("");
                        }
                        nowPlace = s;
                        if (s > -1)
                        {
                            e = sql.IndexOf(")}", s);
                        }
                    }
                }
            }

            return(sql);
        }
Exemple #2
0
        /// <summary>
        /// 替换默认参数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="gu"></param>
        /// <returns></returns>
        public string ReplacePer(string sql, GlobalUser gu, IList <QueryPara> paraList)
        {
            try
            {
                sql = sql.Replace("@{DISTRICT_ID}", (paraList.SingleOrDefault(x => x.ParaName == "DISTRICT_ID") == null) ? gu.DistrictId.ToString() : paraList.SingleOrDefault(x => x.ParaName == "DISTRICT_ID").Value);
                sql = sql.Replace("@{DISTRICT_CODE}", (paraList.SingleOrDefault(x => x.ParaName == "DISTRICT_CODE") == null) ? gu.DistrictCode.ToString() : paraList.SingleOrDefault(x => x.ParaName == "DISTRICT_CODE").Value);
                sql = sql.Replace("@{USER_ID}", (paraList.SingleOrDefault(x => x.ParaName == "USER_ID") == null) ? gu.UserId.ToString() : paraList.SingleOrDefault(x => x.ParaName == "USER_ID").Value);
                sql = sql.Replace("@{REGION}", (paraList.SingleOrDefault(x => x.ParaName == "REGION") == null) ? gu.Region : paraList.SingleOrDefault(x => x.ParaName == "REGION").Value);
                sql = sql.Replace("@{ALL_ROLE}", (paraList.SingleOrDefault(x => x.ParaName == "ALL_ROLE") == null) ? gu.GetRoleAllStr() : paraList.SingleOrDefault(x => x.ParaName == "ALL_ROLE").Value);
                sql = sql.Replace("@{ALL_REGION}", (paraList.SingleOrDefault(x => x.ParaName == "ALL_REGION") == null) ? gu.GetRegionLeveStr() : paraList.SingleOrDefault(x => x.ParaName == "ALL_REGION").Value);
                sql = sql.Replace("@{RULE_REGION}", (paraList.SingleOrDefault(x => x.ParaName == "RULE_REGION") == null) ? gu.RuleRegionStr : paraList.SingleOrDefault(x => x.ParaName == "RULE_REGION").Value);
                sql = sql.Replace("@{NOW_LEVEL_ID}", (paraList.SingleOrDefault(x => x.ParaName == "NOW_LEVEL_ID") == null) ? gu.LevelId.ToString() : paraList.SingleOrDefault(x => x.ParaName == "NOW_LEVEL_ID").Value);
            }
            catch { }
            var nowDt = DateTime.Now;

            sql = Fun.ReplaceDataTime(sql, nowDt);

            return(sql);
        }