using SysBaseLibs;
using SysDataLibs.TableClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace GSMarketSys.WS
{
///
/// GetCheckDataService 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class GetCheckDataService : System.Web.Services.WebService
{
public GetCheckDataService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
///
/// [],[],[]
///
///
///
[WebMethod]
public string GetCheckData(string jsonFile)
{
string AllResult = "";
string reIP = Context.Request.ServerVariables["REMOTE_HOST"];
// string reIP = "123.89.1.1";
SysBaseLibs.ThreadLog.LogInfo("GetCheckData :" + reIP + " " + jsonFile);
string[] arrRemoteIPs = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataRemoteIP").Split('@');
//string lcWhereSql = "";
string testIP = arrRemoteIPs[arrRemoteIPs.Length - 1];
arrRemoteIPs[arrRemoteIPs.Length - 1] = "";
DBConnSql loConn = new DBConnSql();
loConn.Open();
if (arrRemoteIPs.Contains(reIP) || reIP == "127.0.0.1")
{
if (isCanGetCheckData(reIP, loConn))
{
List loJsonPara = new List();
Sys_AttachTables_info laTab = new Sys_AttachTables_info(reIP, "ForGetCheckData", "GetCheckDataMan", loConn);
string lcLastGetDatTime = "";
if (!string.IsNullOrWhiteSpace(laTab.SourceKey))
lcLastGetDatTime = laTab.ActionTime;
if (!CheckSearchTimeNot(jsonFile, loJsonPara, lcLastGetDatTime))
{
return "输入为不正确的时间格式!";
}
AllResult = GetCheckDataByWhere(GetWhereSqlByJsonFile(loJsonPara), reIP, loConn);
}
else
{
AllResult = "距离上次查询时间不到" + SysDataLibs.AppEnv.SysSetObj.GetString("GetCDataFrequency") + "分钟,限制访问!";
}
}
else if (reIP == testIP)
{
AllResult = @"
测试IP
---
葱蒜类
大葱
DefaultUser
2010-01-01 00:00:00
---
甲醛
合格
0.0000
WB967197
";
}
else
{
AllResult = reIP + "是非约定的IP!";
}
return AllResult;
}
///
/// 校验输入的第一个日期格式是否正确
///
///
/// false:正确;true:错误
private bool CheckSearchTimeNot(string pJsonFile, List poParas, string pcLastGetDatTime)
{
bool lbRetval = false;
if (!string.IsNullOrEmpty(pJsonFile))
{
Array loPara = SysBaseLibs.UtilStr.StrToArrayEx(pJsonFile, ",");
if (loPara != null && loPara.Length > 0)
{
if (loPara != null && loPara.Length == 2)
{
lbRetval = CheckDateTime(loPara.GetValue(1));
if (lbRetval)
{
poParas.Add(loPara.GetValue(1).ToString());
}
}
lbRetval = CheckDateTime(loPara.GetValue(0));
if (lbRetval)
{
poParas.Add(loPara.GetValue(0).ToString());
if (SysDataLibs.AppEnv.SysSetObj.GetBool("CheckLastGetDatTime") && !string.IsNullOrWhiteSpace(pcLastGetDatTime))
{
try
{
if (DateTime.Parse(pcLastGetDatTime).CompareTo(DateTime.Parse(loPara.GetValue(0).ToString())) > 0)
{
poParas.RemoveAt(poParas.Count - 1);
poParas.Add(pcLastGetDatTime);
}
}
catch { }
}
}
}
poParas.Reverse();
}
return lbRetval;
}
private bool CheckDateTime(object poDateTime)
{
bool lbRetval = false;
try
{
DateTime loTime = DateTime.Parse(SysBaseLibs.UtilStr.StrFromObj(poDateTime));
lbRetval = true;
}
catch { }
return lbRetval;
}
///
/// 根据输入条件生成对应的Sql条件
///
///
///
private string GetWhereSqlByJsonFile(List poJsonParas)
{
string lcWhereSql = "";
if (poJsonParas.Count > 0)
{
lcWhereSql = " and UploadTime >= '" + CompareEarilyTime(poJsonParas[0]) + "'";
if (poJsonParas.Count == 2)
{
lcWhereSql += " and UploadTime < '" + poJsonParas[1] + "'";
}
}
else
{
lcWhereSql = " and UploadTime >= '" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "'"; ; //DateTime.Now.ToString("yyyy/MM/dd 00:00:00") + "'";
}
return lcWhereSql;
}
private string GetCheckDataByWhere(string poWhereSql, string pRemoteIP, DBConnSql poDBConn)
{
string lcSql = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataViewSql") + " where 1=1 " + poWhereSql + " order by UploadTime Asc";
rsQuery loQuery = poDBConn.OpenQuery(lcSql);
rsXmlNode rsAll = rsXmlNode.ParseGenericXml("" + pRemoteIP + "");
if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
{
loQuery.MoveFirst();
rsXmlNode snResultsNodes = rsXmlNode.ParseGenericXml("");
rsXmlNode snNodes = SysBaseLibs.rsXmlNode.ParseGenericXml(SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataXml"));
for (int i = 0; i < loQuery.RecCount; i++)
{
rsXmlNode loResultNode = snNodes.Clone() as rsXmlNode;
foreach (rsXmlNode loNode in loResultNode.Nodes)
{
loNode.Value = loQuery.GetString(loNode.Name);
}
snResultsNodes.AddChild(loResultNode);
loQuery.MoveNext();
}
rsAll.AddChild(snResultsNodes);
AddOrUpdateGetDataTimes(pRemoteIP, poDBConn);
}
return rsAll.ToXmlString();
//return sb.ToString();
}
///
/// 判断是否小于或等于最早的查询时间
///
/// 输入的查询时间
///
private string CompareEarilyTime(string pTime)
{
string lbRetval = pTime;
DateTime loTime = DateTime.Parse(pTime);
DateTime loEarliestTime;
string lcSqlTime = SysDataLibs.AppEnv.SysSetObj.GetString("GetCheckDataEarliest");
if (!string.IsNullOrEmpty(lcSqlTime))
{
if (DateTime.TryParse(lcSqlTime, out loEarliestTime))
{
if (loTime.CompareTo(loEarliestTime) < 0)
{
lbRetval = loEarliestTime.ToString("yyyy/MM/dd 00:00:00");
}
}
}
return lbRetval;
}
///
/// 判断当前访问的主机IP是不是可以获取数据
///
///
/// true:可以获取
private bool isCanGetCheckData(string remoteIP, DBConnSql poDBConn)
{
bool lbRetval = true;
Sys_AttachTables_info laTab = new Sys_AttachTables_info(remoteIP, "ForGetCheckData", "GetCheckDataMan", poDBConn);
if (laTab != null && !string.IsNullOrEmpty(laTab.SourceKey))
{
TimeSpan ts = DateTime.Now - DateTime.Parse(laTab.ActionTime);
string GetCDataFrequency = SysDataLibs.AppEnv.SysSetObj.GetString("GetCDataFrequency");
if (ts.Minutes < Utils.ValI(GetCDataFrequency))
{
lbRetval = false;
}
}
return lbRetval;
}
///
/// 更改查询的操作次数
///
///
///
private void AddOrUpdateGetDataTimes(string remoteIP, DBConnSql poDBConn)
{
Sys_AttachTables_info laTab = new Sys_AttachTables_info(remoteIP, "ForGetCheckData", "GetCheckDataMan", poDBConn);
if (laTab != null && !string.IsNullOrEmpty(laTab.SourceKey))
{
laTab.ActionTime = DateTime.Now.ToString();
laTab.ActionTimes = UtilStr.StrFromObj(Utils.ValI(laTab.ActionTimes) + 1);
if (poDBConn.ExcuteSqlTran(laTab.UpdateSql()))
{
}
}
else
{
Sys_AttachTables_info laTabNew = new Sys_AttachTables_info();
laTabNew.TableId = "ForGetCheckData";
laTabNew.SourceKey = remoteIP;
laTabNew.UserID = "GetCheckDataMan";
laTabNew.ColumnId = "RemoteIP";
laTabNew.ActionTime = DateTime.Now.ToString();
laTabNew.ActionTimes = "1";
laTabNew.Actiion = "已查询";
if (poDBConn.ExcuteSqlTran(laTabNew.InsertSql()))
{
//SysBaseLibs.ThreadLog.SysLog.WriteInfor(laTabNew.DataXMLNode.ToXmlString());
}
}
}
}
}