Browse Source

发奖算法基础代码编写

lcl 6 months ago
parent
commit
1d1254f555

+ 7 - 0
Model/Database/PriList.cs

@@ -101,6 +101,13 @@ namespace Model
         public string? prizeContent { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        [SugarColumn(ColumnDescription = "入参字段", Length = 50, ColumnName = "request_param_field")]
+        public string? requestParamField { get; set; }
+
+
 
     }
 }

+ 7 - 0
Model/Database/PriPrizeInField.cs

@@ -80,6 +80,13 @@ namespace Model
         public string? fieldQueryValue { get; set; }
 
 
+        /// <summary>
+        /// 匹配值类型
+        /// </summary>
+        [SugarColumn(ColumnDescription = "匹配值类型", Length = 20, ColumnName = "field_query_value_type")]
+        public string? fieldQueryValueType { get; set; }
+
+
         /// <summary>
         /// 入库表
         /// </summary>

+ 7 - 0
Model/Database/PriRecord.cs

@@ -87,6 +87,13 @@ namespace Model
         public string? batchNo { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        [SugarColumn(ColumnDescription = "入参字段", Length = 50, ColumnName = "request_param_field")]
+        public string? requestParamField { get; set; }
+
+
 
     }
 }

+ 6 - 0
Model/Vo/Admin/GetPriListListVo.cs

@@ -49,6 +49,12 @@ namespace Vo.Admin
         public string prizeContent { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        public string requestParamField { get; set; }
+
+
 
     }
 }

+ 6 - 0
Model/Vo/Admin/GetPriListQueryVo.cs

@@ -55,6 +55,12 @@ namespace Vo.Admin
         public string prizeContent { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        public string requestParamField { get; set; }
+
+
 
     }
 }

+ 6 - 0
Model/Vo/Admin/GetPriPrizeInFieldQueryVo.cs

@@ -37,6 +37,12 @@ namespace Vo.Admin
         public string fieldQueryValue { get; set; }
 
 
+        /// <summary>
+        /// 匹配值类型
+        /// </summary>
+        public string fieldQueryValueType { get; set; }
+
+
         /// <summary>
         /// 入库表
         /// </summary>

+ 6 - 0
Model/Vo/Admin/GetPriRecordListVo.cs

@@ -37,6 +37,12 @@ namespace Vo.Admin
         public string batchNo { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        public string requestParamField { get; set; }
+
+
 
     }
 }

+ 6 - 0
Model/Vo/Admin/GetPriRecordQueryVo.cs

@@ -37,6 +37,12 @@ namespace Vo.Admin
         public string batchNo { get; set; }
 
 
+        /// <summary>
+        /// 入参字段
+        /// </summary>
+        public string requestParamField { get; set; }
+
+
 
     }
 }

+ 196 - 22
Util/PrizeDo.cs

@@ -10,6 +10,7 @@ namespace Util
 {
     public class PrizeDo
     {
+        public static string batchNo = "";
         //接收入参
         public static Dictionary<string, string> getRequestParams(string content)
         {
@@ -83,7 +84,7 @@ namespace Util
                         }
                         else if(fieldQueryModel == "fixed_value")
                         {
-                            condi += "'%" + fieldQueryValue + "%'";
+                            condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
                         }
                     }
                     else if(fieldQueryKind == "2") //精确匹配
@@ -97,7 +98,7 @@ namespace Util
                         }
                         else if(fieldQueryModel == "fixed_value")
                         {
-                            val = fieldQueryValue;
+                            val = GetExpressionVal(fieldQueryValue);
                         }
                         if(fieldQueryValueType == "text")
                         {
@@ -131,8 +132,8 @@ namespace Util
                             }
                             else
                             {
-                                if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
-                                if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
+                                if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
+                                if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
                             }
                         }
                     }
@@ -147,7 +148,7 @@ namespace Util
                         }
                         else if(fieldQueryModel == "fixed_value")
                         {
-                            val = fieldQueryValue;
+                            val = GetExpressionVal(fieldQueryValue);
                         }
                         if(fieldQueryValueType == "text")
                         {
@@ -165,7 +166,7 @@ namespace Util
         }
 
         //奖励发放对象
-        public static Dictionary<string, object> prizeObject(int projectId, string content)
+        public static object prizeObject(int projectId, string content)
         {
             Dictionary<string, object> dic = new Dictionary<string, object>();
             var prizeObjectTableService = App.GetService<IPriPrizeObjectTableService>();
@@ -195,7 +196,7 @@ namespace Util
                     }
                     else if(fieldQueryModel == "fixed_value")
                     {
-                        condi += "'%" + fieldQueryValue + "%'";
+                        condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
                     }
                 }
                 else if(fieldQueryKind == "2") //精确匹配
