StoreStockChangeController.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using Microsoft.AspNetCore.Authorization;
  10. using System.Web;
  11. using MySystem.MainModels;
  12. using LitJson;
  13. using Library;
  14. namespace MySystem.Areas.Api.Controllers.v1
  15. {
  16. [Area("Api")]
  17. [Route("Api/v1/[controller]/[action]")]
  18. public class StoreStockChangeController : BaseController
  19. {
  20. public StoreStockChangeController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  21. {
  22. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  23. }
  24. #region 首页-仓库管理-整箱划拨
  25. [Authorize]
  26. public JsonResult TransferWhole(string value)
  27. {
  28. value = DesDecrypt(value);
  29. JsonData data = JsonMapper.ToObject(value);
  30. AppResultJson result = TransferWholeDo(value);
  31. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  32. }
  33. public AppResultJson TransferWholeDo(string value)
  34. {
  35. JsonData data = JsonMapper.ToObject(value);
  36. int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString()));
  37. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  38. int ToUserId = int.Parse(function.CheckInt(data["ToUserId"].ToString())); //收货人
  39. string SnNo = data["SnNo"].ToString(); //首台SN编号
  40. Dictionary<string, object> Obj = new Dictionary<string, object>();
  41. if (string.IsNullOrEmpty(SnNo))
  42. {
  43. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  44. }
  45. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  46. string[] SnIdList = SnNo.Split(',');
  47. string SnNos = ""; //划拨的机具号(多个)
  48. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == OrderId);
  49. if (order != null)
  50. {
  51. if (order.Status == 2)
  52. {
  53. return new AppResultJson() { Status = "-1", Info = "该订单已处理,请勿重复操作" };
  54. }
  55. int ApplyType = 0; // 申请类型,1-机具SN,2-200兑换码,3-300券
  56. List<string> SourceSnNos = new List<string>();
  57. if (order.Sort > 0)
  58. {
  59. MachineApply Apply = maindb.MachineApply.FirstOrDefault(m => m.Id == order.Sort);
  60. if (Apply != null)
  61. {
  62. Apply.Status = 1;
  63. Apply.QueryCount = order.Id;
  64. ApplyType = Apply.Sort;
  65. if (!string.IsNullOrEmpty(Apply.SwapSnExpand))
  66. {
  67. string[] SwapSnExpands = Apply.SwapSnExpand.Split('\n');
  68. foreach (string sub in SwapSnExpands)
  69. {
  70. if (!string.IsNullOrEmpty(sub))
  71. {
  72. SourceSnNos.Add(sub.Split(':')[0]);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. Dictionary<int, string> couponIds = new Dictionary<int, string>();
  79. int SnIndex = 0;
  80. foreach (string SnId in SnIdList)
  81. {
  82. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  83. int SnIdNum = int.Parse(SnId);
  84. int LeaderUserId = 0;
  85. // int OpId = 0;
  86. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  87. if (SourceSnNos.Count > SnIndex)
  88. {
  89. string SourceSnNo = SourceSnNos[SnIndex];
  90. if (ApplyType <= 1)
  91. {
  92. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSnNo) ?? new MachineForSnNo();
  93. PosMachinesTwo spos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  94. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  95. LeaderUserId = spos.LeaderUserId;
  96. // OpId = spos.OpId;
  97. }
  98. else if (ApplyType > 1)
  99. {
  100. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.ExchangeCode == SourceSnNo) ?? new PosCoupons();
  101. LeaderUserId = coupon.LeaderUserId;
  102. if (coupon.OpId > 0)
  103. {
  104. if (couponIds.ContainsKey(coupon.OpId))
  105. {
  106. string[] datas = couponIds[coupon.OpId].Split(',');
  107. int Num = int.Parse(datas[2]) + 1;
  108. couponIds[coupon.OpId] = datas[0] + "," + datas[1] + "," + Num;
  109. }
  110. else
  111. {
  112. couponIds.Add(coupon.OpId, coupon.Id + "," + coupon.QueryCount + ",1");
  113. }
  114. // OpId = coupon.OpId;
  115. }
  116. }
  117. }
  118. PosMachinesTwo pos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == SnIdNum) ?? new PosMachinesTwo();
  119. //扣减分仓所关联的运营中心总机具数
  120. if (pos.OpId > 0)
  121. {
  122. var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.QueryCount == 1 && m.UserId == pos.OpId) ?? new OpModels.SysAdmin();
  123. if (sysAdmin.Id > 0)
  124. {
  125. sysAdmin.TotalMachineCount -= 1;
  126. opdb.SaveChanges();
  127. }
  128. }
  129. SnNos += pos.PosSn + ",";
  130. Users toUser = UsersDbconn.Instance.Get(ToUserId) ?? new Users();
  131. StoreStockChange query = maindb.StoreStockChange.Add(new StoreStockChange()
  132. {
  133. CreateDate = DateTime.Now,
  134. StoreId = StoreId, //仓库
  135. BrandId = pos.BrandId, //产品类型
  136. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  137. ChangeNo = ChangeNo, //变更单号
  138. TransType = 10, //交易类型
  139. SnNo = pos.PosSn, //SN编号
  140. SnType = pos.PosSnType, //SN机具类型
  141. StockOpDirect = 1, //库存操作方向
  142. DeviceType = pos.DeviceType, //设备类型
  143. FromUserId = store.UserId, //出货人
  144. FromDate = DateTime.Now, //出库时间
  145. ToUserId = ToUserId, //收货人
  146. ToStoreId = StoreId, //退货收货仓库
  147. OpId = pos.OpId
  148. }).Entity;
  149. UserStoreChange userstore = maindb.UserStoreChange.Add(new UserStoreChange()
  150. {
  151. CreateDate = DateTime.Now,
  152. UserId = store.UserId, //创客
  153. BrandId = pos.BrandId, //产品类型
  154. ChangeRecordNo = ChangeNo, //变更记录单号
  155. TransType = 0, //交易类型
  156. SnNo = pos.PosSn, //SN编号
  157. SnType = pos.PosSnType, //SN机具类型
  158. StockOpDirect = 0, //库存操作方向
  159. DeviceVendor = pos.DeviceName, //设备厂商
  160. DeviceType = pos.DeviceKind, //设备类型
  161. DeviceModel = pos.DeviceType, //设备型号
  162. ToUserId = ToUserId, //收货创客
  163. ToDate = DateTime.Now, //入库时间
  164. SourceStoreId = pos.SourceStoreId, //源仓库
  165. SnStatus = 1, //SN状态
  166. BindStatus = (int)pos.BindingState, //绑定状态
  167. BindMerchantId = pos.BindMerchantId, //绑定商户
  168. ActiveStatus = (int)pos.ActivationState, //激活状态
  169. ActRewardUserId = pos.BuyUserId, //激活奖励创客
  170. BrandType = pos.DeviceType, //品牌类型
  171. }).Entity;
  172. StoreChangeHistory history = maindb.StoreChangeHistory.Add(new StoreChangeHistory()
  173. {
  174. CreateDate = DateTime.Now,
  175. UserId = store.UserId, //创客
  176. BrandId = pos.BrandId, //产品类型
  177. ChangeRecordNo = ChangeNo, //变更记录单号
  178. TransType = 2, //交易类型
  179. SnNo = pos.PosSn, //SN编号
  180. SnType = pos.PosSnType, //SN机具类型
  181. StockOpDirect = 1, //库存操作方向
  182. DeviceVendor = pos.DeviceName, //设备厂商
  183. DeviceModel = pos.DeviceKind, //设备型号
  184. DeviceType = pos.DeviceType, //设备类型
  185. ToUserId = ToUserId, //收货创客
  186. FromUserId = store.UserId, //出货创客
  187. FromDate = DateTime.Now, //出库时间
  188. SourceStoreId = pos.SourceStoreId, //源仓库
  189. StoreId = store.Id, //仓库
  190. OpId = pos.OpId
  191. }).Entity;
  192. maindb.SaveChanges();
  193. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  194. store.LaveNum -= 1;
  195. store.OutNum += 1;
  196. pos.OrderId = OrderId;
  197. pos.BuyUserId = ToUserId;
  198. pos.UserId = ToUserId;
  199. pos.TransferTime = DateTime.Now;
  200. pos.RecycEndDate = RecycEndDate; // 循环结束时间
  201. pos.PosSnType = order.QueryCount; //机具类型,0-兑换机,1-循环机
  202. pos.LeaderUserId = LeaderUserId;
  203. // pos.OpId = OpId;
  204. maindb.SaveChanges();
  205. SnIndex += 1;
  206. }
  207. order.Status = 2;
  208. order.SendStatus = 1;
  209. order.SendDate = DateTime.Now;
  210. order.SnNos = SnNos.TrimEnd(',');
  211. maindb.SaveChanges();
  212. //扣减分仓所关联的运营和中心总机具数
  213. if (store.OpId > 0)
  214. {
  215. var count = SnIdList.Count();
  216. var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.QueryCount == 1 && m.UserId == store.OpId) ?? new OpModels.SysAdmin();
  217. if (sysAdmin.Id > 0)
  218. {
  219. sysAdmin.TotalMachineCount -= count;
  220. opdb.SaveChanges();
  221. }
  222. }
  223. string SendData = "{\"Kind\":\"4\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"SnIds\":\"" + SnNo + "\"}}";
  224. //只给对应分仓增加可用额度
  225. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  226. // if(ApplyType > 1)
  227. // {
  228. // // 兑换成机具将向运营中心返还对应机具额度
  229. // foreach(int OpId in couponIds.Keys)
  230. // {
  231. // string[] datalist = couponIds[OpId].Split(',');
  232. // decimal OperateAmount = 200 + int.Parse(datalist[2]);
  233. // if(datalist[1] == "2") OperateAmount = 300 + int.Parse(datalist[2]);
  234. // string OperateData = "{\"UserId\":\"" + OpId + "\",\"DataId\":\"" + datalist[0] + "\",\"Kind\":\"1\",\"Amount\":\"" + OperateAmount + "\"}";
  235. // RedisDbconn.Instance.AddList("OperateAmountQueue", OperateData);
  236. // }
  237. // }
  238. }
  239. maindb.Dispose();
  240. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  241. }
  242. #endregion
  243. #region 首页-仓库管理-逐台划拨
  244. [Authorize]
  245. public JsonResult TransferOne(string value)
  246. {
  247. value = DesDecrypt(value);
  248. JsonData data = JsonMapper.ToObject(value);
  249. AppResultJson result = TransferOneDo(value);
  250. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  251. }
  252. public AppResultJson TransferOneDo(string value)
  253. {
  254. JsonData data = JsonMapper.ToObject(value);
  255. int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString()));
  256. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  257. int ToUserId = int.Parse(function.CheckInt(data["ToUserId"].ToString())); //收货人
  258. string SnIds = data["SnIds"].ToString(); //SN的Id集合
  259. Dictionary<string, object> Obj = new Dictionary<string, object>();
  260. if (string.IsNullOrEmpty(SnIds))
  261. {
  262. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  263. }
  264. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  265. string[] SnIdList = SnIds.Split(',');
  266. string SnNos = ""; //划拨的机具号(多个)
  267. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == OrderId);
  268. if (order != null)
  269. {
  270. if (order.Status == 2)
  271. {
  272. return new AppResultJson() { Status = "-1", Info = "该订单已处理,请勿重复操作" };
  273. }
  274. int ApplyType = 0; // 申请类型,1-机具SN,2-200兑换码,3-300券
  275. List<string> SourceSnNos = new List<string>();
  276. if (order.Sort > 0)
  277. {
  278. MachineApply Apply = maindb.MachineApply.FirstOrDefault(m => m.Id == order.Sort);
  279. if (Apply != null)
  280. {
  281. Apply.Status = 1;
  282. Apply.QueryCount = order.Id;
  283. ApplyType = Apply.Sort;
  284. if (!string.IsNullOrEmpty(Apply.SwapSnExpand))
  285. {
  286. string[] SwapSnExpands = Apply.SwapSnExpand.Split('\n');
  287. foreach (string sub in SwapSnExpands)
  288. {
  289. if (!string.IsNullOrEmpty(sub))
  290. {
  291. SourceSnNos.Add(sub.Split(':')[0]);
  292. }
  293. }
  294. }
  295. }
  296. }
  297. Dictionary<int, string> couponIds = new Dictionary<int, string>();
  298. int SnIndex = 0;
  299. foreach (string SnId in SnIdList)
  300. {
  301. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  302. int SnIdNum = int.Parse(SnId);
  303. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  304. int LeaderUserId = 0;
  305. // int OpId = 0;
  306. if (SourceSnNos.Count > SnIndex)
  307. {
  308. string SourceSnNo = SourceSnNos[SnIndex];
  309. if (ApplyType <= 1)
  310. {
  311. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSnNo) ?? new MachineForSnNo();
  312. PosMachinesTwo spos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  313. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  314. LeaderUserId = spos.LeaderUserId;
  315. // OpId = spos.OpId;
  316. }
  317. else if (ApplyType > 1)
  318. {
  319. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.ExchangeCode == SourceSnNo) ?? new PosCoupons();
  320. LeaderUserId = coupon.LeaderUserId;
  321. if (coupon.OpId > 0)
  322. {
  323. if (couponIds.ContainsKey(coupon.OpId))
  324. {
  325. string[] datas = couponIds[coupon.OpId].Split(',');
  326. int Num = int.Parse(datas[2]) + 1;
  327. couponIds[coupon.OpId] = datas[0] + "," + datas[1] + "," + Num;
  328. }
  329. else
  330. {
  331. couponIds.Add(coupon.OpId, coupon.Id + "," + coupon.QueryCount + ",1");
  332. }
  333. }
  334. }
  335. }
  336. PosMachinesTwo pos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == SnIdNum) ?? new PosMachinesTwo();
  337. //扣减分仓所关联的运营中心总机具数
  338. if (pos.OpId > 0)
  339. {
  340. var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.QueryCount == 1 && m.UserId == pos.OpId) ?? new OpModels.SysAdmin();
  341. if (sysAdmin.Id > 0)
  342. {
  343. sysAdmin.TotalMachineCount -= 1;
  344. opdb.SaveChanges();
  345. }
  346. }
  347. if (StoreId == 0)
  348. {
  349. StoreId = pos.StoreId;
  350. }
  351. SnNos += pos.PosSn + ",";
  352. Users toUser = UsersDbconn.Instance.Get(ToUserId) ?? new Users();
  353. StoreStockChange query = maindb.StoreStockChange.Add(new StoreStockChange()
  354. {
  355. CreateDate = DateTime.Now,
  356. StoreId = StoreId, //仓库
  357. BrandId = pos.BrandId, //产品类型
  358. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  359. ChangeNo = ChangeNo, //变更单号
  360. TransType = 11, //交易类型
  361. SnNo = pos.PosSn, //SN编号
  362. SnType = pos.PosSnType, //SN机具类型
  363. StockOpDirect = 1, //库存操作方向
  364. DeviceType = pos.DeviceType, //设备类型
  365. FromUserId = store.UserId, //出货人
  366. FromDate = DateTime.Now, //出库时间
  367. ToUserId = ToUserId, //收货人
  368. ToStoreId = StoreId, //退货收货仓库
  369. OpId = pos.OpId
  370. }).Entity;
  371. UserStoreChange userstore = maindb.UserStoreChange.Add(new UserStoreChange()
  372. {
  373. CreateDate = DateTime.Now,
  374. UserId = store.UserId, //创客
  375. BrandId = pos.BrandId, //产品类型
  376. ChangeRecordNo = ChangeNo, //变更记录单号
  377. TransType = 0, //交易类型
  378. SnNo = pos.PosSn, //SN编号
  379. SnType = pos.PosSnType, //SN机具类型
  380. StockOpDirect = 0, //库存操作方向
  381. DeviceVendor = pos.DeviceName, //设备厂商
  382. DeviceType = pos.DeviceKind, //设备类型
  383. DeviceModel = pos.DeviceType, //设备型号
  384. ToUserId = ToUserId, //收货创客
  385. ToDate = DateTime.Now, //入库时间
  386. SourceStoreId = pos.SourceStoreId, //源仓库
  387. SnStatus = 1, //SN状态
  388. BindStatus = (int)pos.BindingState, //绑定状态
  389. BindMerchantId = pos.BindMerchantId, //绑定商户
  390. ActiveStatus = (int)pos.ActivationState, //激活状态
  391. ActRewardUserId = pos.BuyUserId, //激活奖励创客
  392. BrandType = pos.DeviceType, //品牌类型
  393. }).Entity;
  394. StoreChangeHistory history = maindb.StoreChangeHistory.Add(new StoreChangeHistory()
  395. {
  396. CreateDate = DateTime.Now,
  397. UserId = store.UserId, //创客
  398. BrandId = pos.BrandId, //产品类型
  399. ChangeRecordNo = ChangeNo, //变更记录单号
  400. TransType = 2, //交易类型
  401. SnNo = pos.PosSn, //SN编号
  402. SnType = pos.PosSnType, //SN机具类型
  403. StockOpDirect = 1, //库存操作方向
  404. DeviceVendor = pos.DeviceName, //设备厂商
  405. DeviceModel = pos.DeviceKind, //设备型号
  406. DeviceType = pos.DeviceType, //设备类型
  407. ToUserId = ToUserId, //收货创客
  408. FromUserId = store.UserId, //出货创客
  409. FromDate = DateTime.Now, //出库时间
  410. SourceStoreId = pos.SourceStoreId, //源仓库
  411. StoreId = store.Id, //仓库
  412. OpId = pos.OpId
  413. }).Entity;
  414. maindb.SaveChanges();
  415. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  416. store.LaveNum -= 1;
  417. store.OutNum += 1;
  418. pos.OrderId = OrderId;
  419. pos.BuyUserId = ToUserId;
  420. pos.UserId = ToUserId;
  421. pos.TransferTime = DateTime.Now;
  422. pos.RecycEndDate = RecycEndDate; // 循环结束时间
  423. pos.PosSnType = order.QueryCount;
  424. pos.LeaderUserId = LeaderUserId;
  425. //机子无标记就赋运营中心的值
  426. // if ((pos.OpId == 0 && ApplyType > 1) || ApplyType <= 1)
  427. // {
  428. // pos.OpId = OpId;
  429. // }
  430. maindb.SaveChanges();
  431. SnIndex += 1;
  432. }
  433. order.Status = 2;
  434. order.SendStatus = 1;
  435. order.SendDate = DateTime.Now;
  436. order.SnNos = SnNos.TrimEnd(',');
  437. maindb.SaveChanges();
  438. string SendData = "{\"Kind\":\"4\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"SnIds\":\"" + SnIds + "\"}}";
  439. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  440. // if(ApplyType > 1)
  441. // {
  442. // // 兑换成机具将向运营中心返还对应机具额度
  443. // foreach(int OpId in couponIds.Keys)
  444. // {
  445. // string[] datalist = couponIds[OpId].Split(',');
  446. // decimal OperateAmount = 200 + int.Parse(datalist[2]);
  447. // if(datalist[1] == "2") OperateAmount = 300 + int.Parse(datalist[2]);
  448. // string OperateData = "{\"UserId\":\"" + OpId + "\",\"DataId\":\"" + datalist[0] + "\",\"Kind\":\"1\",\"Amount\":\"" + OperateAmount + "\"}";
  449. // RedisDbconn.Instance.AddList("OperateAmountQueue", OperateData);
  450. // }
  451. // }
  452. }
  453. maindb.Dispose();
  454. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  455. }
  456. #endregion
  457. #region 首页-仓库管理-售后单-划拨
  458. [Authorize]
  459. public JsonResult TransferForChange(string value)
  460. {
  461. value = DesDecrypt(value);
  462. JsonData data = JsonMapper.ToObject(value);
  463. AppResultJson result = TransferForChangeDo(value);
  464. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  465. }
  466. public AppResultJson TransferForChangeDo(string value)
  467. {
  468. JsonData data = JsonMapper.ToObject(value);
  469. int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString()));
  470. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  471. string SnIds = data["SnIds"].ToString(); //SN的Id集合
  472. Dictionary<string, object> Obj = new Dictionary<string, object>();
  473. if (string.IsNullOrEmpty(SnIds))
  474. {
  475. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  476. }
  477. int CheckCount = maindb.MachineChange.Count(m => m.OutStoreId == StoreId && m.AuditResult == 0);
  478. if (CheckCount >= 10)
  479. {
  480. return new AppResultJson() { Status = "-1", Info = "待审核订单超过10单,请等待管理员审核" };
  481. }
  482. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == StoreId) ?? new StoreHouse();
  483. string[] SnIdList = SnIds.Split(',');
  484. string SnNos = ""; //划拨的机具号(多个)
  485. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == OrderId);
  486. if (order != null)
  487. {
  488. if (order.Status == 2)
  489. {
  490. return new AppResultJson() { Status = "-1", Info = "该订单已处理,请勿重复操作" };
  491. }
  492. int ToUserId = order.UserId;
  493. int ChangeId = 0;
  494. List<string> SourceSnNos = new List<string>();
  495. if (order.Sort > 0)
  496. {
  497. MachineChange Apply = maindb.MachineChange.FirstOrDefault(m => m.Id == order.Sort);
  498. if (Apply != null)
  499. {
  500. Apply.Status = 1;
  501. if (!string.IsNullOrEmpty(Apply.ChangeSnExpand))
  502. {
  503. string[] ChangeSnExpand = Apply.ChangeSnExpand.Split('\n');
  504. foreach (string sub in ChangeSnExpand)
  505. {
  506. if (!string.IsNullOrEmpty(sub))
  507. {
  508. SourceSnNos.Add(sub.Split(':')[0]);
  509. }
  510. }
  511. }
  512. ChangeId = Apply.Id;
  513. }
  514. }
  515. int SnIndex = 0;
  516. List<string> contents = new List<string>();
  517. foreach (string SnId in SnIdList)
  518. {
  519. string ChangeNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  520. int SnIdNum = int.Parse(SnId);
  521. DateTime RecycEndDate = DateTime.Now.AddDays(360);
  522. string SourceSnNo = "";
  523. if (SourceSnNos.Count > SnIndex)
  524. {
  525. SourceSnNo = SourceSnNos[SnIndex];
  526. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSnNo) ?? new MachineForSnNo();
  527. PosMachinesTwo spos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId) ?? new PosMachinesTwo();
  528. RecycEndDate = spos.RecycEndDate == null ? RecycEndDate = DateTime.Now.AddDays(360) : spos.RecycEndDate.Value;
  529. }
  530. PosMachinesTwo pos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == SnIdNum) ?? new PosMachinesTwo();
  531. //扣减分仓所关联的运营中心总机具数
  532. if (pos.OpId > 0)
  533. {
  534. var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.QueryCount == 1 && m.UserId == pos.OpId) ?? new OpModels.SysAdmin();
  535. if (sysAdmin.Id > 0)
  536. {
  537. sysAdmin.TotalMachineCount -= 1;
  538. opdb.SaveChanges();
  539. }
  540. }
  541. PosMerchantInfo merchant = maindb.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  542. SnNos += pos.PosSn + ",";
  543. Users toUser = UsersDbconn.Instance.Get(ToUserId) ?? new Users();
  544. StoreStockChange query = maindb.StoreStockChange.Add(new StoreStockChange()
  545. {
  546. CreateDate = DateTime.Now,
  547. StoreId = StoreId, //仓库
  548. BrandId = pos.BrandId, //产品类型
  549. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  550. ChangeNo = ChangeNo, //变更单号
  551. TransType = 11, //交易类型
  552. SnNo = pos.PosSn, //SN编号
  553. SnType = pos.PosSnType, //SN机具类型
  554. StockOpDirect = 1, //库存操作方向
  555. DeviceType = pos.DeviceType, //设备类型
  556. FromUserId = store.UserId, //出货人
  557. FromDate = DateTime.Now, //出库时间
  558. ToUserId = ToUserId, //收货人
  559. ToStoreId = StoreId, //退货收货仓库
  560. }).Entity;
  561. UserStoreChange userstore = maindb.UserStoreChange.Add(new UserStoreChange()
  562. {
  563. CreateDate = DateTime.Now,
  564. UserId = store.UserId, //创客
  565. BrandId = pos.BrandId, //产品类型
  566. ChangeRecordNo = ChangeNo, //变更记录单号
  567. TransType = 0, //交易类型
  568. SnNo = pos.PosSn, //SN编号
  569. SnType = pos.PosSnType, //SN机具类型
  570. StockOpDirect = 0, //库存操作方向
  571. DeviceVendor = pos.DeviceName, //设备厂商
  572. DeviceType = pos.DeviceKind, //设备类型
  573. DeviceModel = pos.DeviceType, //设备型号
  574. ToUserId = ToUserId, //收货创客
  575. ToDate = DateTime.Now, //入库时间
  576. SourceStoreId = pos.SourceStoreId, //源仓库
  577. SnStatus = 1, //SN状态
  578. BindStatus = (int)pos.BindingState, //绑定状态
  579. BindMerchantId = pos.BindMerchantId, //绑定商户
  580. ActiveStatus = (int)pos.ActivationState, //激活状态
  581. ActRewardUserId = pos.BuyUserId, //激活奖励创客
  582. BrandType = pos.DeviceType, //品牌类型
  583. }).Entity;
  584. StoreChangeHistory history = maindb.StoreChangeHistory.Add(new StoreChangeHistory()
  585. {
  586. CreateDate = DateTime.Now,
  587. UserId = store.UserId, //创客
  588. BrandId = pos.BrandId, //产品类型
  589. ChangeRecordNo = ChangeNo, //变更记录单号
  590. TransType = 2, //交易类型
  591. SnNo = pos.PosSn, //SN编号
  592. SnType = pos.PosSnType, //SN机具类型
  593. StockOpDirect = 1, //库存操作方向
  594. DeviceVendor = pos.DeviceName, //设备厂商
  595. DeviceModel = pos.DeviceKind, //设备型号
  596. DeviceType = pos.DeviceType, //设备类型
  597. ToUserId = ToUserId, //收货创客
  598. FromUserId = store.UserId, //出货创客
  599. FromDate = DateTime.Now, //出库时间
  600. SourceStoreId = pos.SourceStoreId, //源仓库
  601. StoreId = store.Id, //仓库
  602. }).Entity;
  603. maindb.SaveChanges();
  604. PublicFunction.StatUserMachineData(ToUserId, pos.BrandId, 1);
  605. store.LaveNum -= 1;
  606. store.OutNum += 1;
  607. pos.OrderId = OrderId;
  608. pos.BuyUserId = ToUserId;
  609. pos.UserId = ToUserId;
  610. pos.TransferTime = DateTime.Now;
  611. pos.RecycEndDate = RecycEndDate; // 循环结束时间
  612. pos.PosSnType = order.QueryCount;
  613. maindb.SaveChanges();
  614. SnIndex += 1;
  615. string content = "{"; //执行机具数据同步
  616. content += "\"OldSn\":\"" + SourceSnNo + "\",";
  617. content += "\"NewSn\":\"" + pos.PosSn + "\",";
  618. content += "\"MerNo\":\"" + merchant.KqMerNo + "\",";
  619. content += "\"ChangeId\":\"" + ChangeId + "\"";
  620. content += "}";
  621. contents.Add(content);
  622. }
  623. order.Status = 2;
  624. order.SendStatus = 1;
  625. order.SendDate = DateTime.Now;
  626. order.SnNos = SnNos.TrimEnd(',');
  627. maindb.SaveChanges();
  628. foreach (string content in contents)
  629. {
  630. RedisDbconn.Instance.AddList("ChangePosTimerQueue", content);
  631. }
  632. string SendData = "{\"Kind\":\"4\",\"Data\":{\"StoreId\":\"" + StoreId + "\",\"SnIds\":\"" + SnIds + "\"}}";
  633. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  634. }
  635. maindb.Dispose();
  636. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  637. }
  638. #endregion
  639. #region 首页-仓库管理-售后单-划拨验证
  640. [Authorize]
  641. public JsonResult TransferForCheck(string value)
  642. {
  643. value = DesDecrypt(value);
  644. JsonData data = JsonMapper.ToObject(value);
  645. AppResultJson result = TransferForCheckDo(value);
  646. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  647. }
  648. public AppResultJson TransferForCheckDo(string value)
  649. {
  650. JsonData data = JsonMapper.ToObject(value);
  651. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  652. int CheckCount = maindb.MachineChange.Count(m => m.OutStoreId == StoreId && m.AuditResult == 0);
  653. if (CheckCount >= 10)
  654. {
  655. return new AppResultJson() { Status = "-1", Info = "待审核订单超过10单,请等待管理员审核" };
  656. }
  657. return new AppResultJson() { Status = "1", Info = "" };
  658. }
  659. #endregion
  660. #region 首页-仓库管理-售后单-驳回
  661. [Authorize]
  662. public JsonResult RefuseForChange(string value)
  663. {
  664. value = DesDecrypt(value);
  665. JsonData data = JsonMapper.ToObject(value);
  666. AppResultJson result = RefuseForChangeDo(value);
  667. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  668. }
  669. public AppResultJson RefuseForChangeDo(string value)
  670. {
  671. JsonData data = JsonMapper.ToObject(value);
  672. int OrderId = int.Parse(function.CheckInt(data["OrderId"].ToString()));
  673. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  674. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == OrderId && m.StoreId == StoreId);
  675. if (order != null)
  676. {
  677. order.Status = -1;
  678. MachineChange change = maindb.MachineChange.FirstOrDefault(m => m.Id == order.Sort);
  679. if (change != null)
  680. {
  681. change.AuditResult = 2;
  682. }
  683. maindb.SaveChanges();
  684. maindb.Dispose();
  685. return new AppResultJson() { Status = "1", Info = "" };
  686. }
  687. maindb.Dispose();
  688. return new AppResultJson() { Status = "-1", Info = "" };
  689. }
  690. #endregion
  691. #region 创客-首页-仓库管理-划拨记录-拨入
  692. [Authorize]
  693. public JsonResult In(string value)
  694. {
  695. value = DesDecrypt(value);
  696. JsonData data = JsonMapper.ToObject(value);
  697. List<Dictionary<string, object>> dataList = InDo(value);
  698. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  699. }
  700. public List<Dictionary<string, object>> InDo(string value)
  701. {
  702. JsonData data = JsonMapper.ToObject(value);
  703. string SearchKey = data["SearchKey"].ToString();
  704. int ToStoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //退货收货仓库
  705. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  706. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  707. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  708. IQueryable<StoreStockChange> query = maindb.StoreStockChange.Where(m => m.ToStoreId == ToStoreId);
  709. if (!string.IsNullOrEmpty(SearchKey))
  710. {
  711. query = query.Where(m => m.SnNo == SearchKey);
  712. }
  713. if (PageNum == 1)
  714. {
  715. query = query.Take(PageSize);
  716. }
  717. else
  718. {
  719. int skipNum = PageSize * (PageNum - 1);
  720. query = query.Skip(skipNum).Take(PageSize);
  721. }
  722. query = query.OrderByDescending(m => m.Id);
  723. foreach (var subdata in query.ToList())
  724. {
  725. Dictionary<string, object> curData = new Dictionary<string, object>();
  726. curData.Add("StoreId", subdata.StoreId); //仓库
  727. Dictionary<string, object> StoreInfo = new Dictionary<string, object>();
  728. StoreHouse StoreHouseData = maindb.StoreHouse.FirstOrDefault(m => m.Id == subdata.StoreId) ?? new StoreHouse();
  729. StoreInfo.Add("StoreName", StoreHouseData.StoreName); //仓库名称
  730. curData.Add("StoreInfo", StoreInfo);
  731. curData.Add("SnNo", subdata.SnNo); //SN编号
  732. curData.Add("FromUserId", subdata.FromUserId); //出货人
  733. Dictionary<string, object> UserInfo = new Dictionary<string, object>();
  734. Users UsersData = maindb.Users.FirstOrDefault(m => m.Id == subdata.FromUserId) ?? new Users();
  735. UserInfo.Add("RealName", UsersData.RealName); //真实姓名
  736. UserInfo.Add("MakerCode", UsersData.MakerCode); //创客编号
  737. UserInfo.Add("Mobile", UsersData.Mobile); //手机号
  738. curData.Add("UserInfo", UserInfo);
  739. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  740. dataList.Add(curData);
  741. }
  742. return dataList;
  743. }
  744. #endregion
  745. #region 创客-首页-仓库管理-划拨记录-拨出
  746. [Authorize]
  747. public JsonResult Out(string value)
  748. {
  749. value = DesDecrypt(value);
  750. JsonData data = JsonMapper.ToObject(value);
  751. List<Dictionary<string, object>> dataList = OutDo(value);
  752. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  753. }
  754. public List<Dictionary<string, object>> OutDo(string value)
  755. {
  756. JsonData data = JsonMapper.ToObject(value);
  757. string SearchKey = data["SearchKey"].ToString();
  758. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  759. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  760. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  761. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  762. IQueryable<StoreStockChange> query = maindb.StoreStockChange.Where(m => m.StoreId == StoreId && m.ToUserId > 0);
  763. if (!string.IsNullOrEmpty(SearchKey))
  764. {
  765. query = query.Where(m => m.SnNo == SearchKey);
  766. }
  767. if (PageNum == 1)
  768. {
  769. query = query.Take(PageSize);
  770. }
  771. else
  772. {
  773. int skipNum = PageSize * (PageNum - 1);
  774. query = query.Skip(skipNum).Take(PageSize);
  775. }
  776. query = query.OrderByDescending(m => m.Id);
  777. foreach (var subdata in query.ToList())
  778. {
  779. Dictionary<string, object> curData = new Dictionary<string, object>();
  780. curData.Add("ChangeNo", subdata.ChangeNo); //变更单号
  781. curData.Add("SnNo", subdata.SnNo); //SN编号
  782. curData.Add("ToUserId", subdata.ToUserId); //收货人
  783. Dictionary<string, object> UserInfo = new Dictionary<string, object>();
  784. Users UsersData = maindb.Users.FirstOrDefault(m => m.Id == subdata.ToUserId) ?? new Users();
  785. UserInfo.Add("RealName", UsersData.RealName); //真实姓名
  786. UserInfo.Add("MakerCode", UsersData.MakerCode); //创客编号
  787. UserInfo.Add("Mobile", UsersData.Mobile); //手机号
  788. curData.Add("UserInfo", UserInfo);
  789. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  790. dataList.Add(curData);
  791. }
  792. return dataList;
  793. }
  794. #endregion
  795. #region 创客-首页-仓库管理-出货记录-按天
  796. [Authorize]
  797. public JsonResult ForDate(string value)
  798. {
  799. value = DesDecrypt(value);
  800. JsonData data = JsonMapper.ToObject(value);
  801. List<Dictionary<string, object>> dataList = ForDateDo(value);
  802. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  803. }
  804. public List<Dictionary<string, object>> ForDateDo(string value)
  805. {
  806. JsonData data = JsonMapper.ToObject(value);
  807. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  808. // int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
  809. string Month = data["Month"].ToString(); //月份
  810. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  811. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  812. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  813. DataTable dt = OtherMySqlConn.dtable("SELECT DATE_FORMAT(CreateDate,'%Y%m%d') date,COUNT(0) count FROM StoreStockChange WHERE StoreId = '" + StoreId + "' AND TransType in (2,10,11) AND DATE_FORMAT(CreateDate,'%Y%m') = '" + Month + "' GROUP BY DATE_FORMAT(CreateDate,'%Y%m%d') ORDER BY DATE_FORMAT(CreateDate,'%Y%m%d') DESC");
  814. foreach (DataRow item in dt.Rows)
  815. {
  816. Dictionary<string, object> curData = new Dictionary<string, object>();
  817. curData.Add("Date", item["date"]); //时间
  818. curData.Add("ActTotal", Convert.ToInt32(function.CheckInt(item["count"].ToString()))); //总激活
  819. List<Dictionary<string, object>> snList = new List<Dictionary<string, object>>();
  820. DataTable dts = OtherMySqlConn.dtable("SELECT DATE_FORMAT(CreateDate,'%Y%m%d') date,SnNo FROM StoreStockChange WHERE StoreId = '" + StoreId + "' AND TransType in (2,10,11) AND DATE_FORMAT(CreateDate,'%Y%m%d') = '" + item["date"] + "'");
  821. foreach (DataRow items in dts.Rows)
  822. {
  823. Dictionary<string, object> sn = new Dictionary<string, object>();
  824. sn.Add("Date", items["date"]); //时间
  825. sn.Add("Sn", items["SnNo"]); //Sn
  826. snList.Add(sn);
  827. }
  828. curData.Add("SnList", snList);
  829. dataList.Add(curData);
  830. }
  831. dataList = dataList.Skip((PageNum - 1) * PageSize).Take(PageSize).ToList();//分页的重点
  832. return dataList;
  833. }
  834. #endregion
  835. #region 创客-首页-仓库管理-出货记录-按月
  836. [Authorize]
  837. public JsonResult ForMonth(string value)
  838. {
  839. value = DesDecrypt(value);
  840. JsonData data = JsonMapper.ToObject(value);
  841. List<Dictionary<string, object>> dataList = ForMonthDo(value);
  842. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  843. }
  844. public List<Dictionary<string, object>> ForMonthDo(string value)
  845. {
  846. JsonData data = JsonMapper.ToObject(value);
  847. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  848. // int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
  849. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  850. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  851. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  852. DataTable dt = OtherMySqlConn.dtable("SELECT DATE_FORMAT(CreateDate,'%Y%m') date,COUNT(0) count FROM StoreStockChange WHERE StoreId = '" + StoreId + "' AND TransType in (2,10,11) GROUP BY DATE_FORMAT(CreateDate,'%Y%m') ORDER BY DATE_FORMAT(CreateDate,'%Y%m') DESC");
  853. foreach (DataRow item in dt.Rows)
  854. {
  855. Dictionary<string, object> curData = new Dictionary<string, object>();
  856. curData.Add("Date", item["date"]); //时间
  857. curData.Add("ActTotal", Convert.ToInt32(function.CheckInt(item["count"].ToString()))); //总激活
  858. dataList.Add(curData);
  859. }
  860. dataList = dataList.Skip((PageNum - 1) * PageSize).Take(PageSize).ToList();//分页的重点
  861. return dataList;
  862. }
  863. #endregion
  864. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  865. /// <summary>
  866. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  867. /// </summary>
  868. /// <param name="value">请求的参数(json字符串)</param>
  869. /// <param name="signField">要签名的字段</param>
  870. /// <returns></returns>
  871. private string CheckSign(string value, string[] signField)
  872. {
  873. JsonData json = JsonMapper.ToObject(value);
  874. Dictionary<string, string> dic = new Dictionary<string, string>();
  875. for (int i = 0; i < signField.Length; i++)
  876. {
  877. dic.Add(signField[i], json[signField[i]].ToString());
  878. }
  879. string sign = json["sign"].ToString(); //客户端签名字符串
  880. return new Sign().sign(dic, sign);
  881. }
  882. #endregion
  883. }
  884. }