MachineApplyController.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.MainModels;
  11. using LitJson;
  12. using Library;
  13. namespace MySystem.Areas.Api.Controllers.v1.pos
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/pos/[controller]/[action]")]
  17. public class MachineApplyController : BaseController
  18. {
  19. public MachineApplyController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 首页-客小爽产品-机具管理-机具申请-申请记录
  23. [Authorize]
  24. public JsonResult ApplyRecords(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = ApplyRecordsDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> ApplyRecordsDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  35. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  36. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  37. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  38. IQueryable<MachineApply> query = maindb.MachineApply.Where(m => m.UserId == UserId).OrderByDescending(m => m.Id);
  39. if (PageNum == 1)
  40. {
  41. query = query.Take(PageSize);
  42. }
  43. else
  44. {
  45. int skipNum = PageSize * (PageNum - 1);
  46. query = query.Skip(skipNum).Take(PageSize);
  47. }
  48. // List<int> query = MachineApplyDbconn.Instance.GetList(UserId, PageNum, PageSize);
  49. foreach (MachineApply subdata in query)
  50. {
  51. // MachineApply subdata = MachineApplyDbconn.Instance.Get(Convert.ToInt32(ApplyId)) ?? new MachineApply();
  52. Dictionary<string, object> curData = new Dictionary<string, object>();
  53. curData.Add("ApplyNo", subdata.ApplyNo); //申请单号
  54. curData.Add("Areas", function.CheckNull(subdata.Areas).Replace(",", "")); //收货所在地区
  55. curData.Add("Address", string.IsNullOrEmpty(subdata.Address) ? "上门自提" : subdata.Address); //收货详细地址
  56. curData.Add("ApplyDeviceNum", subdata.ApplyDeviceNum); //申请机具数量
  57. curData.Add("Id", subdata.Id); //Id
  58. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  59. curData.Add("Status", RelationClass.GetMachineApplyStatusInfo(subdata.Status)); //Status
  60. dataList.Add(curData);
  61. }
  62. return dataList;
  63. }
  64. #endregion
  65. #region 首页-客小爽产品-机具管理-机具申请-申请记录详情
  66. [Authorize]
  67. public JsonResult ApplyDetail(string value)
  68. {
  69. value = DesDecrypt(value);
  70. JsonData data = JsonMapper.ToObject(value);
  71. Dictionary<string, object> Obj = ApplyDetailDo(value);
  72. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  73. }
  74. public Dictionary<string, object> ApplyDetailDo(string value)
  75. {
  76. JsonData data = JsonMapper.ToObject(value);
  77. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  78. Dictionary<string, object> Obj = new Dictionary<string, object>();
  79. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  80. MachineApply query = MachineApplyDbconn.Instance.Get(Id) ?? new MachineApply();
  81. StoreHouse store = StoreHouseDbconn.Instance.Get(query.StoreId) ?? new StoreHouse();
  82. Obj.Add("ApplyNo", query.ApplyNo); //申请单号
  83. Obj.Add("ProductName", query.ProductName); //产品名称
  84. Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //收货所在地区
  85. Obj.Add("Address", string.IsNullOrEmpty(query.Address) ? "上门自提" : query.Address); //收货详细地址
  86. Obj.Add("RealName", query.RealName); //收件人姓名
  87. Obj.Add("Mobile", query.Mobile); //收件人手机号
  88. Obj.Add("ApplyDeviceNum", query.ApplyDeviceNum); //申请机具数量
  89. Obj.Add("DeliveryType", query.DeliveryType); //提货类型
  90. if (store.UserId == 1)
  91. {
  92. Obj.Add("StoreManagerMobile", "19141324516"); //仓库联系人手机号
  93. }
  94. else
  95. {
  96. Obj.Add("StoreManagerMobile", query.StoreManagerMobile); //仓库联系人手机号
  97. }
  98. Obj.Add("StoreName", store.StoreName); //提货仓库
  99. Obj.Add("Status", RelationClass.GetMachineApplyStatusInfo(query.Status)); //状态
  100. Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
  101. return Obj;
  102. }
  103. #endregion
  104. #region 首页-客小爽产品-机具管理-机具申请-申请记录详情-V2
  105. [Authorize]
  106. public JsonResult ApplyDetail2(string value)
  107. {
  108. value = DesDecrypt(value);
  109. JsonData data = JsonMapper.ToObject(value);
  110. Dictionary<string, object> Obj = ApplyDetail2Do(value);
  111. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  112. }
  113. public Dictionary<string, object> ApplyDetail2Do(string value)
  114. {
  115. JsonData data = JsonMapper.ToObject(value);
  116. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  117. Dictionary<string, object> Obj = new Dictionary<string, object>();
  118. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  119. MachineApply query = MachineApplyDbconn.Instance.Get(Id) ?? new MachineApply();
  120. StoreHouse store = StoreHouseDbconn.Instance.Get(query.StoreId) ?? new StoreHouse();
  121. Obj.Add("ApplyNo", query.ApplyNo); //申请单号
  122. Obj.Add("ProductName", query.ProductName); //产品名称
  123. Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //收货所在地区
  124. Obj.Add("Address", string.IsNullOrEmpty(query.Address) ? "上门自提" : query.Address); //收货详细地址
  125. Obj.Add("RealName", query.RealName); //收件人姓名
  126. Obj.Add("Mobile", query.Mobile); //收件人手机号
  127. Obj.Add("ApplyDeviceNum", query.ApplyDeviceNum); //申请机具数量
  128. Obj.Add("DeliveryType", query.DeliveryType); //提货类型
  129. if (store.UserId == 1)
  130. {
  131. Obj.Add("StoreManagerMobile", "19141324516"); //仓库联系人手机号
  132. }
  133. else
  134. {
  135. Obj.Add("StoreManagerMobile", query.StoreManagerMobile); //仓库联系人手机号
  136. }
  137. Obj.Add("StoreName", store.StoreName); //提货仓库
  138. Obj.Add("Status", RelationClass.GetMachineApplyStatusInfo(query.Status)); //状态
  139. Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
  140. string SwapSnExpand = query.SwapSnExpand;
  141. int Kind = 0;
  142. if (SwapSnExpand.StartsWith("02") || SwapSnExpand.StartsWith("03")) Kind = 2;
  143. else Kind = 1;
  144. List<string> list = new List<string>();
  145. if (!string.IsNullOrEmpty(SwapSnExpand))
  146. {
  147. list = SwapSnExpand.Split('\n').ToList();
  148. }
  149. Obj.Add("Kind", Kind); //申请类别:1-机具,2-兑换券
  150. Obj.Add("SnList", list); //机具SN/兑换码列表
  151. return Obj;
  152. }
  153. #endregion
  154. #region 首页-客小爽产品-机具管理-机具申请-确认申请
  155. [Authorize]
  156. public JsonResult ConfirmApply(string value)
  157. {
  158. value = DesDecrypt(value);
  159. JsonData data = JsonMapper.ToObject(value);
  160. AppResultJson result = ConfirmApplyDo(value);
  161. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  162. }
  163. public AppResultJson ConfirmApplyDo(string value)
  164. {
  165. function.WriteLog(DateTime.Now.ToString() + "\r\n" + value, "首页-客小爽产品-机具管理-机具申请-确认申请");
  166. JsonData data = JsonMapper.ToObject(value);
  167. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  168. string SendSn = data["SendSn"].ToString(); //发货SN
  169. string ProductType = data["ProductType"].ToString(); //产品类型
  170. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  171. int DeliveryType = int.Parse(function.CheckInt(data["DeliveryType"].ToString())); //提货类型
  172. string Remark = data["Remark"].ToString(); //订单备注
  173. int AddressId = int.Parse(function.CheckInt(data["AddressId"].ToString())); //收货地址Id
  174. if (StoreId == 0)
  175. {
  176. return new AppResultJson() { Status = "-1", Info = "请选择仓库" };
  177. }
  178. Dictionary<string, object> Obj = new Dictionary<string, object>();
  179. if (!string.IsNullOrEmpty(SendSn))
  180. {
  181. string SwapSnExpand = "";
  182. string[] SnIds = SendSn.Split(',');
  183. int OpId = 0;
  184. foreach (string SnId in SnIds)
  185. {
  186. int SnIdNum = int.Parse(SnId);
  187. PosMachinesTwo machine = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.Id == SnIdNum);
  188. if (machine != null)
  189. {
  190. machine.IsPurchase = 1;
  191. maindb.SaveChanges();
  192. SwapSnExpand += machine.PosSn + ":" + RelationClass.GetPosSnTypeInfo(machine.PosSnType) + "\n";
  193. OpId = machine.OpId;
  194. }
  195. }
  196. if (string.IsNullOrEmpty(SwapSnExpand))
  197. {
  198. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  199. }
  200. string ApplyNo = "BA" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  201. int BrandId = int.Parse(function.CheckInt(ProductType));
  202. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  203. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  204. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  205. StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
  206. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  207. string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  208. bool check = maindb.MachineApply.Any(m => m.SwapSnExpand == SwapSnExpand);
  209. if (check)
  210. {
  211. return new AppResultJson() { Status = "-1", Info = "申请已提交,请勿重复申请" };
  212. }
  213. RedisDbconn.Instance.GetLock("MachineApply:" + UserId);
  214. MachineApply query = maindb.MachineApply.Add(new MachineApply()
  215. {
  216. // Id = MachineApplyId,
  217. CreateDate = DateTime.Now,
  218. ApplyNo = ApplyNo, //申请单号
  219. UserId = UserId, //创客
  220. BrandId = BrandId, //品牌
  221. ProductName = protype.Name, //产品名称
  222. Areas = address.Areas, //收货所在地区
  223. Address = address.Address, //收货详细地址
  224. RealName = address.RealName, //收件人姓名
  225. Mobile = address.Mobile, //收件人手机号
  226. ApplyDeviceName = protype.Name, //申请机具名称
  227. ApplyDeviceNum = SnIds.Length, //申请机具数量
  228. ApplyTime = DateTime.Now, //申请时间
  229. DeliveryType = DeliveryType, //提货类型
  230. SwapSnExpand = SwapSnExpand, //兑换机器SN来源
  231. StoreId = store.Id,
  232. StoreUserId = store.UserId,
  233. StoreAreas = store.Areas,
  234. StoreAddress = store.Address,
  235. StoreManager = manager.RealName,
  236. StoreManagerMobile = store.ManageMobile,
  237. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  238. OperateId = OpId,
  239. }).Entity;
  240. maindb.SaveChanges();
  241. function.WriteLog("applyid:" + query.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  242. RedisDbconn.Instance.ReleaseLock("MachineApply:" + UserId);
  243. Products product = ProductsDbconn.Instance.Get(protype.QueryCount) ?? new Products();
  244. Orders order = maindb.Orders.Add(new Orders()
  245. {
  246. OrderNo = OrderNo,
  247. RealName = address.RealName,
  248. Mobile = address.Mobile,
  249. Areas = address.Areas,
  250. Address = address.Address,
  251. StoreContact = manager.RealName,
  252. StoreContactMobile = store.ManageMobile,
  253. StoreUserId = store.UserId,
  254. StoreType = store.StoreType,
  255. CreateDate = DateTime.Now, //创建时间
  256. UserId = UserId, //创客
  257. StoreId = StoreId, //仓库
  258. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  259. DeliveryType = DeliveryType, //提货类型
  260. BuyCount = SnIds.Length,
  261. TotalPrice = 0, //订单总额
  262. Remark = Remark, //订单备注
  263. Status = 1,
  264. PayStatus = 1,
  265. PayDate = DateTime.Now,
  266. QueryCount = 1, //申请循环机标记
  267. Sort = query.Id,
  268. OpId = OpId,
  269. }).Entity;
  270. maindb.SaveChanges();
  271. function.WriteLog("orderid:" + order.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  272. MachineApply edit = maindb.MachineApply.FirstOrDefault(m => m.Id == query.Id);
  273. if (edit != null)
  274. {
  275. edit.QueryCount = order.Id;
  276. maindb.SaveChanges();
  277. }
  278. OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
  279. {
  280. CreateDate = DateTime.Now, //创建时间
  281. OrderId = order.Id,
  282. ProductId = product.Id,
  283. ProductCount = SnIds.Length,
  284. ProductName = product.ProductName,
  285. ProductPhoto = product.ListPicPath,
  286. ProductPrice = 0,
  287. TotalPrice = 0,
  288. ProductCode = product.ProductCode,
  289. UserId = UserId,
  290. StoreId = StoreId,
  291. }).Entity;
  292. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  293. {
  294. OrderNo = OrderNo,
  295. OrderIds = order.Id.ToString(),
  296. }).Entity;
  297. maindb.SaveChanges();
  298. function.WriteLog("2", "首页-客小爽产品-机具管理-机具申请-确认申请");
  299. function.WriteLog("ok\r\n\r\n", "首页-客小爽产品-机具管理-机具申请-确认申请");
  300. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  301. }
  302. return new AppResultJson() { Status = "-1", Info = "请选择机具", Data = Obj };
  303. }
  304. #endregion
  305. #region 首页-客小爽产品-机具管理-机具申请-确认申请-V2
  306. [Authorize]
  307. public JsonResult ConfirmApply2(string value)
  308. {
  309. value = DesDecrypt(value);
  310. JsonData data = JsonMapper.ToObject(value);
  311. AppResultJson result = ConfirmApply2Do(value);
  312. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  313. }
  314. public AppResultJson ConfirmApply2Do(string value)
  315. {
  316. function.WriteLog("\r\n\r\n" + DateTime.Now.ToString(), "机具申请-确认申请-V2");
  317. function.WriteLog(value, "机具申请-确认申请-V2");
  318. JsonData data = JsonMapper.ToObject(value);
  319. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  320. int Kind = int.Parse(function.CheckInt(data["Kind"].ToString())); //申请类型,1-机具SN,2-200兑换码,3-300券
  321. string SendSn = data["SendSn"].ToString(); //发货SN
  322. string key = function.MD5_16(UserId + SendSn);
  323. string checkOp = RedisDbconn.Instance.Get<string>("ConfirmApply2:" + key);
  324. if (!string.IsNullOrEmpty(checkOp))
  325. {
  326. return new AppResultJson() { Status = "-1", Info = "机具已申请,请勿重复提交" };
  327. }
  328. string ProductType = data["ProductType"].ToString(); //产品类型
  329. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  330. int IsPre = 0;
  331. if (value.Contains("\"IsPre\""))
  332. {
  333. IsPre = int.Parse(function.CheckInt(data["IsPre"].ToString()));
  334. }
  335. string SnIdString = "";
  336. if (IsPre == 1)
  337. {
  338. string SnIds = data["SnIds"].ToString(); //发货SN
  339. if (string.IsNullOrEmpty(SnIds))
  340. {
  341. return new AppResultJson() { Status = "-1", Info = "请选择预发机具" };
  342. }
  343. string[] SnIdList = SnIds.Split(',');
  344. foreach (string SnId in SnIdList)
  345. {
  346. var amount = 0;
  347. int Id = int.Parse(SnId);
  348. PreSendStockDetail pre = maindb.PreSendStockDetail.FirstOrDefault(m => m.Id == Id && m.ToUserId == UserId);
  349. if (pre != null)
  350. {
  351. pre.ApplyFlag = 1;
  352. pre.ApplyDate = DateTime.Now;
  353. maindb.SaveChanges();
  354. SnIdString += pre.SnId + ",";
  355. //超时预发机押金申请循环后退还
  356. var brandInfo = maindb.KqProducts.FirstOrDefault(m => m.Id == pre.BrandId);
  357. if (brandInfo.Name.Contains("电签"))
  358. {
  359. amount = 200;
  360. }
  361. if (brandInfo.Name.Contains("大POS"))
  362. {
  363. amount = 300;
  364. }
  365. var userAccount = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  366. var fuserAccount = maindb.UserAccount.FirstOrDefault(m => m.Id == pre.FromUserId) ?? new UserAccount();
  367. if (pre.CreateDate <= DateTime.Now.AddDays(-30))
  368. {
  369. if (pre.AuthFlag == 1)
  370. {
  371. if (userAccount.Id > 0 && userAccount.SmallStoreDeposit >= amount)
  372. {
  373. userAccount.SmallStoreDeposit -= amount;//扣减小分仓押金
  374. var add = maindb.UserAccountRecord.Add(new UserAccountRecord()
  375. {
  376. CreateDate = DateTime.Now,
  377. Remark = "小分仓押金解冻",
  378. ChangeType = 66,
  379. BeforeBalanceAmount = userAccount.SmallStoreDeposit + amount, //变更前小分仓押金
  380. AfterBalanceAmount = userAccount.SmallStoreDeposit, //变更后小分仓押金
  381. ChangeAmount = amount,//变动金额
  382. UserId = userAccount.Id,
  383. }).Entity;
  384. maindb.SaveChanges();
  385. }
  386. }
  387. }
  388. else
  389. {
  390. if (pre.AuthFlag == 1)
  391. {
  392. if (userAccount.Id > 0)
  393. {
  394. userAccount.ValidPreAmount += amount;//增加小分仓可用额度
  395. maindb.SaveChanges();
  396. }
  397. }
  398. }
  399. }
  400. }
  401. SmallStoreHouse house = maindb.SmallStoreHouse.FirstOrDefault(m => m.UserId == UserId);
  402. if (house != null)
  403. {
  404. house.TotalNum -= SnIdList.Length;
  405. house.LaveNum += SnIdList.Length;
  406. maindb.SaveChanges();
  407. string text = string.Format("首页-客小爽产品-机具管理-机具申请-确认申请-V2,UserId: '" + UserId + "',LaveNum:'" + house.LaveNum + "',TotalNum:'" + house.TotalNum + "',ChangeCount:'" + SnIdList.Length + "',Time'" + DateTime.Now + "'");
  408. function.WriteLog(text, "smallstorehouse");//小分仓记录日志
  409. }
  410. }
  411. function.WriteLog("SnIdString:" + SnIdString, "机具申请-确认申请-V2");
  412. int DeliveryType = int.Parse(function.CheckInt(data["DeliveryType"].ToString())); //提货类型
  413. string Remark = data["Remark"].ToString(); //订单备注
  414. int AddressId = int.Parse(function.CheckInt(data["AddressId"].ToString())); //收货地址Id
  415. if (StoreId == 0 && IsPre != 1)
  416. {
  417. return new AppResultJson() { Status = "-1", Info = "请选择仓库" };
  418. }
  419. Dictionary<string, object> Obj = new Dictionary<string, object>();
  420. int MachineCount = 0;
  421. if (Kind == 1)
  422. {
  423. if (!string.IsNullOrEmpty(SendSn))
  424. {
  425. int OpId = 0;
  426. string SwapSnExpand = "";
  427. string[] SnIds = SendSn.Split(',');
  428. foreach (string SnId in SnIds)
  429. {
  430. int SnIdNum = int.Parse(SnId);
  431. PosMachinesTwo machine = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.Id == SnIdNum);
  432. if (machine != null)
  433. {
  434. bool check = maindb.MachineApply.Any(m => m.SwapSnExpand.Contains(machine.PosSn));
  435. if (check)
  436. {
  437. if (IsPre == 1)
  438. {
  439. BackPre(data, UserId);
  440. }
  441. return new AppResultJson() { Status = "-1", Info = "机具" + machine.PosSn + "已申请,请勿重复申请" };
  442. }
  443. }
  444. }
  445. foreach (string SnId in SnIds)
  446. {
  447. int SnIdNum = int.Parse(SnId);
  448. PosMachinesTwo machine = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.Id == SnIdNum);
  449. if (machine != null)
  450. {
  451. if (!SwapSnExpand.Contains(machine.PosSn))
  452. {
  453. machine.IsPurchase = 1;
  454. maindb.SaveChanges();
  455. SwapSnExpand += machine.PosSn + ":" + RelationClass.GetPosSnTypeInfo(machine.PosSnType) + "\n";
  456. MachineCount += 1;
  457. OpId = machine.OpId;
  458. }
  459. }
  460. }
  461. if (string.IsNullOrEmpty(SwapSnExpand))
  462. {
  463. if (IsPre == 1)
  464. {
  465. BackPre(data, UserId);
  466. }
  467. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  468. }
  469. function.WriteLog("Kind:" + Kind, "机具申请-确认申请-V2");
  470. string ApplyNo = "BA" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  471. int BrandId = int.Parse(function.CheckInt(ProductType));
  472. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  473. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  474. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  475. StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
  476. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  477. string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  478. RedisDbconn.Instance.GetLock("MachineApply:" + UserId);
  479. MachineApply query = maindb.MachineApply.Add(new MachineApply()
  480. {
  481. // Id = MachineApplyId,
  482. CreateDate = DateTime.Now,
  483. ApplyNo = ApplyNo, //申请单号
  484. UserId = UserId, //创客
  485. BrandId = BrandId, //品牌
  486. ProductName = protype.Name, //产品名称
  487. Areas = address.Areas, //收货所在地区
  488. Address = address.Address, //收货详细地址
  489. RealName = address.RealName, //收件人姓名
  490. Mobile = address.Mobile, //收件人手机号
  491. ApplyDeviceName = protype.Name, //申请机具名称
  492. ApplyDeviceNum = MachineCount, //申请机具数量
  493. ApplyTime = DateTime.Now, //申请时间
  494. DeliveryType = DeliveryType, //提货类型
  495. SwapSnExpand = SwapSnExpand, //兑换机器SN来源
  496. OrderExpand = SendSn,
  497. StoreId = store.Id,
  498. StoreUserId = store.UserId,
  499. StoreAreas = store.Areas,
  500. StoreAddress = store.Address,
  501. StoreManager = manager.RealName,
  502. StoreManagerMobile = store.ManageMobile,
  503. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  504. OperateId = OpId,
  505. }).Entity;
  506. maindb.SaveChanges();
  507. function.WriteLog("in", "机具申请-确认申请-V2");
  508. function.WriteLog("applyid:" + query.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  509. RedisDbconn.Instance.ReleaseLock("MachineApply:" + UserId);
  510. Products product = ProductsDbconn.Instance.Get(protype.QueryCount) ?? new Products();
  511. Orders order = maindb.Orders.Add(new Orders()
  512. {
  513. OrderNo = OrderNo,
  514. RealName = address.RealName,
  515. Mobile = address.Mobile,
  516. Areas = address.Areas,
  517. Address = address.Address,
  518. StoreContact = manager.RealName,
  519. StoreContactMobile = store.ManageMobile,
  520. StoreUserId = store.UserId,
  521. StoreType = store.StoreType,
  522. CreateDate = DateTime.Now, //创建时间
  523. UserId = UserId, //创客
  524. StoreId = StoreId, //仓库
  525. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  526. DeliveryType = DeliveryType, //提货类型
  527. BuyCount = MachineCount,
  528. TotalPrice = 0, //订单总额
  529. Remark = Remark, //订单备注
  530. Status = 1,
  531. PayStatus = 1,
  532. PayDate = DateTime.Now,
  533. QueryCount = 1, //申请循环机标记
  534. Sort = query.Id,
  535. OpId = OpId,
  536. }).Entity;
  537. maindb.SaveChanges();
  538. function.WriteLog("in2", "机具申请-确认申请-V2");
  539. function.WriteLog("orderid:" + order.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  540. MachineApply edit = maindb.MachineApply.FirstOrDefault(m => m.Id == query.Id);
  541. if (edit != null)
  542. {
  543. edit.QueryCount = order.Id;
  544. maindb.SaveChanges();
  545. }
  546. OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
  547. {
  548. CreateDate = DateTime.Now, //创建时间
  549. OrderId = order.Id,
  550. ProductId = product.Id,
  551. ProductCount = MachineCount,
  552. ProductName = product.ProductName,
  553. ProductPhoto = product.ListPicPath,
  554. ProductPrice = 0,
  555. TotalPrice = 0,
  556. ProductCode = product.ProductCode,
  557. UserId = UserId,
  558. StoreId = StoreId,
  559. }).Entity;
  560. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  561. {
  562. OrderNo = OrderNo,
  563. OrderIds = order.Id.ToString(),
  564. }).Entity;
  565. maindb.SaveChanges();
  566. function.WriteLog("2", "首页-客小爽产品-机具管理-机具申请-确认申请");
  567. function.WriteLog("ok\r\n\r\n", "首页-客小爽产品-机具管理-机具申请-确认申请");
  568. if (IsPre == 1)
  569. {
  570. string reqdata = "{";
  571. reqdata += "\"OrderId\":\"" + order.Id + "\",";
  572. reqdata += "\"StoreId\":\"" + StoreId + "\",";
  573. reqdata += "\"ToUserId\":\"" + UserId + "\",";
  574. reqdata += "\"SnIds\":\"" + SnIdString.TrimEnd(',') + "\"";
  575. reqdata += "}";
  576. new StoreStockChangeController(_accessor, _logger, _setting).TransferOneDo(reqdata);
  577. }
  578. RedisDbconn.Instance.Set("ConfirmApply2:" + key, "1", 3600 * 24);
  579. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  580. }
  581. return new AppResultJson() { Status = "-1", Info = "请选择机具", Data = Obj };
  582. }
  583. else
  584. {
  585. if (!string.IsNullOrEmpty(SendSn))
  586. {
  587. int OpId = 0;
  588. string SwapSnExpand = "";
  589. string[] SnIds = SendSn.Split(',');
  590. foreach (string SnId in SnIds)
  591. {
  592. int SnIdNum = int.Parse(SnId);
  593. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.Id == SnIdNum);
  594. if (coupon != null)
  595. {
  596. if (!SwapSnExpand.Contains(coupon.ExchangeCode))
  597. {
  598. coupon.IsLock = 1;
  599. coupon.IsUse = 1;
  600. coupon.UseDate = DateTime.Now;
  601. SwapSnExpand += coupon.ExchangeCode + "\n";
  602. MachineCount += 1;
  603. OpId = coupon.OpId;
  604. }
  605. }
  606. }
  607. if (string.IsNullOrEmpty(SwapSnExpand))
  608. {
  609. if (IsPre == 1)
  610. {
  611. BackPre(data, UserId);
  612. }
  613. return new AppResultJson() { Status = "-1", Info = "请选择兑换码" };
  614. }
  615. function.WriteLog("Kind:" + Kind, "机具申请-确认申请-V2");
  616. string ApplyNo = "BA" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  617. int BrandId = int.Parse(function.CheckInt(ProductType));
  618. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  619. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  620. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  621. StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
  622. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  623. string OrderNo = "C" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  624. bool check = maindb.MachineApply.Any(m => m.SwapSnExpand == SwapSnExpand);
  625. if (check)
  626. {
  627. if (IsPre == 1)
  628. {
  629. BackPre(data, UserId);
  630. }
  631. return new AppResultJson() { Status = "-1", Info = "申请已提交,请勿重复申请" };
  632. }
  633. RedisDbconn.Instance.GetLock("MachineApply:" + UserId);
  634. MachineApply query = maindb.MachineApply.Add(new MachineApply()
  635. {
  636. // Id = MachineApplyId,
  637. CreateDate = DateTime.Now,
  638. ApplyNo = ApplyNo, //申请单号
  639. UserId = UserId, //创客
  640. BrandId = BrandId, //品牌
  641. ProductName = protype.Name, //产品名称
  642. Areas = address.Areas, //收货所在地区
  643. Address = address.Address, //收货详细地址
  644. RealName = address.RealName, //收件人姓名
  645. Mobile = address.Mobile, //收件人手机号
  646. ApplyDeviceName = protype.Name, //申请机具名称
  647. ApplyDeviceNum = MachineCount, //申请机具数量
  648. ApplyTime = DateTime.Now, //申请时间
  649. DeliveryType = DeliveryType, //提货类型
  650. SwapSnExpand = SwapSnExpand, //兑换机器SN来源
  651. OrderExpand = SendSn,
  652. StoreId = store.Id,
  653. StoreUserId = store.UserId,
  654. StoreAreas = store.Areas,
  655. StoreAddress = store.Address,
  656. StoreManager = manager.RealName,
  657. StoreManagerMobile = store.ManageMobile,
  658. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  659. Sort = Kind, // 申请类型,1-机具SN,2-200兑换码,3-300券
  660. OperateId = OpId,
  661. }).Entity;
  662. maindb.SaveChanges();
  663. RedisDbconn.Instance.ReleaseLock("MachineApply:" + UserId);
  664. function.WriteLog("in", "机具申请-确认申请-V2");
  665. Products product = ProductsDbconn.Instance.Get(protype.Sort) ?? new Products();
  666. Orders order = maindb.Orders.Add(new Orders()
  667. {
  668. OrderNo = OrderNo,
  669. RealName = address.RealName,
  670. Mobile = address.Mobile,
  671. Areas = address.Areas,
  672. Address = address.Address,
  673. StoreContact = manager.RealName,
  674. StoreContactMobile = store.ManageMobile,
  675. StoreUserId = store.UserId,
  676. StoreType = store.StoreType,
  677. CreateDate = DateTime.Now, //创建时间
  678. UserId = UserId, //创客
  679. StoreId = StoreId, //仓库
  680. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  681. DeliveryType = DeliveryType, //提货类型
  682. BuyCount = MachineCount,
  683. TotalPrice = 0, //订单总额
  684. Remark = Remark, //订单备注
  685. Status = 1,
  686. PayStatus = 1,
  687. PayDate = DateTime.Now,
  688. // QueryCount = 1, //申请循环机标记
  689. Sort = query.Id,
  690. OpId = OpId,
  691. }).Entity;
  692. maindb.SaveChanges();
  693. function.WriteLog("in2", "机具申请-确认申请-V2");
  694. MachineApply edit = maindb.MachineApply.FirstOrDefault(m => m.Id == query.Id);
  695. if (edit != null)
  696. {
  697. edit.QueryCount = order.Id;
  698. maindb.SaveChanges();
  699. }
  700. OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
  701. {
  702. CreateDate = DateTime.Now, //创建时间
  703. OrderId = order.Id,
  704. ProductId = product.Id,
  705. ProductCount = MachineCount,
  706. ProductName = product.ProductName,
  707. ProductPhoto = product.ListPicPath,
  708. ProductPrice = 0,
  709. TotalPrice = 0,
  710. ProductCode = product.ProductCode,
  711. UserId = UserId,
  712. StoreId = StoreId,
  713. }).Entity;
  714. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  715. {
  716. OrderNo = OrderNo,
  717. OrderIds = order.Id.ToString(),
  718. }).Entity;
  719. maindb.SaveChanges();
  720. if (IsPre == 1)
  721. {
  722. string reqdata = "{";
  723. reqdata += "\"OrderId\":\"" + order.Id + "\",";
  724. reqdata += "\"StoreId\":\"" + StoreId + "\",";
  725. reqdata += "\"ToUserId\":\"" + UserId + "\",";
  726. reqdata += "\"SnIds\":\"" + SnIdString.TrimEnd(',') + "\"";
  727. reqdata += "}";
  728. new StoreStockChangeController(_accessor, _logger, _setting).TransferOneDo(reqdata);
  729. }
  730. RedisDbconn.Instance.Set("ConfirmApply2:" + key, "1", 3600 * 24);
  731. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  732. }
  733. return new AppResultJson() { Status = "-1", Info = "请选择兑换码", Data = Obj };
  734. }
  735. }
  736. private void BackPre(JsonData data, int UserId)
  737. {
  738. string SnIds = data["SnIds"].ToString(); //发货SN
  739. if (string.IsNullOrEmpty(SnIds))
  740. {
  741. return;
  742. }
  743. string[] SnIdList = SnIds.Split(',');
  744. foreach (string SnId in SnIdList)
  745. {
  746. int Id = int.Parse(SnId);
  747. PreSendStockDetail pre = maindb.PreSendStockDetail.FirstOrDefault(m => m.Id == Id && m.ToUserId == UserId);
  748. if (pre != null)
  749. {
  750. pre.ApplyFlag = 0;
  751. pre.ApplyDate = null;
  752. maindb.SaveChanges();
  753. }
  754. }
  755. SmallStoreHouse house = maindb.SmallStoreHouse.FirstOrDefault(m => m.UserId == UserId);
  756. if (house != null)
  757. {
  758. house.TotalNum += SnIdList.Length;
  759. house.LaveNum -= SnIdList.Length;
  760. maindb.SaveChanges();
  761. string text = string.Format("首页-客小爽产品-机具管理-机具申请-确认申请-V2-退回,UserId: '" + UserId + "',LaveNum:'" + house.LaveNum + "',TotalNum:'" + house.TotalNum + "',ChangeCount:'" + SnIdList.Length + "',Time'" + DateTime.Now + "'");
  762. function.WriteLog(text, "smallstorehouse");//小分仓记录日志
  763. }
  764. }
  765. #endregion
  766. #region 超时预发机押金申请循环后退还
  767. private string ReturnSmallStoreDeposit(int UserId, string[] SnIdList)
  768. {
  769. foreach (string SnId in SnIdList)
  770. {
  771. var amount = 0;
  772. int Id = int.Parse(SnId);
  773. PreSendStockDetail pre = maindb.PreSendStockDetail.FirstOrDefault(m => m.Id == Id && m.ToUserId == UserId);
  774. if (pre != null)
  775. {
  776. if (pre.CreateDate <= DateTime.Now.AddDays(-30))
  777. {
  778. //超时预发机押金申请循环后退还
  779. var brandInfo = maindb.KqProducts.FirstOrDefault(m => m.Id == pre.BrandId);
  780. if (brandInfo.Name.Contains("电签"))
  781. {
  782. amount = 200;
  783. }
  784. if (brandInfo.Name.Contains("大POS"))
  785. {
  786. amount = 300;
  787. }
  788. var userAccount = maindb.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  789. if (userAccount.Id > 0 && userAccount.SmallStoreDeposit >= amount)
  790. {
  791. userAccount.SmallStoreDeposit -= amount;//扣减小分仓押金
  792. var add = maindb.UserAccountRecord.Add(new UserAccountRecord()
  793. {
  794. CreateDate = DateTime.Now,
  795. Remark = "小分仓押金退还",
  796. ChangeType = 66,
  797. BeforeBalanceAmount = userAccount.SmallStoreDeposit + amount, //变更前小分仓押金
  798. AfterBalanceAmount = userAccount.SmallStoreDeposit, //变更后小分仓押金
  799. ChangeAmount = amount,//变动金额
  800. UserId = userAccount.Id,
  801. }).Entity;
  802. maindb.SaveChanges();
  803. }
  804. }
  805. }
  806. }
  807. return "success";
  808. }
  809. #endregion
  810. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  811. /// <summary>
  812. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  813. /// </summary>
  814. /// <param name="value">请求的参数(json字符串)</param>
  815. /// <param name="signField">要签名的字段</param>
  816. /// <returns></returns>
  817. private string CheckSign(string value, string[] signField)
  818. {
  819. JsonData json = JsonMapper.ToObject(value);
  820. Dictionary<string, string> dic = new Dictionary<string, string>();
  821. for (int i = 0; i < signField.Length; i++)
  822. {
  823. dic.Add(signField[i], json[signField[i]].ToString());
  824. }
  825. string sign = json["sign"].ToString(); //客户端签名字符串
  826. return new Sign().sign(dic, sign);
  827. }
  828. #endregion
  829. }
  830. }