@@ -209,7 +210,7 @@ namespace Util
                     }
                     else if(fieldQueryModel == "fixed_value")
                     {
-                        val = fieldQueryValue;
+                        val = GetExpressionVal(fieldQueryValue);
                     }
                     if(fieldQueryValueType == "text")
                     {
@@ -243,8 +244,8 @@ namespace Util
                         }
                         else
                         {
-                            if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
-                            if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
+                            if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
+                            if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
                         }
                     }
                 }
@@ -259,7 +260,7 @@ namespace Util
                     }
                     else if(fieldQueryModel == "fixed_value")
                     {
-                        val = fieldQueryValue;
+                        val = GetExpressionVal(fieldQueryValue);
                     }
                     if(fieldQueryValueType == "text")
                     {
@@ -271,9 +272,23 @@ namespace Util
 
             var db = initDb(projectId);
             var item = db.SqlQueryable<Dictionary<string, object>>("select " + field + " from " + prizeObjectTable.tableEnName + " where 1=1" + condi).First();
-            dic.AddRange(item);
 
-            var priList = prizeList(projectId, prizeObjectTable.prizeListIds);
+            return item[field];
+        }
+
+        //奖励列表
+        public static List<PriList> prizeList(int projectId, string prizeIds)
+        {
+            List<int> ids = Tools.SpitIntArrary(prizeIds, ',').ToList();
+            var priListService = App.GetService<IPriListService>();
+            var priList = priListService.GetList(m => m.projectId == projectId && ids.Contains(m.id)).ToList();
+            return priList;
+        }
+
+        //奖励发放
+        public static void prizeSend(int projectId, string prizeIds, string prizeObjectId, string content)
+        {
+            var priList = prizeList(projectId, prizeIds);
             foreach(var sub in priList)
             {
                 var prizePercent = sub.prizePercent; //奖励比例
@@ -285,9 +300,26 @@ namespace Util
                 {
 
                 }
-                else if(prizeAmount > 0)
+                if(prizeAmount > 0)
                 {
-                    
+                    var recordService = App.GetService<IPriRecordService>();
+                    var req = getRequestParams(content);
+                    var requestParamField = req[sub.requestParamField];
+                    var sendFlag = recordService.Any(m => m.prizeObjId == prizeObjectId && m.listId == sub.id && m.requestParamField == requestParamField);
+                    if(!sendFlag)
+                    {
+                        recordService.Add(new PriRecord()
+                        {
+                            createDate = DateTime.Now,
+                            projectId = projectId,
+                            listId = sub.id,
+                            prizeAmount = sub.prizeAmount,
+                            prizeObjId = prizeObjectId,
+                            requestParamField = requestParamField,
+                            batchNo = batchNo,
+                        });
+                        // prizeToDatabase(projectId, content); //入库
+                    }
                 }
                 else
                 {
@@ -303,16 +335,158 @@ namespace Util
                     }
                 }
             }
-            return dic;
         }
 
