SIMPosMerchantTradeService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /// <summary>
  9. /// 广电卡交易数据导入
  10. /// </summary>
  11. namespace MySystem
  12. {
  13. public class SIMPosMerchantTradeService
  14. {
  15. public readonly static SIMPosMerchantTradeService Instance = new SIMPosMerchantTradeService();
  16. private SIMPosMerchantTradeService()
  17. {
  18. }
  19. public void Start()//启动
  20. {
  21. Thread thread = new Thread(ImportPostDo);
  22. thread.IsBackground = true;
  23. thread.Start();
  24. }
  25. public void ImportPostDo()
  26. {
  27. while (true)
  28. {
  29. try
  30. {
  31. string data = RedisDbconn.Instance.RPop<string>("SIMPosMerchantTradeQueue");
  32. if (!string.IsNullOrEmpty(data))
  33. {
  34. string[] dataList = data.Split("#cut#");
  35. string _ExcelPath = dataList[0];
  36. string _Kind = dataList[1];
  37. string checkKey = dataList[2];
  38. string Operator = dataList[3]; // 操作人
  39. int SuccessCount = 0;
  40. int DoCount = 0;
  41. string FullExcelPath = function.getPath(_ExcelPath);
  42. FullExcelPath = FullExcelPath.Replace("//", "/");
  43. DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
  44. int TotalCount = list.Rows.Count;
  45. while (DoCount < list.Rows.Count)
  46. {
  47. WebCMSEntities db = new WebCMSEntities();
  48. SpModels.WebCMSEntities spdb = new SpModels.WebCMSEntities();
  49. //导激活
  50. if (_Kind == "1")
  51. {
  52. var tran = db.Database.BeginTransaction();
  53. try
  54. {
  55. int Size = 100;
  56. if (list.Rows.Count - DoCount < 100)
  57. {
  58. Size = list.Rows.Count - DoCount;
  59. }
  60. Dictionary<string, int> storeData = new Dictionary<string, int>();
  61. for (int i = DoCount; i < DoCount + Size; i++)
  62. {
  63. DataRow dr = list.Rows[i];
  64. string OrderNo = dr["订单编码"].ToString();
  65. string CreateDate = dr["订单创建时间"].ToString();
  66. string PhoneNumber = dr["选择号码"].ToString();
  67. string HasFirstCharge = dr["是否有首充"].ToString();
  68. string FirstChargeAmount = dr["首充金额(元)"].ToString();
  69. string Province = dr["号码归属省份"].ToString();
  70. string City = dr["号码归属地市"].ToString();
  71. string ProductName = dr["商品名称"].ToString();
  72. string ActiveName = dr["促销活动名称"].ToString();
  73. string ProductType = dr["商品类型名称"].ToString();
  74. string ActualAmount = dr["实付金额(元)"].ToString();
  75. string OrderStatus = dr["订单状态"].ToString();
  76. string ActStatus = dr["激活状态名称"].ToString();
  77. string ActDate = dr["激活时间"].ToString();
  78. string WorkNo = dr["BOSS员工工号"].ToString();
  79. string AccountName = dr["账号姓名"].ToString();
  80. string SimNo = dr["SIM卡号"].ToString();
  81. // PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == PhoneNumber && m.Field5 == ActStatus);
  82. // if (checkMer == null)
  83. // {
  84. string json = "{";
  85. json += "\"OrderNo\":\"" + OrderNo + "\",";
  86. json += "\"CreateDate\":\"" + CreateDate + "\",";
  87. json += "\"PhoneNumber\":\"" + PhoneNumber + "\",";
  88. json += "\"HasFirstCharge\":\"" + HasFirstCharge + "\",";
  89. json += "\"FirstChargeAmount\":\"" + FirstChargeAmount + "\",";
  90. json += "\"Province\":\"" + Province + "\",";
  91. json += "\"City\":\"" + City + "\",";
  92. json += "\"ProductName\":\"" + ProductName + "\",";
  93. json += "\"ActiveName\":\"" + ActiveName + "\",";
  94. json += "\"ProductType\":\"" + ProductType + "\",";
  95. json += "\"ActualAmount\":\"" + ActualAmount + "\",";
  96. json += "\"OrderStatus\":\"" + OrderStatus + "\",";
  97. json += "\"ActStatus\":\"" + ActStatus + "\",";
  98. json += "\"ActDate\":\"" + ActDate + "\",";
  99. json += "\"WorkNo\":\"" + WorkNo + "\",";
  100. json += "\"AccountName\":\"" + AccountName + "\",";
  101. json += "\"SimNo\":\"" + SimNo + "\"";
  102. json += "}";
  103. spdb.BindRecord.Add(new SpModels.BindRecord()
  104. {
  105. CreateDate = DateTime.Parse(CreateDate),
  106. UpdateTime = DateTime.Parse(CreateDate), //机具绑定、解绑时间
  107. CreateTime = DateTime.Parse(CreateDate), //商户操作时间
  108. MerNewSnNo = PhoneNumber, //商户手机号
  109. MerSnNo = SimNo, //序列号
  110. MerName = AccountName, //商户名称
  111. MerNo = PhoneNumber, //商户编号
  112. Remark = ProductType,
  113. Field1 = Province,
  114. Field2 = ActiveName,
  115. Field3 = ProductName,
  116. Field4 = WorkNo,
  117. Field5 = ActStatus,
  118. SeoTitle = ActualAmount,
  119. SeoKeyword = ActDate,
  120. SeoDescription = json,
  121. ProductType = "14",
  122. Status = 1,
  123. });
  124. spdb.Merchants.Add(new SpModels.Merchants()
  125. {
  126. SnNo = SimNo,
  127. CreateTime = DateTime.Parse(CreateDate),
  128. UpdateTime = DateTime.Parse(CreateDate),
  129. AgentName = AccountName,
  130. MerRealName = AccountName,
  131. MerMobile = PhoneNumber,
  132. MerNo = PhoneNumber,
  133. MerName = AccountName,
  134. ProductType = "14",
  135. Status = 1,
  136. });
  137. SuccessCount += 1;
  138. }
  139. DoCount += Size;
  140. spdb.SaveChanges();
  141. tran.Commit();
  142. if (DoCount >= list.Rows.Count)
  143. {
  144. RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|" + SuccessCount);
  145. RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. DoCount = list.Rows.Count;
  151. function.WriteLog(ex.ToString(), "导入广电卡激活数据");
  152. tran.Rollback();
  153. ErrorMsg msg = new ErrorMsg()
  154. {
  155. Time = DateTime.Now,
  156. ErrorContent = ex.ToString(),
  157. };
  158. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡激活数据Excel文件异常");
  159. }
  160. tran.Dispose();
  161. }
  162. //导交易
  163. if (_Kind == "2")
  164. {
  165. var tran = db.Database.BeginTransaction();
  166. try
  167. {
  168. int Size = 100;
  169. if (list.Rows.Count - DoCount < 100)
  170. {
  171. Size = list.Rows.Count - DoCount;
  172. }
  173. Dictionary<string, int> storeData = new Dictionary<string, int>();
  174. for (int i = DoCount; i < DoCount + Size; i++)
  175. {
  176. DataRow dr = list.Rows[i];
  177. string Mobile = dr["用户手机号"].ToString();
  178. string SimNo = dr["SIM卡号"].ToString();
  179. string CreateMonth = dr["入网月份"].ToString();
  180. string SettleMonth = dr["结算月份"].ToString();
  181. string SettleTime = dr["结算账期"].ToString();
  182. string SettleSpace = dr["结算场景"].ToString();
  183. string BusinessType = dr["业务类型"].ToString();
  184. string Province = dr["用户省份"].ToString();
  185. string City = dr["用户地市"].ToString();
  186. string Team = dr["团队归属"].ToString();
  187. string WorkName = dr["行销宝姓名"].ToString();
  188. string WorkNo = dr["行销宝工号"].ToString();
  189. string ProductName = dr["商品名称"].ToString();
  190. string TotalAmount = dr["账单总金额"].ToString();
  191. string InAmount = dr["套内金额"].ToString();
  192. string OutAmount = dr["套外金额"].ToString();
  193. string UserStatus = dr["用户状态"].ToString();
  194. string ChannelCode = dr["渠道编码"].ToString();
  195. string ChannelName = dr["渠道名称"].ToString();
  196. string OrderCreateTime = dr["订单创建时间"].ToString();
  197. string OrderStatus = dr["订单状态"].ToString();
  198. string ActTime = dr["激活时间"].ToString();
  199. string SourceProductName = dr["原订单商品名称"].ToString();
  200. string ProductType = dr["商品类型名称"].ToString();
  201. string ActivityName = dr["促销活动名称"].ToString();
  202. string Percent = dr["分成比例"].ToString();
  203. string OrgDivi = dr["机构分成"].ToString();
  204. string json = "{";
  205. json += "\"Mobile\":\"" + Mobile + "\",";
  206. json += "\"SimNo\":\"" + SimNo + "\",";
  207. json += "\"CreateMonth\":\"" + CreateMonth + "\",";
  208. json += "\"SettleMonth\":\"" + SettleMonth + "\",";
  209. json += "\"SettleTime\":\"" + SettleTime + "\",";
  210. json += "\"SettleSpace\":\"" + SettleSpace + "\",";
  211. json += "\"BusinessType\":\"" + BusinessType + "\",";
  212. json += "\"Province\":\"" + Province + "\",";
  213. json += "\"City\":\"" + City + "\",";
  214. json += "\"Team\":\"" + Team + "\",";
  215. json += "\"WorkName\":\"" + WorkName + "\",";
  216. json += "\"WorkNo\":\"" + WorkNo + "\",";
  217. json += "\"ProductName\":\"" + ProductName + "\",";
  218. json += "\"TotalAmount\":\"" + TotalAmount + "\",";
  219. json += "\"InAmount\":\"" + InAmount + "\",";
  220. json += "\"OutAmount\":\"" + OutAmount + "\",";
  221. json += "\"UserStatus\":\"" + UserStatus + "\",";
  222. json += "\"ChannelCode\":\"" + ChannelCode + "\",";
  223. json += "\"ChannelName\":\"" + ChannelName + "\",";
  224. json += "\"OrderCreateTime\":\"" + OrderCreateTime + "\",";
  225. json += "\"OrderStatus\":\"" + OrderStatus + "\",";
  226. json += "\"ActTime\":\"" + ActTime + "\",";
  227. json += "\"SourceProductName\":\"" + SourceProductName + "\",";
  228. json += "\"ProductType\":\"" + ProductType + "\",";
  229. json += "\"ActivityName\":\"" + ActivityName + "\",";
  230. json += "\"Percent\":\"" + Percent + "\",";
  231. json += "\"OrgDivi\":\"" + OrgDivi + "\"";
  232. json += "}";
  233. bool op = true;
  234. string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  235. decimal TotalAmountNum = decimal.Parse(function.CheckNum(numberString: TotalAmount));
  236. DateTime CreateDate = DateTime.Parse(OrderCreateTime);
  237. string CheckDate = DateTime.Now.AddMonths(-3).ToString("yyyyMM");
  238. if (int.Parse(CreateMonth) >= int.Parse(CheckDate)) op = false;
  239. if (OrderStatus == "待激活") op = false;
  240. if (UserStatus.Contains("注销") || UserStatus.Contains("欠费"))
  241. {
  242. op = false;
  243. SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  244. if (checkSimMer != null)
  245. {
  246. checkSimMer.Status = -1;
  247. if (UserStatus.Contains("欠费") && checkSimMer.Status != -1)
  248. {
  249. checkSimMer.Status = -2;
  250. }
  251. db.SaveChanges();
  252. }
  253. PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  254. if (checkMer != null)
  255. {
  256. checkMer.Status = -1;
  257. if (UserStatus.Contains("欠费") && checkMer.Status != -1)
  258. {
  259. checkMer.Status = -2;
  260. }
  261. db.SaveChanges();
  262. }
  263. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
  264. if (pos != null)
  265. {
  266. pos.Status = -1;
  267. if (UserStatus.Contains("欠费") && pos.Status != -1)
  268. {
  269. pos.Status = -2;
  270. }
  271. db.SaveChanges();
  272. }
  273. }
  274. if (UserStatus.Contains("正常"))
  275. {
  276. SimposMerchantInfo checkSimMer = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  277. if (checkSimMer != null)
  278. {
  279. if (checkSimMer.Status == -2) checkSimMer.Status = 0;
  280. db.SaveChanges();
  281. }
  282. PosMerchantInfo checkMer = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mobile && m.KqSnNo == SimNo);
  283. if (checkMer != null)
  284. {
  285. if (checkMer.Status == -2) checkMer.Status = 1;
  286. db.SaveChanges();
  287. }
  288. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SimNo);
  289. if (pos != null && pos.Status < 0)
  290. {
  291. if (pos.Status == -2)
  292. {
  293. pos.Status = 0;
  294. }
  295. db.SaveChanges();
  296. }
  297. }
  298. if (TotalAmountNum > 0 && op)
  299. {
  300. spdb.TradeRecord.Add(new SpModels.TradeRecord()
  301. {
  302. CreateDate = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-20 HH:mm:ss")), //创建时间
  303. TradeSerialNo = OrderNo, //订单编号
  304. AgentNo = WorkName + WorkNo, //一级服务商编号
  305. MerNo = Mobile, //商户编
  306. TradeSnNo = SimNo, //sim卡号
  307. ChannelSerial = OrderNo, //请求号
  308. TradeAmount = TotalAmountNum, //账单总金额
  309. TradeStatus = OrderStatus, //订单状态
  310. TradeDate = CreateMonth,
  311. TradeTime = CreateDate.ToString("yyyyMMdd"),
  312. BankCardType = "01",
  313. TradeType = "01",
  314. ProductType = "14",
  315. Status = 1,
  316. Field1 = ProductName,
  317. Field2 = SourceProductName,
  318. Field3 = ProductType,
  319. Field4 = BusinessType,
  320. SeoDescription = json,
  321. });
  322. spdb.SaveChanges();
  323. }
  324. }
  325. DoCount += Size;
  326. db.SaveChanges();
  327. tran.Commit();
  328. if (DoCount >= list.Rows.Count)
  329. {
  330. RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|");
  331. RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. DoCount = list.Rows.Count;
  337. function.WriteLog(ex.ToString(), "导入广电卡交易数据");
  338. tran.Rollback();
  339. ErrorMsg msg = new ErrorMsg()
  340. {
  341. Time = DateTime.Now,
  342. ErrorContent = ex.ToString(),
  343. };
  344. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入广电卡交易数据Excel文件异常");
  345. }
  346. tran.Dispose();
  347. }
  348. }
  349. }
  350. else
  351. {
  352. Thread.Sleep(5000);
  353. }
  354. }
  355. catch (Exception ex)
  356. {
  357. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
  358. }
  359. }
  360. }
  361. }
  362. }