MajorGoodsSourcePuller.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Timers;
  5. using System.Web;
  6. using Newtonsoft.Json;
  7. using SysBaseLibs;
  8. using SysDataLibs.TableClass;
  9. using SysSecLibs;
  10. namespace GSMarketSys
  11. {
  12. public class MajorGoodsSourcePuller : IDisposable
  13. {
  14. // 肉类数据
  15. public class MeatSourceData
  16. {
  17. public string CommitDate; // 供货日期
  18. public string CommitCorp; // 供货单位名称
  19. public string EnterCorp; // 市场名称
  20. public string BossName; // 经营户名称
  21. public string EnterNum; // 进货数量
  22. public string QuarNum; // 检疫证号
  23. public string Unit; // 进货数量单位
  24. public string BossRegNo; // 经营户的工商注册号
  25. public string MarketRegNo; // 市场的工商注册号
  26. public string QuarantineUnit; // 检疫单位名称
  27. }
  28. // 神码接口和工商系统之间的市场注册号映射
  29. public class MarketMapData
  30. {
  31. public string MarketID; // 工商系统中的市场工商注册号
  32. public string PlatMarketID; // 接口的市场工商注册号
  33. public int MarketTypeId; // 市场类型
  34. public string PlatMarketName; // 接口的市场名称
  35. }
  36. private Timer _TimerMeatSrc = null;
  37. private Timer _TimerVegSrc = null;
  38. private List<MarketMapData> _listMarketMeatSrc = new List<MarketMapData>();
  39. private DateTime _LastStockTimeMeatSrc = new DateTime(2030, 1, 1);
  40. int _IntervalMeatSrc = 20;
  41. private string _MktMapFile = "";
  42. private string _LastStkTimeFile = "";
  43. private MajorGoodsSourcePuller()
  44. {
  45. _TimerMeatSrc = new Timer();
  46. _TimerMeatSrc.AutoReset = true; // 是否重复触发
  47. _TimerMeatSrc.Enabled = false; // 另外用Start()启动
  48. _TimerMeatSrc.Interval = 20 * 60000; // 20分钟
  49. _TimerMeatSrc.Elapsed += new ElapsedEventHandler(_Timer_Elapsed_MeatSrc);
  50. }
  51. private void InitConfigMeatSrc()
  52. {
  53. try
  54. {
  55. // 时间间隔(分钟)
  56. _IntervalMeatSrc = SysDataLibs.AppEnv.SysSetObj.GetInt("MeatSourceInterval");
  57. if (_IntervalMeatSrc <= 0 || _IntervalMeatSrc > 60)
  58. _IntervalMeatSrc = 20;
  59. // 重新设置
  60. _TimerMeatSrc.Interval = _IntervalMeatSrc * 60000;
  61. /*string strPath = AppDomain.CurrentDomain.BaseDirectory;
  62. // 注意bin目录下的文件修改时会导致Application_End,所以不能放在bin目录下
  63. strPath += "ISipPlat\\";
  64. _MktMapFile = strPath + "MeatSrcMktMap.xml";
  65. _LastStkTimeFile = strPath + "MeatSrcStkTime.txt";*/
  66. }
  67. catch (Exception)
  68. {
  69. }
  70. }
  71. #region IDisposable 成员
  72. public void Dispose()
  73. {
  74. if (_TimerMeatSrc != null)
  75. {
  76. _TimerMeatSrc.Close();
  77. _TimerMeatSrc.Dispose();
  78. }
  79. }
  80. #endregion
  81. // 单键模式
  82. private static MajorGoodsSourcePuller _Instance = null;
  83. public static MajorGoodsSourcePuller Instance
  84. {
  85. get
  86. {
  87. if (_Instance == null)
  88. {
  89. _Instance = new MajorGoodsSourcePuller();
  90. }
  91. return _Instance;
  92. }
  93. }
  94. public void Start()
  95. {
  96. // 肉类数据
  97. StartPullMeatSrc();
  98. }
  99. // 启动
  100. public bool StartPullMeatSrc()
  101. {
  102. if (_TimerMeatSrc.Enabled == true)
  103. {
  104. return true;
  105. }
  106. bool bRet = false;
  107. try
  108. {
  109. InitConfigMeatSrc();
  110. // 启动
  111. _TimerMeatSrc.Start();
  112. bRet = true;
  113. if (_IntervalMeatSrc >= 10)
  114. {
  115. // 先调用一次,否则需要等待_IntervalMeatSrc分钟才会触发
  116. PullDataMeatSrc();
  117. }
  118. }
  119. catch (Exception)
  120. {
  121. bRet = false;
  122. }
  123. return bRet;
  124. }
  125. public void Stop()
  126. {
  127. // 肉类数据
  128. StopPullMeatSrc();
  129. }
  130. // 停止
  131. public bool StopPullMeatSrc()
  132. {
  133. if (_TimerMeatSrc.Enabled == false)
  134. {
  135. return true;
  136. }
  137. bool bRet = false;
  138. try
  139. {
  140. _TimerMeatSrc.Stop();
  141. bRet = true;
  142. ThreadLog.LogInfo("[PullDataMeatSrc] Stopping... ");
  143. }
  144. catch (Exception)
  145. {
  146. bRet = false;
  147. }
  148. return bRet;
  149. }
  150. // 肉类来源数据Timer触发
  151. private void _Timer_Elapsed_MeatSrc(object sender, ElapsedEventArgs e)
  152. {
  153. try
  154. {
  155. PullDataMeatSrc();
  156. }
  157. catch (Exception)
  158. {
  159. }
  160. }
  161. // 抓取肉类来源数据
  162. private void PullDataMeatSrc()
  163. {
  164. DBConnSql loConn = null;
  165. string strMsg = string.Empty;
  166. int nMajorGoodsTypeID = 2;
  167. try
  168. {
  169. loConn = new DBConnSql();
  170. //loConn.OnlyExec = false;
  171. //loConn.OnDBConnectionAction += new evDBConnectionAction(loConn_OnDBConnectionAction);
  172. if (loConn.Open() == false)
  173. {
  174. throw new Exception("[PullDataMeatSrc] DBConnSql open failed");
  175. }
  176. // 加载市场名称映射
  177. LoadMarketMapFromDB(loConn, nMajorGoodsTypeID, ref _listMarketMeatSrc);
  178. if (_listMarketMeatSrc.Count < 1)
  179. {
  180. throw new Exception("[PullDataMeatSrc] MarketMap is empty");
  181. }
  182. // 获取最后更新时间
  183. string strTemp = LoadLastStockTimeFromDB(loConn, nMajorGoodsTypeID);
  184. if (strTemp != "")
  185. {
  186. strMsg = "[PullDataMeatSrc] LoadLastStockTimeFromDB ok, start from " + strTemp;
  187. ThreadLog.LogInfo(strMsg);
  188. _LastStockTimeMeatSrc = DateTime.Parse(strTemp);
  189. }
  190. else
  191. {
  192. strMsg = "[PullDataMeatSrc] LoadLastStockTimeFromDB failed, start from current time";
  193. ThreadLog.LogInfo(strMsg);
  194. // 如果读取不到,缺省从当前时间开始查询
  195. _LastStockTimeMeatSrc = DateTime.Now;
  196. }
  197. // 查询条件
  198. List<nsPlatDataQuery.Condition> queryConList = new List<nsPlatDataQuery.Condition>();
  199. nsPlatDataQuery.Condition queryCon = new nsPlatDataQuery.Condition();
  200. queryCon.FieldName = "CommitDate";
  201. queryCon.BeginLeftTag = 0;
  202. nsPlatDataQuery.QueryOperate queryOpr = new nsPlatDataQuery.QueryOperate();
  203. queryOpr.FieldType = 1; //datetime
  204. // 因没有>比较符,只能将时间+1秒,避免取到重复数据
  205. DateTime timeAfter = _LastStockTimeMeatSrc.AddSeconds(1);
  206. queryOpr.FieldValue = timeAfter.ToString("yyyy-MM-dd HH:mm:ss"); //"2012-08-01 15:8:9";
  207. queryOpr.OptTag = 4; // >=
  208. queryCon.Operate = queryOpr;
  209. queryCon.EndRightTag = 0;
  210. queryCon.LogicJoin = 0;
  211. queryConList.Add(queryCon);
  212. // 调用接口
  213. string strRet = ISipPlatHelper.QueryMeatSource(queryConList);
  214. //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':'公斤'}]}";
  215. List<MeatSourceData> listRet = DeserializeDataMeatSrc(strRet);
  216. if ((listRet != null) && (listRet.Count > 0))
  217. {
  218. foreach (MeatSourceData info in listRet)
  219. {
  220. // 检查数据格式
  221. if (ValidateDataMeatSrc(info) == true)
  222. {
  223. // CommitDate和EnterNum已经在ValidateDataMeatSrc中通过检查,不必再AreaToSQL
  224. info.CommitDate = info.CommitDate.Trim();
  225. info.CommitCorp = SysBaseLibs.Utils.AreaToSQL(info.CommitCorp).Trim();
  226. info.EnterCorp = SysBaseLibs.Utils.AreaToSQL(info.EnterCorp).Trim();
  227. info.BossName = SysBaseLibs.Utils.AreaToSQL(info.BossName).Trim();
  228. info.EnterNum = info.EnterNum.Trim();
  229. info.BossRegNo = SysBaseLibs.Utils.AreaToSQL(info.BossRegNo).Trim();
  230. info.Unit = SysBaseLibs.Utils.AreaToSQL(info.Unit).Trim();
  231. info.QuarantineUnit = SysBaseLibs.Utils.AreaToSQL(info.QuarantineUnit).Trim();
  232. info.MarketRegNo = SysBaseLibs.Utils.AreaToSQL(info.MarketRegNo).Trim();
  233. if (info.QuarNum != null)
  234. info.QuarNum = SysBaseLibs.Utils.AreaToSQL(info.QuarNum).Trim();
  235. else
  236. info.QuarNum = "";
  237. // 处理当前记录
  238. ProcessRecordMeatSrc(info, loConn);
  239. }
  240. else
  241. {
  242. strMsg = "[PullDataMeatSrc] Invalid data," + DumpDataDetailMeatSrc(info);
  243. ThreadLog.LogErr(strMsg);
  244. }
  245. }//for
  246. }
  247. }
  248. catch (Exception err)
  249. {
  250. ThreadLog.LogException(err);
  251. }
  252. finally
  253. {
  254. if (loConn != null && loConn.IsOpened)
  255. loConn.Close();
  256. }
  257. }
  258. void loConn_OnDBConnectionAction(string pcCommand, string pcMessage)
  259. {
  260. ThreadLog.LogInfo("Command: " + pcCommand + " \r\n Message" + pcMessage);
  261. }
  262. // 从数据库加载市场注册码映射表
  263. private void LoadMarketMapFromDB(DBConnSql loConn, int nMajorGoodsTypeID, ref List<MarketMapData> ListMarket)
  264. {
  265. ListMarket.Clear();
  266. try
  267. {
  268. string strSql = "SELECT PlatMarketID, PlatMarketName, " + Markets_info.cMarketTypeID;
  269. strSql += ", " + Tn.Markets + "." + Markets_info.cMarketID + " as MarketID ";
  270. strSql += " FROM MajorGoodsSrcMarketMap AS a," + Tn.Markets + " WHERE a.MarketID=" + Tn.Markets + "." + Markets_info.cMarketID;
  271. strSql += " AND MajorGoodsTypeID=" + nMajorGoodsTypeID + " AND " + Markets_info.cIsLock + "=0";
  272. rsQuery loQuery = loConn.OpenQuery(strSql);
  273. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  274. {
  275. loQuery.MoveFirst();
  276. for (int i = 0; i < loQuery.RecCount; i++)
  277. {
  278. MarketMapData info = new MarketMapData();
  279. info.MarketID = loQuery.GetString(Markets_info.cMarketID).Trim();
  280. info.PlatMarketName = loQuery.GetString("PlatMarketName").Trim();
  281. info.PlatMarketID = loQuery.GetString("PlatMarketID").Trim();
  282. info.MarketTypeId = loQuery.GetInt(Markets_info.cMarketTypeID);
  283. ListMarket.Add(info);
  284. loQuery.MoveNext();
  285. }
  286. }
  287. }
  288. catch (Exception err)
  289. {
  290. ThreadLog.LogErr("[PullDataMeatSrc] Failed to load LoadMarketMapFromDB, Reason=" + err.Message);
  291. }
  292. }
  293. // 检查肉类数据格式
  294. private bool ValidateDataMeatSrc(MeatSourceData info)
  295. {
  296. bool bRet = true;
  297. try
  298. {
  299. // 错误时间格式会抛出异常
  300. DateTime.Parse(info.CommitDate);
  301. // 错误数值格式也会抛出异常
  302. double dTotal = Convert.ToDouble(info.EnterNum);
  303. if (dTotal <= 0.0)
  304. bRet = false;
  305. if ((info.CommitCorp == null) || (info.CommitCorp == ""))
  306. return false;
  307. if ((info.EnterCorp == null) || (info.EnterCorp == ""))
  308. return false;
  309. if ((info.BossName == null) || (info.BossName == ""))
  310. return false;
  311. // 检疫证号QuarNum暂时允许为空
  312. if ((info.Unit == null) || (info.Unit == ""))
  313. return false;
  314. if ((info.BossRegNo == null) || (info.BossRegNo == ""))
  315. return false;
  316. if ((info.MarketRegNo == null) || (info.MarketRegNo == ""))
  317. return false;
  318. if ((info.QuarantineUnit == null) || (info.QuarantineUnit == ""))
  319. return false;
  320. }
  321. catch (Exception)
  322. {
  323. bRet = false;
  324. }
  325. return bRet;
  326. }
  327. // 处理单条肉菜商品来源数据
  328. private void ProcessRecordMeatSrc(MeatSourceData info, DBConnSql loConn)
  329. {
  330. bool bRet = false;
  331. try
  332. {
  333. string strMsg = string.Empty;
  334. string strMarketID = "";
  335. int nMajorGoodsTypeID = 2;
  336. // 匹配市场注册号
  337. bool bMarketID = GetMarketIdMeatSrc(info, ref strMarketID, ref nMajorGoodsTypeID);
  338. if (bMarketID == false)
  339. {
  340. strMsg = "[PullDataMeatSrc] EnterCorp no match," + DumpDataDetailMeatSrc(info);
  341. ThreadLog.LogErr(strMsg);
  342. return;
  343. }
  344. // 匹配经营户
  345. int nSellerID = GetSellerID(strMarketID, info, loConn);
  346. if (nSellerID <= 0)
  347. {
  348. strMsg = "[PullDataMeatSrc] BossName no match," + DumpDataDetailMeatSrc(info);
  349. ThreadLog.LogErr(strMsg);
  350. return;
  351. }
  352. // 匹配供应商信息
  353. int nSupplierID = GetSupplierID(info.CommitCorp, loConn);
  354. if (nSupplierID <= 0)
  355. {
  356. // 增加供应商信息
  357. bRet = AddSupplier(info.CommitCorp, loConn);
  358. if (bRet == true)
  359. {
  360. // 插入后查询SupplierID
  361. nSupplierID = GetSupplierID(info.CommitCorp, loConn);
  362. }
  363. }
  364. // 供应商有匹配或者增加供应商成功,则写入数据库
  365. if (nSupplierID > 0)
  366. {
  367. bool bUpdateLastTime = false;
  368. // 查询是否存在
  369. bRet = QueryRecordMeatSrc(strMarketID, nSellerID, nSupplierID, info, nMajorGoodsTypeID, loConn);
  370. if (bRet == true)
  371. {
  372. bUpdateLastTime = true;
  373. }
  374. else
  375. {
  376. bRet = InsertRecordMeatSrc(strMarketID, nSellerID, nSupplierID, info, nMajorGoodsTypeID, loConn);
  377. if (bRet == true)
  378. {
  379. bUpdateLastTime = true;
  380. }
  381. }
  382. // 修改最后更新时间
  383. if (bUpdateLastTime)
  384. {
  385. DateTime lastTime = DateTime.Parse(info.CommitDate);
  386. if (lastTime > _LastStockTimeMeatSrc)
  387. {
  388. _LastStockTimeMeatSrc = lastTime;
  389. SaveLastStockTimeToDB(loConn, nMajorGoodsTypeID, info.CommitDate);
  390. }
  391. }
  392. }
  393. }
  394. catch (Exception err)
  395. {
  396. ThreadLog.LogException(err);
  397. }
  398. }
  399. // 匹配市场注册号
  400. private bool GetMarketIdMeatSrc(MeatSourceData info, ref string strMarketID, ref int nMajorGoodsTypeID)
  401. {
  402. bool bRet = false;
  403. foreach (MarketMapData item in _listMarketMeatSrc)
  404. {
  405. // 通过市场注册号进行匹配
  406. //if (item.PlatMarketName == info.EnterCorp)
  407. if (item.PlatMarketID == info.MarketRegNo)
  408. {
  409. strMarketID = item.MarketID;
  410. // 农贸市场和超市
  411. if (item.MarketTypeId == 1)
  412. nMajorGoodsTypeID = 2;
  413. else if (item.MarketTypeId == 6)
  414. nMajorGoodsTypeID = 2; // 11(农贸市场和超市都用2)
  415. bRet = true;
  416. break;
  417. }
  418. }
  419. return bRet;
  420. }
  421. // 匹配经营户
  422. private int GetSellerID(string strMarketID, MeatSourceData info, DBConnSql loConn)
  423. {
  424. int nSellerID = 0;
  425. try
  426. {
  427. // 先通过工商注册号匹配
  428. string strSql = "SELECT " + Tn.SellerHasWarrant + "." + SellerHasWarrant_info.cSellerID + " AS SELLERID ";
  429. strSql += " FROM " + Tn.MarketSellers + "," + Tn.SellerHasWarrant;
  430. strSql += " WHERE " + Tn.SellerHasWarrant + "." + SellerHasWarrant_info.cSellerID + "=" + Tn.MarketSellers + "." + MarketSellers_info.cSellerID;
  431. strSql += " AND " + SellerHasWarrant_info.cWarrantTypeID + "=4 AND " + SellerHasWarrant_info.cWarrantNumber + "='" + info.BossRegNo + "' ";
  432. strSql += " AND " + Tn.MarketSellers + "." + MarketSellers_info.cMarketID + "='" + strMarketID + "' ";
  433. strSql += " AND " + MarketSellers_info.cIsLock + "='N'";
  434. rsQuery loQuery = loConn.OpenQuery(strSql);
  435. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  436. {
  437. nSellerID = loQuery.GetInt(MarketSellers_info.cSellerID);
  438. }
  439. // 工商注册号无匹配, 再通过经营户名称匹配
  440. if (nSellerID <= 0)
  441. {
  442. string strSqlName = "SELECT " + MarketSellers_info.cSellerID + " FROM " + Tn.MarketSellers;
  443. strSqlName += " WHERE " + MarketSellers_info.cMarketID + "='" + strMarketID + "' AND " + MarketSellers_info.cName + "='" + info.BossName + "'";
  444. strSqlName += " AND " + MarketSellers_info.cIsLock + "='N'";
  445. rsQuery loQuery2 = loConn.OpenQuery(strSqlName);
  446. if (loQuery2 != null && loQuery2.IsOpened && loQuery2.RecCount > 0)
  447. {
  448. nSellerID = loQuery2.GetInt(MarketSellers_info.cSellerID);
  449. }
  450. }
  451. }
  452. catch (Exception err)
  453. {
  454. string strMsg = "[PullDataMeatSrc] GetSellerID exception, Reason=" + err.Message;
  455. ThreadLog.LogErr(strMsg);
  456. }
  457. return nSellerID;
  458. }
  459. // 匹配供应商信息
  460. private int GetSupplierID(string strSupplier, DBConnSql loConn)
  461. {
  462. int nSupplierID = 0;
  463. try
  464. {
  465. string strSql = "SELECT " + Suppliers_info.cSupplierID + " FROM " + Tn.Suppliers;
  466. strSql += " WHERE " + Suppliers_info.cName + "='" + strSupplier + "'";
  467. rsQuery loQuery = loConn.OpenQuery(strSql);
  468. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  469. {
  470. nSupplierID = loQuery.GetInt(Suppliers_info.cSupplierID);
  471. }
  472. }
  473. catch (Exception err)
  474. {
  475. ThreadLog.LogException(err);
  476. }
  477. return nSupplierID;
  478. }
  479. // 增加供应商信息
  480. private bool AddSupplier(string strSupplier, DBConnSql loConn)
  481. {
  482. bool bRet = false;
  483. string strMsg = string.Empty;
  484. try
  485. {
  486. Suppliers_info supplier = new Suppliers_info();
  487. supplier.Name = strSupplier;
  488. supplier.Address = "";
  489. supplier.Linkman = "";
  490. supplier.Tel = "";
  491. supplier.HabitatID = "1010"; // 缺省为江苏
  492. supplier.UserID = "admin";
  493. string strSql = supplier.InsertSql();
  494. bRet = loConn.ExcuteSqlTran(strSql);
  495. if (bRet == false)
  496. {
  497. strMsg = "[PullMajorGoodsSrcData] AddSupplier error, SupplierName=" + strSupplier;
  498. ThreadLog.LogErr(strMsg);
  499. }
  500. }
  501. catch (Exception err)
  502. {
  503. bRet = false;
  504. strMsg = "[PullMajorGoodsSrcData] AddSupplier error, SupplierName=" + strSupplier + ",Reason=" + err.Message;
  505. ThreadLog.LogErr(strMsg);
  506. }
  507. return bRet;
  508. }
  509. // 写入数据库
  510. private bool InsertRecordMeatSrc(string strMarketID, int nSellerID, int nSupplierID, MeatSourceData info,
  511. int nMajorGoodsTypeID, DBConnSql loConn)
  512. {
  513. bool bRet = false;
  514. string strMsg = string.Empty;
  515. try
  516. {
  517. MajorGoodsLog_info record = new MajorGoodsLog_info();
  518. record.MarketID = strMarketID;
  519. record.SellerID = nSellerID.ToString();
  520. record.MajorGoodsTypeID = nMajorGoodsTypeID.ToString(); // 猪肉
  521. record.SupplierID = nSupplierID.ToString();
  522. record.StockTime = info.CommitDate;
  523. record.VarietyCategoryNo = "19037";
  524. record.VarietyCategoryName = "猪肉";
  525. record.TotalCount = info.EnterNum;
  526. record.QuarantineUnit = info.QuarantineUnit + "(" + info.QuarNum + ")";
  527. record.UserID = "MeatSource"; // 用于标示这是自动导入的数据
  528. record.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  529. record.UnitID = info.Unit;
  530. string strSql = record.InsertSql();
  531. bRet = loConn.ExcuteSqlTran(strSql);
  532. if (bRet == false)
  533. {
  534. strMsg = "[PullDataMeatSrc] InsertRecordMeatSrc error," + DumpDataDetailMeatSrc(info);
  535. ThreadLog.LogErr(strMsg);
  536. }
  537. }
  538. catch (Exception err)
  539. {
  540. bRet = false;
  541. strMsg = "[PullDataMeatSrc] InsertRecordMeatSrc error," + DumpDataDetailMeatSrc(info) + ",Reason=" + err.Message;
  542. ThreadLog.LogErr(strMsg);
  543. }
  544. return bRet;
  545. }
  546. // 查询数据库,避免重复写入
  547. private bool QueryRecordMeatSrc(string strMarketID, int nSellerID, int nSupplierID,
  548. MeatSourceData info, int nMajorGoodsTypeID, DBConnSql loConn)
  549. {
  550. bool bRet = false;
  551. string strMsg = string.Empty;
  552. try
  553. {
  554. string strSql = "SELECT COUNT(*) AS TotalCount FROM " + Tn.MajorGoodsLog;
  555. strSql += " WHERE " + MajorGoodsLog_info.cMarketID + "='" + strMarketID + "'";
  556. strSql += " AND " + MajorGoodsLog_info.cSellerID + "=" + nSellerID;
  557. strSql += " AND " + MajorGoodsLog_info.cMajorGoodsTypeID + "=" + nMajorGoodsTypeID;
  558. strSql += " AND " + MajorGoodsLog_info.cSupplierID + "=" + nSupplierID;
  559. // 注意StockTime为smalldatetime类型
  560. strSql += " AND " + MajorGoodsLog_info.cStockTime + "=CAST('" + info.CommitDate + "' as smalldatetime) ";
  561. strSql += " AND " + MajorGoodsLog_info.cTotalCount + "=" + info.EnterNum;
  562. // QuarantineUnit是接口中两个字段(检疫单位名称+检疫证)拼接起来的,不能直接比较;其他字段如UnitID等暂时不用来进行比较
  563. rsQuery loQuery = loConn.OpenQuery(strSql);
  564. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  565. {
  566. int nTotal = loQuery.GetInt("TotalCount");
  567. if (nTotal > 0)
  568. bRet = true;
  569. }
  570. }
  571. catch (Exception err)
  572. {
  573. bRet = false;
  574. strMsg = "[PullDataMeatSrc] QueryRecordMeatSrc error," + DumpDataDetailMeatSrc(info) + ",Reason=" + err.Message;
  575. ThreadLog.LogErr(strMsg);
  576. }
  577. return bRet;
  578. }
  579. private string DumpDataDetailMeatSrc(MeatSourceData info)
  580. {
  581. string strRet = "CommitDate=" + info.CommitDate + ",CommitCorp=" + info.CommitCorp + ",EnterCorp=" + info.EnterCorp;
  582. strRet += ",BossName=" + info.BossName + ",EnterNum=" + info.EnterNum + ",QuarNum=" + info.QuarNum + ",Unit=" + info.Unit;
  583. strRet += ",BossRegNo=" + info.BossRegNo + ",MarketRegNo=" + info.MarketRegNo + ",QuarantineUnit=" + info.QuarantineUnit;
  584. return strRet;
  585. }
  586. // 将原始的json数据反序列化
  587. private List<MeatSourceData> DeserializeDataMeatSrc(string strSource)
  588. {
  589. List<MeatSourceData> result = null;
  590. try
  591. {
  592. string strHead = "{table:[";
  593. string strTail = "]}";
  594. int nHead = strSource.IndexOf(strHead);
  595. if (nHead < 0)
  596. return null;
  597. int nTail = strSource.LastIndexOf(strTail);
  598. if (nTail < 0)
  599. return null;
  600. // 取最左边[ 和 最右边]之间的字符串
  601. int nLen = nTail - 6;
  602. if (nLen < 1)
  603. return null;
  604. string strData = strSource.Substring(7, nLen);
  605. result = JsonConvert.DeserializeObject<List<MeatSourceData>>(strData);
  606. }
  607. catch (Exception err)
  608. {
  609. ThreadLog.LogErr(err.Message);
  610. }
  611. return result;
  612. }
  613. #region 配置文件读取,现已经改成数据库方式
  614. // 加载 市场代码-市场名称 对照表
  615. private void LoadMarketNameMap()
  616. {
  617. /*
  618. string strConfigData =
  619. @"<marketmaps>
  620. <record>
  621. <MarketID>10001</MarketID>
  622. <MarketTypeId>1</MarketTypeId>
  623. <Name>吴淞农贸</Name>
  624. </record>
  625. <record>
  626. <MarketID>10002</MarketID>
  627. <MarketTypeId>1</MarketTypeId>
  628. <Name>胜浦农贸</Name>
  629. </record>
  630. </marketmaps>";
  631. */
  632. _listMarketMeatSrc.Clear();
  633. try
  634. {
  635. string strConfigData = FileFuns.ReadFileAsString(_MktMapFile);
  636. rsXmlNode loMainNode = rsXmlNode.ParseGenericXml(strConfigData);
  637. if (loMainNode != null && loMainNode.Nodes.Count > 0)
  638. {
  639. foreach (rsXmlNode loNode in loMainNode.Nodes)
  640. {
  641. if (loNode != null && loNode.Nodes.Count > 0)
  642. {
  643. MarketMapData info = new MarketMapData();
  644. info.MarketID = loNode.GetChildValue("MarketID").Trim();
  645. info.PlatMarketName = loNode.GetChildValue("Name").Trim();
  646. info.MarketTypeId = Convert.ToInt32(loNode.GetChildValue("MarketTypeId").Trim());
  647. _listMarketMeatSrc.Add(info);
  648. }
  649. }
  650. }
  651. }
  652. catch (Exception err)
  653. {
  654. ThreadLog.LogErr("[PullMajorGoodsSrcData] Failed to load MarketNameMap, Reason=" + err.Message);
  655. }
  656. }
  657. private string LoadLastStockTime()
  658. {
  659. string strRet = string.Empty;
  660. try
  661. {
  662. string strTemp = FileFuns.ReadFileAsString(_LastStkTimeFile);
  663. if (strTemp != "")
  664. {
  665. DateTime result = DateTime.Parse(strTemp);
  666. strRet = strTemp;
  667. }
  668. }
  669. catch (Exception err)
  670. {
  671. strRet = "";
  672. ThreadLog.LogException(err);
  673. }
  674. return strRet;
  675. }
  676. // 修改最后更新时间
  677. private void SaveLastStockTime(string strTime)
  678. {
  679. try
  680. {
  681. DateTime lastTime = DateTime.Parse(strTime);
  682. if (lastTime > _LastStockTimeMeatSrc)
  683. {
  684. _LastStockTimeMeatSrc = lastTime;
  685. FileFuns.WriteStringToFile(strTime, _LastStkTimeFile);
  686. ThreadLog.LogInfo("Update LastStockTime " + _LastStockTimeMeatSrc.ToString());
  687. }
  688. }
  689. catch (Exception err)
  690. {
  691. ThreadLog.LogErr("[PullMajorGoodsSrcData] SaveLastStockTime failed, Reason=" + err.Message);
  692. }
  693. }
  694. #endregion
  695. // 从数据库读取最新商品来源时间
  696. private string LoadLastStockTimeFromDB(DBConnSql loConn, int nMajorGoodsTypeID)
  697. {
  698. string strRet = string.Empty;
  699. try
  700. {
  701. string strSql = "SELECT StockTime FROM MajorGoodsSrcStockTime WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
  702. rsQuery loQuery = loConn.OpenQuery(strSql);
  703. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  704. {
  705. strRet = loQuery.GetString("StockTime");
  706. }
  707. }
  708. catch (Exception err)
  709. {
  710. strRet = "";
  711. ThreadLog.LogException(err);
  712. }
  713. return strRet;
  714. }
  715. // 将最新商品来源时间写入数据库
  716. private void SaveLastStockTimeToDB(DBConnSql loConn, int nMajorGoodsTypeID, string strTime)
  717. {
  718. string strMsg = string.Empty;
  719. try
  720. {
  721. // 插入还是更新
  722. int nTotalCount = 0;
  723. string strSql = "SELECT COUNT(*) AS TotalCnt FROM MajorGoodsSrcStockTime WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
  724. rsQuery loQuery = loConn.OpenQuery(strSql);
  725. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  726. {
  727. nTotalCount = loQuery.GetInt("TotalCnt");
  728. }
  729. // 数据库更新或者插入
  730. string strSqlCmd = "Update MajorGoodsSrcStockTime Set StockTime='" + strTime + "' WHERE MajorGoodsTypeID=" + nMajorGoodsTypeID;
  731. if (nTotalCount <= 0)
  732. strSqlCmd = "Insert into MajorGoodsSrcStockTime (MajorGoodsTypeID, StockTime) values (" + nMajorGoodsTypeID + ",'" + strTime + "')";
  733. bool bRet = loConn.ExcuteSqlTran(strSqlCmd);
  734. if (bRet == false)
  735. {
  736. strMsg = "[PullMajorGoodsSrcData] SaveLastStockTimeToDB failed, Time=" + strTime + ", MajorGoodsType=" + nMajorGoodsTypeID.ToString();
  737. ThreadLog.LogInfo(strMsg);
  738. }
  739. }
  740. catch (Exception err)
  741. {
  742. strMsg = "[PullMajorGoodsSrcData] SaveLastStockTimeToDB failed, Time=" + strTime + ", MajorGoodsType=" + nMajorGoodsTypeID.ToString();
  743. strMsg += ",Reason=" + err.Message;
  744. ThreadLog.LogInfo(strMsg);
  745. }
  746. }
  747. }
  748. }