PrizeDo.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. using Common;
  2. using Infrastructure;
  3. using LitJson;
  4. using Microsoft.Extensions.Localization;
  5. using Model;
  6. using NuGet.Packaging;
  7. using Services;
  8. namespace Util
  9. {
  10. public class PrizeDo
  11. {
  12. public static string batchNo = "";
  13. //发奖入口
  14. public static void sendPrize(string content)
  15. {
  16. batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Function.get_Random(3);
  17. int projectId = 5;
  18. string priObjString = prizeObject(projectId, content);
  19. if(!string.IsNullOrEmpty(priObjString))
  20. {
  21. string[] priObjData = priObjString.Split('|');
  22. string priObj = priObjData[0];
  23. string priListIds = priObjData[1];
  24. prizeSend(projectId, priListIds, priObj, content);
  25. }
  26. }
  27. //接收入参
  28. public static Dictionary<string, string> getRequestParams(int projectId, string content)
  29. {
  30. Dictionary<string, string> dic = new Dictionary<string, string>();
  31. JsonData jsonData = JsonMapper.ToObject(content);
  32. var paramService = App.GetService<IPriProjectParamService>();
  33. var paramList = paramService.GetList(m => m.projectId == projectId);
  34. foreach(var param in paramList)
  35. {
  36. dic.Add(param.fieldEnName, jsonData[param.fieldEnName].ToString());
  37. }
  38. return dic;
  39. }
  40. //数据库链接
  41. public static SqlSugarClient initDb(int projectId)
  42. {
  43. var databaseSetService = App.GetService<IPriDatabaseSetService>();
  44. var connectionString = databaseSetService.GetFirst(m => m.projectId == projectId);
  45. string server = connectionString.ipAddress;
  46. int port = connectionString.port;
  47. string user = connectionString.userId;
  48. string password = connectionString.password;
  49. string database = connectionString.databaseName;
  50. var db = new SqlSugarClient(new ConnectionConfig()
  51. {
  52. ConnectionString = "server=" + server + ";port=" + port + ";user=" + user + ";password=" + password + ";database=" + database + ";charset=utf8;",
  53. DbType = DbType.MySql,
  54. IsAutoCloseConnection = true,
  55. });
  56. return db;
  57. }
  58. //查询条件匹配数据
  59. public static Dictionary<string, object> condition(int projectId, string content)
  60. {
  61. Dictionary<string, object> dic = new Dictionary<string, object>();
  62. var queryTableService = App.GetService<IPriQueryTableService>();
  63. var queryFieldService = App.GetService<IPriQueryFieldService>();
  64. var returnFieldService = App.GetService<IPriReturnFieldService>();
  65. //查询表
  66. var queryTables = queryTableService.GetList(m => m.projectId == projectId);
  67. foreach(var queryTable in queryTables)
  68. {
  69. //查询返回字段
  70. string fields = "";
  71. var returnFields = returnFieldService.GetList(m => m.queryTableId == queryTable.id);
  72. foreach(var returnField in returnFields)
  73. {
  74. fields += returnField.fieldEnName + " " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  75. }
  76. fields = fields.TrimEnd(',');
  77. //查询匹配条件
  78. string condi = "";
  79. var queryFields = queryFieldService.GetList(m => m.queryTableId == queryTable.id);
  80. foreach(var queryField in queryFields)
  81. {
  82. string fieldEnName = queryField.fieldEnName;
  83. string fieldQueryKind = queryField.fieldQueryKind;
  84. string fieldQueryModel = queryField.fieldQueryModel;
  85. string fieldQueryValue = queryField.fieldQueryValue;
  86. string fieldQueryValueType = queryField.fieldQueryValueType;
  87. if(fieldQueryKind == "1") //模糊匹配
  88. {
  89. condi += " and " + fieldEnName + " like ";
  90. if(fieldQueryModel == "request_param")
  91. {
  92. Dictionary<string, string> req = getRequestParams(projectId, content);
  93. condi += "'%" + req[fieldQueryValue] + "%'";
  94. }
  95. else if(fieldQueryModel == "fixed_value")
  96. {
  97. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  98. }
  99. }
  100. else if(fieldQueryKind == "2") //精确匹配
  101. {
  102. if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is null";
  103. else
  104. {
  105. condi += " and " + fieldEnName + "=";
  106. string val = "";
  107. if(fieldQueryModel == "request_param")
  108. {
  109. Dictionary<string, string> req = getRequestParams(projectId, content);
  110. val = req[fieldQueryValue];
  111. }
  112. else if(fieldQueryModel == "fixed_value")
  113. {
  114. val = GetExpressionVal(fieldQueryValue);
  115. }
  116. if(fieldQueryValueType == "text")
  117. {
  118. val = "'" + val + "'";
  119. }
  120. condi += val;
  121. }
  122. }
  123. else if(fieldQueryKind == "3") //范围匹配
  124. {
  125. string[] val = fieldQueryValue.Split(':');
  126. if(fieldQueryModel == "request_param")
  127. {
  128. Dictionary<string, string> req = getRequestParams(projectId, content);
  129. if(fieldQueryValueType == "number")
  130. {
  131. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  132. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  133. }
  134. else
  135. {
  136. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  137. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  138. }
  139. }
  140. else if(fieldQueryModel == "fixed_value")
  141. {
  142. if(fieldQueryValueType == "number")
  143. {
  144. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  145. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  146. }
  147. else
  148. {
  149. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  150. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  151. }
  152. }
  153. }
  154. else if(fieldQueryKind == "4") //取反匹配
  155. {
  156. if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is not null";
  157. else
  158. {
  159. condi += " and " + fieldEnName + "!=";
  160. string val = "";
  161. if(fieldQueryModel == "request_param")
  162. {
  163. Dictionary<string, string> req = getRequestParams(projectId, content);
  164. val = req[fieldQueryValue];
  165. }
  166. else if(fieldQueryModel == "fixed_value")
  167. {
  168. val = GetExpressionVal(fieldQueryValue);
  169. }
  170. if(fieldQueryValueType == "text")
  171. {
  172. val = "'" + val + "'";
  173. }
  174. condi += val;
  175. }
  176. }
  177. }
  178. var db = initDb(projectId);
  179. string sql = "select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi;
  180. var items = db.Ado.GetDataTable(sql);
  181. if(items.Rows.Count > 0)
  182. {
  183. foreach(System.Data.DataColumn item in items.Columns)
  184. {
  185. dic.Add(item.ColumnName, items.Rows[0][item.ColumnName].ToString());
  186. }
  187. }
  188. }
  189. return dic;
  190. }
  191. //奖励发放对象
  192. public static string prizeObject(int projectId, string content)
  193. {
  194. Dictionary<string, object> dic = new Dictionary<string, object>();
  195. var prizeObjectTableService = App.GetService<IPriPrizeObjectTableService>();
  196. var prizeObjectFieldService = App.GetService<IPriPrizeObjectFieldService>();
  197. //查询表
  198. var prizeObjectTable = prizeObjectTableService.GetFirst(m => m.projectId == projectId) ?? new PriPrizeObjectTable();
  199. //查询返回字段
  200. string field = prizeObjectTable.selectField;
  201. //查询匹配条件
  202. string condi = "";
  203. var queryFields = prizeObjectFieldService.GetList(m => m.objectTableId == prizeObjectTable.id);
  204. foreach(var queryField in queryFields)
  205. {
  206. string fieldEnName = queryField.fieldEnName;
  207. string fieldQueryKind = queryField.fieldQueryKind;
  208. string fieldQueryModel = queryField.fieldQueryModel;
  209. string fieldQueryValue = queryField.fieldQueryValue;
  210. string fieldQueryValueType = queryField.fieldQueryValueType;
  211. if(fieldQueryKind == "1") //模糊匹配
  212. {
  213. condi += " and " + fieldEnName + " like ";
  214. if(fieldQueryModel == "request_param")
  215. {
  216. Dictionary<string, string> req = getRequestParams(projectId, content);
  217. condi += "'%" + req[fieldQueryValue] + "%'";
  218. }
  219. else if(fieldQueryModel == "fixed_value")
  220. {
  221. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  222. }
  223. }
  224. else if(fieldQueryKind == "2") //精确匹配
  225. {
  226. condi += " and " + fieldEnName + "=";
  227. string val = "";
  228. if(fieldQueryModel == "request_param")
  229. {
  230. Dictionary<string, string> req = getRequestParams(projectId, content);
  231. val = req[fieldQueryValue];
  232. }
  233. else if(fieldQueryModel == "fixed_value")
  234. {
  235. val = GetExpressionVal(fieldQueryValue);
  236. }
  237. if(fieldQueryValueType == "text")
  238. {
  239. val = "'" + val + "'";
  240. }
  241. condi += val;
  242. }
  243. else if(fieldQueryKind == "3") //范围匹配
  244. {
  245. string[] val = fieldQueryValue.Split(':');
  246. if(fieldQueryModel == "request_param")
  247. {
  248. Dictionary<string, string> req = getRequestParams(projectId, content);
  249. if(fieldQueryValueType == "number")
  250. {
  251. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  252. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  253. }
  254. else
  255. {
  256. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  257. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  258. }
  259. }
  260. else if(fieldQueryModel == "fixed_value")
  261. {
  262. if(fieldQueryValueType == "number")
  263. {
  264. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  265. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  266. }
  267. else
  268. {
  269. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  270. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  271. }
  272. }
  273. }
  274. else if(fieldQueryKind == "4") //取反匹配
  275. {
  276. condi += " and " + fieldEnName + "!=";
  277. string val = "";
  278. if(fieldQueryModel == "request_param")
  279. {
  280. Dictionary<string, string> req = getRequestParams(projectId, content);
  281. val = req[fieldQueryValue];
  282. }
  283. else if(fieldQueryModel == "fixed_value")
  284. {
  285. val = GetExpressionVal(fieldQueryValue);
  286. }
  287. if(fieldQueryValueType == "text")
  288. {
  289. val = "'" + val + "'";
  290. }
  291. condi += val;
  292. }
  293. }
  294. var db = initDb(projectId);
  295. var item = db.Ado.GetScalar("select " + field + " from " + prizeObjectTable.tableEnName + " where 1=1" + condi);
  296. if(item != null)
  297. {
  298. return item.ToString() + "|" + prizeObjectTable.prizeListIds;
  299. }
  300. return "";
  301. }
  302. //奖励列表
  303. public static List<PriList> prizeList(int projectId, string prizeIds)
  304. {
  305. List<int> ids = Tools.SpitIntArrary(prizeIds, ',').ToList();
  306. var priListService = App.GetService<IPriListService>();
  307. var priList = priListService.GetList(m => m.projectId == projectId && ids.Contains(m.id)).ToList();
  308. return priList;
  309. }
  310. //奖励发放
  311. public static void prizeSend(int projectId, string prizeIds, string prizeObjectId, string content)
  312. {
  313. var priList = prizeList(projectId, prizeIds);
  314. foreach(var sub in priList)
  315. {
  316. var recursionFlag = sub.recursionFlag; //是否递归
  317. if(recursionFlag)
  318. {
  319. var loopSetService = App.GetService<IPriLoopSetService>();
  320. var recursionStartTableService = App.GetService<IPriRecursionStartTableService>();
  321. var loopSet = loopSetService.GetFirst(m => m.listId == sub.id) ?? new PriLoopSet();
  322. var tableEnName = loopSet.tableEnName; //递归查询表
  323. var parentField = loopSet.parentField; //父字段
  324. var sonField = loopSet.sonField; //子字段
  325. var afterPrizeFlag = loopSet.afterPrizeFlag; //发奖后是否继续
  326. var db = initDb(projectId);
  327. string objId = prizeObjectId;
  328. bool op = true;
  329. while(!string.IsNullOrEmpty(objId) && objId != "0" && op)
  330. {
  331. if(loopCondition(projectId, sub, objId, content))
  332. {
  333. prizeSendDo(projectId, sub, objId, content);
  334. op = afterPrizeFlag;
  335. }
  336. var parent = db.Ado.GetScalar("select " + parentField + " from " + tableEnName + " where " + sonField + "=" + objId);
  337. if(parent != null)
  338. {
  339. objId = parent.ToString();
  340. }
  341. }
  342. }
  343. else
  344. {
  345. prizeSendDo(projectId, sub, prizeObjectId, content);
  346. }
  347. }
  348. }
  349. public static void prizeSendDo(int projectId, PriList sub, string prizeObjectId, string content)
  350. {
  351. var recordService = App.GetService<IPriRecordService>();
  352. var conditionService = App.GetService<IPriConditionService>();
  353. var returnFieldService = App.GetService<IPriReturnFieldService>();
  354. var queryTableService = App.GetService<IPriQueryTableService>();
  355. var amountSetService = App.GetService<IPriListAmountSetService>();
  356. //查询匹配条件
  357. var condiDic = condition(projectId, content);
  358. var prizeSourceField = sub.prizeSourceField; //奖励金额来源字段(对应条件匹配返回字段)
  359. var prizePercent = sub.prizePercent; //奖励比例
  360. var prizeAmount = sub.prizeAmount; //奖励固定值
  361. var prizeContent = sub.prizeContent; //奖励内容
  362. var conditionMode = sub.conditionMode; //条件模式
  363. var recursionFlag = sub.recursionFlag; //是否递归
  364. var conditions = conditionService.GetList(m => m.listId == sub.id);
  365. int allCount = conditions.Count; //所有奖励条件数
  366. int passCount = 0; //通过条件数
  367. foreach(var condition in conditions)
  368. {
  369. var returnFieldId = condition.returnFieldId; //条件返回字段
  370. var fieldQueryKind = condition.fieldQueryKind; //匹配条件
  371. var fieldQueryModel = condition.fieldQueryModel; //匹配方式
  372. var fieldQueryValue = condition.fieldQueryValue; //匹配值
  373. var fieldQueryValueType = condition.fieldQueryValueType; //匹配值类型
  374. var returnField = returnFieldService.GetFirst(m => m.id == returnFieldId);
  375. var queryTable = queryTableService.GetFirst(m => m.id == returnField.queryTableId);
  376. var checkObj = condiDic.ContainsKey(queryTable.tableEnName + "_" + returnField.fieldEnName) ? condiDic[queryTable.tableEnName + "_" + returnField.fieldEnName] : "0";
  377. var checkVal = fieldQueryValue;
  378. if(fieldQueryKind == "1") //模糊匹配
  379. {
  380. if(fieldQueryValueType == "text")
  381. {
  382. if(checkObj.ToString().Contains(GetExpressionVal(checkVal))) passCount += 1;
  383. }
  384. }
  385. else if(fieldQueryKind == "2") //精确匹配
  386. {
  387. if(fieldQueryValueType == "int")
  388. {
  389. if(int.Parse(Function.CheckInt(checkObj.ToString())) == int.Parse(Function.CheckNum(checkVal))) passCount += 1;
  390. }
  391. else if(fieldQueryValueType == "number")
  392. {
  393. if(decimal.Parse(Function.CheckNum(checkObj.ToString())) == decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  394. }
  395. else
  396. {
  397. if(checkObj.ToString() == GetExpressionVal(checkVal)) passCount += 1;
  398. }
  399. }
  400. else if(fieldQueryKind == "3") //范围匹配
  401. {
  402. string[] val = checkVal.Split(':');
  403. if(fieldQueryModel == "request_param")
  404. {
  405. Dictionary<string, string> req = getRequestParams(projectId, content);
  406. if(fieldQueryValueType == "number")
  407. {
  408. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (decimal)checkObj >= decimal.Parse(Function.CheckNum(req[val[0]])) && (decimal)checkObj <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
  409. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (decimal)checkObj >= decimal.Parse(Function.CheckNum(req[val[0]]))) passCount += 1;
  410. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (decimal)checkObj <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
  411. }
  412. else if(fieldQueryValueType == "int")
  413. {
  414. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (int)checkObj >= int.Parse(Function.CheckInt(req[val[0]])) && (int)checkObj <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
  415. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (int)checkObj >= int.Parse(Function.CheckInt(req[val[0]]))) passCount += 1;
  416. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (int)checkObj <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
  417. }
  418. else if(fieldQueryValueType.StartsWith("date"))
  419. {
  420. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (DateTime)checkObj >= DateTime.Parse(req[val[0]]) && (DateTime)checkObj <= DateTime.Parse(req[val[1]])) passCount += 1;
  421. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (DateTime)checkObj >= DateTime.Parse(req[val[0]])) passCount += 1;
  422. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (DateTime)checkObj <= DateTime.Parse(req[val[1]])) passCount += 1;
  423. }
  424. }
  425. else if(fieldQueryModel == "fixed_value")
  426. {
  427. if(fieldQueryValueType == "number")
  428. {
  429. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && (decimal)checkObj >= decimal.Parse(Function.CheckNum(val[0])) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  430. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0]))) passCount += 1;
  431. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  432. }
  433. else if(fieldQueryValueType == "int")
  434. {
  435. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && (int)checkObj >= int.Parse(Function.CheckInt(val[0])) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  436. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0]))) passCount += 1;
  437. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  438. }
  439. else if(fieldQueryValueType.StartsWith("date"))
  440. {
  441. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && (DateTime)checkObj >= DateTime.Parse(GetExpressionVal(val[0])) && (DateTime)checkObj <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  442. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && (DateTime)checkObj >= DateTime.Parse(GetExpressionVal(val[0]))) passCount += 1;
  443. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && (DateTime)checkObj <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  444. }
  445. }
  446. }
  447. else if(fieldQueryKind == "4") //取反匹配
  448. {
  449. if(fieldQueryValueType == "int")
  450. {
  451. if((int)checkObj != int.Parse(Function.CheckInt(checkVal))) passCount += 1;
  452. }
  453. else if(fieldQueryValueType == "number")
  454. {
  455. if((decimal)checkObj != decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  456. }
  457. else if(fieldQueryValueType == "text")
  458. {
  459. if(checkObj.ToString() != GetExpressionVal(checkVal)) passCount += 1;
  460. }
  461. }
  462. }
  463. bool op = false;
  464. if(conditionMode == "all" && passCount == allCount) op = true;
  465. else if(conditionMode == "one" && passCount == 1) op = true;
  466. if(op) //满足条件
  467. {
  468. decimal number = 0;
  469. if(prizeSourceField.Contains("/"))
  470. {
  471. number = decimal.Parse(prizeSourceField.Split('/')[1]);
  472. prizeSourceField = prizeSourceField.Split('/')[0];
  473. }
  474. string prizeSourceData = condiDic.ContainsKey(prizeSourceField) ? condiDic[prizeSourceField].ToString() : "0";
  475. if(prizeSourceField.Contains("/") && number > 0)
  476. {
  477. decimal prizeSourceDataNum = decimal.Parse(prizeSourceData) / number;
  478. prizeSourceData = prizeSourceDataNum.ToString("f2");
  479. }
  480. decimal prizeAmt = 0;
  481. var amountSet = amountSetService.GetFirst(m => m.listId == sub.id && m.prizeSourceField == prizeSourceData);
  482. if(amountSet != null)
  483. {
  484. prizePercent = amountSet.prizePercent;
  485. prizeAmount = amountSet.prizeAmount;
  486. }
  487. if(prizePercent > 0) prizeAmt += decimal.Parse(Function.CheckNum(prizeSourceData)) * prizePercent;
  488. if(prizeAmount > 0) prizeAmt += prizeAmount;
  489. if(prizeAmt > 0)
  490. {
  491. var req = getRequestParams(projectId, content);
  492. var requestParamField = req[sub.requestParamField];
  493. var sendFlag = recordService.Any(m => m.prizeObjId == prizeObjectId && m.listId == sub.id && m.requestParamField == requestParamField);
  494. if(!sendFlag)
  495. {
  496. recordService.Add(new PriRecord()
  497. {
  498. createDate = DateTime.Now,
  499. projectId = projectId,
  500. listId = sub.id,
  501. prizeAmount = prizeAmt,
  502. prizeObjId = prizeObjectId,
  503. requestParamField = requestParamField,
  504. batchNo = batchNo,
  505. });
  506. // prizeToDatabase(projectId, content); //入库
  507. }
  508. }
  509. }
  510. }
  511. //递归条件判断
  512. public static bool loopCondition(int projectId, PriList sub, string objId, string content)
  513. {
  514. Dictionary<string, object> dic = new Dictionary<string, object>();
  515. var queryTableService = App.GetService<IPriRecursionStartTableService>();
  516. var queryFieldService = App.GetService<IPriRecursionStartFieldService>();
  517. int passCount = 0;
  518. //查询表
  519. var queryTables = queryTableService.GetList(m => m.listId == sub.id);
  520. int allCount = queryTables.Count;
  521. foreach(var queryTable in queryTables)
  522. {
  523. //查询返回字段
  524. string fields = "1";
  525. //查询匹配条件
  526. string condi = "";
  527. var queryFields = queryFieldService.GetList(m => m.objectTableId == queryTable.id);
  528. foreach(var queryField in queryFields)
  529. {
  530. string fieldEnName = queryField.fieldEnName;
  531. string fieldQueryKind = queryField.fieldQueryKind;
  532. string fieldQueryModel = queryField.fieldQueryModel;
  533. string fieldQueryValue = queryField.fieldQueryValue;
  534. if(fieldQueryModel == "loop_field") fieldQueryValue = objId;
  535. string fieldQueryValueType = queryField.fieldQueryValueType;
  536. if(fieldQueryKind == "1") //模糊匹配
  537. {
  538. condi += " and " + fieldEnName + " like ";
  539. if(fieldQueryModel == "request_param")
  540. {
  541. Dictionary<string, string> req = getRequestParams(projectId, content);
  542. condi += "'%" + req[fieldQueryValue] + "%'";
  543. }
  544. else if(fieldQueryModel == "fixed_value")
  545. {
  546. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  547. }
  548. }
  549. else if(fieldQueryKind == "2") //精确匹配
  550. {
  551. condi += " and " + fieldEnName + "=";
  552. string val = fieldQueryValue;
  553. if(fieldQueryModel == "request_param")
  554. {
  555. Dictionary<string, string> req = getRequestParams(projectId, content);
  556. val = req[fieldQueryValue];
  557. }
  558. else if(fieldQueryModel == "fixed_value")
  559. {
  560. val = GetExpressionVal(fieldQueryValue);
  561. }
  562. if(fieldQueryValueType == "text")
  563. {
  564. val = "'" + val + "'";
  565. }
  566. condi += val;
  567. }
  568. else if(fieldQueryKind == "3") //范围匹配
  569. {
  570. string[] val = fieldQueryValue.Split(':');
  571. if(fieldQueryModel == "request_param")
  572. {
  573. Dictionary<string, string> req = getRequestParams(projectId, content);
  574. if(fieldQueryValueType == "number")
  575. {
  576. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  577. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  578. }
  579. else
  580. {
  581. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  582. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  583. }
  584. }
  585. else if(fieldQueryModel == "fixed_value")
  586. {
  587. if(fieldQueryValueType == "number")
  588. {
  589. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  590. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  591. }
  592. else
  593. {
  594. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  595. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  596. }
  597. }
  598. }
  599. else if(fieldQueryKind == "4") //取反匹配
  600. {
  601. condi += " and " + fieldEnName + "!=";
  602. string val = "";
  603. if(fieldQueryModel == "request_param")
  604. {
  605. Dictionary<string, string> req = getRequestParams(projectId, content);
  606. val = req[fieldQueryValue];
  607. }
  608. else if(fieldQueryModel == "fixed_value")
  609. {
  610. val = GetExpressionVal(fieldQueryValue);
  611. }
  612. if(fieldQueryValueType == "text")
  613. {
  614. val = "'" + val + "'";
  615. }
  616. condi += val;
  617. }
  618. }
  619. var db = initDb(projectId);
  620. var item = db.Ado.GetScalar("select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi);
  621. if(item != null) passCount += 1;
  622. }
  623. bool op = false;
  624. if(sub.conditionMode == "all" && passCount == allCount) op = true;
  625. else if(sub.conditionMode == "one" && passCount == 1) op = true;
  626. return op;
  627. }
  628. //奖励入库
  629. public static void prizeToDatabase(int projectId, string content)
  630. {
  631. var prizeInTableService = App.GetService<IPriPrizeInTableService>();
  632. var prizeInFieldService = App.GetService<IPriPrizeInFieldService>();
  633. var prizeInQueryFieldService = App.GetService<IPriPrizeInQueryFieldService>();
  634. var db = initDb(projectId);
  635. //入库表
  636. var prizeInTables = prizeInTableService.GetList(m => m.projectId == projectId);
  637. foreach(var prizeInTable in prizeInTables)
  638. {
  639. Dictionary<string, object> doFields = new Dictionary<string, object>();
  640. //入库字段
  641. var prizeInFields = prizeInFieldService.GetList(m => m.inTableId == prizeInTable.id);
  642. foreach(var prizeInField in prizeInFields)
  643. {
  644. string fieldEnName = prizeInField.fieldEnName;
  645. string fieldQueryModel = prizeInField.fieldQueryModel;
  646. string fieldQueryValue = prizeInField.fieldQueryValue;
  647. string fieldQueryValueType = prizeInField.fieldQueryValueType;
  648. if(fieldQueryModel == "request_param")
  649. {
  650. Dictionary<string, string> req = getRequestParams(projectId, content);
  651. fieldQueryValue = req[fieldQueryValue];
  652. }
  653. else if(fieldQueryModel == "fixed_value")
  654. {
  655. fieldQueryValue = GetExpressionVal(fieldQueryValue);
  656. }
  657. if(fieldQueryValueType == "text") doFields.Add(fieldEnName, fieldQueryValue);
  658. if(fieldQueryValueType == "int") doFields.Add(fieldEnName, int.Parse(fieldQueryValue));
  659. if(fieldQueryValueType == "number") doFields.Add(fieldEnName, decimal.Parse(fieldQueryValue));
  660. if(fieldQueryValueType == "datetime") doFields.Add(fieldEnName, DateTime.Parse(fieldQueryValue));
  661. if(fieldQueryValueType == "bool") doFields.Add(fieldEnName, bool.Parse(fieldQueryValue));
  662. }
  663. //查询匹配条件
  664. string condi = "";
  665. var queryFields = prizeInQueryFieldService.GetList(m => m.inTableId == prizeInTable.id);
  666. foreach(var queryField in queryFields)
  667. {
  668. string fieldEnName = queryField.fieldEnName;
  669. string fieldQueryKind = queryField.fieldQueryKind;
  670. string fieldQueryModel = queryField.fieldQueryModel;
  671. string fieldQueryValue = queryField.fieldQueryValue;
  672. string fieldQueryValueType = queryField.fieldQueryValueType;
  673. if(fieldQueryKind == "1") //模糊匹配
  674. {
  675. condi += " and " + fieldEnName + " like ";
  676. if(fieldQueryModel == "request_param")
  677. {
  678. Dictionary<string, string> req = getRequestParams(projectId, content);
  679. condi += "'%" + req[fieldQueryValue] + "%'";
  680. }
  681. else if(fieldQueryModel == "fixed_value")
  682. {
  683. condi += "'%" + fieldQueryValue + "%'";
  684. }
  685. }
  686. else if(fieldQueryKind == "2") //精确匹配
  687. {
  688. condi += " and " + fieldEnName + "=";
  689. string val = "";
  690. if(fieldQueryModel == "request_param")
  691. {
  692. Dictionary<string, string> req = getRequestParams(projectId, content);
  693. val = req[fieldQueryValue];
  694. }
  695. else if(fieldQueryModel == "fixed_value")
  696. {
  697. val = fieldQueryValue;
  698. }
  699. if(fieldQueryValueType == "text")
  700. {
  701. val = "'" + val + "'";
  702. }
  703. condi += val;
  704. }
  705. else if(fieldQueryKind == "3") //范围匹配
  706. {
  707. string[] val = fieldQueryValue.Split(':');
  708. if(fieldQueryModel == "request_param")
  709. {
  710. Dictionary<string, string> req = getRequestParams(projectId, content);
  711. if(fieldQueryValueType == "number")
  712. {
  713. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  714. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  715. }
  716. else
  717. {
  718. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  719. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  720. }
  721. }
  722. else if(fieldQueryModel == "fixed_value")
  723. {
  724. if(fieldQueryValueType == "number")
  725. {
  726. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  727. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  728. }
  729. else
  730. {
  731. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
  732. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
  733. }
  734. }
  735. }
  736. else if(fieldQueryKind == "4") //取反匹配
  737. {
  738. condi += " and " + fieldEnName + "!=";
  739. string val = "";
  740. if(fieldQueryModel == "request_param")
  741. {
  742. Dictionary<string, string> req = getRequestParams(projectId, content);
  743. val = req[fieldQueryValue];
  744. }
  745. else if(fieldQueryModel == "fixed_value")
  746. {
  747. val = fieldQueryValue;
  748. }
  749. if(fieldQueryValueType == "text")
  750. {
  751. val = "'" + val + "'";
  752. }
  753. condi += val;
  754. }
  755. }
  756. if(prizeInTable.excuteKind == "add") db.Insertable(doFields).ExecuteCommand();
  757. if(prizeInTable.excuteKind == "update") db.Updateable(doFields).Where("1=1" + condi).ExecuteCommand();
  758. }
  759. }
  760. //固定值表达式
  761. public static string GetExpressionVal(string str)
  762. {
  763. if(str == "#{now}#") str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  764. if(str == "#{today}#") str = DateTime.Now.ToString("yyyy-MM-dd");
  765. if(str == "#{this_month}#") str = DateTime.Now.ToString("yyyy-MM");
  766. 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");
  767. 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");
  768. if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  769. if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  770. if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  771. if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  772. return str;
  773. }
  774. }
  775. }