PrizeDo.cs 73 KB

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