-        //奖励列表
-        public static List<PriList> prizeList(int projectId, string prizeIds)
+        //奖励入库
+        public static void prizeToDatabase(int projectId, string content)
         {
-            List<int> ids = Tools.SpitIntArrary(prizeIds, ',').ToList();
-            var priListService = App.GetService<IPriListService>();
-            var priList = priListService.GetList(m => m.projectId == projectId && ids.Contains(m.id)).ToList();
-            return priList;
+            var prizeInTableService = App.GetService<IPriPrizeInTableService>();
+            var prizeInFieldService = App.GetService<IPriPrizeInFieldService>();
+            var prizeInQueryFieldService = App.GetService<IPriPrizeInQueryFieldService>();
+            var db = initDb(projectId);
+
+            //入库表
+            var prizeInTables = prizeInTableService.GetList(m => m.projectId == projectId);
+            foreach(var prizeInTable in prizeInTables)
+            {
+                Dictionary<string, object> doFields = new Dictionary<string, object>();
+                //入库字段
+                var prizeInFields = prizeInFieldService.GetList(m => m.inTableId == prizeInTable.id);
+                foreach(var prizeInField in prizeInFields)
+                {
+                    string fieldEnName = prizeInField.fieldEnName;
+                    string fieldQueryModel = prizeInField.fieldQueryModel;
+                    string fieldQueryValue = prizeInField.fieldQueryValue;
+                    string fieldQueryValueType = prizeInField.fieldQueryValueType;
+                    if(fieldQueryModel == "request_param")
+                    {
+                        Dictionary<string, string> req = getRequestParams(content);
+                        fieldQueryValue = req[fieldQueryValue];
+                    }
+                    else if(fieldQueryModel == "fixed_value")
+                    {
+                        fieldQueryValue = GetExpressionVal(fieldQueryValue);
+                    }
+                    if(fieldQueryValueType == "text") doFields.Add(fieldEnName, fieldQueryValue);
+                    if(fieldQueryValueType == "int") doFields.Add(fieldEnName, int.Parse(fieldQueryValue));
+                    if(fieldQueryValueType == "number") doFields.Add(fieldEnName, decimal.Parse(fieldQueryValue));
+                    if(fieldQueryValueType == "datetime") doFields.Add(fieldEnName, DateTime.Parse(fieldQueryValue));
+                    if(fieldQueryValueType == "bool") doFields.Add(fieldEnName, bool.Parse(fieldQueryValue));
+                }
+
+                //查询匹配条件
+                string condi = "";
+                var queryFields = prizeInQueryFieldService.GetList(m => m.inTableId == prizeInTable.id);
+                foreach(var queryField in queryFields)
+                {
+                    string fieldEnName = queryField.fieldEnName;
+                    string fieldQueryKind = queryField.fieldQueryKind;
+                    string fieldQueryModel = queryField.fieldQueryModel;
+                    string fieldQueryValue = queryField.fieldQueryValue;
+                    string fieldQueryValueType = queryField.fieldQueryValueType;
+                    if(fieldQueryKind == "1") //模糊匹配
+                    {
+                        condi += " and " + fieldEnName + " like ";
+                        if(fieldQueryModel == "request_param")
+                        {
+                            Dictionary<string, string> req = getRequestParams(content);
+                            condi += "'%" + req[fieldQueryValue] + "%'";
+                        }
+                        else if(fieldQueryModel == "fixed_value")
+                        {
+                            condi += "'%" + fieldQueryValue + "%'";
+                        }
+                    }
+                    else if(fieldQueryKind == "2") //精确匹配
+                    {
+                        condi += " and " + fieldEnName + "=";
+                        string val = "";
+                        if(fieldQueryModel == "request_param")
+                        {
+                            Dictionary<string, string> req = getRequestParams(content);
+                            val = req[fieldQueryValue];
+                        }
+                        else if(fieldQueryModel == "fixed_value")
+                        {
+                            val = fieldQueryValue;
+                        }
+                        if(fieldQueryValueType == "text")
+                        {
+                            val = "'" + val + "'";
+                        }
+                        condi += val;
+                    }
+                    else if(fieldQueryKind == "3") //范围匹配
+                    {
+                        string[] val = fieldQueryValue.Split(':');
+                        if(fieldQueryModel == "request_param")
+                        {
+                            Dictionary<string, string> req = getRequestParams(content);
+                            if(fieldQueryValueType == "number")
+                            {
+                                if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
+                                if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
+                            }
+                            else
+                            {
+                                if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
+                                if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
+                            }
+                        }
+                        else if(fieldQueryModel == "fixed_value")
+                        {
+                            if(fieldQueryValueType == "number")
+                            {
+                                if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
+                                if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
+                            }
+                            else
+                            {
+                                if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
+                                if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
+                            }
+                        }
+                    }
+                    else if(fieldQueryKind == "4") //取反匹配
+                    {
+                        condi += " and " + fieldEnName + "!=";
+                        string val = "";
+                        if(fieldQueryModel == "request_param")
+                        {
+                            Dictionary<string, string> req = getRequestParams(content);
+                            val = req[fieldQueryValue];
+                        }
+                        else if(fieldQueryModel == "fixed_value")
+                        {
+                            val = fieldQueryValue;
+                        }
+                        if(fieldQueryValueType == "text")
+                        {
+                            val = "'" + val + "'";
+                        }
+                        condi += val;
+                    }
+                }
+
+                if(prizeInTable.excuteKind == "add") db.Insertable(doFields).ExecuteCommand();
+                if(prizeInTable.excuteKind == "update") db.Updateable(doFields).Where("1=1" + condi).ExecuteCommand();
+            }
+
+        }
+
+        //固定值表达式
+        public static string GetExpressionVal(string str)
+        {
+            if(str == "#{now}#") str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+            if(str == "#{today}#") str = DateTime.Now.ToString("yyyy-MM-dd");
+            if(str == "#{this_month}#") str = DateTime.Now.ToString("yyyy-MM");
+            if(str.StartsWith("#{now") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
+            if(str.StartsWith("#{now") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
+            if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
+            if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
+            if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
+            if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
+            return str;
         }
     }
 }