PrizeDo.cs 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. using System.Data;
  2. using Common;
  3. using Infrastructure;
  4. using LitJson;
  5. using Microsoft.Extensions.Localization;
  6. using Model;
  7. using NuGet.Packaging;
  8. using Services;
  9. namespace Util
  10. {
  11. public class PrizeDo
  12. {
  13. public static string batchNo = "";
  14. //发奖入口
  15. public static void sendPrize(string content)
  16. {
  17. batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Function.get_Random(3);
  18. int projectId = 5;
  19. string priObjString = prizeObject(projectId, content);
  20. if(!string.IsNullOrEmpty(priObjString))
  21. {
  22. string[] priObjData = priObjString.Split('|');
  23. string priObj = priObjData[0];
  24. string priListIds = priObjData[1];
  25. prizeSend(projectId, priListIds, priObj, content);
  26. }
  27. }
  28. //接收入参
  29. public static Dictionary<string, string> getRequestParams(int projectId, string content)
  30. {
  31. Dictionary<string, string> dic = new Dictionary<string, string>();
  32. JsonData jsonData = JsonMapper.ToObject(content);
  33. var paramService = App.GetService<IPriProjectParamService>();
  34. var paramList = paramService.GetList(m => m.projectId == projectId);
  35. foreach(var param in paramList)
  36. {
  37. if(content.Contains("\"" + param.fieldEnName + "\"")) dic.Add(param.fieldEnName, jsonData[param.fieldEnName].ToString());
  38. }
  39. return dic;
  40. }
  41. //数据库链接
  42. public static SqlSugarClient initDb(int id)
  43. {
  44. var databaseSetService = App.GetService<IPriDatabaseSetService>();
  45. var connectionString = databaseSetService.GetFirst(m => m.id == id);
  46. string server = connectionString.ipAddress;
  47. int port = connectionString.port;
  48. string user = connectionString.userId;
  49. string password = connectionString.password;
  50. string database = connectionString.databaseName;
  51. var db = new SqlSugarClient(new ConnectionConfig()
  52. {
  53. ConnectionString = "server=" + server + ";port=" + port + ";user=" + user + ";password=" + password + ";database=" + database + ";charset=utf8;",
  54. DbType = SqlSugar.DbType.MySql,
  55. IsAutoCloseConnection = true,
  56. });
  57. return db;
  58. }
  59. //查询条件匹配数据
  60. public static Dictionary<string, object> condition(int projectId, string content)
  61. {
  62. Dictionary<string, object> dic = new Dictionary<string, object>();
  63. var queryTableService = App.GetService<IPriQueryTableService>();
  64. var queryFieldService = App.GetService<IPriQueryFieldService>();
  65. var returnFieldService = App.GetService<IPriReturnFieldService>();
  66. //查询表
  67. var queryTables = queryTableService.GetList(m => m.projectId == projectId);
  68. foreach(var queryTable in queryTables)
  69. {
  70. //查询返回字段
  71. string fields = "";
  72. var returnFields = returnFieldService.GetList(m => m.queryTableId == queryTable.id);
  73. foreach(var returnField in returnFields)
  74. {
  75. if(returnField.fieldReturnKind == "2")
  76. {
  77. fields += "count(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  78. }
  79. else if(returnField.fieldReturnKind == "3")
  80. {
  81. fields += "sum(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  82. }
  83. else if(returnField.fieldReturnKind == "4")
  84. {
  85. fields += "avg(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  86. }
  87. else
  88. {
  89. fields += returnField.fieldEnName + " " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  90. }
  91. }
  92. fields = fields.TrimEnd(',');
  93. //查询匹配条件
  94. string condi = "";
  95. var queryFields = queryFieldService.GetList(m => m.queryTableId == queryTable.id);
  96. foreach(var queryField in queryFields)
  97. {
  98. string fieldEnName = queryField.fieldEnName;
  99. string fieldQueryKind = queryField.fieldQueryKind;
  100. string fieldQueryModel = queryField.fieldQueryModel;
  101. string fieldQueryValue = queryField.fieldQueryValue;
  102. string fieldQueryValueType = queryField.fieldQueryValueType;
  103. if(fieldQueryKind == "1") //模糊匹配
  104. {
  105. condi += " and " + fieldEnName + " like ";
  106. if(fieldQueryModel == "request_param")
  107. {
  108. Dictionary<string, string> req = getRequestParams(projectId, content);
  109. string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  110. condi += "'%" + val + "%'";
  111. }
  112. else if(fieldQueryModel == "fixed_value")
  113. {
  114. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  115. }
  116. else if(fieldQueryModel == "db_field")
  117. {
  118. condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
  119. }
  120. else if(fieldQueryModel == "query_field")
  121. {
  122. string val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
  123. condi += "'%" + val + "%'";
  124. }
  125. }
  126. else if(fieldQueryKind == "2") //精确匹配
  127. {
  128. if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is null";
  129. else
  130. {
  131. condi += " and " + fieldEnName + "=";
  132. string val = "";
  133. if(fieldQueryModel == "request_param")
  134. {
  135. Dictionary<string, string> req = getRequestParams(projectId, content);
  136. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  137. }
  138. else if(fieldQueryModel == "fixed_value")
  139. {
  140. val = GetExpressionVal(fieldQueryValue);
  141. }
  142. else if(fieldQueryModel == "db_field")
  143. {
  144. val = GetDbExpressionVal(fieldQueryValue);
  145. }
  146. else if(fieldQueryModel == "query_field")
  147. {
  148. val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
  149. }
  150. if(fieldQueryValueType == "text")
  151. {
  152. val = "'" + val + "'";
  153. }
  154. condi += val;
  155. }
  156. }
  157. else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
  158. {
  159. if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
  160. else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
  161. string val = "";
  162. string[] valList = fieldQueryValue.Split(',');
  163. if(fieldQueryModel == "request_param")
  164. {
  165. Dictionary<string, string> req = getRequestParams(projectId, content);
  166. foreach(string sub in valList)
  167. {
  168. if(fieldQueryValueType == "text")
  169. {
  170. val += "'" + req[sub] + "',";
  171. }
  172. else
  173. {
  174. val += req[sub] + ",";
  175. }
  176. }
  177. }
  178. else if(fieldQueryModel == "fixed_value")
  179. {
  180. foreach(string sub in valList)
  181. {
  182. if(fieldQueryValueType == "text")
  183. {
  184. val += "'" + GetExpressionVal(sub) + "',";
  185. }
  186. else
  187. {
  188. val += GetExpressionVal(sub) + ",";
  189. }
  190. }
  191. }
  192. else if(fieldQueryModel == "db_field")
  193. {
  194. foreach(string sub in valList)
  195. {
  196. if(fieldQueryValueType == "text")
  197. {
  198. val += "'" + GetDbExpressionVal(sub) + "',";
  199. }
  200. else
  201. {
  202. val += GetDbExpressionVal(sub) + ",";
  203. }
  204. }
  205. }
  206. else if(fieldQueryModel == "query_field")
  207. {
  208. foreach(string sub in valList)
  209. {
  210. string dicVal = dic.ContainsKey(sub) ? dic[sub].ToString() : "0";
  211. if(fieldQueryValueType == "text")
  212. {
  213. val += "'" + dicVal + "',";
  214. }
  215. else
  216. {
  217. val += dicVal + ",";
  218. }
  219. }
  220. }
  221. val = val.TrimEnd(',');
  222. condi += val + ")";
  223. }
  224. else if(fieldQueryKind == "3") //范围匹配
  225. {
  226. string[] val = fieldQueryValue.Split(':');
  227. if(fieldQueryModel == "request_param")
  228. {
  229. Dictionary<string, string> req = getRequestParams(projectId, content);
  230. if(fieldQueryValueType == "number")
  231. {
  232. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  233. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  234. }
  235. else
  236. {
  237. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  238. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  239. }
  240. }
  241. else if(fieldQueryModel == "fixed_value")
  242. {
  243. if(fieldQueryValueType == "number")
  244. {
  245. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  246. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  247. }
  248. else
  249. {
  250. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  251. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  252. }
  253. }
  254. else if(fieldQueryModel == "db_field")
  255. {
  256. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + GetDbExpressionVal(val[0]);
  257. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + GetDbExpressionVal(val[1]);
  258. }
  259. else if(fieldQueryModel == "query_field")
  260. {
  261. string dicValLeft = dic.ContainsKey(val[0]) ? dic[val[0]].ToString() : "0";
  262. string dicValRight = dic.ContainsKey(val[1]) ? dic[val[1]].ToString() : "0";
  263. if(fieldQueryValueType == "number")
  264. {
  265. if(!string.IsNullOrEmpty(dicValLeft)) condi += " and " + fieldEnName + ">=" + dicValLeft;
  266. if(!string.IsNullOrEmpty(dicValRight)) condi += " and " + fieldEnName + "<=" + dicValRight;
  267. }
  268. else
  269. {
  270. if(!string.IsNullOrEmpty(dicValLeft)) condi += " and " + fieldEnName + ">='" + GetExpressionVal(dicValLeft) + "'";
  271. if(!string.IsNullOrEmpty(dicValRight)) condi += " and " + fieldEnName + "<='" + GetExpressionVal(dicValRight) + "'";
  272. }
  273. }
  274. }
  275. else if(fieldQueryKind == "4") //取反匹配
  276. {
  277. if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is not null";
  278. else
  279. {
  280. condi += " and " + fieldEnName + "!=";
  281. string val = "";
  282. if(fieldQueryModel == "request_param")
  283. {
  284. Dictionary<string, string> req = getRequestParams(projectId, content);
  285. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  286. }
  287. else if(fieldQueryModel == "fixed_value")
  288. {
  289. val = GetExpressionVal(fieldQueryValue);
  290. }
  291. else if(fieldQueryModel == "db_field")
  292. {
  293. val = GetDbExpressionVal(fieldQueryValue);
  294. }
  295. else if(fieldQueryModel == "query_field")
  296. {
  297. val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
  298. }
  299. if(fieldQueryValueType == "text")
  300. {
  301. val = "'" + val + "'";
  302. }
  303. condi += val;
  304. }
  305. }
  306. }
  307. var db = initDb(queryTable.databaseId);
  308. string sql = "select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi;
  309. var items = db.Ado.GetDataTable(sql);
  310. if(items.Rows.Count > 0)
  311. {
  312. foreach(System.Data.DataColumn item in items.Columns)
  313. {
  314. if(!dic.ContainsKey(item.ColumnName)) dic.Add(item.ColumnName, items.Rows[0][item.ColumnName].ToString());
  315. }
  316. }
  317. }
  318. return dic;
  319. }
  320. //奖励发放对象
  321. public static string prizeObject(int projectId, string content)
  322. {
  323. Dictionary<string, object> dic = new Dictionary<string, object>();
  324. var prizeObjectTableService = App.GetService<IPriPrizeObjectTableService>();
  325. var prizeObjectFieldService = App.GetService<IPriPrizeObjectFieldService>();
  326. //查询表
  327. var prizeObjectTable = prizeObjectTableService.GetFirst(m => m.projectId == projectId) ?? new PriPrizeObjectTable();
  328. //查询返回字段
  329. string field = prizeObjectTable.selectField;
  330. //查询匹配条件
  331. string condi = "";
  332. var queryFields = prizeObjectFieldService.GetList(m => m.objectTableId == prizeObjectTable.id);
  333. foreach(var queryField in queryFields)
  334. {
  335. string fieldEnName = queryField.fieldEnName;
  336. string fieldQueryKind = queryField.fieldQueryKind;
  337. string fieldQueryModel = queryField.fieldQueryModel;
  338. string fieldQueryValue = queryField.fieldQueryValue;
  339. string fieldQueryValueType = queryField.fieldQueryValueType;
  340. if(fieldQueryKind == "1") //模糊匹配
  341. {
  342. condi += " and " + fieldEnName + " like ";
  343. if(fieldQueryModel == "request_param")
  344. {
  345. Dictionary<string, string> req = getRequestParams(projectId, content);
  346. string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  347. condi += "'%" + val + "%'";
  348. }
  349. else if(fieldQueryModel == "fixed_value")
  350. {
  351. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  352. }
  353. else if(fieldQueryModel == "db_field")
  354. {
  355. condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
  356. }
  357. }
  358. else if(fieldQueryKind == "2") //精确匹配
  359. {
  360. condi += " and " + fieldEnName + "=";
  361. string val = "";
  362. if(fieldQueryModel == "request_param")
  363. {
  364. Dictionary<string, string> req = getRequestParams(projectId, content);
  365. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  366. }
  367. else if(fieldQueryModel == "fixed_value")
  368. {
  369. val = GetExpressionVal(fieldQueryValue);
  370. }
  371. else if(fieldQueryModel == "db_field")
  372. {
  373. val = GetDbExpressionVal(fieldQueryValue);
  374. }
  375. if(fieldQueryValueType == "text")
  376. {
  377. val = "'" + val + "'";
  378. }
  379. condi += val;
  380. }
  381. else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
  382. {
  383. if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
  384. else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
  385. string val = "";
  386. string[] valList = fieldQueryValue.Split(',');
  387. if(fieldQueryModel == "request_param")
  388. {
  389. Dictionary<string, string> req = getRequestParams(projectId, content);
  390. foreach(string sub in valList)
  391. {
  392. if(fieldQueryValueType == "text")
  393. {
  394. val += "'" + req[sub] + "',";
  395. }
  396. else
  397. {
  398. val += req[sub] + ",";
  399. }
  400. }
  401. }
  402. else if(fieldQueryModel == "fixed_value")
  403. {
  404. foreach(string sub in valList)
  405. {
  406. if(fieldQueryValueType == "text")
  407. {
  408. val += "'" + GetExpressionVal(sub) + "',";
  409. }
  410. else
  411. {
  412. val += GetExpressionVal(sub) + ",";
  413. }
  414. }
  415. }
  416. else if(fieldQueryModel == "db_field")
  417. {
  418. foreach(string sub in valList)
  419. {
  420. if(fieldQueryValueType == "text")
  421. {
  422. val += "'" + GetDbExpressionVal(sub) + "',";
  423. }
  424. else
  425. {
  426. val += GetDbExpressionVal(sub) + ",";
  427. }
  428. }
  429. }
  430. val = val.TrimEnd(',');
  431. condi += val + ")";
  432. }
  433. else if(fieldQueryKind == "3") //范围匹配
  434. {
  435. string[] val = fieldQueryValue.Split(':');
  436. if(fieldQueryModel == "request_param")
  437. {
  438. Dictionary<string, string> req = getRequestParams(projectId, content);
  439. if(fieldQueryValueType == "number")
  440. {
  441. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  442. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  443. }
  444. else
  445. {
  446. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  447. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  448. }
  449. }
  450. else if(fieldQueryModel == "fixed_value")
  451. {
  452. if(fieldQueryValueType == "number")
  453. {
  454. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  455. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  456. }
  457. else
  458. {
  459. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  460. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  461. }
  462. }
  463. else if(fieldQueryModel == "db_field")
  464. {
  465. if(fieldQueryValueType == "number")
  466. {
  467. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  468. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  469. }
  470. else
  471. {
  472. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetDbExpressionVal(val[0]) + "'";
  473. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetDbExpressionVal(val[1]) + "'";
  474. }
  475. }
  476. }
  477. else if(fieldQueryKind == "4") //取反匹配
  478. {
  479. condi += " and " + fieldEnName + "!=";
  480. string val = "";
  481. if(fieldQueryModel == "request_param")
  482. {
  483. Dictionary<string, string> req = getRequestParams(projectId, content);
  484. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  485. }
  486. else if(fieldQueryModel == "fixed_value")
  487. {
  488. val = GetExpressionVal(fieldQueryValue);
  489. }
  490. else if(fieldQueryModel == "db_field")
  491. {
  492. val = GetDbExpressionVal(fieldQueryValue);
  493. }
  494. if(fieldQueryValueType == "text")
  495. {
  496. val = "'" + val + "'";
  497. }
  498. condi += val;
  499. }
  500. }
  501. var db = initDb(prizeObjectTable.databaseId);
  502. var item = db.Ado.GetScalar("select " + field + " from " + prizeObjectTable.tableEnName + " where 1=1" + condi);
  503. if(item != null)
  504. {
  505. return item.ToString() + "|" + prizeObjectTable.prizeListIds;
  506. }
  507. return "";
  508. }
  509. //奖励列表
  510. public static List<PriList> prizeList(int projectId, string prizeIds)
  511. {
  512. List<int> ids = Tools.SpitIntArrary(prizeIds, ',').ToList();
  513. var priListService = App.GetService<IPriListService>();
  514. var priList = priListService.GetList(m => m.projectId == projectId && ids.Contains(m.id)).ToList();
  515. return priList;
  516. }
  517. //奖励发放
  518. public static void prizeSend(int projectId, string prizeIds, string prizeObjectId, string content)
  519. {
  520. var loopSetService = App.GetService<IPriLoopSetService>();
  521. var recursionStartTableService = App.GetService<IPriRecursionStartTableService>();
  522. var recursionStartConditionService = App.GetService<IPriRecursionStartConditionService>();
  523. var priList = prizeList(projectId, prizeIds);
  524. foreach(var sub in priList)
  525. {
  526. var recursionFlag = sub.recursionFlag; //是否递归
  527. if(recursionFlag)
  528. {
  529. var loopSet = loopSetService.GetFirst(m => m.listId == sub.id) ?? new PriLoopSet();
  530. var recursionStartTable = recursionStartTableService.GetFirst(m => m.listId == sub.id) ?? new PriRecursionStartTable();
  531. var tableEnName = loopSet.tableEnName; //递归查询表
  532. var parentField = loopSet.parentField; //父字段
  533. var sonField = loopSet.sonField; //子字段
  534. var afterPrizeFlag = loopSet.afterPrizeFlag; //发奖后是否继续
  535. var db = initDb(recursionStartTable.databaseId);
  536. string objId = prizeObjectId;
  537. bool op = true;
  538. while(!string.IsNullOrEmpty(objId) && objId != "0" && op)
  539. {
  540. Dictionary<string, object> condiDic = loopCondition(projectId, sub, objId, content);
  541. if(condiDic.Count > 0)
  542. {
  543. bool prizeFlag = true;
  544. var conditions = recursionStartConditionService.GetList(m => m.loopSetId == loopSet.id);
  545. if(conditions.Count > 0)
  546. {
  547. int allCount = conditions.Count; //所有奖励条件数
  548. int passCount = 0; //通过条件数
  549. foreach(var condition in conditions)
  550. {
  551. var returnFieldId = condition.returnFieldId; //条件返回字段
  552. var fieldQueryKind = condition.fieldQueryKind; //匹配条件
  553. var fieldQueryModel = condition.fieldQueryModel; //匹配方式
  554. var fieldQueryValue = condition.fieldQueryValue; //匹配值
  555. var fieldQueryValueType = condition.fieldQueryValueType; //匹配值类型
  556. string checkObj = "";
  557. if(returnFieldId.Contains(","))
  558. {
  559. string[] returnFieldIdList = returnFieldId.Split(',');
  560. foreach(string subField in returnFieldIdList)
  561. {
  562. checkObj += condiDic.ContainsKey(subField) + ",";
  563. }
  564. checkObj = checkObj.TrimEnd(',');
  565. }
  566. else if (returnFieldId.Contains("+") || returnFieldId.Contains("-") || returnFieldId.Contains("*") || returnFieldId.Contains("/") || returnFieldId.Contains("(") || returnFieldId.Contains(")"))
  567. {
  568. string expresssion = returnFieldId;
  569. string[] returnFields = returnFieldId.Split(new char[] { '+', '-', '*', '/', '(', ')' });
  570. foreach(string returnField in returnFields)
  571. {
  572. string val = condiDic.ContainsKey(returnFieldId) ? condiDic[returnFieldId].ToString() : "0";
  573. expresssion = expresssion.Replace(returnField, val);
  574. }
  575. DataTable dt = new DataTable();
  576. checkObj = dt.Compute(expresssion, "false").ToString();
  577. }
  578. else
  579. {
  580. checkObj = condiDic.ContainsKey(returnFieldId) ? condiDic[returnFieldId].ToString() : "0";
  581. }
  582. var checkVal = fieldQueryValue;
  583. if(fieldQueryKind == "1") //模糊匹配
  584. {
  585. if(fieldQueryValueType == "text")
  586. {
  587. if(checkObj.ToString().Contains(GetExpressionVal(checkVal))) passCount += 1;
  588. }
  589. }
  590. else if(fieldQueryKind == "2") //精确匹配
  591. {
  592. if(fieldQueryValueType == "int")
  593. {
  594. if(int.Parse(Function.CheckInt(checkObj.ToString())) == int.Parse(Function.CheckNum(checkVal))) passCount += 1;
  595. }
  596. else if(fieldQueryValueType == "number")
  597. {
  598. if(decimal.Parse(Function.CheckNum(checkObj.ToString())) == decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  599. }
  600. else
  601. {
  602. if(checkObj.ToString() == GetExpressionVal(checkVal)) passCount += 1;
  603. }
  604. }
  605. else if(fieldQueryKind == "3") //范围匹配
  606. {
  607. string[] val = checkVal.Split(':');
  608. if(fieldQueryModel == "request_param")
  609. {
  610. Dictionary<string, string> req = getRequestParams(projectId, content);
  611. if(fieldQueryValueType == "number")
  612. {
  613. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) >= decimal.Parse(Function.CheckNum(req[val[0]])) && decimal.Parse(checkObj) <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
  614. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) >= decimal.Parse(Function.CheckNum(req[val[0]]))) passCount += 1;
  615. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
  616. }
  617. else if(fieldQueryValueType == "int")
  618. {
  619. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) >= int.Parse(Function.CheckInt(req[val[0]])) && int.Parse(checkObj) <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
  620. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) >= int.Parse(Function.CheckInt(req[val[0]]))) passCount += 1;
  621. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
  622. }
  623. else if(fieldQueryValueType.StartsWith("date"))
  624. {
  625. if(!string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) >= DateTime.Parse(req[val[0]]) && DateTime.Parse(checkObj) <= DateTime.Parse(req[val[1]])) passCount += 1;
  626. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) >= DateTime.Parse(req[val[0]])) passCount += 1;
  627. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) <= DateTime.Parse(req[val[1]])) passCount += 1;
  628. }
  629. }
  630. else if(fieldQueryModel == "fixed_value")
  631. {
  632. if(fieldQueryValueType == "number")
  633. {
  634. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(checkObj) >= decimal.Parse(Function.CheckNum(val[0])) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  635. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0]))) passCount += 1;
  636. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  637. }
  638. else if(fieldQueryValueType == "int")
  639. {
  640. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(checkObj) >= int.Parse(Function.CheckInt(val[0])) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  641. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0]))) passCount += 1;
  642. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  643. }
  644. else if(fieldQueryValueType.StartsWith("date"))
  645. {
  646. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj) >= DateTime.Parse(GetExpressionVal(val[0])) && DateTime.Parse(checkObj) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  647. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj) >= DateTime.Parse(GetExpressionVal(val[0]))) passCount += 1;
  648. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  649. }
  650. }
  651. }
  652. else if(fieldQueryKind == "4") //取反匹配
  653. {
  654. if(fieldQueryValueType == "int")
  655. {
  656. if(int.Parse(checkObj) != int.Parse(Function.CheckInt(checkVal))) passCount += 1;
  657. }
  658. else if(fieldQueryValueType == "number")
  659. {
  660. if(decimal.Parse(checkObj) != decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  661. }
  662. else if(fieldQueryValueType == "text")
  663. {
  664. if(checkObj.ToString() != GetExpressionVal(checkVal)) passCount += 1;
  665. }
  666. }
  667. }
  668. if(loopSet.conditionMode == "all" && passCount == allCount) prizeFlag = true;
  669. else if(loopSet.conditionMode == "one" && passCount == 1) prizeFlag = true;
  670. else prizeFlag = false;
  671. }
  672. if(prizeFlag)
  673. {
  674. prizeSendDo(projectId, sub, objId, content);
  675. op = afterPrizeFlag;
  676. }
  677. }
  678. var parent = db.Ado.GetScalar("select " + parentField + " from " + tableEnName + " where " + sonField + "=" + objId);
  679. if(parent != null)
  680. {
  681. objId = parent.ToString();
  682. }
  683. }
  684. }
  685. else
  686. {
  687. prizeSendDo(projectId, sub, prizeObjectId, content);
  688. }
  689. }
  690. }
  691. public static void prizeSendDo(int projectId, PriList sub, string prizeObjectId, string content)
  692. {
  693. var recordService = App.GetService<IPriRecordService>();
  694. var conditionService = App.GetService<IPriConditionService>();
  695. var returnFieldService = App.GetService<IPriReturnFieldService>();
  696. var queryTableService = App.GetService<IPriQueryTableService>();
  697. var amountSetService = App.GetService<IPriListAmountSetService>();
  698. //查询匹配条件
  699. var condiDic = condition(projectId, content);
  700. var prizeSourceField = sub.prizeSourceField; //奖励金额来源字段(对应条件匹配返回字段)
  701. var prizePercent = sub.prizePercent; //奖励比例
  702. var prizeAmount = sub.prizeAmount; //奖励固定值
  703. var prizeContent = sub.prizeContent; //奖励内容
  704. var conditionMode = sub.conditionMode; //条件模式
  705. var recursionFlag = sub.recursionFlag; //是否递归
  706. var conditions = conditionService.GetList(m => m.listId == sub.id);
  707. int allCount = conditions.Count; //所有奖励条件数
  708. int passCount = 0; //通过条件数
  709. foreach(var condition in conditions)
  710. {
  711. var returnFieldId = condition.returnFieldId; //条件返回字段
  712. var fieldQueryKind = condition.fieldQueryKind; //匹配条件
  713. var fieldQueryModel = condition.fieldQueryModel; //匹配方式
  714. var fieldQueryValue = condition.fieldQueryValue; //匹配值
  715. var fieldQueryValueType = condition.fieldQueryValueType; //匹配值类型
  716. var checkObj = condiDic.ContainsKey(returnFieldId) ? condiDic[returnFieldId] : "0";
  717. var checkVal = fieldQueryValue;
  718. if(fieldQueryKind == "1") //模糊匹配
  719. {
  720. if(fieldQueryValueType == "text")
  721. {
  722. if(checkObj.ToString().Contains(GetExpressionVal(checkVal))) passCount += 1;
  723. }
  724. }
  725. else if(fieldQueryKind == "2") //精确匹配
  726. {
  727. if(fieldQueryValueType == "int")
  728. {
  729. if(int.Parse(Function.CheckInt(checkObj.ToString())) == int.Parse(Function.CheckNum(checkVal))) passCount += 1;
  730. }
  731. else if(fieldQueryValueType == "number")
  732. {
  733. if(decimal.Parse(Function.CheckNum(checkObj.ToString())) == decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  734. }
  735. else
  736. {
  737. if(checkObj.ToString() == GetExpressionVal(checkVal)) passCount += 1;
  738. }
  739. }
  740. else if(fieldQueryKind == "3") //范围匹配
  741. {
  742. string[] val = checkVal.Split(':');
  743. if(fieldQueryModel == "request_param")
  744. {
  745. Dictionary<string, string> req = getRequestParams(projectId, content);
  746. if(fieldQueryValueType == "number")
  747. {
  748. 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;
  749. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (decimal)checkObj >= decimal.Parse(Function.CheckNum(req[val[0]]))) passCount += 1;
  750. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (decimal)checkObj <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
  751. }
  752. else if(fieldQueryValueType == "int")
  753. {
  754. 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;
  755. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (int)checkObj >= int.Parse(Function.CheckInt(req[val[0]]))) passCount += 1;
  756. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (int)checkObj <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
  757. }
  758. else if(fieldQueryValueType.StartsWith("date"))
  759. {
  760. 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;
  761. else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && (DateTime)checkObj >= DateTime.Parse(req[val[0]])) passCount += 1;
  762. else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && (DateTime)checkObj <= DateTime.Parse(req[val[1]])) passCount += 1;
  763. }
  764. }
  765. else if(fieldQueryModel == "fixed_value")
  766. {
  767. if(fieldQueryValueType == "number")
  768. {
  769. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0])) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  770. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0]))) passCount += 1;
  771. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
  772. }
  773. else if(fieldQueryValueType == "int")
  774. {
  775. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckNum(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0])) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  776. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0]))) passCount += 1;
  777. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
  778. }
  779. else if(fieldQueryValueType.StartsWith("date"))
  780. {
  781. if(!string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj.ToString()) >= DateTime.Parse(GetExpressionVal(val[0])) && DateTime.Parse(checkObj.ToString()) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  782. else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj.ToString()) >= DateTime.Parse(GetExpressionVal(val[0]))) passCount += 1;
  783. else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj.ToString()) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
  784. }
  785. }
  786. }
  787. else if(fieldQueryKind == "4") //取反匹配
  788. {
  789. if(fieldQueryValueType == "int")
  790. {
  791. if(int.Parse(Function.CheckNum(checkObj.ToString())) != int.Parse(Function.CheckInt(checkVal))) passCount += 1;
  792. }
  793. else if(fieldQueryValueType == "number")
  794. {
  795. if(decimal.Parse(Function.CheckNum(checkObj.ToString())) != decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
  796. }
  797. else if(fieldQueryValueType == "text")
  798. {
  799. if(checkObj.ToString() != GetExpressionVal(checkVal)) passCount += 1;
  800. }
  801. }
  802. }
  803. bool op = false;
  804. if(conditionMode == "all" && passCount == allCount) op = true;
  805. else if(conditionMode == "one" && passCount == 1) op = true;
  806. if(op) //满足条件
  807. {
  808. decimal number = 0;
  809. if(prizeSourceField.Contains("/"))
  810. {
  811. number = decimal.Parse(prizeSourceField.Split('/')[1]);
  812. prizeSourceField = prizeSourceField.Split('/')[0];
  813. }
  814. string prizeSourceData = "";
  815. if(prizeSourceField.Contains(","))
  816. {
  817. string[] prizeSourceFieldList = prizeSourceField.Split(',');
  818. foreach(string subField in prizeSourceFieldList)
  819. {
  820. prizeSourceData += condiDic.ContainsKey(subField) + ",";
  821. }
  822. prizeSourceData = prizeSourceData.TrimEnd(',');
  823. }
  824. else
  825. {
  826. prizeSourceData = condiDic.ContainsKey(prizeSourceField) ? condiDic[prizeSourceField].ToString() : "0";
  827. }
  828. if(string.IsNullOrEmpty(prizeSourceData)) prizeSourceData = "0";
  829. if(prizeSourceField.Contains("/") && number > 0)
  830. {
  831. decimal prizeSourceDataNum = decimal.Parse(prizeSourceData) / number;
  832. prizeSourceData = prizeSourceDataNum.ToString("f2");
  833. }
  834. decimal prizeAmt = 0;
  835. var amountSet = amountSetService.GetFirst(m => m.listId == sub.id && m.prizeSourceField == prizeSourceData);
  836. if(amountSet != null)
  837. {
  838. prizePercent = amountSet.prizePercent;
  839. prizeAmount = amountSet.prizeAmount;
  840. }
  841. if(prizeSourceData.Contains(","))
  842. {
  843. string checkField = prizeSourceData.Split(',')[0] + ",";
  844. amountSet = amountSetService.GetFirst(m => m.listId == sub.id && m.prizeSourceField.StartsWith(checkField) && m.prizeSourceField.EndsWith(",%"));
  845. if(amountSet != null)
  846. {
  847. prizePercent = amountSet.prizePercent;
  848. prizeAmount = amountSet.prizeAmount;
  849. prizeSourceData = prizeSourceData.Split(',')[1];
  850. }
  851. }
  852. if(prizePercent > 0) prizeAmt += decimal.Parse(Function.CheckNum(prizeSourceData)) * prizePercent;
  853. if(prizeAmount > 0) prizeAmt += prizeAmount;
  854. if(prizeAmt > 0)
  855. {
  856. var req = getRequestParams(projectId, content);
  857. var requestParamField = req[sub.requestParamField];
  858. var sendFlag = recordService.Any(m => m.prizeObjId == prizeObjectId && m.listId == sub.id && m.requestParamField == requestParamField);
  859. if(!sendFlag)
  860. {
  861. recordService.Add(new PriRecord()
  862. {
  863. createDate = DateTime.Now,
  864. projectId = projectId,
  865. listId = sub.id,
  866. prizeAmount = prizeAmt,
  867. prizeObjId = prizeObjectId,
  868. requestParamField = requestParamField,
  869. batchNo = batchNo,
  870. });
  871. // prizeToDatabase(projectId, content); //入库
  872. }
  873. }
  874. }
  875. }
  876. //递归条件判断
  877. public static Dictionary<string, object> loopCondition(int projectId, PriList sub, string objId, string content)
  878. {
  879. Dictionary<string, object> dic = new Dictionary<string, object>();
  880. var queryTableService = App.GetService<IPriRecursionStartTableService>();
  881. var queryFieldService = App.GetService<IPriRecursionStartFieldService>();
  882. var queryReturnFieldService = App.GetService<IPriRecursionStartReturnFieldService>();
  883. int passCount = 0;
  884. //查询表
  885. var queryTables = queryTableService.GetList(m => m.listId == sub.id);
  886. int allCount = queryTables.Count;
  887. foreach(var queryTable in queryTables)
  888. {
  889. //查询返回字段
  890. string fields = "1";
  891. var returnFields = queryReturnFieldService.GetList(m => m.objectTableId == queryTable.id);
  892. if(returnFields.Count > 0)
  893. {
  894. fields = "";
  895. foreach(var returnField in returnFields)
  896. {
  897. if(returnField.fieldReturnKind == "2")
  898. {
  899. fields += "count(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  900. }
  901. else if(returnField.fieldReturnKind == "3")
  902. {
  903. fields += "sum(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  904. }
  905. else if(returnField.fieldReturnKind == "4")
  906. {
  907. fields += "avg(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  908. }
  909. else
  910. {
  911. fields += returnField.fieldEnName + " " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
  912. }
  913. }
  914. fields = fields.TrimEnd(',');
  915. }
  916. //查询匹配条件
  917. string condi = "";
  918. var queryFields = queryFieldService.GetList(m => m.objectTableId == queryTable.id);
  919. foreach(var queryField in queryFields)
  920. {
  921. string fieldEnName = queryField.fieldEnName;
  922. string fieldQueryKind = queryField.fieldQueryKind;
  923. string fieldQueryModel = queryField.fieldQueryModel;
  924. string fieldQueryValue = queryField.fieldQueryValue;
  925. if(fieldQueryModel == "loop_field") fieldQueryValue = objId;
  926. string fieldQueryValueType = queryField.fieldQueryValueType;
  927. if(fieldQueryKind == "1") //模糊匹配
  928. {
  929. condi += " and " + fieldEnName + " like ";
  930. if(fieldQueryModel == "request_param")
  931. {
  932. Dictionary<string, string> req = getRequestParams(projectId, content);
  933. string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  934. condi += "'%" + val + "%'";
  935. }
  936. else if(fieldQueryModel == "fixed_value")
  937. {
  938. condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
  939. }
  940. else if(fieldQueryModel == "db_field")
  941. {
  942. condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
  943. }
  944. }
  945. else if(fieldQueryKind == "2") //精确匹配
  946. {
  947. condi += " and " + fieldEnName + "=";
  948. string val = fieldQueryValue;
  949. if(fieldQueryModel == "request_param")
  950. {
  951. Dictionary<string, string> req = getRequestParams(projectId, content);
  952. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  953. }
  954. else if(fieldQueryModel == "fixed_value")
  955. {
  956. val = GetExpressionVal(fieldQueryValue);
  957. }
  958. else if(fieldQueryModel == "db_field")
  959. {
  960. val = GetDbExpressionVal(fieldQueryValue);
  961. }
  962. if(fieldQueryValueType == "text")
  963. {
  964. val = "'" + val + "'";
  965. }
  966. condi += val;
  967. }
  968. else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
  969. {
  970. if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
  971. else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
  972. string val = "";
  973. string[] valList = fieldQueryValue.Split(',');
  974. if(fieldQueryModel == "request_param")
  975. {
  976. Dictionary<string, string> req = getRequestParams(projectId, content);
  977. foreach(string subval in valList)
  978. {
  979. if(fieldQueryValueType == "text")
  980. {
  981. val += "'" + req[subval] + "',";
  982. }
  983. else
  984. {
  985. val += req[subval] + ",";
  986. }
  987. }
  988. }
  989. else if(fieldQueryModel == "fixed_value")
  990. {
  991. foreach(string subval in valList)
  992. {
  993. if(fieldQueryValueType == "text")
  994. {
  995. val += "'" + GetExpressionVal(subval) + "',";
  996. }
  997. else
  998. {
  999. val += GetExpressionVal(subval) + ",";
  1000. }
  1001. }
  1002. }
  1003. else if(fieldQueryModel == "db_field")
  1004. {
  1005. foreach(string subval in valList)
  1006. {
  1007. if(fieldQueryValueType == "text")
  1008. {
  1009. val += "'" + GetDbExpressionVal(subval) + "',";
  1010. }
  1011. else
  1012. {
  1013. val += GetDbExpressionVal(subval) + ",";
  1014. }
  1015. }
  1016. }
  1017. val = val.TrimEnd(',');
  1018. condi += val + ")";
  1019. }
  1020. else if(fieldQueryKind == "3") //范围匹配
  1021. {
  1022. string[] val = fieldQueryValue.Split(':');
  1023. if(fieldQueryModel == "request_param")
  1024. {
  1025. Dictionary<string, string> req = getRequestParams(projectId, content);
  1026. if(fieldQueryValueType == "number")
  1027. {
  1028. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  1029. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  1030. }
  1031. else
  1032. {
  1033. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  1034. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  1035. }
  1036. }
  1037. else if(fieldQueryModel == "fixed_value")
  1038. {
  1039. if(fieldQueryValueType == "number")
  1040. {
  1041. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  1042. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  1043. }
  1044. else
  1045. {
  1046. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
  1047. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
  1048. }
  1049. }
  1050. else if(fieldQueryModel == "db_field")
  1051. {
  1052. if(fieldQueryValueType == "number")
  1053. {
  1054. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  1055. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  1056. }
  1057. else
  1058. {
  1059. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetDbExpressionVal(val[0]) + "'";
  1060. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetDbExpressionVal(val[1]) + "'";
  1061. }
  1062. }
  1063. }
  1064. else if(fieldQueryKind == "4") //取反匹配
  1065. {
  1066. condi += " and " + fieldEnName + "!=";
  1067. string val = "";
  1068. if(fieldQueryModel == "request_param")
  1069. {
  1070. Dictionary<string, string> req = getRequestParams(projectId, content);
  1071. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  1072. }
  1073. else if(fieldQueryModel == "fixed_value")
  1074. {
  1075. val = GetExpressionVal(fieldQueryValue);
  1076. }
  1077. else if(fieldQueryModel == "db_field")
  1078. {
  1079. val = GetDbExpressionVal(fieldQueryValue);
  1080. }
  1081. if(fieldQueryValueType == "text")
  1082. {
  1083. val = "'" + val + "'";
  1084. }
  1085. condi += val;
  1086. }
  1087. }
  1088. var db = initDb(queryTable.databaseId);
  1089. string sql = "select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi;
  1090. var items = db.Ado.GetDataTable(sql);
  1091. if(items.Rows.Count > 0)
  1092. {
  1093. foreach(System.Data.DataColumn item in items.Columns)
  1094. {
  1095. dic.Add(item.ColumnName, items.Rows[0][item.ColumnName].ToString());
  1096. }
  1097. }
  1098. }
  1099. return dic;
  1100. }
  1101. //奖励入库
  1102. public static void prizeToDatabase(int projectId, string content)
  1103. {
  1104. var prizeInTableService = App.GetService<IPriPrizeInTableService>();
  1105. var prizeInFieldService = App.GetService<IPriPrizeInFieldService>();
  1106. var prizeInQueryFieldService = App.GetService<IPriPrizeInQueryFieldService>();
  1107. //入库表
  1108. var prizeInTables = prizeInTableService.GetList(m => m.projectId == projectId);
  1109. foreach(var prizeInTable in prizeInTables)
  1110. {
  1111. var db = initDb(prizeInTable.databaseId);
  1112. Dictionary<string, object> doFields = new Dictionary<string, object>();
  1113. //入库字段
  1114. var prizeInFields = prizeInFieldService.GetList(m => m.inTableId == prizeInTable.id);
  1115. foreach(var prizeInField in prizeInFields)
  1116. {
  1117. string fieldEnName = prizeInField.fieldEnName;
  1118. string fieldQueryModel = prizeInField.fieldQueryModel;
  1119. string fieldQueryValue = prizeInField.fieldQueryValue;
  1120. string fieldQueryValueType = prizeInField.fieldQueryValueType;
  1121. if(fieldQueryModel == "request_param")
  1122. {
  1123. Dictionary<string, string> req = getRequestParams(projectId, content);
  1124. fieldQueryValue = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  1125. }
  1126. else if(fieldQueryModel == "fixed_value")
  1127. {
  1128. fieldQueryValue = GetExpressionVal(fieldQueryValue);
  1129. }
  1130. else if(fieldQueryModel == "db_field")
  1131. {
  1132. fieldQueryValue = GetDbExpressionVal(fieldQueryValue);
  1133. }
  1134. if(fieldQueryValueType == "text") doFields.Add(fieldEnName, fieldQueryValue);
  1135. if(fieldQueryValueType == "int") doFields.Add(fieldEnName, int.Parse(fieldQueryValue));
  1136. if(fieldQueryValueType == "number") doFields.Add(fieldEnName, decimal.Parse(fieldQueryValue));
  1137. if(fieldQueryValueType == "datetime") doFields.Add(fieldEnName, DateTime.Parse(fieldQueryValue));
  1138. if(fieldQueryValueType == "bool") doFields.Add(fieldEnName, bool.Parse(fieldQueryValue));
  1139. }
  1140. //查询匹配条件
  1141. string condi = "";
  1142. var queryFields = prizeInQueryFieldService.GetList(m => m.inTableId == prizeInTable.id);
  1143. foreach(var queryField in queryFields)
  1144. {
  1145. string fieldEnName = queryField.fieldEnName;
  1146. string fieldQueryKind = queryField.fieldQueryKind;
  1147. string fieldQueryModel = queryField.fieldQueryModel;
  1148. string fieldQueryValue = queryField.fieldQueryValue;
  1149. string fieldQueryValueType = queryField.fieldQueryValueType;
  1150. if(fieldQueryKind == "1") //模糊匹配
  1151. {
  1152. condi += " and " + fieldEnName + " like ";
  1153. if(fieldQueryModel == "request_param")
  1154. {
  1155. Dictionary<string, string> req = getRequestParams(projectId, content);
  1156. string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  1157. condi += "'%" + val + "%'";
  1158. }
  1159. else if(fieldQueryModel == "fixed_value")
  1160. {
  1161. condi += "'%" + fieldQueryValue + "%'";
  1162. }
  1163. }
  1164. else if(fieldQueryKind == "2") //精确匹配
  1165. {
  1166. condi += " and " + fieldEnName + "=";
  1167. string val = "";
  1168. if(fieldQueryModel == "request_param")
  1169. {
  1170. Dictionary<string, string> req = getRequestParams(projectId, content);
  1171. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  1172. }
  1173. else if(fieldQueryModel == "fixed_value")
  1174. {
  1175. val = fieldQueryValue;
  1176. }
  1177. if(fieldQueryValueType == "text")
  1178. {
  1179. val = "'" + val + "'";
  1180. }
  1181. condi += val;
  1182. }
  1183. else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
  1184. {
  1185. if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
  1186. else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
  1187. string val = "";
  1188. string[] valList = fieldQueryValue.Split(',');
  1189. if(fieldQueryModel == "request_param")
  1190. {
  1191. Dictionary<string, string> req = getRequestParams(projectId, content);
  1192. foreach(string sub in valList)
  1193. {
  1194. if(fieldQueryValueType == "text")
  1195. {
  1196. val += "'" + req[sub] + "',";
  1197. }
  1198. else
  1199. {
  1200. val += req[sub] + ",";
  1201. }
  1202. }
  1203. }
  1204. else if(fieldQueryModel == "fixed_value")
  1205. {
  1206. foreach(string sub in valList)
  1207. {
  1208. if(fieldQueryValueType == "text")
  1209. {
  1210. val += "'" + GetExpressionVal(sub) + "',";
  1211. }
  1212. else
  1213. {
  1214. val += GetExpressionVal(sub) + ",";
  1215. }
  1216. }
  1217. }
  1218. else if(fieldQueryModel == "db_field")
  1219. {
  1220. foreach(string sub in valList)
  1221. {
  1222. if(fieldQueryValueType == "text")
  1223. {
  1224. val += "'" + GetDbExpressionVal(sub) + "',";
  1225. }
  1226. else
  1227. {
  1228. val += GetDbExpressionVal(sub) + ",";
  1229. }
  1230. }
  1231. }
  1232. val = val.TrimEnd(',');
  1233. condi += val + ")";
  1234. }
  1235. else if(fieldQueryKind == "3") //范围匹配
  1236. {
  1237. string[] val = fieldQueryValue.Split(':');
  1238. if(fieldQueryModel == "request_param")
  1239. {
  1240. Dictionary<string, string> req = getRequestParams(projectId, content);
  1241. if(fieldQueryValueType == "number")
  1242. {
  1243. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
  1244. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
  1245. }
  1246. else
  1247. {
  1248. if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
  1249. if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
  1250. }
  1251. }
  1252. else if(fieldQueryModel == "fixed_value")
  1253. {
  1254. if(fieldQueryValueType == "number")
  1255. {
  1256. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
  1257. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
  1258. }
  1259. else
  1260. {
  1261. if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
  1262. if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
  1263. }
  1264. }
  1265. }
  1266. else if(fieldQueryKind == "4") //取反匹配
  1267. {
  1268. condi += " and " + fieldEnName + "!=";
  1269. string val = "";
  1270. if(fieldQueryModel == "request_param")
  1271. {
  1272. Dictionary<string, string> req = getRequestParams(projectId, content);
  1273. val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
  1274. }
  1275. else if(fieldQueryModel == "fixed_value")
  1276. {
  1277. val = fieldQueryValue;
  1278. }
  1279. if(fieldQueryValueType == "text")
  1280. {
  1281. val = "'" + val + "'";
  1282. }
  1283. condi += val;
  1284. }
  1285. }
  1286. if(prizeInTable.excuteKind == "add") db.Insertable(doFields).ExecuteCommand();
  1287. if(prizeInTable.excuteKind == "update") db.Updateable(doFields).Where("1=1" + condi).ExecuteCommand();
  1288. }
  1289. }
  1290. //固定值表达式
  1291. public static string GetExpressionVal(string str)
  1292. {
  1293. if(str == "#{now}#") str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  1294. if(str == "#{today}#") str = DateTime.Now.ToString("yyyy-MM-dd");
  1295. if(str == "#{this_month}#") str = DateTime.Now.ToString("yyyy-MM");
  1296. 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");
  1297. 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");
  1298. if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  1299. if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
  1300. if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  1301. if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
  1302. return str;
  1303. }
  1304. //库内字段值表达式
  1305. public static string GetDbExpressionVal(string str)
  1306. {
  1307. string[] data = str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',');
  1308. if(str.StartsWith("#{") && str.EndsWith("DAY}#")) str = "DATE_ADD(" + data[0] + ",INTERVAL " + data[1] + " DAY)";
  1309. if(str.StartsWith("#{") && str.EndsWith("MONTH}#")) str = "DATE_ADD(" + data[0] + ",INTERVAL " + data[1] + " MONTH)";
  1310. return str;
  1311. }
  1312. }
  1313. }