PosCouponRecordController.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.Models.Main;
  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 PosCouponRecordController : BaseController
  18. {
  19. public PosCouponRecordController(IHttpContextAccessor accessor) : base(accessor)
  20. {
  21. }
  22. #region 创客-首页-客小爽产品-码牌管理-划拨
  23. // [Authorize]
  24. public JsonResult Add(string value)
  25. {
  26. value = DesDecrypt(value);
  27. value = value.Replace("null", "\"\"");
  28. JsonData data = JsonMapper.ToObject(value);
  29. AppResultJson result = AddDo(value);
  30. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  31. }
  32. int[] ToUserIds = { 1, 76922 };
  33. public AppResultJson AddDo(string value)
  34. {
  35. JsonData data = JsonMapper.ToObject(value);
  36. int ToUserId = int.Parse(function.CheckInt(data["ToUserId"].ToString())); //目标创客
  37. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
  38. string SnIds = data["SnIds"].ToString(); //码牌列表返回的Id集合
  39. Dictionary<string, object> Obj = new Dictionary<string, object>();
  40. bool checkUser = kxsdb.Users.Any(m => m.Id == ToUserId);
  41. if (!checkUser)
  42. {
  43. return new AppResultJson() { Status = "-1", Info = "请输入正确的创客编号", Data = Obj };
  44. }
  45. Models.KxsMainModels.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == UserId) ?? new Models.KxsMainModels.Users();
  46. Models.KxsMainModels.Users touser = kxsdb.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Models.KxsMainModels.Users();
  47. if (!ToUserIds.Contains(UserId))
  48. {
  49. if (!function.CheckNull(touser.ParentNav).Contains("," + UserId + ",") || function.CheckNull(user.ParentNav).Contains("," + ToUserId + ","))
  50. {
  51. return new AppResultJson() { Status = "-1", Info = "划拨对象不在您的权限范围", Data = Obj };
  52. }
  53. }
  54. if (!string.IsNullOrEmpty(SnIds))
  55. {
  56. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  57. string[] SnIdList = SnIds.Split(',');
  58. foreach (string SnId in SnIdList)
  59. {
  60. int SnIdNum = int.Parse(SnId);
  61. Models.Main1.PosMachinesTwo pos = main1db.PosMachinesTwo.FirstOrDefault(m => m.Id == SnIdNum) ?? new Models.Main1.PosMachinesTwo();
  62. if (pos.BuyUserId != UserId)
  63. {
  64. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已划拨,请勿重复划拨", Data = Obj };
  65. }
  66. if (pos.BindingState == 1)
  67. {
  68. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已绑定,无法划拨", Data = Obj };
  69. }
  70. PosCouponRecord query = maindb.PosCouponRecord.Add(new PosCouponRecord()
  71. {
  72. CreateDate = DateTime.Now,
  73. OrderNo = ChangeRecordNo,
  74. ToUserId = ToUserId,
  75. FromUserId = UserId,
  76. PosCouponId = SnIdNum,
  77. ChangeKind = 1,
  78. }).Entity;
  79. pos.BuyUserId = ToUserId;
  80. pos.UserId = ToUserId;
  81. }
  82. maindb.SaveChanges();
  83. main1db.SaveChanges();
  84. int SnCount = SnIdList.Length;
  85. PosCouponOrders tostat = maindb.PosCouponOrders.Add(new PosCouponOrders()
  86. {
  87. CreateDate = DateTime.Now,
  88. ChangeCount = SnCount,
  89. OrderNo = ChangeRecordNo,
  90. ToUserId = ToUserId,
  91. FromUserId = UserId,
  92. ChangeKind = 1,
  93. }).Entity;
  94. PublicFunction.StatUserMachineData(UserId, 0, -SnCount);
  95. PublicFunction.StatUserMachineData(ToUserId, 0, SnCount);
  96. maindb.SaveChanges();
  97. }
  98. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  99. }
  100. #endregion
  101. #region 创客-首页-客小爽产品-音响管理-划拨
  102. // [Authorize]
  103. public JsonResult Add2(string value)
  104. {
  105. value = DesDecrypt(value);
  106. value = value.Replace("null", "\"\"");
  107. JsonData data = JsonMapper.ToObject(value);
  108. AppResultJson result = Add2Do(value);
  109. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  110. }
  111. public AppResultJson Add2Do(string value)
  112. {
  113. JsonData data = JsonMapper.ToObject(value);
  114. int ToUserId = int.Parse(function.CheckInt(data.getItem("ToUserId").ToString())); //目标创客
  115. int UserId = int.Parse(function.CheckInt(data.getItem("UserId").ToString())); //创客Id
  116. string SnIds = data.getItem("SnIds").ToString(); //码牌列表返回的Id集合
  117. Dictionary<string, object> Obj = new Dictionary<string, object>();
  118. bool checkUser = kxsdb.Users.Any(m => m.Id == ToUserId);
  119. if (!checkUser)
  120. {
  121. return new AppResultJson() { Status = "-1", Info = "请输入正确的创客编号", Data = Obj };
  122. }
  123. Models.KxsMainModels.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == UserId) ?? new Models.KxsMainModels.Users();
  124. Models.KxsMainModels.Users touser = kxsdb.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Models.KxsMainModels.Users();
  125. if (!ToUserIds.Contains(UserId))
  126. {
  127. if (!function.CheckNull(touser.ParentNav).Contains("," + UserId + ",") || function.CheckNull(user.ParentNav).Contains("," + ToUserId + ","))
  128. {
  129. return new AppResultJson() { Status = "-1", Info = "划拨对象不在您的权限范围", Data = Obj };
  130. }
  131. }
  132. if (!string.IsNullOrEmpty(SnIds))
  133. {
  134. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  135. string[] SnIdList = SnIds.Split(',');
  136. foreach (string SnId in SnIdList)
  137. {
  138. int SnIdNum = int.Parse(SnId);
  139. Models.Main1.PosMachines pos = main1db.PosMachines.FirstOrDefault(m => m.Id == SnIdNum) ?? new Models.Main1.PosMachines();
  140. if (pos.BuyUserId != UserId)
  141. {
  142. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已划拨,请勿重复划拨", Data = Obj };
  143. }
  144. if (pos.BindingState == 1)
  145. {
  146. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已绑定,无法划拨", Data = Obj };
  147. }
  148. PosCouponRecord query = maindb.PosCouponRecord.Add(new PosCouponRecord()
  149. {
  150. CreateDate = DateTime.Now,
  151. OrderNo = ChangeRecordNo,
  152. ToUserId = ToUserId,
  153. FromUserId = UserId,
  154. PosCouponId = SnIdNum,
  155. ChangeKind = 2,
  156. }).Entity;
  157. pos.BuyUserId = ToUserId;
  158. pos.UserId = ToUserId;
  159. }
  160. maindb.SaveChanges();
  161. main1db.SaveChanges();
  162. int SnCount = SnIdList.Length;
  163. PosCouponOrders tostat = maindb.PosCouponOrders.Add(new PosCouponOrders()
  164. {
  165. CreateDate = DateTime.Now,
  166. ChangeCount = SnCount,
  167. OrderNo = ChangeRecordNo,
  168. ToUserId = ToUserId,
  169. FromUserId = UserId,
  170. ChangeKind = 2,
  171. }).Entity;
  172. PublicFunction.StatUserMachineData(UserId, 1, -SnCount);
  173. PublicFunction.StatUserMachineData(ToUserId, 1, SnCount);
  174. maindb.SaveChanges();
  175. }
  176. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  177. }
  178. #endregion
  179. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  180. /// <summary>
  181. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  182. /// </summary>
  183. /// <param name="value">请求的参数(json字符串)</param>
  184. /// <param name="signField">要签名的字段</param>
  185. /// <returns></returns>
  186. private string CheckSign(string value, string[] signField)
  187. {
  188. JsonData json = JsonMapper.ToObject(value);
  189. Dictionary<string, string> dic = new Dictionary<string, string>();
  190. for (int i = 0; i < signField.Length; i++)
  191. {
  192. dic.Add(signField[i], json[signField[i]].ToString());
  193. }
  194. string sign = json["sign"].ToString(); //客户端签名字符串
  195. return new Sign().sign(dic, sign);
  196. }
  197. #endregion
  198. }
  199. }