ExcelHelper.cs 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. namespace MySystem
  9. {
  10. public class ExcelHelper
  11. {
  12. public readonly static ExcelHelper Instance = new ExcelHelper();
  13. private ExcelHelper()
  14. {
  15. }
  16. public void Start()//启动
  17. {
  18. Thread thread = new Thread(ImportPostDo);
  19. thread.IsBackground = true;
  20. thread.Start();
  21. }
  22. public void ImportPostDo()
  23. {
  24. while (true)
  25. {
  26. string data = RedisDbconn.Instance.RPop<string>("ExcelImportV2");
  27. // string data = RedisDbconn.Instance.RPop<string>("ExcelImportV3");
  28. if (!string.IsNullOrEmpty(data))
  29. {
  30. string[] dataList = data.Split("#cut#");
  31. string _ExcelPath = dataList[0];
  32. string _Kind = dataList[1];
  33. string checkKey = dataList[2];
  34. string Operator = dataList[3]; // 操作人
  35. int SuccessCount = 0;
  36. int DoCount = 0;
  37. string FullExcelPath = function.getPath(_ExcelPath);
  38. FullExcelPath = FullExcelPath.Replace("//", "/");
  39. DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
  40. int TotalCount = list.Rows.Count;
  41. while (DoCount < list.Rows.Count)
  42. {
  43. WebCMSEntities db = new WebCMSEntities();
  44. if (_Kind == "1")
  45. {
  46. var tran = db.Database.BeginTransaction();
  47. try
  48. {
  49. int Size = 100;
  50. if (list.Rows.Count - DoCount < 100)
  51. {
  52. Size = list.Rows.Count - DoCount;
  53. }
  54. Dictionary<string, int> storeData = new Dictionary<string, int>();
  55. for (int i = DoCount; i < DoCount + Size; i++)
  56. {
  57. DataRow dr = list.Rows[i];
  58. string BatchNo = dr[0].ToString(); // 业务批次号
  59. string SnNo = dr[1].ToString(); // SN编号
  60. SnNo = SnNo.Replace(" ", "");
  61. string DeviceName = dr[2].ToString(); // 设备厂商
  62. string DeviceKind = dr[3].ToString(); // 设备型号
  63. string DeviceType = dr[4].ToString(); // 设备类型区分(客小爽电签POS填写:KysSignPos/KssSignPos)
  64. string StoreNo = dr[5].ToString(); // 收货仓库编号
  65. string BrandIdString = dr[6].ToString(); // 产品类型(5-客小爽Mpos,6-客小爽大POS,61-客小爽电签POS,8-客小爽超级Mpos)
  66. string InputMan = dr[7].ToString(); // 入库操作人
  67. string InputNote = dr[8].ToString(); // 入库备注
  68. MachineForSnNo machinefor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo);
  69. if (machinefor == null)
  70. {
  71. int BrandId = int.Parse(function.CheckInt(BrandIdString));
  72. KqProducts kqProduct = db.KqProducts.FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  73. if (kqProduct.Id > 0)
  74. {
  75. StoreForCode storefor = db.StoreForCode.FirstOrDefault(m => m.Code == StoreNo) ?? new StoreForCode();
  76. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storefor.StoreId) ?? new StoreHouse();
  77. PosMachinesTwo pos = db.PosMachinesTwo.Add(new PosMachinesTwo()
  78. {
  79. CreateDate = DateTime.Now,
  80. PosSn = SnNo, //SN编号
  81. BrandId = BrandId, //产品类型
  82. StoreId = storefor.StoreId, //所在仓库
  83. DeviceType = DeviceType, //设备类型
  84. BatchNo = BatchNo,
  85. DeviceName = DeviceName,
  86. DeviceKind = DeviceKind,
  87. SourceStoreId = storefor.StoreId, //源仓库
  88. }).Entity;
  89. store.ProductName = kqProduct.Name;
  90. store.BrandId = BrandId.ToString();
  91. StoreStockChange stockchange = db.StoreStockChange.Add(new StoreStockChange()
  92. {
  93. CreateDate = DateTime.Now,
  94. CreateMan = Operator,
  95. StoreId = storefor.StoreId, //仓库
  96. BrandId = BrandId, //产品类型
  97. ProductName = RelationClass.GetKqProductBrandInfo(BrandId), //产品名称
  98. BizBatchNo = BatchNo, //业务批次号
  99. TransType = 0, //交易类型
  100. SnNo = SnNo, //SN编号
  101. StockOpDirect = 0, //库存操作方向
  102. SnStatus = 0, //SN状态
  103. DeviceVendor = DeviceName, //设备厂商
  104. DeviceModel = DeviceKind, //设备型号
  105. DeviceType = DeviceType, //设备类型
  106. ToDate = DateTime.Now, //入库时间
  107. ToRemark = InputNote, //入库备注
  108. BrandType = DeviceType, //品牌类型
  109. SourceStoreId = storefor.StoreId, //源仓库
  110. }).Entity;
  111. db.SaveChanges();
  112. string ChangeNo = "SC";
  113. int StoreStockChangeId = stockchange.Id;
  114. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  115. for (int j = 0; j < 18 - StoreStockChangeId.ToString().Length; j++)
  116. {
  117. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  118. }
  119. ChangeNo += StoreStockChangeIdString;
  120. stockchange.ChangeNo = ChangeNo; //交易流水编号
  121. db.StoreChangeHistory.Add(new StoreChangeHistory()
  122. {
  123. CreateDate = DateTime.Now,
  124. CreateMan = Operator,
  125. UserId = store.UserId, //创客
  126. BrandId = BrandId, //产品类型
  127. ChangeRecordNo = ChangeNo, //变更记录单号
  128. BizBatchNo = BatchNo, //业务批次号
  129. TransType = 0, //交易类型
  130. SnNo = SnNo, //SN编号
  131. StockOpDirect = 0, //库存操作方向
  132. DeviceVendor = DeviceName, //设备厂商
  133. DeviceType = DeviceType, //设备类型
  134. DeviceModel = DeviceKind, //设备型号
  135. ToUserId = store.UserId, //收货创客
  136. ToStoreId = store.Id, //创客退货收货仓库
  137. ToDate = DateTime.Now, //入库时间
  138. ToRemark = InputNote, //入库备注
  139. SourceStoreId = storefor.StoreId, //源仓库
  140. StoreId = storefor.StoreId, //仓库
  141. });
  142. machinefor = db.MachineForSnNo.Add(new MachineForSnNo()
  143. {
  144. SnNo = SnNo,
  145. SnId = pos.Id
  146. }).Entity;
  147. RedisDbconn.Instance.Set("MachineForSnNo:" + SnNo, machinefor);
  148. db.SaveChanges();
  149. RedisDbconn.Instance.AddList("PosMachineList:" + storefor.StoreId, pos);
  150. if (!storeData.ContainsKey(storefor.StoreId + "_" + BrandId))
  151. {
  152. storeData.Add(storefor.StoreId + "_" + BrandId, 1);
  153. }
  154. else
  155. {
  156. storeData[storefor.StoreId + "_" + BrandId] += 1;
  157. }
  158. SuccessCount += 1;
  159. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  160. }
  161. else
  162. {
  163. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "的品牌填写有误");
  164. }
  165. }
  166. else
  167. {
  168. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "已存在,请勿重复录入");
  169. }
  170. }
  171. DoCount += Size;
  172. foreach (string key in storeData.Keys)
  173. {
  174. string[] keylist = key.Split('_');
  175. int StoreId = int.Parse(keylist[0]);
  176. int BrandId = int.Parse(keylist[1]);
  177. int OpStoreNum = storeData[key];
  178. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  179. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  180. {
  181. CreateDate = DateTime.Now,
  182. CreateMan = Operator,
  183. StoreId = StoreId, //仓库
  184. TransType = 0, //交易类型
  185. BrandId = BrandId, //产品类型
  186. OpStoreNum = OpStoreNum, //操作库存数
  187. OpSymbol = "+", //操作符
  188. BeforeTotalNum = store.TotalNum, //操作前总库存数
  189. AfterTotalNum = store.TotalNum + OpStoreNum, //操作后总库存数
  190. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  191. AfterLaveNum = store.LaveNum + OpStoreNum, //操作后剩余库存数
  192. BeforeOutNum = store.OutNum, //操作前出库数
  193. AfterOutNum = store.OutNum, //操作后出库数
  194. }).Entity;
  195. db.SaveChanges();
  196. string ChangeNo = "SC";
  197. int StoreStockChangeId = balance.Id;
  198. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  199. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  200. {
  201. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  202. }
  203. ChangeNo += StoreStockChangeIdString;
  204. balance.TransRecordNo = ChangeNo; //交易流水编号
  205. store.TotalNum += OpStoreNum;
  206. store.LaveNum += OpStoreNum;
  207. db.SaveChanges();
  208. }
  209. db.SaveChanges();
  210. tran.Commit();
  211. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  212. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  213. }
  214. catch (Exception ex)
  215. {
  216. DoCount = list.Rows.Count;
  217. function.WriteLog(ex.ToString(), "机具导入异常");
  218. tran.Rollback();
  219. ErrorMsg msg = new ErrorMsg()
  220. {
  221. Time = DateTime.Now,
  222. ErrorContent = ex.ToString(),
  223. };
  224. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  225. }
  226. tran.Dispose();
  227. }
  228. else if (_Kind == "2")
  229. {
  230. Dictionary<string, int> storeData = new Dictionary<string, int>();
  231. Dictionary<string, int> toStoreData = new Dictionary<string, int>();
  232. using (var tran = db.Database.BeginTransaction())
  233. {
  234. try
  235. {
  236. int Size = 100;
  237. if (list.Rows.Count - DoCount < 100)
  238. {
  239. Size = list.Rows.Count - DoCount;
  240. }
  241. for (int i = DoCount; i < DoCount + Size; i++)
  242. {
  243. DataRow dr = list.Rows[i];
  244. string BatchNo = dr[0].ToString(); // 业务批次号
  245. string OutStoreNo = dr[1].ToString(); // 出货仓库编号
  246. string OutOperator = dr[2].ToString(); // 出库操作人
  247. string SnNo = dr[3].ToString(); // SN编号
  248. SnNo = SnNo.Replace(" ", "");
  249. string StoreNo = dr[4].ToString(); // 收货仓库编号
  250. string BrandIdString = dr[5].ToString(); // 产品类型
  251. string OutNote = "";
  252. if (list.Columns.Count > 6)
  253. {
  254. OutNote = dr[6].ToString(); // 出库备注
  255. }
  256. int BrandId = int.Parse(function.CheckInt(BrandIdString));
  257. //调拨时若出货仓库与填写仓库不符,则该条出货失败
  258. var posinfo = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == SnNo && m.BrandId == BrandId) ?? new PosMachinesTwo();
  259. var storeinfo = db.StoreForCode.FirstOrDefault(m => m.Code == OutStoreNo && m.StoreId == posinfo.StoreId) ?? new StoreForCode();
  260. if (posinfo.Id > 0)
  261. {
  262. if (storeinfo.StoreId > 0)
  263. {
  264. KqProducts kqProduct = db.KqProducts.FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  265. if (kqProduct.Id > 0)
  266. {
  267. StoreForCode storefor = db.StoreForCode.FirstOrDefault(m => m.Code == OutStoreNo) ?? new StoreForCode();
  268. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storefor.StoreId) ?? new StoreHouse();
  269. if (store.LaveNum > 0)
  270. {
  271. StoreForCode tostorefor = db.StoreForCode.FirstOrDefault(m => m.Code == StoreNo) ?? new StoreForCode();
  272. StoreHouse tostore = db.StoreHouse.FirstOrDefault(m => m.Id == tostorefor.StoreId) ?? new StoreHouse();
  273. if (tostore.Id > 0)
  274. {
  275. MachineForSnNo machinefor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo) ?? new MachineForSnNo();
  276. PosMachinesTwo machine = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machinefor.SnId && m.StoreId != tostore.Id && m.BuyUserId == 0 && m.UserId == 0) ?? new PosMachinesTwo();
  277. if (machine.Id > 0)
  278. {
  279. if (machine.PreUserId == 0)
  280. {
  281. StoreStockChange stockchange = db.StoreStockChange.Add(new StoreStockChange()
  282. {
  283. CreateDate = DateTime.Now,
  284. CreateMan = Operator,
  285. StoreId = storefor.StoreId, //仓库
  286. BrandId = BrandId, //产品类型
  287. ProductName = RelationClass.GetKqProductBrandInfo(BrandId), //产品名称
  288. BizBatchNo = BatchNo, //业务批次号
  289. TransType = 1, //交易类型
  290. SnNo = SnNo, //SN编号
  291. StockOpDirect = 1, //库存操作方向
  292. SnStatus = 1, //SN状态
  293. DeviceVendor = machine.DeviceName, //设备厂商
  294. DeviceModel = machine.DeviceKind, //设备型号
  295. DeviceType = machine.DeviceType, //设备类型
  296. FromUserId = store.UserId, //出货人
  297. FromDate = DateTime.Now, //出库时间
  298. FromRemark = OutNote, //出库备注
  299. ToUserId = tostore.UserId, //收货人
  300. ToStoreId = tostorefor.StoreId, //退货收货仓库
  301. SourceStoreId = machine.SourceStoreId, //源仓库编号
  302. BrandType = machine.DeviceType, //品牌类型
  303. }).Entity;
  304. db.SaveChanges();
  305. string ChangeNo = "SC";
  306. int StoreStockChangeId = stockchange.Id;
  307. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  308. for (int j = 0; j < 18 - StoreStockChangeId.ToString().Length; j++)
  309. {
  310. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  311. }
  312. ChangeNo += StoreStockChangeIdString;
  313. stockchange.ChangeNo = ChangeNo; //交易流水编号
  314. machine.StoreId = tostorefor.StoreId;
  315. db.StoreChangeHistory.Add(new StoreChangeHistory()
  316. {
  317. CreateDate = DateTime.Now,
  318. CreateMan = Operator,
  319. UserId = store.UserId, //创客
  320. ToUserId = tostore.UserId, //收货创客
  321. BrandId = BrandId, //产品类型
  322. ChangeRecordNo = ChangeNo, //变更记录单号
  323. BizBatchNo = BatchNo, //业务批次号
  324. TransType = 1, //交易类型
  325. SnNo = SnNo, //SN编号
  326. SnType = machine.PosSnType, //SN机具类型
  327. StockOpDirect = 1, //库存操作方向
  328. DeviceVendor = machine.DeviceName, //设备厂商
  329. DeviceModel = machine.DeviceKind, //设备型号
  330. DeviceType = machine.DeviceType, //设备类型
  331. FromUserId = store.UserId, //出货创客
  332. FromDate = DateTime.Now, //出库时间
  333. FromRemark = OutNote, //出库备注
  334. SourceStoreId = machine.SourceStoreId, //源仓库
  335. ToStoreId = tostore.Id, //收货仓库
  336. StoreId = store.Id, //仓库
  337. });
  338. // RemoveClass.DeletePosMachineList("PosMachineTwoList:" + storefor.StoreId, machine.Id);
  339. // RedisDbconn.Instance.AddList("PosMachineTwoList:" + tostorefor.StoreId, machine);
  340. if (!storeData.ContainsKey(storefor.StoreId + "_" + BrandId))
  341. {
  342. storeData.Add(storefor.StoreId + "_" + BrandId, 1);
  343. }
  344. else
  345. {
  346. storeData[storefor.StoreId + "_" + BrandId] += 1;
  347. }
  348. if (!toStoreData.ContainsKey(tostorefor.StoreId + "_" + BrandId))
  349. {
  350. toStoreData.Add(tostorefor.StoreId + "_" + BrandId, 1);
  351. }
  352. else
  353. {
  354. toStoreData[tostorefor.StoreId + "_" + BrandId] += 1;
  355. }
  356. SuccessCount += 1;
  357. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  358. }
  359. else
  360. {
  361. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "为预发机,不可调拨");
  362. }
  363. }
  364. else
  365. {
  366. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "不是仓库机或已经在填写的收货仓库中");
  367. }
  368. if (string.IsNullOrEmpty(tostore.ProductName))
  369. {
  370. tostore.BrandId = BrandId.ToString();
  371. tostore.ProductName = kqProduct.Name;
  372. }
  373. }
  374. else
  375. {
  376. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "填写的收货仓库编号不正确");
  377. }
  378. }
  379. else
  380. {
  381. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "所在的仓库" + store.StoreName + "(" + store.StoreNo + ")库存不足");
  382. }
  383. db.SaveChanges();
  384. }
  385. else
  386. {
  387. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "的品牌填写有误");
  388. }
  389. }
  390. else
  391. {
  392. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "出货仓库与填写仓库不符");
  393. }
  394. }
  395. else
  396. {
  397. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "的品牌填写有误");
  398. }
  399. }
  400. DoCount += Size;
  401. foreach (string key in storeData.Keys)
  402. {
  403. string[] keylist = key.Split('_');
  404. int StoreId = int.Parse(keylist[0]);
  405. int BrandId = int.Parse(keylist[1]);
  406. int OpStoreNum = storeData[key];
  407. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  408. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  409. {
  410. CreateDate = DateTime.Now,
  411. CreateMan = Operator,
  412. StoreId = StoreId, //仓库
  413. TransType = 1, //交易类型
  414. BrandId = BrandId, //产品类型
  415. OpStoreNum = OpStoreNum, //操作库存数
  416. OpSymbol = "-", //操作符
  417. BeforeTotalNum = store.TotalNum, //操作前总库存数
  418. AfterTotalNum = store.TotalNum, //操作后总库存数
  419. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  420. AfterLaveNum = store.LaveNum - OpStoreNum, //操作后剩余库存数
  421. BeforeOutNum = store.OutNum, //操作前出库数
  422. AfterOutNum = store.OutNum + OpStoreNum, //操作后出库数
  423. }).Entity;
  424. db.SaveChanges();
  425. string ChangeNo = "SC";
  426. int StoreStockChangeId = balance.Id;
  427. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  428. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  429. {
  430. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  431. }
  432. ChangeNo += StoreStockChangeIdString;
  433. balance.TransRecordNo = ChangeNo; //交易流水编号
  434. store.LaveNum -= OpStoreNum;
  435. store.OutNum += OpStoreNum;
  436. db.SaveChanges();
  437. string SendData = "{\"Kind\":\"5\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"BrandId\":\"" + BrandId + "\",\"OpStoreNum\":\"" + OpStoreNum + "\"}}";
  438. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  439. }
  440. foreach (string key in toStoreData.Keys)
  441. {
  442. string[] keylist = key.Split('_');
  443. int StoreId = int.Parse(keylist[0]);
  444. int BrandId = int.Parse(keylist[1]);
  445. int OpStoreNum = toStoreData[key];
  446. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  447. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  448. {
  449. CreateDate = DateTime.Now,
  450. CreateMan = Operator,
  451. StoreId = StoreId, //仓库
  452. TransType = 0, //交易类型
  453. BrandId = BrandId, //产品类型
  454. OpStoreNum = OpStoreNum, //操作库存数
  455. OpSymbol = "+", //操作符
  456. BeforeTotalNum = store.TotalNum, //操作前总库存数
  457. AfterTotalNum = store.TotalNum + OpStoreNum, //操作后总库存数
  458. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  459. AfterLaveNum = store.LaveNum + OpStoreNum, //操作后剩余库存数
  460. BeforeOutNum = store.OutNum, //操作前出库数
  461. AfterOutNum = store.OutNum, //操作后出库数
  462. }).Entity;
  463. db.SaveChanges();
  464. string ChangeNo = "SC";
  465. int StoreStockChangeId = balance.Id;
  466. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  467. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  468. {
  469. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  470. }
  471. ChangeNo += StoreStockChangeIdString;
  472. balance.TransRecordNo = ChangeNo; //交易流水编号
  473. store.TotalNum += OpStoreNum;
  474. store.LaveNum += OpStoreNum;
  475. db.SaveChanges();
  476. string SendData = "{\"Kind\":\"5\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"BrandId\":\"" + BrandId + "\",\"OpStoreNum\":\"-" + OpStoreNum + "\"}}";
  477. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  478. }
  479. db.SaveChanges();
  480. tran.Commit();
  481. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  482. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  483. }
  484. catch (Exception ex)
  485. {
  486. DoCount = list.Rows.Count;
  487. tran.Rollback();
  488. ErrorMsg msg = new ErrorMsg()
  489. {
  490. Time = DateTime.Now,
  491. ErrorContent = ex.ToString(),
  492. };
  493. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  494. }
  495. }
  496. }
  497. else if (_Kind == "3")
  498. {
  499. Dictionary<string, int> storeData = new Dictionary<string, int>();
  500. using (var tran = db.Database.BeginTransaction())
  501. {
  502. try
  503. {
  504. int Size = 100;
  505. if (list.Rows.Count - DoCount < 100)
  506. {
  507. Size = list.Rows.Count - DoCount;
  508. }
  509. for (int i = DoCount; i < DoCount + Size; i++)
  510. {
  511. DataRow dr = list.Rows[i];
  512. string BatchNo = dr[0].ToString(); // 业务批次号
  513. BatchNo = BatchNo.Replace(" ", "");
  514. string OutStoreNo = dr[1].ToString(); // 出货仓库编号
  515. OutStoreNo = OutStoreNo.Replace(" ", "");
  516. string OutOperator = dr[2].ToString(); // 出库操作人
  517. OutOperator = OutOperator.Replace(" ", "");
  518. string SnNo = dr[3].ToString(); // SN编号
  519. SnNo = SnNo.Replace(" ", "");
  520. string MakerCode = dr[4].ToString(); // 收货创客编号
  521. MakerCode = MakerCode.Replace(" ", "");
  522. string BrandIdString = dr[5].ToString(); // 产品类型(5-客小爽Mpos,6-客小爽大POS,61-客小爽电签POS,8-客小爽超级Mpos))
  523. BrandIdString = BrandIdString.Replace(" ", "");
  524. string OutNote = "";
  525. if (list.Columns.Count > 6)
  526. {
  527. OutNote = dr[6].ToString(); // 出库备注
  528. }
  529. int BrandId = int.Parse(function.CheckInt(BrandIdString));
  530. KqProducts kqProduct = db.KqProducts.FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  531. if (kqProduct.Id > 0)
  532. {
  533. StoreForCode storefor = db.StoreForCode.FirstOrDefault(m => m.Code == OutStoreNo) ?? new StoreForCode();
  534. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storefor.StoreId) ?? new StoreHouse();
  535. if (store.LaveNum > 0)
  536. {
  537. UserForMakerCode userfor = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  538. Users touser = db.Users.FirstOrDefault(m => m.Id == userfor.UserId && m.AuthFlag == 1) ?? new Users();
  539. if (touser.Id > 0)
  540. {
  541. MachineForSnNo machinefor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo) ?? new MachineForSnNo();
  542. PosMachinesTwo machine = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machinefor.SnId && m.BuyUserId == 0 && m.UserId == 0) ?? new PosMachinesTwo();
  543. if (machine.Id > 0)
  544. {
  545. if(machine.BrandId == BrandId)
  546. {
  547. StoreStockChange stockchange = db.StoreStockChange.Add(new StoreStockChange()
  548. {
  549. CreateDate = DateTime.Now,
  550. CreateMan = Operator,
  551. StoreId = storefor.StoreId, //仓库
  552. BrandId = BrandId, //产品类型
  553. ProductName = RelationClass.GetKqProductBrandInfo(BrandId), //产品名称
  554. BizBatchNo = BatchNo, //业务批次号
  555. TransType = 2, //交易类型
  556. SnNo = SnNo, //SN编号
  557. SnType = machine.PosSnType, //SN机具类型
  558. StockOpDirect = 1, //库存操作方向
  559. SnStatus = 1, //SN状态
  560. BindStatus = machine.BindingState, //绑定状态
  561. BindMerchant = machine.BindMerchantId, //绑定商户
  562. ActiveStatus = machine.ActivationState, //激活状态
  563. ActRewardUserId = machine.BuyUserId, //激活奖励人
  564. DeviceVendor = machine.DeviceName, //设备厂商
  565. DeviceModel = machine.DeviceKind, //设备型号
  566. DeviceType = machine.DeviceType, //设备类型
  567. FromUserId = store.UserId, //出货人
  568. FromDate = DateTime.Now, //出库时间
  569. FromRemark = OutNote, //出库备注
  570. ToUserId = userfor.UserId, //收货人
  571. SourceStoreId = machine.SourceStoreId, //源仓库编号
  572. BrandType = machine.DeviceType, //品牌类型
  573. }).Entity;
  574. db.SaveChanges();
  575. string ChangeNo = "SC";
  576. int StoreStockChangeId = stockchange.Id;
  577. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  578. for (int j = 0; j < 18 - StoreStockChangeId.ToString().Length; j++)
  579. {
  580. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  581. }
  582. ChangeNo += StoreStockChangeIdString;
  583. stockchange.ChangeNo = ChangeNo; //交易流水编号
  584. db.SaveChanges();
  585. UserStoreChange userstore = db.UserStoreChange.Add(new UserStoreChange()
  586. {
  587. CreateDate = DateTime.Now,
  588. CreateMan = Operator,
  589. UserId = store.UserId, //创客
  590. BrandId = BrandId, //产品类型
  591. ChangeRecordNo = ChangeNo, //变更记录单号
  592. TransType = 0, //交易类型
  593. SnNo = SnNo, //SN编号
  594. SnType = machine.PosSnType, //SN机具类型
  595. StockOpDirect = 0, //库存操作方向
  596. DeviceVendor = machine.DeviceName, //设备厂商
  597. DeviceType = machine.DeviceKind, //设备类型
  598. DeviceModel = machine.DeviceType, //设备型号
  599. ToUserId = userfor.UserId, //收货创客
  600. ToDate = DateTime.Now, //入库时间
  601. ToRemark = OutNote, //入库备注
  602. SourceStoreId = machine.SourceStoreId, //源仓库
  603. SnStatus = 1, //SN状态
  604. BizBatchNo = BatchNo, //业务批次号
  605. BindStatus = (int)machine.BindingState, //绑定状态
  606. BindMerchantId = machine.BindMerchantId, //绑定商户
  607. ActiveStatus = (int)machine.ActivationState, //激活状态
  608. ActRewardUserId = machine.BuyUserId, //激活奖励创客
  609. BrandType = machine.DeviceType, //品牌类型
  610. }).Entity;
  611. db.StoreChangeHistory.Add(new StoreChangeHistory()
  612. {
  613. CreateDate = DateTime.Now,
  614. CreateMan = Operator,
  615. UserId = store.UserId, //创客
  616. ToUserId = touser.Id, //收货创客
  617. BrandId = BrandId, //产品类型
  618. ChangeRecordNo = ChangeNo, //变更记录单号
  619. BizBatchNo = BatchNo, //业务批次号
  620. TransType = 2, //交易类型
  621. SnNo = SnNo, //SN编号
  622. SnType = machine.PosSnType, //SN机具类型
  623. StockOpDirect = 1, //库存操作方向
  624. DeviceVendor = machine.DeviceName, //设备厂商
  625. DeviceModel = machine.DeviceKind, //设备型号
  626. DeviceType = machine.DeviceType, //设备类型
  627. FromUserId = store.UserId, //出货创客
  628. FromDate = DateTime.Now, //出库时间
  629. FromRemark = OutNote, //出库备注
  630. SourceStoreId = machine.SourceStoreId, //源仓库
  631. StoreId = store.Id, //仓库
  632. });
  633. machine.BuyUserId = touser.Id;
  634. machine.UserId = touser.Id;
  635. machine.TransferTime = DateTime.Now;
  636. machine.RecycEndDate = DateTime.Now.AddDays(360); // 循环结束时间
  637. db.SaveChanges();
  638. string IdBrand = touser.Id + "_" + BrandId;
  639. UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  640. if (MachineData == null)
  641. {
  642. MachineData = db.UserMachineData.Add(new UserMachineData()
  643. {
  644. IdBrand = IdBrand,
  645. }).Entity;
  646. db.SaveChanges();
  647. }
  648. MachineData.IdBrand = IdBrand;
  649. MachineData.TotalMachineCount += 1;
  650. MachineData.UnBindCount += 1;
  651. db.SaveChanges();
  652. RemoveClass.DeletePosMachineList("PosMachineTwoList:" + storefor.StoreId, machine.Id);
  653. if (!storeData.ContainsKey(storefor.StoreId + "_" + BrandId))
  654. {
  655. storeData.Add(storefor.StoreId + "_" + BrandId, 1);
  656. }
  657. else
  658. {
  659. storeData[storefor.StoreId + "_" + BrandId] += 1;
  660. }
  661. SuccessCount += 1;
  662. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  663. }
  664. else
  665. {
  666. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "与表格里填写的品牌不一致");
  667. }
  668. }
  669. else
  670. {
  671. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "不是仓库机");
  672. }
  673. }
  674. else
  675. {
  676. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "填写的收货创客编号不正确或创客未认证");
  677. }
  678. }
  679. else
  680. {
  681. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "所在的仓库" + store.StoreName + "(" + store.StoreNo + ")库存不足");
  682. }
  683. }
  684. else
  685. {
  686. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "的品牌填写有误");
  687. }
  688. }
  689. DoCount += Size;
  690. foreach (string key in storeData.Keys)
  691. {
  692. string[] keylist = key.Split('_');
  693. int StoreId = int.Parse(keylist[0]);
  694. int BrandId = int.Parse(keylist[1]);
  695. int OpStoreNum = storeData[key];
  696. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  697. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  698. {
  699. CreateDate = DateTime.Now,
  700. CreateMan = Operator,
  701. StoreId = StoreId, //仓库
  702. TransType = 1, //交易类型
  703. BrandId = BrandId, //产品类型
  704. OpStoreNum = OpStoreNum, //操作库存数
  705. OpSymbol = "-", //操作符
  706. BeforeTotalNum = store.TotalNum, //操作前总库存数
  707. AfterTotalNum = store.TotalNum, //操作后总库存数
  708. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  709. AfterLaveNum = store.LaveNum - OpStoreNum, //操作后剩余库存数
  710. BeforeOutNum = store.OutNum, //操作前出库数
  711. AfterOutNum = store.OutNum + OpStoreNum, //操作后出库数
  712. }).Entity;
  713. db.SaveChanges();
  714. string ChangeNo = "SC";
  715. int StoreStockChangeId = balance.Id;
  716. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  717. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  718. {
  719. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  720. }
  721. ChangeNo += StoreStockChangeIdString;
  722. balance.TransRecordNo = ChangeNo; //交易流水编号
  723. store.LaveNum -= OpStoreNum;
  724. store.OutNum += OpStoreNum;
  725. db.SaveChanges();
  726. }
  727. db.SaveChanges();
  728. tran.Commit();
  729. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  730. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  731. }
  732. catch (Exception ex)
  733. {
  734. DoCount = list.Rows.Count;
  735. tran.Rollback();
  736. ErrorMsg msg = new ErrorMsg()
  737. {
  738. Time = DateTime.Now,
  739. ErrorContent = ex.ToString(),
  740. };
  741. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  742. }
  743. }
  744. }
  745. else if (_Kind == "4")
  746. {
  747. Dictionary<string, int> storeData = new Dictionary<string, int>();
  748. Dictionary<string, int> toStoreData = new Dictionary<string, int>();
  749. using (var tran = db.Database.BeginTransaction())
  750. {
  751. try
  752. {
  753. int Size = 100;
  754. if (list.Rows.Count - DoCount < 100)
  755. {
  756. Size = list.Rows.Count - DoCount;
  757. }
  758. List<string> opData = new List<string>();
  759. for (int i = DoCount; i < DoCount + Size; i++)
  760. {
  761. DataRow dr = list.Rows[i];
  762. string BatchNo = dr[0].ToString(); // 业务批次号
  763. string OldStoreNo = dr[1].ToString(); // 原仓库编号
  764. string OldOperator = dr[2].ToString(); // 操作人
  765. string SnNo = dr[3].ToString(); // SN编号
  766. SnNo = SnNo.Replace(" ", "");
  767. string StoreNo = dr[4].ToString(); // 退回仓库编号
  768. string BrandIdString = dr[5].ToString(); // 产品类型
  769. string OutNote = "";
  770. if (list.Columns.Count > 6)
  771. {
  772. OutNote = dr[6].ToString(); // 出库备注
  773. }
  774. int BrandId = int.Parse(function.CheckInt(BrandIdString));
  775. decimal amount = 0;
  776. KqProducts kqProduct = db.KqProducts.FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  777. if (kqProduct.Name.Contains("电签"))
  778. {
  779. amount = 200;
  780. }
  781. if (kqProduct.Name.Contains("大POS"))
  782. {
  783. amount = 300;
  784. }
  785. if (kqProduct.Id > 0)
  786. {
  787. StoreForCode storefor = db.StoreForCode.FirstOrDefault(m => m.Code == OldStoreNo) ?? new StoreForCode();
  788. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storefor.StoreId) ?? new StoreHouse();
  789. if (store.Id > 0)
  790. {
  791. StoreForCode tostorefor = db.StoreForCode.FirstOrDefault(m => m.Code == StoreNo) ?? new StoreForCode();
  792. StoreHouse tostore = db.StoreHouse.FirstOrDefault(m => m.Id == tostorefor.StoreId && m.Status > -1) ?? new StoreHouse();
  793. if (tostore.Id > 0)
  794. {
  795. MachineForSnNo machinefor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo) ?? new MachineForSnNo();
  796. PosMachinesTwo machine = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machinefor.SnId && m.BuyUserId > 0) ?? new PosMachinesTwo();
  797. if (machine.Id > 0)
  798. {
  799. function.WriteLog(DateTime.Now.ToString() + "\n" + Newtonsoft.Json.JsonConvert.SerializeObject(machine), "机具回仓库日志");
  800. if(!opData.Contains(machine.BuyUserId + ":" + machine.BrandId))
  801. {
  802. opData.Add(machine.BuyUserId + ":" + machine.BrandId);
  803. }
  804. machine.StoreId = tostorefor.StoreId;
  805. machine.BuyUserId = 0;
  806. machine.UserId = 0;
  807. machine.BindingState = 0;
  808. machine.ActivationState = 0;
  809. machine.BindingTime = null;
  810. machine.ActivationTime = null;
  811. machine.OrderId = 0;
  812. machine.RecycEndDate = null;
  813. machine.PosSnType = 0;
  814. machine.BindMerchantId = 0;
  815. machine.ScanQrTrade = 0;
  816. machine.DebitCardTrade = 0;
  817. machine.CreditTrade = 0;
  818. machine.UserNav = null;
  819. machine.IsPurchase = 0;
  820. machine.LeaderUserId = 0;
  821. machine.PreUserId = 0;
  822. PublicFunction.ClearPosHistory(db, SnNo); //清除机具历史记录
  823. db.SaveChanges();
  824. if (!storeData.ContainsKey(storefor.StoreId + "_" + BrandId))
  825. {
  826. storeData.Add(storefor.StoreId + "_" + BrandId, 1);
  827. }
  828. else
  829. {
  830. storeData[storefor.StoreId + "_" + BrandId] += 1;
  831. }
  832. if (!toStoreData.ContainsKey(tostorefor.StoreId + "_" + BrandId))
  833. {
  834. toStoreData.Add(tostorefor.StoreId + "_" + BrandId, 1);
  835. }
  836. else
  837. {
  838. toStoreData[tostorefor.StoreId + "_" + BrandId] += 1;
  839. }
  840. SuccessCount += 1;
  841. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  842. }
  843. else
  844. {
  845. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "已在仓库中");
  846. }
  847. }
  848. else
  849. {
  850. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "填写的退回仓库编号不正确");
  851. }
  852. }
  853. else
  854. {
  855. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "填写的原仓库编号不正确");
  856. }
  857. db.SaveChanges();
  858. }
  859. else
  860. {
  861. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + SnNo + "的品牌填写有误");
  862. }
  863. }
  864. DoCount += Size;
  865. foreach (string key in storeData.Keys)
  866. {
  867. string[] keylist = key.Split('_');
  868. int StoreId = int.Parse(keylist[0]);
  869. int BrandId = int.Parse(keylist[1]);
  870. int OpStoreNum = storeData[key];
  871. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  872. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  873. {
  874. CreateDate = DateTime.Now,
  875. CreateMan = Operator,
  876. StoreId = StoreId, //仓库
  877. TransType = 1, //交易类型
  878. BrandId = BrandId, //产品类型
  879. OpStoreNum = OpStoreNum, //操作库存数
  880. OpSymbol = "-", //操作符
  881. BeforeTotalNum = store.TotalNum, //操作前总库存数
  882. AfterTotalNum = store.TotalNum, //操作后总库存数
  883. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  884. AfterLaveNum = store.LaveNum - OpStoreNum, //操作后剩余库存数
  885. BeforeOutNum = store.OutNum, //操作前出库数
  886. AfterOutNum = store.OutNum + OpStoreNum, //操作后出库数
  887. }).Entity;
  888. db.SaveChanges();
  889. string ChangeNo = "JJBH";
  890. int StoreStockChangeId = balance.Id;
  891. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  892. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  893. {
  894. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  895. }
  896. ChangeNo += StoreStockChangeIdString;
  897. balance.TransRecordNo = ChangeNo; //交易流水编号
  898. store.LaveNum -= OpStoreNum;
  899. db.SaveChanges();
  900. }
  901. foreach (string key in toStoreData.Keys)
  902. {
  903. string[] keylist = key.Split('_');
  904. int StoreId = int.Parse(keylist[0]);
  905. int BrandId = int.Parse(keylist[1]);
  906. int OpStoreNum = toStoreData[key];
  907. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  908. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  909. {
  910. CreateDate = DateTime.Now,
  911. CreateMan = Operator,
  912. StoreId = StoreId, //仓库
  913. TransType = 0, //交易类型
  914. BrandId = BrandId, //产品类型
  915. OpStoreNum = OpStoreNum, //操作库存数
  916. OpSymbol = "+", //操作符
  917. BeforeTotalNum = store.TotalNum, //操作前总库存数
  918. AfterTotalNum = store.TotalNum + OpStoreNum, //操作后总库存数
  919. BeforeLaveNum = store.LaveNum, //操作前剩余库存数
  920. AfterLaveNum = store.LaveNum + OpStoreNum, //操作后剩余库存数
  921. BeforeOutNum = store.OutNum, //操作前出库数
  922. AfterOutNum = store.OutNum, //操作后出库数
  923. }).Entity;
  924. db.SaveChanges();
  925. string ChangeNo = "SC";
  926. int StoreStockChangeId = balance.Id;
  927. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  928. for (int i = 0; i < 18 - StoreStockChangeId.ToString().Length; i++)
  929. {
  930. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  931. }
  932. ChangeNo += StoreStockChangeIdString;
  933. balance.TransRecordNo = ChangeNo; //交易流水编号
  934. store.LaveNum += OpStoreNum;
  935. db.SaveChanges();
  936. }
  937. db.SaveChanges();
  938. foreach(string sub in opData)
  939. {
  940. string[] datalist = sub.Split(":");
  941. PublicFunction.SycnMachineCount(int.Parse(datalist[0]), int.Parse(datalist[1]));
  942. }
  943. tran.Commit();
  944. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  945. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  946. }
  947. catch (Exception ex)
  948. {
  949. DoCount = list.Rows.Count;
  950. tran.Rollback();
  951. ErrorMsg msg = new ErrorMsg()
  952. {
  953. Time = DateTime.Now,
  954. ErrorContent = ex.ToString(),
  955. };
  956. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  957. }
  958. }
  959. }
  960. else if (_Kind == "UserCashRecord")
  961. {
  962. using (var tran = db.Database.BeginTransaction())
  963. {
  964. try
  965. {
  966. int Size = 100;
  967. if (list.Rows.Count - DoCount < 100)
  968. {
  969. Size = list.Rows.Count - DoCount;
  970. }
  971. for (int i = DoCount; i < DoCount + Size; i++)
  972. {
  973. DataRow dr = list.Rows[i];
  974. string CashOrderNo = dr[2].ToString(); // 提现单号
  975. decimal Amount = decimal.Parse(function.CheckNum(dr[3].ToString())); // 发佣金额
  976. int Status = int.Parse(function.CheckInt(dr[4].ToString())); // 提现状态
  977. string Remark = dr[5].ToString(); // 备注
  978. CashOrderNo = CashOrderNo.Replace(" ", "");
  979. UserCashRecord edit = db.UserCashRecord.FirstOrDefault(m => m.CashOrderNo == CashOrderNo && m.ActualTradeAmount == Amount && m.Status == 0);
  980. if (edit != null)
  981. {
  982. if (edit.TradeType == 3)
  983. {
  984. edit.Status = Status;
  985. edit.Remark = Remark;
  986. edit.UpdateMan = Operator;
  987. if (Status == 1)
  988. {
  989. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  990. if (account != null)
  991. {
  992. decimal TradeAmount = edit.TradeAmount;
  993. db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  994. {
  995. CreateDate = DateTime.Now,
  996. UserId = edit.UserId, //创客
  997. ChangeType = 3, //变动类型
  998. ProductType = 99, //产品类型
  999. ChangeAmount = TradeAmount, //变更金额
  1000. BeforeBalanceAmount = account.LeaderBalanceAmount, //变更前余额
  1001. AfterBalanceAmount = account.LeaderBalanceAmount, //变更后余额
  1002. });
  1003. db.SaveChanges();
  1004. RedisDbconn.Instance.Set("UserAccount:" + edit.UserId, account);
  1005. }
  1006. }
  1007. else if (Status == 2)
  1008. {
  1009. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  1010. if (account != null)
  1011. {
  1012. decimal TradeAmount = edit.TradeAmount;
  1013. account.LeaderBalanceAmount += TradeAmount;
  1014. db.LeaderAccountRecord.Add(new LeaderAccountRecord()
  1015. {
  1016. CreateDate = DateTime.Now,
  1017. UserId = edit.UserId, //创客
  1018. ChangeType = 6, //变动类型
  1019. ProductType = 99, //产品类型
  1020. ChangeAmount = TradeAmount, //变更金额
  1021. BeforeBalanceAmount = account.LeaderBalanceAmount - TradeAmount, //变更前余额
  1022. AfterBalanceAmount = account.LeaderBalanceAmount, //变更后余额
  1023. });
  1024. db.SaveChanges();
  1025. RedisDbconn.Instance.Set("UserAccount:" + edit.UserId, account);
  1026. }
  1027. }
  1028. db.SaveChanges();
  1029. RedisDbconn.Instance.Clear("UserCashRecord:" + edit.Id);
  1030. SuccessCount += 1;
  1031. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  1032. }
  1033. else if (edit.TradeType == 4)
  1034. {
  1035. edit.Status = Status;
  1036. edit.Remark = Remark;
  1037. edit.UpdateMan = Operator;
  1038. if (Status == 1)
  1039. {
  1040. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  1041. if (account != null)
  1042. {
  1043. decimal TradeAmount = edit.TradeAmount;
  1044. decimal ActualTradeAmount = edit.ActualTradeAmount;
  1045. account.HelpProfitBalanceAmount -= TradeAmount;
  1046. db.HelpProfitAccountRecord.Add(new HelpProfitAccountRecord()
  1047. {
  1048. CreateDate = DateTime.Now,
  1049. UserId = edit.UserId, //创客
  1050. ChangeType = 3, //变动类型
  1051. ProductType = 99, //产品类型
  1052. ChangeAmount = ActualTradeAmount, //变更金额
  1053. BeforeBalanceAmount = account.HelpProfitBalanceAmount + TradeAmount, //变更前余额
  1054. AfterBalanceAmount = account.HelpProfitBalanceAmount, //变更后余额
  1055. });
  1056. db.HelpProfitAccountRecord.Add(new HelpProfitAccountRecord()
  1057. {
  1058. CreateDate = DateTime.Now,
  1059. UpdateDate = DateTime.Now,
  1060. UserId = edit.UserId, //创客
  1061. ChangeType = 4, //变动类型
  1062. ProductType = 99, //产品类型
  1063. ChangeAmount = edit.TradeFee, //变更金额
  1064. BeforeBalanceAmount = account.HelpProfitBalanceAmount + TradeAmount, //变更前余额
  1065. AfterBalanceAmount = account.HelpProfitBalanceAmount, //变更后余额
  1066. });
  1067. db.HelpProfitAccountRecord.Add(new HelpProfitAccountRecord()
  1068. {
  1069. CreateDate = DateTime.Now,
  1070. UpdateDate = DateTime.Now,
  1071. UserId = edit.UserId, //创客
  1072. ChangeType = 5, //变动类型
  1073. ProductType = 99, //产品类型
  1074. ChangeAmount = edit.ManageFee, //变更金额
  1075. BeforeBalanceAmount = account.HelpProfitBalanceAmount + TradeAmount, //变更前余额
  1076. AfterBalanceAmount = account.HelpProfitBalanceAmount, //变更后余额
  1077. });
  1078. db.SaveChanges();
  1079. RedisDbconn.Instance.Set("HelpProfitAccount:" + edit.UserId, account);
  1080. }
  1081. }
  1082. else if (Status == 2)
  1083. {
  1084. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  1085. if (account != null)
  1086. {
  1087. decimal TradeAmount = edit.TradeAmount;
  1088. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1089. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1090. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1091. account.BalanceAmount += TradeAmount;
  1092. account.FreezeAmount -= TradeAmount;
  1093. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1094. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1095. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1096. db.UserAccountRecord.Add(new UserAccountRecord()
  1097. {
  1098. CreateDate = DateTime.Now,
  1099. UpdateDate = DateTime.Now,
  1100. UserId = edit.UserId, //创客
  1101. ChangeType = 6, //变动类型
  1102. ProductType = 99, //产品类型
  1103. ChangeAmount = TradeAmount, //变更金额
  1104. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1105. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1106. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1107. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1108. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1109. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1110. });
  1111. db.SaveChanges();
  1112. RedisDbconn.Instance.Set("UserAccount:" + edit.UserId, account);
  1113. }
  1114. }
  1115. db.SaveChanges();
  1116. RedisDbconn.Instance.Clear("UserCashRecord:" + edit.Id);
  1117. SuccessCount += 1;
  1118. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  1119. }
  1120. else
  1121. {
  1122. edit.Status = Status;
  1123. edit.Remark = Remark;
  1124. edit.UpdateMan = Operator;
  1125. if (Status == 1)
  1126. {
  1127. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  1128. if (account != null)
  1129. {
  1130. account.QueryCount = 1;
  1131. decimal TradeAmount = edit.TradeAmount;
  1132. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1133. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1134. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1135. account.WithdrawAmount += edit.TradeAmount;
  1136. account.FreezeAmount -= edit.TradeAmount;
  1137. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1138. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1139. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1140. db.UserAccountRecord.Add(new UserAccountRecord()
  1141. {
  1142. CreateDate = DateTime.Now,
  1143. UpdateDate = DateTime.Now,
  1144. UserId = edit.UserId, //创客
  1145. ChangeType = 3, //变动类型
  1146. ProductType = 99, //产品类型
  1147. ChangeAmount = edit.ActualTradeAmount, //变更金额
  1148. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1149. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1150. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1151. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1152. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1153. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1154. });
  1155. db.UserAccountRecord.Add(new UserAccountRecord()
  1156. {
  1157. CreateDate = DateTime.Now,
  1158. UpdateDate = DateTime.Now,
  1159. UserId = edit.UserId, //创客
  1160. ChangeType = 4, //变动类型
  1161. ProductType = 99, //产品类型
  1162. ChangeAmount = edit.TradeFee, //变更金额
  1163. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1164. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1165. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1166. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1167. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1168. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1169. });
  1170. db.UserAccountRecord.Add(new UserAccountRecord()
  1171. {
  1172. CreateDate = DateTime.Now,
  1173. UpdateDate = DateTime.Now,
  1174. UserId = edit.UserId, //创客
  1175. ChangeType = 5, //变动类型
  1176. ProductType = 99, //产品类型
  1177. ChangeAmount = edit.ManageFee, //变更金额
  1178. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1179. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1180. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1181. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1182. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1183. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1184. });
  1185. db.SaveChanges();
  1186. RedisDbconn.Instance.Set("UserAccount:" + edit.UserId, account);
  1187. }
  1188. }
  1189. else if (Status == 2)
  1190. {
  1191. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == edit.UserId);
  1192. if (account != null)
  1193. {
  1194. decimal TradeAmount = edit.TradeAmount;
  1195. if(account.FreezeAmount < TradeAmount)
  1196. {
  1197. RedisDbconn.Instance.AddList("ErrList" + checkKey, "提现记录单号为" + CashOrderNo + "的创客冻结金额异常");
  1198. }
  1199. else
  1200. {
  1201. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  1202. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  1203. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  1204. account.BalanceAmount += TradeAmount;
  1205. account.FreezeAmount -= TradeAmount;
  1206. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  1207. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  1208. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  1209. db.UserAccountRecord.Add(new UserAccountRecord()
  1210. {
  1211. CreateDate = DateTime.Now,
  1212. UpdateDate = DateTime.Now,
  1213. UserId = edit.UserId, //创客
  1214. ChangeType = 6, //变动类型
  1215. ProductType = 99, //产品类型
  1216. ChangeAmount = TradeAmount, //变更金额
  1217. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  1218. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  1219. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  1220. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  1221. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  1222. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  1223. });
  1224. db.SaveChanges();
  1225. RedisDbconn.Instance.Set("UserAccount:" + edit.UserId, account);
  1226. }
  1227. }
  1228. }
  1229. db.SaveChanges();
  1230. RedisDbconn.Instance.Clear("UserCashRecord:" + edit.Id);
  1231. SuccessCount += 1;
  1232. RedisDbconn.Instance.Set("CheckImport:" + checkKey, SuccessCount + " / " + TotalCount);
  1233. }
  1234. }
  1235. else
  1236. {
  1237. RedisDbconn.Instance.AddList("ErrList" + checkKey, "提现记录单号为" + CashOrderNo + "不存在或者金额不符");
  1238. }
  1239. }
  1240. DoCount += Size;
  1241. db.SaveChanges();
  1242. tran.Commit();
  1243. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  1244. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  1245. }
  1246. catch (Exception ex)
  1247. {
  1248. DoCount = list.Rows.Count;
  1249. tran.Rollback();
  1250. ErrorMsg msg = new ErrorMsg()
  1251. {
  1252. Time = DateTime.Now,
  1253. ErrorContent = ex.ToString(),
  1254. };
  1255. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  1256. }
  1257. }
  1258. }
  1259. else if (_Kind == "ChangeFee")
  1260. {
  1261. using (var tran = db.Database.BeginTransaction())
  1262. {
  1263. try
  1264. {
  1265. int Size = 100;
  1266. if (list.Rows.Count - DoCount < 100)
  1267. {
  1268. Size = list.Rows.Count - DoCount;
  1269. }
  1270. string posIds = "";
  1271. string userIds = "";
  1272. string warning = "";
  1273. for (int i = DoCount; i < DoCount + Size; i++)
  1274. {
  1275. DataRow dr = list.Rows[i];
  1276. string PosNo = dr[0].ToString(); // 机具Sn
  1277. decimal Amount = decimal.Parse(function.CheckNum(dr[1].ToString())); // 调整费率
  1278. int Kind = int.Parse(function.CheckInt(dr[2].ToString())); // 1 调升 2 调低
  1279. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosNo) ?? new PosMachinesTwo();
  1280. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 6)
  1281. {
  1282. warning += PosNo + ",";
  1283. }
  1284. }
  1285. if (!string.IsNullOrEmpty(warning))
  1286. {
  1287. RedisDbconn.Instance.AddList("ErrList" + checkKey, "机具" + warning + "该机具应直接设置,请全部重新导入");
  1288. }
  1289. else
  1290. {
  1291. for (int i = DoCount; i < DoCount + Size; i++)
  1292. {
  1293. DataRow dr = list.Rows[i];
  1294. string PosNo = dr[0].ToString(); // 机具Sn
  1295. decimal Amount = decimal.Parse(function.CheckNum(dr[1].ToString())); // 调整费率
  1296. int Kind = int.Parse(function.CheckInt(dr[2].ToString())); // 1 调升 2 调低
  1297. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosNo) ?? new PosMachinesTwo();
  1298. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 6)
  1299. {
  1300. warning += PosNo + ",";
  1301. }
  1302. if (Kind == 1)
  1303. {
  1304. pos.UpFeeFlag = 1;
  1305. pos.UpFeeDate = DateTime.Now;
  1306. pos.UpFeeMan = Operator;
  1307. db.SaveChanges();
  1308. }
  1309. if (Kind == 2)
  1310. {
  1311. pos.DownFeeFlag = 1;
  1312. pos.DownFeeDate = DateTime.Now;
  1313. pos.DownFeeMan = Operator;
  1314. db.SaveChanges();
  1315. }
  1316. if (!userIds.Contains(pos.BuyUserId.ToString()))
  1317. {
  1318. userIds += pos.BuyUserId + ",";
  1319. }
  1320. posIds += pos.Id + ",";
  1321. }
  1322. string[] lists = userIds.Split(new char[] { ',' });
  1323. int times = lists.ToList().Count;
  1324. for (int i = 0; i < times; i++)
  1325. {
  1326. var UserId = lists[i];
  1327. var posInfo = db.PosMachinesTwo.Where(m => posIds.Contains(m.Id.ToString()) && m.BuyUserId == Convert.ToInt32(UserId));
  1328. string snhtml = "<div style='margin-bottom: .48rem;'>";
  1329. foreach (var items in posInfo)
  1330. {
  1331. var mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == items.BindMerchantId) ?? new PosMerchantInfo();
  1332. var brand = db.KqProducts.FirstOrDefault(m => m.Id == items.BrandId) ?? new KqProducts();
  1333. snhtml += "<div style='margin-bottom: .48rem;'><div class='f12'>商户姓名:" + mer.MerRealName + "</div>";
  1334. snhtml += "<div class='f12'>机具品牌:" + brand.Name + "</div>";
  1335. snhtml += "<div class='f12'>SN:" + items.PosSn + "</div>";
  1336. snhtml += "<div class='f12'>当前费率:0.6%</div>";
  1337. snhtml += "<div class='f12'>费率调整时间:" + items.DownFeeDate + "</div>";
  1338. }
  1339. snhtml += "</div>";
  1340. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1341. {
  1342. UserId = Convert.ToInt32(UserId), //创客
  1343. Title = "商户费率变更通知", //标题
  1344. Content = "<div class='f12' style='margin-bottom: .72rem'>您的商户刷卡交易费率已变更成功!</div>" + snhtml, //内容
  1345. Summary = "您的商户刷卡交易费率已变更成功!",
  1346. CreateDate = DateTime.Now,
  1347. }));
  1348. }
  1349. RedisDbconn.Instance.Set("CheckImport:" + checkKey, "success|" + SuccessCount);
  1350. RedisDbconn.Instance.SetExpire("CheckImport:" + checkKey, 60000);
  1351. }
  1352. }
  1353. catch (Exception ex)
  1354. {
  1355. DoCount = list.Rows.Count;
  1356. tran.Rollback();
  1357. ErrorMsg msg = new ErrorMsg()
  1358. {
  1359. Time = DateTime.Now,
  1360. ErrorContent = ex.ToString(),
  1361. };
  1362. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入Excel文件异常");
  1363. }
  1364. }
  1365. }
  1366. db.Dispose();
  1367. }
  1368. }
  1369. else
  1370. {
  1371. Thread.Sleep(5000);
  1372. }
  1373. // return "success|" + SuccessCount;
  1374. }
  1375. }
  1376. }
  1377. }