12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475 |
- using System.Data;
- using Common;
- using Infrastructure;
- using LitJson;
- using Microsoft.Extensions.Localization;
- using Model;
- using NuGet.Packaging;
- using Services;
- namespace Util
- {
- public class PrizeDo
- {
- public static string batchNo = "";
- //发奖入口
- public static void sendPrize(string content)
- {
- try
- {
- batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Function.get_Random(3);
- JsonData jsonObj = JsonMapper.ToObject(content);
- string priObjString = prizeInSet(int.Parse(Function.CheckInt(jsonObj["prize_tag"].ToString())));
- content = jsonObj["content"].ToString();
- if(!string.IsNullOrEmpty(priObjString))
- {
- string[] priObjData = priObjString.Split('|');
- string priListIds = priObjData[0];
- int projectId = int.Parse(priObjData[1]);
- prizeSend(projectId, priListIds, content);
- }
- }
- catch(Exception ex)
- {
- Function.WriteLog(DateTime.Now + "\n" + ex.ToString(), "发奖异常");
- }
- }
- //接收入参
- public static Dictionary<string, string> getRequestParams(int projectId, string content)
- {
- Dictionary<string, string> dic = new Dictionary<string, string>();
- JsonData jsonData = JsonMapper.ToObject(content);
- var paramService = App.GetService<IPriProjectParamService>();
- var paramList = paramService.GetList(m => m.projectId == projectId);
- foreach(var param in paramList)
- {
- if(content.Contains("\"" + param.fieldEnName + "\"")) dic.Add(param.fieldEnName, jsonData[param.fieldEnName].ToString());
- }
- return dic;
- }
- //数据库链接
- public static SqlSugarClient initDb(int id)
- {
- var databaseSetService = App.GetService<IPriDatabaseSetService>();
- var connectionString = databaseSetService.GetFirst(m => m.id == id);
- string server = connectionString.ipAddress;
- int port = connectionString.port;
- string user = connectionString.userId;
- string password = connectionString.password;
- string database = connectionString.databaseName;
- var db = new SqlSugarClient(new ConnectionConfig()
- {
- ConnectionString = "server=" + server + ";port=" + port + ";user=" + user + ";password=" + password + ";database=" + database + ";charset=utf8;",
- DbType = SqlSugar.DbType.MySql,
- IsAutoCloseConnection = true,
- });
- return db;
- }
- //查询条件匹配数据
- public static Dictionary<string, object> condition(int projectId, string content)
- {
- Dictionary<string, object> dic = new Dictionary<string, object>();
- var queryTableService = App.GetService<IPriQueryTableService>();
- var queryFieldService = App.GetService<IPriQueryFieldService>();
- var returnFieldService = App.GetService<IPriReturnFieldService>();
- //查询表
- var queryTables = queryTableService.GetList(m => m.projectId == projectId);
- foreach(var queryTable in queryTables)
- {
- //查询返回字段
- string fields = "";
- var returnFields = returnFieldService.GetList(m => m.queryTableId == queryTable.id);
- foreach(var returnField in returnFields)
- {
- if(returnField.fieldReturnKind == "2")
- {
- fields += "count(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else if(returnField.fieldReturnKind == "3")
- {
- fields += "sum(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else if(returnField.fieldReturnKind == "4")
- {
- fields += "avg(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else
- {
- fields += returnField.fieldEnName + " " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- }
- fields = fields.TrimEnd(',');
- //查询匹配条件
- string condi = "";
- var queryFields = queryFieldService.GetList(m => m.queryTableId == queryTable.id);
- foreach(var queryField in queryFields)
- {
- string fieldEnName = queryField.fieldEnName;
- string fieldQueryKind = queryField.fieldQueryKind;
- string fieldQueryModel = queryField.fieldQueryModel;
- string fieldQueryValue = queryField.fieldQueryValue;
- string fieldQueryValueType = queryField.fieldQueryValueType;
- if(fieldQueryKind == "1") //模糊匹配
- {
- condi += " and " + fieldEnName + " like ";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- condi += "'%" + val + "%'";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
- }
- else if(fieldQueryModel == "db_field")
- {
- condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
- }
- else if(fieldQueryModel == "query_field")
- {
- string val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
- condi += "'%" + val + "%'";
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is null";
- else
- {
- condi += " and " + fieldEnName + "=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "query_field")
- {
- val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
- else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
- string val = "";
- string[] valList = fieldQueryValue.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + req[sub] + "',";
- }
- else
- {
- val += req[sub] + ",";
- }
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetExpressionVal(sub) + "',";
- }
- else
- {
- val += GetExpressionVal(sub) + ",";
- }
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetDbExpressionVal(sub) + "',";
- }
- else
- {
- val += GetDbExpressionVal(sub) + ",";
- }
- }
- }
- else if(fieldQueryModel == "query_field")
- {
- foreach(string sub in valList)
- {
- string dicVal = dic.ContainsKey(sub) ? dic[sub].ToString() : "0";
- if(fieldQueryValueType == "text")
- {
- val += "'" + dicVal + "',";
- }
- else
- {
- val += dicVal + ",";
- }
- }
- }
- val = val.TrimEnd(',');
- condi += val + ")";
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = fieldQueryValue.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
- }
- else
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + GetDbExpressionVal(val[0]);
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + GetDbExpressionVal(val[1]);
- }
- else if(fieldQueryModel == "query_field")
- {
- string dicValLeft = dic.ContainsKey(val[0]) ? dic[val[0]].ToString() : "0";
- string dicValRight = dic.ContainsKey(val[1]) ? dic[val[1]].ToString() : "0";
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(dicValLeft)) condi += " and " + fieldEnName + ">=" + dicValLeft;
- if(!string.IsNullOrEmpty(dicValRight)) condi += " and " + fieldEnName + "<=" + dicValRight;
- }
- else
- {
- if(!string.IsNullOrEmpty(dicValLeft)) condi += " and " + fieldEnName + ">='" + GetExpressionVal(dicValLeft) + "'";
- if(!string.IsNullOrEmpty(dicValRight)) condi += " and " + fieldEnName + "<='" + GetExpressionVal(dicValRight) + "'";
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- if(fieldQueryValue == "null" && fieldQueryValueType == "text") condi += " and " + fieldEnName + " is not null";
- else
- {
- condi += " and " + fieldEnName + "!=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "query_field")
- {
- val = dic.ContainsKey(fieldQueryValue) ? dic[fieldQueryValue].ToString() : "0";
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- }
- }
- var db = initDb(queryTable.databaseId);
- string sql = "select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi;
- var items = db.Ado.GetDataTable(sql);
- if(items.Rows.Count > 0)
- {
- foreach(System.Data.DataColumn item in items.Columns)
- {
- if(!dic.ContainsKey(item.ColumnName)) dic.Add(item.ColumnName, items.Rows[0][item.ColumnName].ToString());
- }
- }
- }
- return dic;
- }
- //奖励发放对象
- public static string prizeObject(int prizeObjectTag, string content)
- {
- Dictionary<string, object> dic = new Dictionary<string, object>();
- var prizeObjectTableService = App.GetService<IPriPrizeObjectTableService>();
- var prizeObjectFieldService = App.GetService<IPriPrizeObjectFieldService>();
- //查询表
- var prizeObjectTable = prizeObjectTableService.GetFirst(m => m.id == prizeObjectTag) ?? new PriPrizeObjectTable();
- //查询返回字段
- string field = prizeObjectTable.selectField;
- int projectId = prizeObjectTable.projectId;
- //查询匹配条件
- string condi = "";
- var queryFields = prizeObjectFieldService.GetList(m => m.objectTableId == prizeObjectTable.id);
- foreach(var queryField in queryFields)
- {
- string fieldEnName = queryField.fieldEnName;
- string fieldQueryKind = queryField.fieldQueryKind;
- string fieldQueryModel = queryField.fieldQueryModel;
- string fieldQueryValue = queryField.fieldQueryValue;
- string fieldQueryValueType = queryField.fieldQueryValueType;
- if(fieldQueryKind == "1") //模糊匹配
- {
- condi += " and " + fieldEnName + " like ";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- condi += "'%" + val + "%'";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
- }
- else if(fieldQueryModel == "db_field")
- {
- condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- condi += " and " + fieldEnName + "=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
- else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
- string val = "";
- string[] valList = fieldQueryValue.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + req[sub] + "',";
- }
- else
- {
- val += req[sub] + ",";
- }
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetExpressionVal(sub) + "',";
- }
- else
- {
- val += GetExpressionVal(sub) + ",";
- }
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetDbExpressionVal(sub) + "',";
- }
- else
- {
- val += GetDbExpressionVal(sub) + ",";
- }
- }
- }
- val = val.TrimEnd(',');
- condi += val + ")";
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = fieldQueryValue.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
- }
- else
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetDbExpressionVal(val[0]) + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetDbExpressionVal(val[1]) + "'";
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- condi += " and " + fieldEnName + "!=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- }
- var db = initDb(prizeObjectTable.databaseId);
- var item = db.Ado.GetScalar("select " + field + " from " + prizeObjectTable.tableEnName + " where 1=1" + condi);
- if(item != null)
- {
- return item.ToString();
- }
- return "0";
- }
- //奖励发放入口
- public static string prizeInSet(int prizeObjectTag)
- {
- Dictionary<string, object> dic = new Dictionary<string, object>();
- var prizeInSetService = App.GetService<IPriPrizeInSetService>();
- //查询表
- var prizeInSet = prizeInSetService.GetFirst(m => m.id == prizeObjectTag) ?? new PriPrizeInSet();
- if(prizeInSet.id > 0)
- {
- int projectId = prizeInSet.projectId;
- return prizeInSet.prizeListIds + "|" + projectId;
- }
- return "";
- }
- //奖励列表
- public static List<PriList> prizeList(int projectId, string prizeIds)
- {
- List<int> ids = Tools.SpitIntArrary(prizeIds, ',').ToList();
- var priListService = App.GetService<IPriListService>();
- var priList = priListService.GetList(m => m.projectId == projectId && ids.Contains(m.id)).ToList();
- return priList;
- }
- //奖励发放
- public static void prizeSend(int projectId, string prizeIds, string content)
- {
- var loopSetService = App.GetService<IPriLoopSetService>();
- var recursionStartTableService = App.GetService<IPriRecursionStartTableService>();
- var recursionStartConditionService = App.GetService<IPriRecursionStartConditionService>();
- var priList = prizeList(projectId, prizeIds);
- foreach(var sub in priList)
- {
- string prizeObjectId = prizeObject(sub.prizeObj, content);
- var recursionFlag = sub.recursionFlag; //是否递归
- if(recursionFlag)
- {
- var loopSet = loopSetService.GetFirst(m => m.listId == sub.id) ?? new PriLoopSet();
- var recursionStartTable = recursionStartTableService.GetFirst(m => m.listId == sub.id) ?? new PriRecursionStartTable();
- var tableEnName = loopSet.tableEnName; //递归查询表
- var parentField = loopSet.parentField; //父字段
- var sonField = loopSet.sonField; //子字段
- var afterPrizeFlag = loopSet.afterPrizeFlag; //发奖后是否继续
- var db = initDb(recursionStartTable.databaseId);
- string objId = prizeObjectId;
- bool op = true;
- int index = 0;
- loopAmount = 0;
- while(!string.IsNullOrEmpty(objId) && objId != "0" && op)
- {
- Dictionary<string, object> condiDic = loopCondition(projectId, sub, objId, content);
- if(condiDic.Count > 0)
- {
- bool prizeFlag = true;
- var conditions = recursionStartConditionService.GetList(m => m.loopSetId == loopSet.id);
- if(conditions.Count > 0)
- {
- int allCount = conditions.Count; //所有奖励条件数
- int passCount = 0; //通过条件数
- foreach(var condition in conditions)
- {
- if(condition.startIndex <= index)
- {
- var returnFieldId = condition.returnFieldId; //条件返回字段
- var fieldQueryKind = condition.fieldQueryKind; //匹配条件
- var fieldQueryModel = condition.fieldQueryModel; //匹配方式
- var fieldQueryValue = condition.fieldQueryValue; //匹配值
- var fieldQueryValueType = condition.fieldQueryValueType; //匹配值类型
- string checkObj = "";
- if(returnFieldId.Contains(","))
- {
- string[] returnFieldIdList = returnFieldId.Split(',');
- foreach(string subField in returnFieldIdList)
- {
- string val = condiDic.ContainsKey(subField) ? condiDic[subField].ToString() : "0";
- checkObj += val + ",";
- }
- checkObj = checkObj.TrimEnd(',');
- }
- else if (returnFieldId.Contains("+") || returnFieldId.Contains("-") || returnFieldId.Contains("*") || returnFieldId.Contains("/") || returnFieldId.Contains("(") || returnFieldId.Contains(")"))
- {
- string expresssion = returnFieldId;
- string[] returnFields = returnFieldId.Split(new char[] { '+', '-', '*', '/', '(', ')' });
- foreach(string returnField in returnFields)
- {
- string val = condiDic.ContainsKey(returnField) ? condiDic[returnField].ToString() : "0";
- expresssion = expresssion.Replace(returnField, val);
- }
- DataTable dt = new DataTable();
- checkObj = dt.Compute(expresssion, "false").ToString();
- }
- else
- {
- checkObj = condiDic.ContainsKey(returnFieldId) ? condiDic[returnFieldId].ToString() : "0";
- }
- var checkVal = fieldQueryValue;
- if(fieldQueryKind == "1") //模糊匹配
- {
- if(fieldQueryValueType == "text")
- {
- if(checkObj.ToString().Contains(GetExpressionVal(checkVal))) passCount += 1;
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- if(fieldQueryValueType == "int")
- {
- if(int.Parse(Function.CheckInt(checkObj.ToString())) == int.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "number")
- {
- if(decimal.Parse(Function.CheckNum(checkObj.ToString())) == decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else
- {
- if(checkObj.ToString() == GetExpressionVal(checkVal)) passCount += 1;
- }
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = checkVal.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) >= decimal.Parse(Function.CheckNum(req[val[0]]))) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
- }
- else if(fieldQueryValueType == "int")
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) >= int.Parse(Function.CheckInt(req[val[0]]))) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
- }
- else if(fieldQueryValueType.StartsWith("date"))
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) >= DateTime.Parse(req[val[0]])) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) <= DateTime.Parse(req[val[1]])) passCount += 1;
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
- }
- else if(fieldQueryValueType == "int")
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
- }
- else if(fieldQueryValueType.StartsWith("date"))
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj) >= DateTime.Parse(GetExpressionVal(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- if(fieldQueryValueType == "int")
- {
- if(int.Parse(checkObj) != int.Parse(Function.CheckInt(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "number")
- {
- if(decimal.Parse(checkObj) != decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "text")
- {
- if(checkObj.ToString() != GetExpressionVal(checkVal)) passCount += 1;
- }
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- string val = ",";
- string[] valList = checkVal.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string subVal in valList)
- {
- val += req[subVal] + ",";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string subVal in valList)
- {
- val += GetExpressionVal(subVal) + ",";
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string subVal in valList)
- {
- val += GetDbExpressionVal(subVal) + ",";
- }
- }
- if(fieldQueryKind == "5" && val.Contains("," + checkObj.ToString() + ",")) passCount += 1;
- if(fieldQueryKind == "6" && val.Contains("," + checkObj.ToString() + ",")) passCount += 1;
- }
- }
- }
- if(loopSet.conditionMode == "all" && passCount == allCount) prizeFlag = true;
- else if(loopSet.conditionMode == "one" && passCount >= 1) prizeFlag = true;
- else prizeFlag = false;
- }
- else
- {
- prizeFlag = false;
- }
- if(prizeFlag)
- {
- prizeSendDo(projectId, sub, objId, content, loopSet);
- op = afterPrizeFlag;
- }
- }
- var parent = db.Ado.GetScalar("select " + parentField + " from " + tableEnName + " where " + sonField + "=" + objId);
- if(parent != null)
- {
- objId = parent.ToString();
- }
- index += 1;
- }
- }
- else
- {
- prizeSendDo(projectId, sub, prizeObjectId, content, new PriLoopSet());
- }
- }
- }
- static decimal loopAmount = 0;
- public static void prizeSendDo(int projectId, PriList sub, string prizeObjectId, string content, PriLoopSet set)
- {
- var recordService = App.GetService<IPriRecordService>();
- var conditionService = App.GetService<IPriConditionService>();
- var returnFieldService = App.GetService<IPriReturnFieldService>();
- var queryTableService = App.GetService<IPriQueryTableService>();
- var amountSetService = App.GetService<IPriListAmountSetService>();
- //查询匹配条件
- Dictionary<string, object> condiDic = new Dictionary<string, object>();
- if(sub.prizeSourceFieldType == "condi") condiDic = condition(projectId, content);
- if(sub.prizeSourceFieldType == "loop") condiDic = loopCondition(projectId, sub, prizeObjectId, content);
- var prizeSourceField = sub.prizeSourceField; //奖励金额来源字段(对应条件匹配返回字段)
- var prizePercent = sub.prizePercent; //奖励比例
- var prizeAmount = sub.prizeAmount; //奖励固定值
- var prizeContent = sub.prizeContent; //奖励内容
- var conditionMode = sub.conditionMode; //条件模式
- var recursionFlag = sub.recursionFlag; //是否递归
-
- var conditions = conditionService.GetList(m => m.listId == sub.id);
- int allCount = conditions.Count; //所有奖励条件数
- int passCount = 0; //通过条件数
- foreach(var condition in conditions)
- {
- var returnFieldId = condition.returnFieldId; //条件返回字段
- var fieldQueryKind = condition.fieldQueryKind; //匹配条件
- var fieldQueryModel = condition.fieldQueryModel; //匹配方式
- var fieldQueryValue = condition.fieldQueryValue; //匹配值
- var fieldQueryValueType = condition.fieldQueryValueType; //匹配值类型
- string checkObj = "";
- if(returnFieldId.Contains(","))
- {
- string[] returnFieldIdList = returnFieldId.Split(',');
- foreach(string subField in returnFieldIdList)
- {
- string val = condiDic.ContainsKey(subField) ? condiDic[subField].ToString() : "0";
- checkObj += val + ",";
- }
- checkObj = checkObj.TrimEnd(',');
- }
- else if (returnFieldId.Contains("+") || returnFieldId.Contains("-") || returnFieldId.Contains("*") || returnFieldId.Contains("/") || returnFieldId.Contains("(") || returnFieldId.Contains(")"))
- {
- string expresssion = returnFieldId;
- string[] returnFields = returnFieldId.Split(new char[] { '+', '-', '*', '/', '(', ')' });
- foreach(string returnField in returnFields)
- {
- string val = condiDic.ContainsKey(returnField) ? condiDic[returnField].ToString() : "0";
- expresssion = expresssion.Replace(returnField, val);
- }
- DataTable dt = new DataTable();
- checkObj = dt.Compute(expresssion, "false").ToString();
- }
- else
- {
- checkObj = condiDic.ContainsKey(returnFieldId) ? condiDic[returnFieldId].ToString() : "0";
- }
- string checkVal = fieldQueryValue;
- if(fieldQueryKind == "1") //模糊匹配
- {
- if(fieldQueryValueType == "text")
- {
- if(checkObj.ToString().Contains(GetExpressionVal(checkVal))) passCount += 1;
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- if(fieldQueryValueType == "int")
- {
- if(int.Parse(Function.CheckInt(checkObj.ToString())) == int.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "number")
- {
- if(decimal.Parse(Function.CheckNum(checkObj.ToString())) == decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else
- {
- if(checkObj.ToString() == GetExpressionVal(checkVal)) passCount += 1;
- }
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = checkVal.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) >= decimal.Parse(Function.CheckNum(req[val[0]]))) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && decimal.Parse(checkObj) <= decimal.Parse(Function.CheckNum(req[val[1]]))) passCount += 1;
- }
- else if(fieldQueryValueType == "int")
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) >= int.Parse(Function.CheckInt(req[val[0]]))) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && int.Parse(checkObj) <= int.Parse(Function.CheckInt(req[val[1]]))) passCount += 1;
- }
- else if(fieldQueryValueType.StartsWith("date"))
- {
- 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;
- else if(!string.IsNullOrEmpty(req[val[0]]) && string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) >= DateTime.Parse(req[val[0]])) passCount += 1;
- else if(string.IsNullOrEmpty(req[val[0]]) && !string.IsNullOrEmpty(req[val[1]]) && DateTime.Parse(checkObj) <= DateTime.Parse(req[val[1]])) passCount += 1;
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) >= decimal.Parse(Function.CheckNum(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && decimal.Parse(Function.CheckNum(checkObj.ToString())) <= decimal.Parse(Function.CheckNum(val[1]))) passCount += 1;
- }
- else if(fieldQueryValueType == "int")
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) >= int.Parse(Function.CheckInt(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && int.Parse(Function.CheckInt(checkObj.ToString())) <= int.Parse(Function.CheckInt(val[1]))) passCount += 1;
- }
- else if(fieldQueryValueType.StartsWith("date"))
- {
- 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;
- else if(!string.IsNullOrEmpty(val[0]) && string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj.ToString()) >= DateTime.Parse(GetExpressionVal(val[0]))) passCount += 1;
- else if(string.IsNullOrEmpty(val[0]) && !string.IsNullOrEmpty(val[1]) && DateTime.Parse(checkObj.ToString()) <= DateTime.Parse(GetExpressionVal(val[1]))) passCount += 1;
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- if(fieldQueryValueType == "int")
- {
- if(int.Parse(Function.CheckNum(checkObj.ToString())) != int.Parse(Function.CheckInt(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "number")
- {
- if(decimal.Parse(Function.CheckNum(checkObj.ToString())) != decimal.Parse(Function.CheckNum(checkVal))) passCount += 1;
- }
- else if(fieldQueryValueType == "text")
- {
- if(checkObj.ToString() != GetExpressionVal(checkVal)) passCount += 1;
- }
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- string val = ",";
- string[] valList = checkVal.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string subVal in valList)
- {
- val += req[subVal] + ",";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string subVal in valList)
- {
- val += GetExpressionVal(subVal) + ",";
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string subVal in valList)
- {
- val += GetDbExpressionVal(subVal) + ",";
- }
- }
- if(fieldQueryKind == "5" && val.Contains("," + checkObj.ToString() + ",")) passCount += 1;
- if(fieldQueryKind == "6" && val.Contains("," + checkObj.ToString() + ",")) passCount += 1;
- }
- }
- bool op = false;
- if(conditionMode == "all" && passCount == allCount && passCount > 0) op = true;
- else if(conditionMode == "one" && passCount >= 1) op = true;
- if(op) //满足条件
- {
- decimal number = 0;
- if(prizeSourceField.Contains("/"))
- {
- number = decimal.Parse(prizeSourceField.Split('/')[1]);
- prizeSourceField = prizeSourceField.Split('/')[0];
- }
- string prizeSourceData = "";
- if(prizeSourceField.Contains(","))
- {
- string[] prizeSourceFieldList = prizeSourceField.Split(',');
- foreach(string subField in prizeSourceFieldList)
- {
- string val = condiDic.ContainsKey(subField) ? condiDic[subField].ToString() : "0";
- prizeSourceData += val + ",";
- }
- prizeSourceData = prizeSourceData.TrimEnd(',');
- }
- else
- {
- prizeSourceData = condiDic.ContainsKey(prizeSourceField) ? condiDic[prizeSourceField].ToString() : "0";
- }
- if(string.IsNullOrEmpty(prizeSourceData)) prizeSourceData = "0";
- if(prizeSourceField.Contains("/") && number > 0)
- {
- decimal prizeSourceDataNum = decimal.Parse(prizeSourceData) / number;
- prizeSourceData = prizeSourceDataNum.ToString("f2");
- }
- decimal prizeAmt = 0;
- var amountSet = amountSetService.GetFirst(m => m.listId == sub.id && m.prizeSourceField == prizeSourceData);
- if(amountSet != null)
- {
- prizePercent = amountSet.prizePercent;
- prizeAmount = amountSet.prizeAmount;
- }
- if(prizeSourceData.Contains(","))
- {
- string checkField = prizeSourceData.Split(',')[0] + ",";
- amountSet = amountSetService.GetFirst(m => m.listId == sub.id && m.prizeSourceField.StartsWith(checkField) && m.prizeSourceField.EndsWith(",%"));
- if(amountSet != null)
- {
- prizePercent = amountSet.prizePercent;
- prizeAmount = amountSet.prizeAmount;
- prizeSourceData = prizeSourceData.Split(',')[1];
- }
- }
- if(prizePercent > 0) prizeAmt += decimal.Parse(Function.CheckNum(prizeSourceData)) * prizePercent;
- if(prizeAmount > 0) prizeAmt += prizeAmount;
- decimal getPrizeAmount = prizeAmt;
- if(set.levelDiffFlag) prizeAmt -= loopAmount;
- if(prizeAmt > 0)
- {
- var req = getRequestParams(projectId, content);
- var requestParamField = req[sub.requestParamField];
- var sendFlag = recordService.Any(m => m.prizeObjId == prizeObjectId && m.listId == sub.id && m.requestParamField == requestParamField);
- if(!sendFlag)
- {
- recordService.Add(new PriRecord()
- {
- createDate = DateTime.Now,
- projectId = projectId,
- listId = sub.id,
- prizeAmount = prizeAmt,
- prizeObjId = prizeObjectId,
- requestParamField = requestParamField,
- batchNo = batchNo,
- });
- loopAmount = getPrizeAmount;
- prizeToDatabase(projectId, sub.id, content); //入库
- }
- }
- }
- }
- //递归条件判断
- public static Dictionary<string, object> loopCondition(int projectId, PriList sub, string objId, string content)
- {
- Dictionary<string, object> dic = new Dictionary<string, object>();
- var queryTableService = App.GetService<IPriRecursionStartTableService>();
- var queryFieldService = App.GetService<IPriRecursionStartFieldService>();
- var queryReturnFieldService = App.GetService<IPriRecursionStartReturnFieldService>();
- int passCount = 0;
- //查询表
- var queryTables = queryTableService.GetList(m => m.listId == sub.id);
- int allCount = queryTables.Count;
- foreach(var queryTable in queryTables)
- {
- //查询返回字段
- string fields = "1";
- var returnFields = queryReturnFieldService.GetList(m => m.objectTableId == queryTable.id);
- if(returnFields.Count > 0)
- {
- fields = "";
- foreach(var returnField in returnFields)
- {
- if(returnField.fieldReturnKind == "2")
- {
- fields += "count(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else if(returnField.fieldReturnKind == "3")
- {
- fields += "sum(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else if(returnField.fieldReturnKind == "4")
- {
- fields += "avg(" + returnField.fieldEnName + ") " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- else
- {
- fields += returnField.fieldEnName + " " + queryTable.tableEnName + "_" + returnField.fieldEnName + ",";
- }
- }
- fields = fields.TrimEnd(',');
- }
- //查询匹配条件
- string condi = "";
- var queryFields = queryFieldService.GetList(m => m.objectTableId == queryTable.id);
- foreach(var queryField in queryFields)
- {
- string fieldEnName = queryField.fieldEnName;
- string fieldQueryKind = queryField.fieldQueryKind;
- string fieldQueryModel = queryField.fieldQueryModel;
- string fieldQueryValue = queryField.fieldQueryValue;
- if(fieldQueryModel == "loop_field") fieldQueryValue = objId;
- string fieldQueryValueType = queryField.fieldQueryValueType;
- if(fieldQueryKind == "1") //模糊匹配
- {
- condi += " and " + fieldEnName + " like ";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- condi += "'%" + val + "%'";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- condi += "'%" + GetExpressionVal(fieldQueryValue) + "%'";
- }
- else if(fieldQueryModel == "db_field")
- {
- condi += "'%" + GetDbExpressionVal(fieldQueryValue) + "%'";
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- condi += " and " + fieldEnName + "=";
- string val = fieldQueryValue;
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
- else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
- string val = "";
- string[] valList = fieldQueryValue.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string subval in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + req[subval] + "',";
- }
- else
- {
- val += req[subval] + ",";
- }
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string subval in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetExpressionVal(subval) + "',";
- }
- else
- {
- val += GetExpressionVal(subval) + ",";
- }
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string subval in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetDbExpressionVal(subval) + "',";
- }
- else
- {
- val += GetDbExpressionVal(subval) + ",";
- }
- }
- }
- val = val.TrimEnd(',');
- condi += val + ")";
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = fieldQueryValue.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
- }
- else
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetExpressionVal(val[0]) + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetExpressionVal(val[1]) + "'";
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + GetDbExpressionVal(val[0]) + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + GetDbExpressionVal(val[1]) + "'";
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- condi += " and " + fieldEnName + "!=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- val = GetDbExpressionVal(fieldQueryValue);
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- }
- var db = initDb(queryTable.databaseId);
- string sql = "select " + fields + " from " + queryTable.tableEnName + " where 1=1" + condi;
- var items = db.Ado.GetDataTable(sql);
- if(items.Rows.Count > 0)
- {
- foreach(System.Data.DataColumn item in items.Columns)
- {
- if(!dic.ContainsKey(item.ColumnName)) dic.Add(item.ColumnName, items.Rows[0][item.ColumnName].ToString());
- }
- }
- }
- return dic;
- }
- //奖励入库
- public static void prizeToDatabase(int projectId, int listId, string content)
- {
- var prizeInTableService = App.GetService<IPriPrizeInTableService>();
- var prizeInFieldService = App.GetService<IPriPrizeInFieldService>();
- var prizeInQueryFieldService = App.GetService<IPriPrizeInQueryFieldService>();
- //入库表
- var prizeInTables = prizeInTableService.GetList(m => m.listId == listId);
- foreach(var prizeInTable in prizeInTables)
- {
- var db = initDb(prizeInTable.databaseId);
- Dictionary<string, object> doFields = new Dictionary<string, object>();
- //入库字段
- var prizeInFields = prizeInFieldService.GetList(m => m.inTableId == prizeInTable.id);
- foreach(var prizeInField in prizeInFields)
- {
- string fieldEnName = prizeInField.fieldEnName;
- string fieldQueryModel = prizeInField.fieldQueryModel;
- string fieldQueryValue = prizeInField.fieldQueryValue;
- string fieldQueryValueType = prizeInField.fieldQueryValueType;
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- fieldQueryValue = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "query_field")
- {
-
- }
- else if(fieldQueryModel == "fixed_value")
- {
- fieldQueryValue = GetExpressionVal(fieldQueryValue);
- }
- else if(fieldQueryModel == "db_field")
- {
- fieldQueryValue = GetDbExpressionVal(fieldQueryValue);
- }
- if(fieldQueryValueType == "text") doFields.Add(fieldEnName, fieldQueryValue);
- if(fieldQueryValueType == "int") doFields.Add(fieldEnName, int.Parse(fieldQueryValue));
- if(fieldQueryValueType == "number") doFields.Add(fieldEnName, decimal.Parse(fieldQueryValue));
- if(fieldQueryValueType == "datetime") doFields.Add(fieldEnName, DateTime.Parse(fieldQueryValue));
- if(fieldQueryValueType == "bool") doFields.Add(fieldEnName, bool.Parse(fieldQueryValue));
- }
- //查询匹配条件
- string condi = "";
- var queryFields = prizeInQueryFieldService.GetList(m => m.inTableId == prizeInTable.id);
- foreach(var queryField in queryFields)
- {
- string fieldEnName = queryField.fieldEnName;
- string fieldQueryKind = queryField.fieldQueryKind;
- string fieldQueryModel = queryField.fieldQueryModel;
- string fieldQueryValue = queryField.fieldQueryValue;
- string fieldQueryValueType = queryField.fieldQueryValueType;
- if(fieldQueryKind == "1") //模糊匹配
- {
- condi += " and " + fieldEnName + " like ";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- string val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- condi += "'%" + val + "%'";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- condi += "'%" + fieldQueryValue + "%'";
- }
- }
- else if(fieldQueryKind == "2") //精确匹配
- {
- condi += " and " + fieldEnName + "=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = fieldQueryValue;
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- else if(fieldQueryKind == "5" || fieldQueryKind == "6") //数组匹配/排除
- {
- if(fieldQueryKind == "5") condi += " and " + fieldEnName + " in (";
- else if(fieldQueryKind == "6") condi += " and " + fieldEnName + " not in (";
- string val = "";
- string[] valList = fieldQueryValue.Split(',');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + req[sub] + "',";
- }
- else
- {
- val += req[sub] + ",";
- }
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetExpressionVal(sub) + "',";
- }
- else
- {
- val += GetExpressionVal(sub) + ",";
- }
- }
- }
- else if(fieldQueryModel == "db_field")
- {
- foreach(string sub in valList)
- {
- if(fieldQueryValueType == "text")
- {
- val += "'" + GetDbExpressionVal(sub) + "',";
- }
- else
- {
- val += GetDbExpressionVal(sub) + ",";
- }
- }
- }
- val = val.TrimEnd(',');
- condi += val + ")";
- }
- else if(fieldQueryKind == "3") //范围匹配
- {
- string[] val = fieldQueryValue.Split(':');
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">=" + req[val[0]];
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<=" + req[val[1]];
- }
- else
- {
- if(!string.IsNullOrEmpty(req[val[0]])) condi += " and " + fieldEnName + ">='" + req[val[0]] + "'";
- if(!string.IsNullOrEmpty(req[val[1]])) condi += " and " + fieldEnName + "<='" + req[val[1]] + "'";
- }
- }
- else if(fieldQueryModel == "fixed_value")
- {
- if(fieldQueryValueType == "number")
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">=" + val[0];
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<=" + val[1];
- }
- else
- {
- if(!string.IsNullOrEmpty(val[0])) condi += " and " + fieldEnName + ">='" + val[0] + "'";
- if(!string.IsNullOrEmpty(val[1])) condi += " and " + fieldEnName + "<='" + val[1] + "'";
- }
- }
- }
- else if(fieldQueryKind == "4") //取反匹配
- {
- condi += " and " + fieldEnName + "!=";
- string val = "";
- if(fieldQueryModel == "request_param")
- {
- Dictionary<string, string> req = getRequestParams(projectId, content);
- val = req.ContainsKey(fieldQueryValue) ? req[fieldQueryValue] : "0";
- }
- else if(fieldQueryModel == "fixed_value")
- {
- val = fieldQueryValue;
- }
- if(fieldQueryValueType == "text")
- {
- val = "'" + val + "'";
- }
- condi += val;
- }
- }
- if(prizeInTable.excuteKind == "add") db.Insertable(doFields).AS(prizeInTable.tableEnName).ExecuteCommand();
- if(prizeInTable.excuteKind == "update") db.Updateable(doFields).AS(prizeInTable.tableEnName).Where("1=1" + condi).ExecuteCommand();
- Function.WriteLog(DateTime.Now.ToString() + "\n" + prizeInTable.excuteKind + "\n" + prizeInTable.tableEnName + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(doFields) + "\n\n", "奖励入库数据");
- }
- }
- //固定值表达式
- public static string GetExpressionVal(string str)
- {
- if(str == "#{now}#") str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- if(str == "#{today}#") str = DateTime.Now.ToString("yyyy-MM-dd");
- if(str == "#{this_month}#") str = DateTime.Now.ToString("yyyy-MM");
- if(str.StartsWith("#{now") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
- if(str.StartsWith("#{now") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd HH:mm:ss");
- if(str.StartsWith("#{today") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
- if(str.StartsWith("#{today") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM-dd");
- if(str.StartsWith("#{this_month") && str.EndsWith("DAY}#")) str = DateTime.Now.AddDays(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
- if(str.StartsWith("#{this_month") && str.EndsWith("MONTH}#")) str = DateTime.Now.AddMonths(int.Parse(str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',')[1])).ToString("yyyy-MM");
- return str;
- }
- //库内字段值表达式
- public static string GetDbExpressionVal(string str)
- {
- string[] data = str.Replace("#", "").Replace("{", "").Replace("}", "").Split(',');
- if(str.StartsWith("#{") && str.EndsWith("DAY}#")) str = "DATE_ADD(" + data[0] + ",INTERVAL " + data[1] + " DAY)";
- if(str.StartsWith("#{") && str.EndsWith("MONTH}#")) str = "DATE_ADD(" + data[0] + ",INTERVAL " + data[1] + " MONTH)";
- return str;
- }
- }
- }
|