123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Timers;
- using System.Web;
- using Newtonsoft.Json;
- using SysBaseLibs;
- using SysDataLibs.TableClass;
- using SysSecLibs;
- namespace GSMarketSys
- {
- public class MajorGoodsSourcePuller : IDisposable
- {
- // 肉类数据
- public class MeatSourceData
- {
- public string CommitDate; // 供货日期
- public string CommitCorp; // 供货单位名称
- public string EnterCorp; // 市场名称
- public string BossName; // 经营户名称
- public string EnterNum; // 进货数量
- public string QuarNum; // 检疫证号
- public string Unit; // 进货数量单位
- public string BossRegNo; // 经营户的工商注册号
- public string MarketRegNo; // 市场的工商注册号
- public string QuarantineUnit; // 检疫单位名称
- }
- // 神码接口和工商系统之间的市场注册号映射
- public class MarketMapData
- {
- public string MarketID; // 工商系统中的市场工商注册号
- public string PlatMarketID; // 接口的市场工商注册号
- public int MarketTypeId; // 市场类型
- public string PlatMarketName; // 接口的市场名称
- }
- private Timer _TimerMeatSrc = null;
- private Timer _TimerVegSrc = null;
- private List<MarketMapData> _listMarketMeatSrc = new List<MarketMapData>();
- private DateTime _LastStockTimeMeatSrc = new DateTime(2030, 1, 1);
- int _IntervalMeatSrc = 20;
- private string _MktMapFile = "";
- private string _LastStkTimeFile = "";
- private MajorGoodsSourcePuller()
- {
- _TimerMeatSrc = new Timer();
- _TimerMeatSrc.AutoReset = true; // 是否重复触发
- _TimerMeatSrc.Enabled = false; // 另外用Start()启动
- _TimerMeatSrc.Interval = 20 * 60000; // 20分钟
- _TimerMeatSrc.Elapsed += new ElapsedEventHandler(_Timer_Elapsed_MeatSrc);
- }
- private void InitConfigMeatSrc()
- {
- try
- {
- // 时间间隔(分钟)
- _IntervalMeatSrc = SysDataLibs.AppEnv.SysSetObj.GetInt("MeatSourceInterval");
- if (_IntervalMeatSrc <= 0 || _IntervalMeatSrc > 60)
- _IntervalMeatSrc = 20;
- // 重新设置
- _TimerMeatSrc.Interval = _IntervalMeatSrc * 60000;
- /*string strPath = AppDomain.CurrentDomain.BaseDirectory;
- // 注意bin目录下的文件修改时会导致Application_End,所以不能放在bin目录下
- strPath += "ISipPlat\\";
- _MktMapFile = strPath + "MeatSrcMktMap.xml";
- _LastStkTimeFile = strPath + "MeatSrcStkTime.txt";*/
- }
- catch (Exception)
- {
- }
- }
- #region IDisposable 成员
- public void Dispose()
- {
- if (_TimerMeatSrc != null)
- {
- _TimerMeatSrc.Close();
- _TimerMeatSrc.Dispose();
- }
- }
- #endregion
- // 单键模式
- private static MajorGoodsSourcePuller _Instance = null;
- public static MajorGoodsSourcePuller Instance
- {
- get
- {
- if (_Instance == null)
- {
- _Instance = new MajorGoodsSourcePuller();
- }
- return _Instance;
- }
- }
- public void Start()
- {
- // 肉类数据
- StartPullMeatSrc();
- }
- // 启动
- public bool StartPullMeatSrc()
- {
- if (_TimerMeatSrc.Enabled == true)
- {
- return true;
- }
- bool bRet = false;
- try
- {
- InitConfigMeatSrc();
- // 启动
- _TimerMeatSrc.Start();
- bRet = true;
- if (_IntervalMeatSrc >= 10)
- {
- // 先调用一次,否则需要等待_IntervalMeatSrc分钟才会触发
- PullDataMeatSrc();
- }
- }
- catch (Exception)
- {
- bRet = false;
- }
- return bRet;
- }
- public void Stop()
- {
- // 肉类数据
- StopPullMeatSrc();
- }
- // 停止
- public bool StopPullMeatSrc()
- {
- if (_TimerMeatSrc.Enabled == false)
- {
- return true;
- }
- bool bRet = false;
- try
- {
- _TimerMeatSrc.Stop();
- bRet = true;
- ThreadLog.LogInfo("[PullDataMeatSrc] Stopping... ");
- }
- catch (Exception)
- {
- bRet = false;
- }
- return bRet;
- }
- // 肉类来源数据Timer触发
- private void _Timer_Elapsed_MeatSrc(object sender, ElapsedEventArgs e)
- {
- try
- {
- PullDataMeatSrc();
- }
- catch (Exception)
- {
- }
- }
- // 抓取肉类来源数据
- private void PullDataMeatSrc()
- {
- DBConnSql loConn = null;
- string strMsg = string.Empty;
- int nMajorGoodsTypeID = 2;
- try
- {
- loConn = new DBConnSql();
- //loConn.OnlyExec = false;
- //loConn.OnDBConnectionAction += new evDBConnectionAction(loConn_OnDBConnectionAction);
- if (loConn.Open() == false)
- {
- throw new Exception("[PullDataMeatSrc] DBConnSql open failed");
- }
- // 加载市场名称映射
- LoadMarketMapFromDB(loConn, nMajorGoodsTypeID, ref _listMarketMeatSrc);
- if (_listMarketMeatSrc.Count < 1)
- {
- throw new Exception("[PullDataMeatSrc] MarketMap is empty");
- }
- // 获取最后更新时间
- string strTemp = LoadLastStockTimeFromDB(loConn, nMajorGoodsTypeID);
- if (strTemp != "")
- {
- strMsg = "[PullDataMeatSrc] LoadLastStockTimeFromDB ok, start from " + strTemp;
- ThreadLog.LogInfo(strMsg);
- _LastStockTimeMeatSrc = DateTime.Parse(strTemp);
- }
- else
- {
- strMsg = "[PullDataMeatSrc] LoadLastStockTimeFromDB failed, start from current time";
- ThreadLog.LogInfo(strMsg);
- // 如果读取不到,缺省从当前时间开始查询
- _LastStockTimeMeatSrc = DateTime.Now;
- }
- // 查询条件
- List<nsPlatDataQuery.Condition> queryConList = new List<nsPlatDataQuery.Condition>();
- nsPlatDataQuery.Condition queryCon = new nsPlatDataQuery.Condition();
- queryCon.FieldName = "CommitDate";
- queryCon.BeginLeftTag = 0;
- nsPlatDataQuery.QueryOperate queryOpr = new nsPlatDataQuery.QueryOperate();
- queryOpr.FieldType = 1; //datetime
- // 因没有>比较符,只能将时间+1秒,避免取到重复数据
- DateTime timeAfter = _LastStockTimeMeatSrc.AddSeconds(1);
- queryOpr.FieldValue = timeAfter.ToString("yyyy-MM-dd HH:mm:ss"); //"2012-08-01 15:8:9";
- queryOpr.OptTag = 4; // >=
- queryCon.Operate = queryOpr;
- queryCon.EndRightTag = 0;
- queryCon.LogicJoin = 0;
- queryConList.Add(queryCon);
- // 调用接口
- string strRet = ISipPlatHelper.QueryMeatSource(queryConList);
- //string strRet = @"{table:[{'CommitDate':'2012-8-10 8:15:08','CommitCorp':'苏州市太湖牧业有限责任公司','EnterCorp':'苏州工业园区荷韵市场管理服务有限公司','BossName':'罗士元','EnterNum':'50','QuarNum':'3208055244','Unit':'公斤'},{'CommitDate':'2012-8-10 8:13:06','CommitCorp':'苏州市吴中食品有限公司生猪定点屠宰场','EnterCorp':'苏州工业园区荷韵市场管理服务有限公司','BossName':'吴惠根','EnterNum':'50','QuarNum':'3203701577','Unit':'公斤'},{'CommitDate':'2012-8-10 8:11:43','CommitCorp':'友联批发市场','EnterCorp':'苏州工业园区荷韵市场管理服务有限公司','BossName':'尤大男','EnterNum':'50','QuarNum':'3203701577','Unit':'公斤'}]}";
- List<MeatSourceData> listRet = DeserializeDataMeatSrc(strRet);
- if ((listRet != null) && (listRet.Count > 0))
- {
- foreach (MeatSourceData info in listRet)
- {
- // 检查数据格式
- if (ValidateDataMeatSrc(info) == true)
- {
- // CommitDate和EnterNum已经在ValidateDataMeatSrc中通过检查,不必再AreaToSQL
- info.CommitDate = info.CommitDate.Trim();
- info.CommitCorp = SysBaseLibs.Utils.AreaToSQL(info.CommitCorp).Trim();
- info.EnterCorp = SysBaseLibs.Utils.AreaToSQL(info.EnterCorp).Trim();
- info.BossName = SysBaseLibs.Utils.AreaToSQL(info.BossName).Trim();
- info.EnterNum = info.EnterNum.Trim();
- info.BossRegNo = SysBaseLibs.Utils.AreaToSQL(info.BossRegNo).Trim();
- info.Unit = SysBaseLibs.Utils.AreaToSQL(info.Unit).Trim();
- info.QuarantineUnit = SysBaseLibs.Utils.AreaToSQL(info.QuarantineUnit).Trim();
- info.MarketRegNo = SysBaseLibs.Utils.AreaToSQL(info.MarketRegNo).Trim();
- if (info.QuarNum != null)
- info.QuarNum = SysBaseLibs.Utils.AreaToSQL(info.QuarNum).Trim();
- else
- info.QuarNum = "";
- // 处理当前记录
- ProcessRecordMeatSrc(info, loConn);
- }
- else
- {
- strMsg = "[PullDataMeatSrc] Invalid data," + DumpDataDetailMeatSrc(info);
- ThreadLog.LogErr(strMsg);
- }
- }//for
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogException(err);
- }
- finally
- {
- if (loConn != null && loConn.IsOpened)
- loConn.Close();
- }
- }
- void loConn_OnDBConnectionAction(string pcCommand, string pcMessage)
- {
- ThreadLog.LogInfo("Command: " + pcCommand + " \r\n Message" + pcMessage);
- }
- // 从数据库加载市场注册码映射表
- private void LoadMarketMapFromDB(DBConnSql loConn, int nMajorGoodsTypeID, ref List<MarketMapData> ListMarket)
- {
- ListMarket.Clear();
- try
- {
- string strSql = "SELECT PlatMarketID, PlatMarketName, " + Markets_info.cMarketTypeID;
- strSql += ", " + Tn.Markets + "." + Markets_info.cMarketID + " as MarketID ";
- strSql += " FROM MajorGoodsSrcMarketMap AS a," + Tn.Markets + " WHERE a.MarketID=" + Tn.Markets + "." + Markets_info.cMarketID;
- strSql += " AND MajorGoodsTypeID=" + nMajorGoodsTypeID + " AND " + Markets_info.cIsLock + "=0";
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- loQuery.MoveFirst();
- for (int i = 0; i < loQuery.RecCount; i++)
- {
- MarketMapData info = new MarketMapData();
- info.MarketID = loQuery.GetString(Markets_info.cMarketID).Trim();
- info.PlatMarketName = loQuery.GetString("PlatMarketName").Trim();
- info.PlatMarketID = loQuery.GetString("PlatMarketID").Trim();
- info.MarketTypeId = loQuery.GetInt(Markets_info.cMarketTypeID);
- ListMarket.Add(info);
- loQuery.MoveNext();
- }
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogErr("[PullDataMeatSrc] Failed to load LoadMarketMapFromDB, Reason=" + err.Message);
- }
- }
- // 检查肉类数据格式
- private bool ValidateDataMeatSrc(MeatSourceData info)
- {
- bool bRet = true;
- try
- {
- // 错误时间格式会抛出异常
- DateTime.Parse(info.CommitDate);
- // 错误数值格式也会抛出异常
- double dTotal = Convert.ToDouble(info.EnterNum);
- if (dTotal <= 0.0)
- bRet = false;
- if ((info.CommitCorp == null) || (info.CommitCorp == ""))
- return false;
- if ((info.EnterCorp == null) || (info.EnterCorp == ""))
- return false;
- if ((info.BossName == null) || (info.BossName == ""))
- return false;
- // 检疫证号QuarNum暂时允许为空
- if ((info.Unit == null) || (info.Unit == ""))
- return false;
- if ((info.BossRegNo == null) || (info.BossRegNo == ""))
- return false;
- if ((info.MarketRegNo == null) || (info.MarketRegNo == ""))
- return false;
- if ((info.QuarantineUnit == null) || (info.QuarantineUnit == ""))
- return false;
- }
- catch (Exception)
- {
- bRet = false;
- }
- return bRet;
- }
- // 处理单条肉菜商品来源数据
- private void ProcessRecordMeatSrc(MeatSourceData info, DBConnSql loConn)
- {
- bool bRet = false;
- try
- {
- string strMsg = string.Empty;
- string strMarketID = "";
- int nMajorGoodsTypeID = 2;
- // 匹配市场注册号
- bool bMarketID = GetMarketIdMeatSrc(info, ref strMarketID, ref nMajorGoodsTypeID);
- if (bMarketID == false)
- {
- strMsg = "[PullDataMeatSrc] EnterCorp no match," + DumpDataDetailMeatSrc(info);
- ThreadLog.LogErr(strMsg);
- return;
- }
- // 匹配经营户
- int nSellerID = GetSellerID(strMarketID, info, loConn);
- if (nSellerID <= 0)
- {
- strMsg = "[PullDataMeatSrc] BossName no match," + DumpDataDetailMeatSrc(info);
- ThreadLog.LogErr(strMsg);
- return;
- }
- // 匹配供应商信息
- int nSupplierID = GetSupplierID(info.CommitCorp, loConn);
- if (nSupplierID <= 0)
- {
- // 增加供应商信息
- bRet = AddSupplier(info.CommitCorp, loConn);
- if (bRet == true)
- {
- // 插入后查询SupplierID
- nSupplierID = GetSupplierID(info.CommitCorp, loConn);
- }
- }
- // 供应商有匹配或者增加供应商成功,则写入数据库
- if (nSupplierID > 0)
- {
- bool bUpdateLastTime = false;
- // 查询是否存在
- bRet = QueryRecordMeatSrc(strMarketID, nSellerID, nSupplierID, info, nMajorGoodsTypeID, loConn);
- if (bRet == true)
- {
- bUpdateLastTime = true;
- }
- else
- {
- bRet = InsertRecordMeatSrc(strMarketID, nSellerID, nSupplierID, info, nMajorGoodsTypeID, loConn);
- if (bRet == true)
- {
- bUpdateLastTime = true;
- }
- }
- // 修改最后更新时间
- if (bUpdateLastTime)
- {
- DateTime lastTime = DateTime.Parse(info.CommitDate);
- if (lastTime > _LastStockTimeMeatSrc)
- {
- _LastStockTimeMeatSrc = lastTime;
- SaveLastStockTimeToDB(loConn, nMajorGoodsTypeID, info.CommitDate);
- }
- }
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogException(err);
- }
- }
- // 匹配市场注册号
- private bool GetMarketIdMeatSrc(MeatSourceData info, ref string strMarketID, ref int nMajorGoodsTypeID)
- {
- bool bRet = false;
- foreach (MarketMapData item in _listMarketMeatSrc)
- {
- // 通过市场注册号进行匹配
- //if (item.PlatMarketName == info.EnterCorp)
- if (item.PlatMarketID == info.MarketRegNo)
- {
- strMarketID = item.MarketID;
- // 农贸市场和超市
- if (item.MarketTypeId == 1)
- nMajorGoodsTypeID = 2;
- else if (item.MarketTypeId == 6)
- nMajorGoodsTypeID = 2; // 11(农贸市场和超市都用2)
- bRet = true;
- break;
- }
- }
- return bRet;
- }
- // 匹配经营户
- private int GetSellerID(string strMarketID, MeatSourceData info, DBConnSql loConn)
- {
- int nSellerID = 0;
- try
- {
- // 先通过工商注册号匹配
- string strSql = "SELECT " + Tn.SellerHasWarrant + "." + SellerHasWarrant_info.cSellerID + " AS SELLERID ";
- strSql += " FROM " + Tn.MarketSellers + "," + Tn.SellerHasWarrant;
- strSql += " WHERE " + Tn.SellerHasWarrant + "." + SellerHasWarrant_info.cSellerID + "=" + Tn.MarketSellers + "." + MarketSellers_info.cSellerID;
- strSql += " AND " + SellerHasWarrant_info.cWarrantTypeID + "=4 AND " + SellerHasWarrant_info.cWarrantNumber + "='" + info.BossRegNo + "' ";
- strSql += " AND " + Tn.MarketSellers + "." + MarketSellers_info.cMarketID + "='" + strMarketID + "' ";
- strSql += " AND " + MarketSellers_info.cIsLock + "='N'";
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- nSellerID = loQuery.GetInt(MarketSellers_info.cSellerID);
- }
- // 工商注册号无匹配, 再通过经营户名称匹配
- if (nSellerID <= 0)
- {
- string strSqlName = "SELECT " + MarketSellers_info.cSellerID + " FROM " + Tn.MarketSellers;
- strSqlName += " WHERE " + MarketSellers_info.cMarketID + "='" + strMarketID + "' AND " + MarketSellers_info.cName + "='" + info.BossName + "'";
- strSqlName += " AND " + MarketSellers_info.cIsLock + "='N'";
- rsQuery loQuery2 = loConn.OpenQuery(strSqlName);
- if (loQuery2 != null && loQuery2.IsOpened && loQuery2.RecCount > 0)
- {
- nSellerID = loQuery2.GetInt(MarketSellers_info.cSellerID);
- }
- }
- }
- catch (Exception err)
- {
- string strMsg = "[PullDataMeatSrc] GetSellerID exception, Reason=" + err.Message;
- ThreadLog.LogErr(strMsg);
- }
- return nSellerID;
- }
- // 匹配供应商信息
- private int GetSupplierID(string strSupplier, DBConnSql loConn)
- {
- int nSupplierID = 0;
- try
- {
- string strSql = "SELECT " + Suppliers_info.cSupplierID + " FROM " + Tn.Suppliers;
- strSql += " WHERE " + Suppliers_info.cName + "='" + strSupplier + "'";
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- nSupplierID = loQuery.GetInt(Suppliers_info.cSupplierID);
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogException(err);
- }
- return nSupplierID;
- }
- // 增加供应商信息
- private bool AddSupplier(string strSupplier, DBConnSql loConn)
- {
- bool bRet = false;
- string strMsg = string.Empty;
- try
- {
- Suppliers_info supplier = new Suppliers_info();
- supplier.Name = strSupplier;
- supplier.Address = "";
- supplier.Linkman = "";
- supplier.Tel = "";
- supplier.HabitatID = "1010"; // 缺省为江苏
- supplier.UserID = "admin";
- string strSql = supplier.InsertSql();
- bRet = loConn.ExcuteSqlTran(strSql);
- if (bRet == false)
- {
- strMsg = "[PullMajorGoodsSrcData] AddSupplier error, SupplierName=" + strSupplier;
- ThreadLog.LogErr(strMsg);
- }
- }
- catch (Exception err)
- {
- bRet = false;
- strMsg = "[PullMajorGoodsSrcData] AddSupplier error, SupplierName=" + strSupplier + ",Reason=" + err.Message;
- ThreadLog.LogErr(strMsg);
- }
- return bRet;
- }
- // 写入数据库
- private bool InsertRecordMeatSrc(string strMarketID, int nSellerID, int nSupplierID, MeatSourceData info,
- int nMajorGoodsTypeID, DBConnSql loConn)
- {
- bool bRet = false;
- string strMsg = string.Empty;
- try
- {
- MajorGoodsLog_info record = new MajorGoodsLog_info();
- record.MarketID = strMarketID;
- record.SellerID = nSellerID.ToString();
- record.MajorGoodsTypeID = nMajorGoodsTypeID.ToString(); // 猪肉
- record.SupplierID = nSupplierID.ToString();
- record.StockTime = info.CommitDate;
- record.VarietyCategoryNo = "19037";
- record.VarietyCategoryName = "猪肉";
- record.TotalCount = info.EnterNum;
- record.QuarantineUnit = info.QuarantineUnit + "(" + info.QuarNum + ")";
- record.UserID = "MeatSource"; // 用于标示这是自动导入的数据
- record.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- record.UnitID = info.Unit;
- string strSql = record.InsertSql();
- bRet = loConn.ExcuteSqlTran(strSql);
- if (bRet == false)
- {
- strMsg = "[PullDataMeatSrc] InsertRecordMeatSrc error," + DumpDataDetailMeatSrc(info);
- ThreadLog.LogErr(strMsg);
- }
- }
- catch (Exception err)
- {
- bRet = false;
- strMsg = "[PullDataMeatSrc] InsertRecordMeatSrc error," + DumpDataDetailMeatSrc(info) + ",Reason=" + err.Message;
- ThreadLog.LogErr(strMsg);
- }
- return bRet;
- }
- // 查询数据库,避免重复写入
- private bool QueryRecordMeatSrc(string strMarketID, int nSellerID, int nSupplierID,
- MeatSourceData info, int nMajorGoodsTypeID, DBConnSql loConn)
- {
- bool bRet = false;
- string strMsg = string.Empty;
- try
- {
- string strSql = "SELECT COUNT(*) AS TotalCount FROM " + Tn.MajorGoodsLog;
- strSql += " WHERE " + MajorGoodsLog_info.cMarketID + "='" + strMarketID + "'";
- strSql += " AND " + MajorGoodsLog_info.cSellerID + "=" + nSellerID;
- strSql += " AND " + MajorGoodsLog_info.cMajorGoodsTypeID + "=" + nMajorGoodsTypeID;
- strSql += " AND " + MajorGoodsLog_info.cSupplierID + "=" + nSupplierID;
- // 注意StockTime为smalldatetime类型
- strSql += " AND " + MajorGoodsLog_info.cStockTime + "=CAST('" + info.CommitDate + "' as smalldatetime) ";
- strSql += " AND " + MajorGoodsLog_info.cTotalCount + "=" + info.EnterNum;
- // QuarantineUnit是接口中两个字段(检疫单位名称+检疫证)拼接起来的,不能直接比较;其他字段如UnitID等暂时不用来进行比较
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- int nTotal = loQuery.GetInt("TotalCount");
- if (nTotal > 0)
- bRet = true;
- }
- }
- catch (Exception err)
- {
- bRet = false;
- strMsg = "[PullDataMeatSrc] QueryRecordMeatSrc error," + DumpDataDetailMeatSrc(info) + ",Reason=" + err.Message;
- ThreadLog.LogErr(strMsg);
- }
- return bRet;
- }
- private string DumpDataDetailMeatSrc(MeatSourceData info)
- {
- string strRet = "CommitDate=" + info.CommitDate + ",CommitCorp=" + info.CommitCorp + ",EnterCorp=" + info.EnterCorp;
- strRet += ",BossName=" + info.BossName + ",EnterNum=" + info.EnterNum + ",QuarNum=" + info.QuarNum + ",Unit=" + info.Unit;
- strRet += ",BossRegNo=" + info.BossRegNo + ",MarketRegNo=" + info.MarketRegNo + ",QuarantineUnit=" + info.QuarantineUnit;
- return strRet;
- }
- // 将原始的json数据反序列化
- private List<MeatSourceData> DeserializeDataMeatSrc(string strSource)
- {
- List<MeatSourceData> result = null;
- try
- {
- string strHead = "{table:[";
- string strTail = "]}";
- int nHead = strSource.IndexOf(strHead);
- if (nHead < 0)
- return null;
- int nTail = strSource.LastIndexOf(strTail);
- if (nTail < 0)
- return null;
- // 取最左边[ 和 最右边]之间的字符串
- int nLen = nTail - 6;
- if (nLen < 1)
- return null;
- string strData = strSource.Substring(7, nLen);
- result = JsonConvert.DeserializeObject<List<MeatSourceData>>(strData);
- }
- catch (Exception err)
- {
- ThreadLog.LogErr(err.Message);
- }
- return result;
- }
- #region 配置文件读取,现已经改成数据库方式
- // 加载 市场代码-市场名称 对照表
- private void LoadMarketNameMap()
- {
- /*
- string strConfigData =
- @"<marketmaps>
- <record>
- <MarketID>10001</MarketID>
- <MarketTypeId>1</MarketTypeId>
- <Name>吴淞农贸</Name>
- </record>
- <record>
- <MarketID>10002</MarketID>
- <MarketTypeId>1</MarketTypeId>
- <Name>胜浦农贸</Name>
- </record>
- </marketmaps>";
- */
- _listMarketMeatSrc.Clear();
- try
- {
- string strConfigData = FileFuns.ReadFileAsString(_MktMapFile);
- rsXmlNode loMainNode = rsXmlNode.ParseGenericXml(strConfigData);
- if (loMainNode != null && loMainNode.Nodes.Count > 0)
- {
- foreach (rsXmlNode loNode in loMainNode.Nodes)
- {
- if (loNode != null && loNode.Nodes.Count > 0)
- {
- MarketMapData info = new MarketMapData();
- info.MarketID = loNode.GetChildValue("MarketID").Trim();
- info.PlatMarketName = loNode.GetChildValue("Name").Trim();
- info.MarketTypeId = Convert.ToInt32(loNode.GetChildValue("MarketTypeId").Trim());
- _listMarketMeatSrc.Add(info);
- }
- }
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogErr("[PullMajorGoodsSrcData] Failed to load MarketNameMap, Reason=" + err.Message);
- }
- }
- private string LoadLastStockTime()
- {
- string strRet = string.Empty;
- try
- {
- string strTemp = FileFuns.ReadFileAsString(_LastStkTimeFile);
- if (strTemp != "")
- {
- DateTime result = DateTime.Parse(strTemp);
- strRet = strTemp;
- }
- }
- catch (Exception err)
- {
- strRet = "";
- ThreadLog.LogException(err);
- }
- return strRet;
- }
- // 修改最后更新时间
- private void SaveLastStockTime(string strTime)
- {
- try
- {
- DateTime lastTime = DateTime.Parse(strTime);
- if (lastTime > _LastStockTimeMeatSrc)
- {
- _LastStockTimeMeatSrc = lastTime;
- FileFuns.WriteStringToFile(strTime, _LastStkTimeFile);
- ThreadLog.LogInfo("Update LastStockTime " + _LastStockTimeMeatSrc.ToString());
- }
- }
- catch (Exception err)
- {
- ThreadLog.LogErr("[PullMajorGoodsSrcData] SaveLastStockTime failed, Reason=" + err.Message);
- }
- }
- #endregion
- // 从数据库读取最新商品来源时间
- private string LoadLastStockTimeFromDB(DBConnSql loConn, int nMajorGoodsTypeID)
- {
- string strRet = string.Empty;
- try
- {
- string strSql = "SELECT StockTime FROM MajorGoodsSrcStockTime WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- strRet = loQuery.GetString("StockTime");
- }
- }
- catch (Exception err)
- {
- strRet = "";
- ThreadLog.LogException(err);
- }
- return strRet;
- }
- // 将最新商品来源时间写入数据库
- private void SaveLastStockTimeToDB(DBConnSql loConn, int nMajorGoodsTypeID, string strTime)
- {
- string strMsg = string.Empty;
- try
- {
- // 插入还是更新
- int nTotalCount = 0;
- string strSql = "SELECT COUNT(*) AS TotalCnt FROM MajorGoodsSrcStockTime WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
- rsQuery loQuery = loConn.OpenQuery(strSql);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- nTotalCount = loQuery.GetInt("TotalCnt");
- }
- // 数据库更新或者插入
- string strSqlCmd = "Update MajorGoodsSrcStockTime Set StockTime='" + strTime + "' WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
- if (nTotalCount <= 0)
- strSqlCmd = "Insert into MajorGoodsSrcStockTime (MajorGoodsTypeID, StockTime) values (" + nMajorGoodsTypeID + ",'" + strTime + "')";
- bool bRet = loConn.ExcuteSqlTran(strSqlCmd);
- if (bRet == false)
- {
- strMsg = "[PullMajorGoodsSrcData] SaveLastStockTimeToDB failed, Time=" + strTime + ", MajorGoodsType=" + nMajorGoodsTypeID.ToString();
- ThreadLog.LogInfo(strMsg);
- }
- }
- catch (Exception err)
- {
- strMsg = "[PullMajorGoodsSrcData] SaveLastStockTimeToDB failed, Time=" + strTime + ", MajorGoodsType=" + nMajorGoodsTypeID.ToString();
- strMsg += ",Reason=" + err.Message;
- ThreadLog.LogInfo(strMsg);
- }
- }
- }
- }
|