MachineApplyController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/[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. foreach (MachineApply subdata in query)
  49. {
  50. Dictionary<string, object> curData = new Dictionary<string, object>();
  51. curData.Add("ApplyNo", subdata.ApplyNo); //申请单号
  52. curData.Add("Areas", function.CheckNull(subdata.Areas).Replace(",", "")); //收货所在地区
  53. curData.Add("Address", string.IsNullOrEmpty(subdata.Address) ? "上门自提" : subdata.Address); //收货详细地址
  54. curData.Add("ApplyDeviceNum", subdata.ApplyDeviceNum); //申请机具数量
  55. curData.Add("Id", subdata.Id); //Id
  56. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  57. curData.Add("Status", RelationClass.GetMachineApplyStatusInfo(subdata.Status)); //Status
  58. dataList.Add(curData);
  59. }
  60. return dataList;
  61. }
  62. #endregion
  63. #region 首页-客小爽产品-机具管理-机具申请-申请记录详情
  64. [Authorize]
  65. public JsonResult ApplyDetail(string value)
  66. {
  67. value = DesDecrypt(value);
  68. JsonData data = JsonMapper.ToObject(value);
  69. Dictionary<string, object> Obj = ApplyDetailDo(value);
  70. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  71. }
  72. public Dictionary<string, object> ApplyDetailDo(string value)
  73. {
  74. JsonData data = JsonMapper.ToObject(value);
  75. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  76. Dictionary<string, object> Obj = new Dictionary<string, object>();
  77. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  78. MachineApply query = MachineApplyDbconn.Instance.Get(Id) ?? new MachineApply();
  79. StoreHouse store = StoreHouseDbconn.Instance.Get(query.StoreId) ?? new StoreHouse();
  80. Obj.Add("ApplyNo", query.ApplyNo); //申请单号
  81. Obj.Add("ProductName", query.ProductName); //产品名称
  82. Obj.Add("Areas", function.CheckNull(query.Areas).Replace(",", "")); //收货所在地区
  83. Obj.Add("Address", string.IsNullOrEmpty(query.Address) ? "上门自提" : query.Address); //收货详细地址
  84. Obj.Add("RealName", query.RealName); //收件人姓名
  85. Obj.Add("Mobile", query.Mobile); //收件人手机号
  86. Obj.Add("ApplyDeviceNum", query.ApplyDeviceNum); //申请机具数量
  87. Obj.Add("DeliveryType", query.DeliveryType); //提货类型
  88. if (store.UserId == 1)
  89. {
  90. Obj.Add("StoreManagerMobile", "19141324516"); //仓库联系人手机号
  91. }
  92. else
  93. {
  94. Obj.Add("StoreManagerMobile", query.StoreManagerMobile); //仓库联系人手机号
  95. }
  96. Obj.Add("StoreName", store.StoreName); //提货仓库
  97. Obj.Add("Status", RelationClass.GetMachineApplyStatusInfo(query.Status)); //状态
  98. Obj.Add("CreateDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
  99. string SwapSnExpand = query.SwapSnExpand;
  100. int Kind = 0;
  101. if (SwapSnExpand.StartsWith("02") || SwapSnExpand.StartsWith("03")) Kind = 2;
  102. else Kind = 1;
  103. List<string> list = new List<string>();
  104. if (!string.IsNullOrEmpty(SwapSnExpand))
  105. {
  106. list = SwapSnExpand.Split('\n').ToList();
  107. }
  108. Obj.Add("Kind", Kind); //申请类别:1-机具,2-兑换券
  109. Obj.Add("SnList", list); //机具SN/兑换码列表
  110. return Obj;
  111. }
  112. #endregion
  113. #region 首页-客小爽产品-机具管理-机具申请-确认申请
  114. [Authorize]
  115. public JsonResult ConfirmApply(string value)
  116. {
  117. value = DesDecrypt(value);
  118. JsonData data = JsonMapper.ToObject(value);
  119. AppResultJson result = ConfirmApplyDo(value);
  120. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  121. }
  122. public AppResultJson ConfirmApplyDo(string value)
  123. {
  124. function.WriteLog("\r\n\r\n" + DateTime.Now.ToString(), "机具申请-确认申请-V2");
  125. function.WriteLog(value, "机具申请-确认申请-V2");
  126. JsonData data = JsonMapper.ToObject(value);
  127. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  128. int Kind = int.Parse(function.CheckInt(data["Kind"].ToString())); //申请类型,1-机具SN,2-200兑换码,3-300券
  129. string SendSn = data["SendSn"].ToString(); //发货SN
  130. string[] Sns = SendSn.Split(',');
  131. foreach (var item in Sns)
  132. {
  133. string checkOp = RedisDbconn.Instance.Get<string>("ConfirmApply:" + UserId + ":" + Kind + ":" + item);
  134. if (!string.IsNullOrEmpty(checkOp))
  135. {
  136. return new AppResultJson() { Status = "-1", Info = "申请已提交,请勿重复提交" };
  137. }
  138. RedisDbconn.Instance.Set("ConfirmApply:" + UserId + ":" + Kind + ":" + item, "1");
  139. RedisDbconn.Instance.SetExpire("ConfirmApply:" + UserId + ":" + Kind + ":" + item, 30);
  140. }
  141. string ProductType = data["ProductType"].ToString(); //产品类型
  142. int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库
  143. var brand = maindb.KqProducts.ToList();
  144. string SnIdString = "";
  145. function.WriteLog("SnIdString:" + SnIdString, "机具申请-确认申请-V2");
  146. int DeliveryType = int.Parse(function.CheckInt(data["DeliveryType"].ToString())); //提货类型
  147. string Remark = data["Remark"].ToString(); //订单备注
  148. int AddressId = int.Parse(function.CheckInt(data["AddressId"].ToString())); //收货地址Id
  149. if (StoreId == 0)
  150. {
  151. return new AppResultJson() { Status = "-1", Info = "请选择仓库" };
  152. }
  153. Dictionary<string, object> Obj = new Dictionary<string, object>();
  154. int MachineCount = 0;
  155. if (Kind == 1)
  156. {
  157. if (!string.IsNullOrEmpty(SendSn))
  158. {
  159. int OpId = 0;
  160. string SwapSnExpand = "";
  161. string[] SnIds = SendSn.Split(',');
  162. foreach (string SnId in SnIds)
  163. {
  164. int SnIdNum = int.Parse(SnId);
  165. PosMachinesTwo machine = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.Id == SnIdNum);
  166. if (machine != null)
  167. {
  168. bool check = maindb.MachineApply.Any(m => m.SwapSnExpand.Contains(machine.PosSn) && m.Status > -1);
  169. if (check)
  170. {
  171. return new AppResultJson() { Status = "-1", Info = "机具" + machine.PosSn + "已申请,请勿重复申请" };
  172. }
  173. }
  174. }
  175. foreach (string SnId in SnIds)
  176. {
  177. int SnIdNum = int.Parse(SnId);
  178. PosMachinesTwo machine = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.Id == SnIdNum);
  179. if (machine != null)
  180. {
  181. if (!SwapSnExpand.Contains(machine.PosSn))
  182. {
  183. machine.IsPurchase = 1;
  184. maindb.SaveChanges();
  185. SwapSnExpand += machine.PosSn + ":" + RelationClass.GetPosSnTypeInfo(machine.PosSnType) + "\n";
  186. MachineCount += 1;
  187. OpId = machine.OpId;
  188. }
  189. }
  190. }
  191. if (string.IsNullOrEmpty(SwapSnExpand))
  192. {
  193. return new AppResultJson() { Status = "-1", Info = "请选择机具" };
  194. }
  195. function.WriteLog("Kind:" + Kind, "机具申请-确认申请-V2");
  196. string ApplyNo = "BA" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  197. int BrandId = int.Parse(function.CheckInt(ProductType));
  198. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  199. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  200. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  201. StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
  202. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  203. string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  204. RedisDbconn.Instance.GetLock("MachineApply:" + UserId);
  205. MachineApply query = maindb.MachineApply.Add(new MachineApply()
  206. {
  207. CreateDate = DateTime.Now,
  208. ApplyNo = ApplyNo, //申请单号
  209. UserId = UserId, //创客
  210. BrandId = BrandId, //品牌
  211. ProductName = protype.Name, //产品名称
  212. Areas = address.Areas, //收货所在地区
  213. Address = address.Address, //收货详细地址
  214. RealName = address.RealName, //收件人姓名
  215. Mobile = address.Mobile, //收件人手机号
  216. ApplyDeviceName = protype.Name, //申请机具名称
  217. ApplyDeviceNum = MachineCount, //申请机具数量
  218. ApplyTime = DateTime.Now, //申请时间
  219. DeliveryType = DeliveryType, //提货类型
  220. SwapSnExpand = SwapSnExpand, //兑换机器SN来源
  221. OrderExpand = SendSn,
  222. StoreId = store.Id,
  223. StoreUserId = store.UserId,
  224. StoreAreas = store.Areas,
  225. StoreAddress = store.Address,
  226. StoreManager = manager.RealName,
  227. StoreManagerMobile = store.ManageMobile,
  228. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  229. OperateId = OpId,
  230. }).Entity;
  231. maindb.SaveChanges();
  232. function.WriteLog("in", "机具申请-确认申请-V2");
  233. function.WriteLog("applyid:" + query.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  234. RedisDbconn.Instance.ReleaseLock("MachineApply:" + UserId);
  235. Products product = ProductsDbconn.Instance.Get(protype.QueryCount) ?? new Products();
  236. Orders order = maindb.Orders.Add(new Orders()
  237. {
  238. OrderNo = OrderNo,
  239. RealName = address.RealName,
  240. Mobile = address.Mobile,
  241. Areas = address.Areas,
  242. Address = address.Address,
  243. StoreContact = manager.RealName,
  244. StoreContactMobile = store.ManageMobile,
  245. StoreUserId = store.UserId,
  246. StoreType = store.StoreType,
  247. CreateDate = DateTime.Now, //创建时间
  248. UserId = UserId, //创客
  249. StoreId = StoreId, //仓库
  250. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  251. DeliveryType = DeliveryType, //提货类型
  252. BuyCount = MachineCount,
  253. TotalPrice = 0, //订单总额
  254. Remark = Remark, //订单备注
  255. Status = 1,
  256. PayStatus = 1,
  257. PayDate = DateTime.Now,
  258. QueryCount = 1, //申请循环机标记
  259. Sort = query.Id,
  260. OpId = OpId,
  261. }).Entity;
  262. maindb.SaveChanges();
  263. function.WriteLog("in2", "机具申请-确认申请-V2");
  264. function.WriteLog("orderid:" + order.Id, "首页-客小爽产品-机具管理-机具申请-确认申请");
  265. MachineApply edit = maindb.MachineApply.FirstOrDefault(m => m.Id == query.Id);
  266. if (edit != null)
  267. {
  268. edit.QueryCount = order.Id;
  269. maindb.SaveChanges();
  270. }
  271. OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
  272. {
  273. CreateDate = DateTime.Now, //创建时间
  274. OrderId = order.Id,
  275. ProductId = product.Id,
  276. ProductCount = MachineCount,
  277. ProductName = product.ProductName,
  278. ProductPhoto = product.ListPicPath,
  279. ProductPrice = 0,
  280. TotalPrice = 0,
  281. ProductCode = product.ProductCode,
  282. UserId = UserId,
  283. StoreId = StoreId,
  284. }).Entity;
  285. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  286. {
  287. OrderNo = OrderNo,
  288. OrderIds = order.Id.ToString(),
  289. }).Entity;
  290. maindb.SaveChanges();
  291. function.WriteLog("2", "首页-客小爽产品-机具管理-机具申请-确认申请");
  292. function.WriteLog("ok\r\n\r\n", "首页-客小爽产品-机具管理-机具申请-确认申请");
  293. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  294. }
  295. return new AppResultJson() { Status = "-1", Info = "请选择机具", Data = Obj };
  296. }
  297. else
  298. {
  299. if (!string.IsNullOrEmpty(SendSn))
  300. {
  301. int OpId = 0;
  302. string SwapSnExpand = "";
  303. string[] SnIds = SendSn.Split(',');
  304. foreach (string SnId in SnIds)
  305. {
  306. int SnIdNum = int.Parse(SnId);
  307. PosCoupons coupon = maindb.PosCoupons.FirstOrDefault(m => m.Id == SnIdNum);
  308. if (coupon != null)
  309. {
  310. if (!SwapSnExpand.Contains(coupon.ExchangeCode))
  311. {
  312. coupon.IsLock = 1;
  313. coupon.IsUse = 1;
  314. coupon.UseDate = DateTime.Now;
  315. SwapSnExpand += coupon.ExchangeCode + "\n";
  316. MachineCount += 1;
  317. OpId = coupon.OpId;
  318. }
  319. }
  320. }
  321. if (string.IsNullOrEmpty(SwapSnExpand))
  322. {
  323. return new AppResultJson() { Status = "-1", Info = "请选择兑换码" };
  324. }
  325. function.WriteLog("Kind:" + Kind, "机具申请-确认申请-V2");
  326. string ApplyNo = "BA" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  327. int BrandId = int.Parse(function.CheckInt(ProductType));
  328. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  329. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  330. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  331. StoreHouse store = StoreHouseDbconn.Instance.Get(StoreId) ?? new StoreHouse();
  332. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  333. string OrderNo = "C" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  334. bool check = maindb.MachineApply.Any(m => m.SwapSnExpand == SwapSnExpand && m.Status > -1);
  335. if (check)
  336. {
  337. return new AppResultJson() { Status = "-1", Info = "申请已提交,请勿重复申请" };
  338. }
  339. RedisDbconn.Instance.GetLock("MachineApply:" + UserId);
  340. MachineApply query = maindb.MachineApply.Add(new MachineApply()
  341. {
  342. CreateDate = DateTime.Now,
  343. ApplyNo = ApplyNo, //申请单号
  344. UserId = UserId, //创客
  345. BrandId = BrandId, //品牌
  346. ProductName = protype.Name, //产品名称
  347. Areas = address.Areas, //收货所在地区
  348. Address = address.Address, //收货详细地址
  349. RealName = address.RealName, //收件人姓名
  350. Mobile = address.Mobile, //收件人手机号
  351. ApplyDeviceName = protype.Name, //申请机具名称
  352. ApplyDeviceNum = MachineCount, //申请机具数量
  353. ApplyTime = DateTime.Now, //申请时间
  354. DeliveryType = DeliveryType, //提货类型
  355. SwapSnExpand = SwapSnExpand, //兑换机器SN来源
  356. OrderExpand = SendSn,
  357. StoreId = store.Id,
  358. StoreUserId = store.UserId,
  359. StoreAreas = store.Areas,
  360. StoreAddress = store.Address,
  361. StoreManager = manager.RealName,
  362. StoreManagerMobile = store.ManageMobile,
  363. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  364. Sort = Kind, // 申请类型,1-机具SN,2-200兑换码,3-300券
  365. OperateId = OpId,
  366. }).Entity;
  367. maindb.SaveChanges();
  368. RedisDbconn.Instance.ReleaseLock("MachineApply:" + UserId);
  369. function.WriteLog("in", "机具申请-确认申请-V2");
  370. Products product = ProductsDbconn.Instance.Get(protype.Sort) ?? new Products();
  371. Orders order = maindb.Orders.Add(new Orders()
  372. {
  373. OrderNo = OrderNo,
  374. RealName = address.RealName,
  375. Mobile = address.Mobile,
  376. Areas = address.Areas,
  377. Address = address.Address,
  378. StoreContact = manager.RealName,
  379. StoreContactMobile = store.ManageMobile,
  380. StoreUserId = store.UserId,
  381. StoreType = store.StoreType,
  382. CreateDate = DateTime.Now, //创建时间
  383. UserId = UserId, //创客
  384. StoreId = StoreId, //仓库
  385. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  386. DeliveryType = DeliveryType, //提货类型
  387. BuyCount = MachineCount,
  388. TotalPrice = 0, //订单总额
  389. Remark = Remark, //订单备注
  390. Status = 1,
  391. PayStatus = 1,
  392. PayDate = DateTime.Now,
  393. Sort = query.Id,
  394. OpId = OpId,
  395. }).Entity;
  396. maindb.SaveChanges();
  397. function.WriteLog("in2", "机具申请-确认申请-V2");
  398. MachineApply edit = maindb.MachineApply.FirstOrDefault(m => m.Id == query.Id);
  399. if (edit != null)
  400. {
  401. edit.QueryCount = order.Id;
  402. maindb.SaveChanges();
  403. }
  404. OrderProduct pro = maindb.OrderProduct.Add(new OrderProduct()
  405. {
  406. CreateDate = DateTime.Now, //创建时间
  407. OrderId = order.Id,
  408. ProductId = product.Id,
  409. ProductCount = MachineCount,
  410. ProductName = product.ProductName,
  411. ProductPhoto = product.ListPicPath,
  412. ProductPrice = 0,
  413. TotalPrice = 0,
  414. ProductCode = product.ProductCode,
  415. UserId = UserId,
  416. StoreId = StoreId,
  417. }).Entity;
  418. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  419. {
  420. OrderNo = OrderNo,
  421. OrderIds = order.Id.ToString(),
  422. }).Entity;
  423. maindb.SaveChanges();
  424. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  425. }
  426. return new AppResultJson() { Status = "-1", Info = "请选择兑换码", Data = Obj };
  427. }
  428. }
  429. #endregion
  430. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  431. /// <summary>
  432. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  433. /// </summary>
  434. /// <param name="value">请求的参数(json字符串)</param>
  435. /// <param name="signField">要签名的字段</param>
  436. /// <returns></returns>
  437. private string CheckSign(string value, string[] signField)
  438. {
  439. JsonData json = JsonMapper.ToObject(value);
  440. Dictionary<string, string> dic = new Dictionary<string, string>();
  441. for (int i = 0; i < signField.Length; i++)
  442. {
  443. dic.Add(signField[i], json[signField[i]].ToString());
  444. }
  445. string sign = json["sign"].ToString(); //客户端签名字符串
  446. return new Sign().sign(dic, sign);
  447. }
  448. #endregion
  449. }
  450. }