SIMPosMerchantTradeService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using MySystem.Models;
  7. using System.Collections.Generic;
  8. using LitJson;
  9. /// <summary>
  10. /// 广电卡交易数据导入
  11. /// </summary>
  12. namespace MySystem
  13. {
  14. public class SIMPosMerchantTradeService
  15. {
  16. public readonly static SIMPosMerchantTradeService Instance = new SIMPosMerchantTradeService();
  17. private SIMPosMerchantTradeService()
  18. {
  19. }
  20. public void Start()//启动
  21. {
  22. Thread thread = new Thread(ImportPostDo); // 广电卡导激活和交易
  23. thread.IsBackground = true;
  24. thread.Start();
  25. // Thread thread1 = new Thread(SIMActDo); // 通过接口获取广电卡激活数据
  26. // thread1.IsBackground = true;
  27. // thread1.Start();
  28. }
  29. public void ImportPostDo()
  30. {
  31. while (true)
  32. {
  33. try
  34. {
  35. string data = RedisDbconn.Instance.RPop<string>("SIMPosMerchantTradeQueue");
  36. if (!string.IsNullOrEmpty(data))
  37. {
  38. string[] dataList = data.Split("#cut#");
  39. string _ExcelPath = dataList[0];
  40. string _Kind = dataList[1];
  41. string checkKey = dataList[2];
  42. string Operator = dataList[3]; // 操作人
  43. int SuccessCount = 0;
  44. int DoCount = 0;
  45. string FullExcelPath = function.getPath(_ExcelPath);
  46. FullExcelPath = FullExcelPath.Replace("//", "/");
  47. DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
  48. int TotalCount = list.Rows.Count;
  49. while (DoCount < list.Rows.Count)
  50. {
  51. WebCMSEntities db = new WebCMSEntities();
  52. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  53. //导激活
  54. if (_Kind == "1")
  55. {
  56. var tran = db.Database.BeginTransaction();
  57. try
  58. {
  59. int Size = 100;
  60. if (list.Rows.Count - DoCount < 100)
  61. {
  62. Size = list.Rows.Count - DoCount;
  63. }
  64. Dictionary<string, int> storeData = new Dictionary<string, int>();
  65. for (int i = DoCount; i < DoCount + Size; i++)
  66. {
  67. DataRow dr = list.Rows[i];
  68. string OrderNo = dr["订单编码"].ToString();
  69. string CreateDate = dr["订单创建时间"].ToString();
  70. string PhoneNumber = dr["选择号码"].ToString();
  71. string HasFirstCharge = dr["是否有首充"].ToString();
  72. string FirstChargeAmount = dr["首充金额(元)"].ToString();
  73. string Province = dr["号码归属省份"].ToString();
  74. string City = dr["号码归属地市"].ToString();
  75. string ProductName = dr["商品名称"].ToString();
  76. string ActiveName = dr["促销活动名称"].ToString();
  77. string ProductType = dr["商品类型名称"].ToString();
  78. string ActualAmount = dr["实付金额(元)"].ToString();
  79. string OrderStatus = dr["订单状态"].ToString();
  80. string ActStatus = dr["激活状态名称"].ToString();
  81. string ActDate = dr["激活时间"].ToString();
  82. string WorkNo = dr["BOSS员工工号"].ToString();
  83. string AccountName = dr["账号姓名"].ToString();
  84. string SimNo = dr["SIM卡号"].ToString();
  85. // PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == PhoneNumber && m.Field5 == ActStatus);
  86. // if (checkMer == null)
  87. // {
  88. string json = "{";
  89. json += "\"OrderNo\":\"" + OrderNo + "\",";
  90. json += "\"CreateDate\":\"" + CreateDate + "\",";
  91. json += "\"PhoneNumber\":\"" + PhoneNumber + "\",";
  92. json += "\"HasFirstCharge\":\"" + HasFirstCharge + "\",";
  93. json += "\"FirstChargeAmount\":\"" + FirstChargeAmount + "\",";
  94. json += "\"Province\":\"" + Province + "\",";
  95. json += "\"City\":\"" + City + "\",";
  96. json += "\"ProductName\":\"" + ProductName + "\",";
  97. json += "\"ActiveName\":\"" + ActiveName + "\",";
  98. json += "\"ProductType\":\"" + ProductType + "\",";
  99. json += "\"ActualAmount\":\"" + ActualAmount + "\",";
  100. json += "\"OrderStatus\":\"" + OrderStatus + "\",";
  101. json += "\"ActStatus\":\"" + ActStatus + "\",";
  102. json += "\"ActDate\":\"" + ActDate + "\",";
  103. json += "\"WorkNo\":\"" + WorkNo + "\",";
  104. json += "\"AccountName\":\"" + AccountName + "\",";
  105. json += "\"SimNo\":\"" + SimNo + "\"";
  106. json += "}";
  107. spdb.BindRecord.Add(new SpModels.BindRecord()
  108. {
  109. CreateDate = DateTime.Parse(CreateDate),
  110. UpdateTime = DateTime.Parse(CreateDate), //机具绑定、解绑时间
  111. CreateTime = DateTime.Parse(CreateDate), //商户操作时间
  112. MerNewSnNo = PhoneNumber, //商户手机号
  113. MerSnNo = SimNo, //序列号
  114. MerName = AccountName, //商户名称
  115. MerNo = PhoneNumber, //商户编号
  116. Remark = ProductType,
  117. Field1 = Province,
  118. Field2 = ActiveName,
  119. Field3 = ProductName,
  120. Field4 = WorkNo,
  121. Field5 = ActStatus,
  122. SeoTitle = ActualAmount,
  123. SeoKeyword = ActDate,
  124. SeoDescription = json,
  125. ProductType = "14",
  126. Status = 1,
  127. });
  128. spdb.Merchants.Add(new SpModels.Merchants()
  129. {
  130. SnNo = SimNo,
  131. CreateTime = DateTime.Parse(CreateDate),
  132. UpdateTime = DateTime.Parse(CreateDate),
  133. AgentName = AccountName,
  134. MerRealName = AccountName,
  135. MerMobile = PhoneNumber,
  136. MerNo = PhoneNumber,
  137. MerName = AccountName,
  138. ProductType = "14",
  139. Status = 1,
  140. });
  141. SuccessCount += 1;
  142. }
  143. DoCount += Size;
  144. spdb.SaveChanges();
  145. tran.Commit();
  146. if (DoCount >= list.Rows.Count)
  147. {
  148. RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|" + SuccessCount);
  149. RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. DoCount = list.Rows.Count;
  155. function.WriteLog(ex.ToString(), "导入广电卡激活数据");
  156. tran.Rollback();
  157. ErrorMsg msg = new ErrorMsg()
  158. {
  159. Time = DateTime.Now,
  160. ErrorContent = ex.ToString(),
  161. };
  162. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡激活数据Excel文件异常");
  163. }
  164. tran.Dispose();
  165. }
  166. //导交易
  167. if (_Kind == "2")
  168. {
  169. var tran = db.Database.BeginTransaction();
  170. try
  171. {
  172. int Size = 100;
  173. if (list.Rows.Count - DoCount < 100)
  174. {
  175. Size = list.Rows.Count - DoCount;
  176. }
  177. Dictionary<string, int> storeData = new Dictionary<string, int>();
  178. for (int i = DoCount; i < DoCount + Size; i++)
  179. {
  180. DataRow dr = list.Rows[i];
  181. string Mobile = dr["用户手机号"].ToString();
  182. string SimNo = dr["SIM卡号"].ToString();
  183. string CreateMonth = dr["入网月份"].ToString();
  184. string SettleMonth = dr["结算月份"].ToString();
  185. string SettleTime = dr["结算账期"].ToString();
  186. string SettleSpace = dr["结算场景"].ToString();
  187. string BusinessType = dr["业务类型"].ToString();
  188. string Province = dr["用户省份"].ToString();
  189. string City = dr["用户地市"].ToString();
  190. string Team = dr["团队归属"].ToString();
  191. string WorkName = dr["行销宝姓名"].ToString();
  192. string WorkNo = dr["行销宝工号"].ToString();
  193. string ProductName = dr["商品名称"].ToString();
  194. string TotalAmount = dr["账单总金额"].ToString();
  195. string InAmount = dr["套内金额"].ToString();
  196. string OutAmount = dr["套外金额"].ToString();
  197. string UserStatus = dr["用户状态"].ToString();
  198. string ChannelCode = dr["渠道编码"].ToString();
  199. string ChannelName = dr["渠道名称"].ToString();
  200. string OrderCreateTime = dr["订单创建时间"].ToString();
  201. string OrderStatus = dr["订单状态"].ToString();
  202. string ActTime = dr["激活时间"].ToString();
  203. string SourceProductName = dr["原订单商品名称"].ToString();
  204. string ProductType = dr["商品类型名称"].ToString();
  205. string ActivityName = dr["促销活动名称"].ToString();
  206. string Percent = dr["分成比例"].ToString();
  207. string OrgDivi = dr["机构分成"].ToString();
  208. string json = "{";
  209. json += "\"Mobile\":\"" + Mobile + "\",";
  210. json += "\"SimNo\":\"" + SimNo + "\",";
  211. json += "\"CreateMonth\":\"" + CreateMonth + "\",";
  212. json += "\"SettleMonth\":\"" + SettleMonth + "\",";
  213. json += "\"SettleTime\":\"" + SettleTime + "\",";
  214. json += "\"SettleSpace\":\"" + SettleSpace + "\",";
  215. json += "\"BusinessType\":\"" + BusinessType + "\",";
  216. json += "\"Province\":\"" + Province + "\",";
  217. json += "\"City\":\"" + City + "\",";
  218. json += "\"Team\":\"" + Team + "\",";
  219. json += "\"WorkName\":\"" + WorkName + "\",";
  220. json += "\"WorkNo\":\"" + WorkNo + "\",";
  221. json += "\"ProductName\":\"" + ProductName + "\",";
  222. json += "\"TotalAmount\":\"" + TotalAmount + "\",";
  223. json += "\"InAmount\":\"" + InAmount + "\",";
  224. json += "\"OutAmount\":\"" + OutAmount + "\",";
  225. json += "\"UserStatus\":\"" + UserStatus + "\",";
  226. json += "\"ChannelCode\":\"" + ChannelCode + "\",";
  227. json += "\"ChannelName\":\"" + ChannelName + "\",";
  228. json += "\"OrderCreateTime\":\"" + OrderCreateTime + "\",";
  229. json += "\"OrderStatus\":\"" + OrderStatus + "\",";
  230. json += "\"ActTime\":\"" + ActTime + "\",";
  231. json += "\"SourceProductName\":\"" + SourceProductName + "\",";
  232. json += "\"ProductType\":\"" + ProductType + "\",";
  233. json += "\"ActivityName\":\"" + ActivityName + "\",";
  234. json += "\"Percent\":\"" + Percent + "\",";
  235. json += "\"OrgDivi\":\"" + OrgDivi + "\"";
  236. json += "}";
  237. bool op = true;
  238. string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  239. decimal TotalAmountNum = decimal.Parse(function.CheckNum(numberString: TotalAmount));
  240. DateTime CreateDate = DateTime.Parse(OrderCreateTime);
  241. string CheckDate = DateTime.Now.AddMonths(-3).ToString("yyyyMM");
  242. if (int.Parse(CreateMonth) >= int.Parse(CheckDate)) op = false;
  243. if (OrderStatus == "待激活") op = false;
  244. if (UserStatus.Contains("注销") || UserStatus.Contains("欠费"))
  245. {
  246. op = false;
  247. SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  248. if (checkSimMer != null)
  249. {
  250. checkSimMer.Status = -1;
  251. if (UserStatus.Contains("欠费") && checkSimMer.Status != -1)
  252. {
  253. checkSimMer.Status = -2;
  254. }
  255. db.SaveChanges();
  256. }
  257. PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  258. if (checkMer != null)
  259. {
  260. checkMer.Status = -1;
  261. if (UserStatus.Contains("欠费") && checkMer.Status != -1)
  262. {
  263. checkMer.Status = -2;
  264. }
  265. db.SaveChanges();
  266. }
  267. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
  268. if (pos != null)
  269. {
  270. pos.Status = -1;
  271. if (UserStatus.Contains("欠费") && pos.Status != -1)
  272. {
  273. pos.Status = -2;
  274. }
  275. db.SaveChanges();
  276. }
  277. }
  278. if (UserStatus.Contains("正常"))
  279. {
  280. SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  281. if (checkSimMer != null)
  282. {
  283. if (checkSimMer.Status == -2) checkSimMer.Status = 0;
  284. db.SaveChanges();
  285. }
  286. PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  287. if (checkMer != null)
  288. {
  289. if (checkMer.Status == -2) checkMer.Status = 1;
  290. db.SaveChanges();
  291. }
  292. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
  293. if (pos != null && pos.Status < 0)
  294. {
  295. if (pos.Status == -2)
  296. {
  297. pos.Status = 0;
  298. }
  299. db.SaveChanges();
  300. }
  301. }
  302. if (TotalAmountNum > 0 && op)
  303. {
  304. spdb.TradeRecord.Add(new SpModels.TradeRecord()
  305. {
  306. CreateDate = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-20 HH:mm:ss")), //创建时间
  307. TradeSerialNo = OrderNo, //订单编号
  308. AgentNo = WorkName + WorkNo, //一级服务商编号
  309. MerNo = Mobile, //商户编
  310. TradeSnNo = SimNo, //sim卡号
  311. ChannelSerial = OrderNo, //请求号
  312. TradeAmount = TotalAmountNum, //账单总金额
  313. TradeStatus = OrderStatus, //订单状态
  314. TradeDate = CreateMonth,
  315. TradeTime = CreateDate.ToString("yyyyMMdd"),
  316. BankCardType = "01",
  317. TradeType = "01",
  318. ProductType = "14",
  319. Status = 1,
  320. Field1 = ProductName,
  321. Field2 = SourceProductName,
  322. Field3 = ProductType,
  323. Field4 = BusinessType,
  324. SeoDescription = json,
  325. });
  326. spdb.SaveChanges();
  327. }
  328. }
  329. DoCount += Size;
  330. db.SaveChanges();
  331. tran.Commit();
  332. if (DoCount >= list.Rows.Count)
  333. {
  334. RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|");
  335. RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. DoCount = list.Rows.Count;
  341. function.WriteLog(ex.ToString(), "导入广电卡交易数据");
  342. tran.Rollback();
  343. ErrorMsg msg = new ErrorMsg()
  344. {
  345. Time = DateTime.Now,
  346. ErrorContent = ex.ToString(),
  347. };
  348. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡交易数据Excel文件异常");
  349. }
  350. tran.Dispose();
  351. }
  352. }
  353. }
  354. else
  355. {
  356. Thread.Sleep(5000);
  357. }
  358. }
  359. catch (Exception ex)
  360. {
  361. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// 通过接口获取广电卡激活数据
  367. /// </summary>
  368. public void SIMActDo()
  369. {
  370. while (true)
  371. {
  372. if (DateTime.Now.Hour > 2 && DateTime.Now.Hour < 19)
  373. {
  374. try
  375. {
  376. var time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  377. string check = function.ReadInstance("/GetSIMActInfo/check" + time + ".txt");
  378. if (string.IsNullOrEmpty(check))
  379. {
  380. function.WritePage("/InstallmentDeduction/", "check" + DateTime.Now.ToString("yyyy-MM-20") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  381. var result = RadioCardHelper.Instance.QueryCardOrderInfo(time, time, "1", "99999");
  382. JsonData jsonObj = JsonMapper.ToObject(result);
  383. if (jsonObj["resultCode"].ToString() == "1")
  384. {
  385. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  386. var data = jsonObj["data"]["records"];
  387. for (int i = 0; i < data.Count; i++)
  388. {
  389. var iccId = data["iccId"].ToString();
  390. var bindRecord = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == iccId) ?? new SpModels.BindRecord();
  391. var merchants = spdb.Merchants.FirstOrDefault(m => m.SnNo == iccId) ?? new SpModels.Merchants();
  392. if (bindRecord.Id == 0 && merchants.Id == 0)
  393. {
  394. // {"orderNo":"B20240311214052744568","orderCreateTime":"03/11/2024 21:40:53","phoneNo":"19288438825","firstCharge":"否","initialChargeAmount":"0.00","province":"贵州省","city":"毕节市","productName":"双百套餐","promotionName":null,"productTypeName":"基础套餐","paidInAmount":"100.00","orderStatus":"已完成","activationStatusName":"已激活","activationTime":"03/11/2024 21:51:47","jobId":"VCH010198795","iccId":"89861500232320197354"}
  395. string OrderNo = data["orderNo"].ToString(); //订单编码
  396. string CreateDate = data["orderCreateTime"].ToString(); //订单创建时间
  397. string PhoneNumber = data["phoneNo"].ToString(); //选择号码
  398. string HasFirstCharge = data["firstCharge"].ToString(); //是否有首充
  399. string FirstChargeAmount = data["initialChargeAmount"].ToString(); //首充金额(元)
  400. string Province = data["province"].ToString(); //号码归属省份
  401. string City = data["city"].ToString(); //号码归属地市
  402. string ProductName = data["productName"].ToString(); //商品名称
  403. string ActiveName = data["promotionName"].ToString(); //促销活动名称
  404. string ProductType = data["productTypeName"].ToString(); //商品类型名称
  405. string ActualAmount = data["paidInAmount"].ToString(); //实付金额(元)
  406. string OrderStatus = data["orderStatus"].ToString(); //订单状态
  407. string ActStatus = data["activationStatusName"].ToString(); //激活状态名称
  408. string ActDate = data["activationTime"].ToString(); //激活时间
  409. string WorkNo = data["jobId"].ToString(); //BOSS员工工号
  410. // string AccountName = data[""].ToString(); //账号姓名
  411. string AccountName = ""; //账号姓名
  412. string SimNo = iccId; //SIM卡号
  413. string json = "{";
  414. json += "\"OrderNo\":\"" + OrderNo + "\",";
  415. json += "\"CreateDate\":\"" + CreateDate + "\",";
  416. json += "\"PhoneNumber\":\"" + PhoneNumber + "\",";
  417. json += "\"HasFirstCharge\":\"" + HasFirstCharge + "\",";
  418. json += "\"FirstChargeAmount\":\"" + FirstChargeAmount + "\",";
  419. json += "\"Province\":\"" + Province + "\",";
  420. json += "\"City\":\"" + City + "\",";
  421. json += "\"ProductName\":\"" + ProductName + "\",";
  422. json += "\"ActiveName\":\"" + ActiveName + "\",";
  423. json += "\"ProductType\":\"" + ProductType + "\",";
  424. json += "\"ActualAmount\":\"" + ActualAmount + "\",";
  425. json += "\"OrderStatus\":\"" + OrderStatus + "\",";
  426. json += "\"ActStatus\":\"" + ActStatus + "\",";
  427. json += "\"ActDate\":\"" + ActDate + "\",";
  428. json += "\"WorkNo\":\"" + WorkNo + "\",";
  429. json += "\"AccountName\":\"" + AccountName + "\",";
  430. json += "\"SimNo\":\"" + SimNo + "\"";
  431. json += "}";
  432. spdb.BindRecord.Add(new SpModels.BindRecord()
  433. {
  434. CreateDate = DateTime.Parse(CreateDate),
  435. UpdateTime = DateTime.Parse(CreateDate), //机具绑定、解绑时间
  436. CreateTime = DateTime.Parse(CreateDate), //商户操作时间
  437. MerNewSnNo = PhoneNumber, //商户手机号
  438. MerSnNo = SimNo, //序列号
  439. MerName = AccountName, //商户名称
  440. MerNo = PhoneNumber, //商户编号
  441. Remark = ProductType,
  442. Field1 = Province,
  443. Field2 = ActiveName,
  444. Field3 = ProductName,
  445. Field4 = WorkNo,
  446. Field5 = ActStatus,
  447. SeoTitle = ActualAmount,
  448. SeoKeyword = ActDate,
  449. SeoDescription = json,
  450. ProductType = "14",
  451. Status = 1,
  452. });
  453. spdb.Merchants.Add(new SpModels.Merchants()
  454. {
  455. SnNo = SimNo,
  456. CreateTime = DateTime.Parse(CreateDate),
  457. UpdateTime = DateTime.Parse(CreateDate),
  458. AgentName = AccountName,
  459. MerRealName = AccountName,
  460. MerMobile = PhoneNumber,
  461. MerNo = PhoneNumber,
  462. MerName = AccountName,
  463. ProductType = "14",
  464. Status = 1,
  465. });
  466. spdb.SaveChanges();
  467. }
  468. }
  469. }
  470. }
  471. }
  472. catch (Exception ex)
  473. {
  474. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "获取广电卡激活信息队列异常");
  475. }
  476. }
  477. Thread.Sleep(600000);
  478. }
  479. }
  480. }
  481. }