GetCheckDataService.asmx.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using SysBaseLibs;
  2. using SysDataLibs.TableClass;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Services;
  8. namespace GSMarketSys.WS
  9. {
  10. /// <summary>
  11. /// GetCheckDataService 的摘要说明
  12. /// </summary>
  13. [WebService(Namespace = "http://tempuri.org/")]
  14. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  15. [System.ComponentModel.ToolboxItem(false)]
  16. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
  17. // [System.Web.Script.Services.ScriptService]
  18. public class GetCheckDataService : System.Web.Services.WebService
  19. {
  20. public GetCheckDataService()
  21. {
  22. //如果使用设计的组件,请取消注释以下行
  23. //InitializeComponent();
  24. }
  25. /// <summary>
  26. /// [],[],[]
  27. /// </summary>
  28. /// <param name="jsonFile"></param>
  29. /// <returns></returns>
  30. [WebMethod]
  31. public string GetCheckData(string jsonFile)
  32. {
  33. string AllResult = "";
  34. string reIP = Context.Request.ServerVariables["REMOTE_HOST"];
  35. // string reIP = "123.89.1.1";
  36. SysBaseLibs.ThreadLog.LogInfo("GetCheckData :" + reIP + " " + jsonFile);
  37. string[] arrRemoteIPs = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataRemoteIP").Split('@');
  38. //string lcWhereSql = "";
  39. string testIP = arrRemoteIPs[arrRemoteIPs.Length - 1];
  40. arrRemoteIPs[arrRemoteIPs.Length - 1] = "";
  41. DBConnSql loConn = new DBConnSql();
  42. loConn.Open();
  43. if (arrRemoteIPs.Contains(reIP) || reIP == "127.0.0.1")
  44. {
  45. if (isCanGetCheckData(reIP, loConn))
  46. {
  47. List<string> loJsonPara = new List<string>();
  48. Sys_AttachTables_info laTab = new Sys_AttachTables_info(reIP, "ForGetCheckData", "GetCheckDataMan", loConn);
  49. string lcLastGetDatTime = "";
  50. if (!string.IsNullOrWhiteSpace(laTab.SourceKey))
  51. lcLastGetDatTime = laTab.ActionTime;
  52. if (!CheckSearchTimeNot(jsonFile, loJsonPara, lcLastGetDatTime))
  53. {
  54. return "输入为不正确的时间格式!";
  55. }
  56. AllResult = GetCheckDataByWhere(GetWhereSqlByJsonFile(loJsonPara), reIP, loConn);
  57. }
  58. else
  59. {
  60. AllResult = "距离上次查询时间不到" + SysDataLibs.AppEnv.SysSetObj.GetString("GetCDataFrequency") + "分钟,限制访问!";
  61. }
  62. }
  63. else if (reIP == testIP)
  64. {
  65. AllResult = @"<Records>
  66. <RemoteIP>测试IP</RemoteIP>
  67. <ResultSet>
  68. <Result>
  69. <BigKindOfFood>---</BigKindOfFood>
  70. <SmallKindOfFood>葱蒜类</SmallKindOfFood>
  71. <SampleFoodName>大葱</SampleFoodName>
  72. <TestMan>DefaultUser</TestMan>
  73. <TestTime>2010-01-01 00:00:00</TestTime>
  74. <ProduceUnit>---</ProduceUnit>
  75. <CheckTypeName>甲醛</CheckTypeName>
  76. <CheckResult>合格</CheckResult>
  77. <CheckRate>0.0000</CheckRate>
  78. <CheckNo>WB967197</CheckNo>
  79. </Result>
  80. </ResultSet>
  81. </Records>";
  82. }
  83. else
  84. {
  85. AllResult = reIP + "是非约定的IP!";
  86. }
  87. return AllResult;
  88. }
  89. /// <summary>
  90. /// 校验输入的第一个日期格式是否正确
  91. /// </summary>
  92. /// <param name="pJsonFile"></param>
  93. /// <returns>false:正确;true:错误</returns>
  94. private bool CheckSearchTimeNot(string pJsonFile, List<string> poParas, string pcLastGetDatTime)
  95. {
  96. bool lbRetval = false;
  97. if (!string.IsNullOrEmpty(pJsonFile))
  98. {
  99. Array loPara = SysBaseLibs.UtilStr.StrToArrayEx(pJsonFile, ",");
  100. if (loPara != null && loPara.Length > 0)
  101. {
  102. if (loPara != null && loPara.Length == 2)
  103. {
  104. lbRetval = CheckDateTime(loPara.GetValue(1));
  105. if (lbRetval)
  106. {
  107. poParas.Add(loPara.GetValue(1).ToString());
  108. }
  109. }
  110. lbRetval = CheckDateTime(loPara.GetValue(0));
  111. if (lbRetval)
  112. {
  113. poParas.Add(loPara.GetValue(0).ToString());
  114. if (SysDataLibs.AppEnv.SysSetObj.GetBool("CheckLastGetDatTime") && !string.IsNullOrWhiteSpace(pcLastGetDatTime))
  115. {
  116. try
  117. {
  118. if (DateTime.Parse(pcLastGetDatTime).CompareTo(DateTime.Parse(loPara.GetValue(0).ToString())) > 0)
  119. {
  120. poParas.RemoveAt(poParas.Count - 1);
  121. poParas.Add(pcLastGetDatTime);
  122. }
  123. }
  124. catch { }
  125. }
  126. }
  127. }
  128. poParas.Reverse();
  129. }
  130. return lbRetval;
  131. }
  132. private bool CheckDateTime(object poDateTime)
  133. {
  134. bool lbRetval = false;
  135. try
  136. {
  137. DateTime loTime = DateTime.Parse(SysBaseLibs.UtilStr.StrFromObj(poDateTime));
  138. lbRetval = true;
  139. }
  140. catch { }
  141. return lbRetval;
  142. }
  143. /// <summary>
  144. /// 根据输入条件生成对应的Sql条件
  145. /// </summary>
  146. /// <param name="pJsonFile"></param>
  147. /// <returns></returns>
  148. private string GetWhereSqlByJsonFile(List<string> poJsonParas)
  149. {
  150. string lcWhereSql = "";
  151. if (poJsonParas.Count > 0)
  152. {
  153. lcWhereSql = " and UploadTime >= '" + CompareEarilyTime(poJsonParas[0]) + "'";
  154. if (poJsonParas.Count == 2)
  155. {
  156. lcWhereSql += " and UploadTime < '" + poJsonParas[1] + "'";
  157. }
  158. }
  159. else
  160. {
  161. lcWhereSql = " and UploadTime >= '" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "'"; ; //DateTime.Now.ToString("yyyy/MM/dd 00:00:00") + "'";
  162. }
  163. return lcWhereSql;
  164. }
  165. private string GetCheckDataByWhere(string poWhereSql, string pRemoteIP, DBConnSql poDBConn)
  166. {
  167. string lcSql = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataViewSql") + " where 1=1 " + poWhereSql + " order by UploadTime Asc";
  168. rsQuery loQuery = poDBConn.OpenQuery(lcSql);
  169. rsXmlNode rsAll = rsXmlNode.ParseGenericXml("<Records><RemoteIP>" + pRemoteIP + "</RemoteIP></Records>");
  170. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  171. {
  172. loQuery.MoveFirst();
  173. rsXmlNode snResultsNodes = rsXmlNode.ParseGenericXml("<ResultSet></ResultSet>");
  174. rsXmlNode snNodes = SysBaseLibs.rsXmlNode.ParseGenericXml(SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataXml"));
  175. for (int i = 0; i < loQuery.RecCount; i++)
  176. {
  177. rsXmlNode loResultNode = snNodes.Clone() as rsXmlNode;
  178. foreach (rsXmlNode loNode in loResultNode.Nodes)
  179. {
  180. loNode.Value = loQuery.GetString(loNode.Name);
  181. }
  182. snResultsNodes.AddChild(loResultNode);
  183. loQuery.MoveNext();
  184. }
  185. rsAll.AddChild(snResultsNodes);
  186. AddOrUpdateGetDataTimes(pRemoteIP, poDBConn);
  187. }
  188. return rsAll.ToXmlString();
  189. //return sb.ToString();
  190. }
  191. /// <summary>
  192. /// 判断是否小于或等于最早的查询时间
  193. /// </summary>
  194. /// <param name="pTime">输入的查询时间</param>
  195. /// <returns></returns>
  196. private string CompareEarilyTime(string pTime)
  197. {
  198. string lbRetval = pTime;
  199. DateTime loTime = DateTime.Parse(pTime);
  200. DateTime loEarliestTime;
  201. string lcSqlTime = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataEarliest");
  202. if (!string.IsNullOrEmpty(lcSqlTime))
  203. {
  204. if (DateTime.TryParse(lcSqlTime, out loEarliestTime))
  205. {
  206. if (loTime.CompareTo(loEarliestTime) < 0)
  207. {
  208. lbRetval = loEarliestTime.ToString("yyyy/MM/dd 00:00:00");
  209. }
  210. }
  211. }
  212. return lbRetval;
  213. }
  214. /// <summary>
  215. /// 判断当前访问的主机IP是不是可以获取数据
  216. /// </summary>
  217. /// <param name="remoteIP"></param>
  218. /// <returns>true:可以获取</returns>
  219. private bool isCanGetCheckData(string remoteIP, DBConnSql poDBConn)
  220. {
  221. bool lbRetval = true;
  222. Sys_AttachTables_info laTab = new Sys_AttachTables_info(remoteIP, "ForGetCheckData", "GetCheckDataMan", poDBConn);
  223. if (laTab != null && !string.IsNullOrEmpty(laTab.SourceKey))
  224. {
  225. TimeSpan ts = DateTime.Now - DateTime.Parse(laTab.ActionTime);
  226. string GetCDataFrequency = SysDataLibs.AppEnv.SysSetObj.GetString("GetCDataFrequency");
  227. if (ts.Minutes < Utils.ValI(GetCDataFrequency))
  228. {
  229. lbRetval = false;
  230. }
  231. }
  232. return lbRetval;
  233. }
  234. /// <summary>
  235. /// 更改查询的操作次数
  236. /// </summary>
  237. /// <param name="remoteIP"></param>
  238. /// <param name="poDBConn"></param>
  239. private void AddOrUpdateGetDataTimes(string remoteIP, DBConnSql poDBConn)
  240. {
  241. Sys_AttachTables_info laTab = new Sys_AttachTables_info(remoteIP, "ForGetCheckData", "GetCheckDataMan", poDBConn);
  242. if (laTab != null && !string.IsNullOrEmpty(laTab.SourceKey))
  243. {
  244. laTab.ActionTime = DateTime.Now.ToString();
  245. laTab.ActionTimes = UtilStr.StrFromObj(Utils.ValI(laTab.ActionTimes) + 1);
  246. if (poDBConn.ExcuteSqlTran(laTab.UpdateSql()))
  247. {
  248. }
  249. }
  250. else
  251. {
  252. Sys_AttachTables_info laTabNew = new Sys_AttachTables_info();
  253. laTabNew.TableId = "ForGetCheckData";
  254. laTabNew.SourceKey = remoteIP;
  255. laTabNew.UserID = "GetCheckDataMan";
  256. laTabNew.ColumnId = "RemoteIP";
  257. laTabNew.ActionTime = DateTime.Now.ToString();
  258. laTabNew.ActionTimes = "1";
  259. laTabNew.Actiion = "已查询";
  260. if (poDBConn.ExcuteSqlTran(laTabNew.InsertSql()))
  261. {
  262. //SysBaseLibs.ThreadLog.SysLog.WriteInfor(laTabNew.DataXMLNode.ToXmlString());
  263. }
  264. }
  265. }
  266. }
  267. }