PosCouponRecordController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 PosCouponRecordController : BaseController
  18. {
  19. public PosCouponRecordController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-首页-客小爽产品-码牌管理-划拨
  23. [Authorize]
  24. public JsonResult Add(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. AppResultJson result = AddDo(value);
  29. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  30. }
  31. int[] ToUserIds = { 1, 76922 };
  32. public AppResultJson AddDo(string value)
  33. {
  34. JsonData data = JsonMapper.ToObject(value);
  35. int ToUserId = int.Parse(function.CheckInt(data["ToUserId"].ToString())); //目标创客
  36. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
  37. string SnIds = data["SnIds"].ToString(); //码牌列表返回的Id集合
  38. Dictionary<string, object> Obj = new Dictionary<string, object>();
  39. bool checkUser = kxsdb.Users.Any(m => m.Id == ToUserId);
  40. if (!checkUser)
  41. {
  42. return new AppResultJson() { Status = "-1", Info = "请输入正确的创客编号", Data = Obj };
  43. }
  44. KxsMainModels.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  45. KxsMainModels.Users touser = kxsdb.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new KxsMainModels.Users();
  46. if (!ToUserIds.Contains(UserId))
  47. {
  48. if (!function.CheckNull(touser.ParentNav).Contains("," + UserId + ",") || function.CheckNull(user.ParentNav).Contains("," + ToUserId + ","))
  49. {
  50. return new AppResultJson() { Status = "-1", Info = "划拨对象不在您的权限范围", Data = Obj };
  51. }
  52. }
  53. if (!string.IsNullOrEmpty(SnIds))
  54. {
  55. string ChangeRecordNo = "SC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  56. string[] SnIdList = SnIds.Split(',');
  57. foreach (string SnId in SnIdList)
  58. {
  59. int SnIdNum = int.Parse(SnId);
  60. PosMachinesTwo pos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Id == SnIdNum) ?? new PosMachinesTwo();
  61. if (pos.BuyUserId != UserId)
  62. {
  63. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已划拨,请勿重复划拨", Data = Obj };
  64. }
  65. if(pos.BindingState == 1)
  66. {
  67. return new AppResultJson() { Status = "-1", Info = pos.PosSn + "已绑定,无法划拨", Data = Obj };
  68. }
  69. PosCouponRecord query = maindb.PosCouponRecord.Add(new PosCouponRecord()
  70. {
  71. CreateDate = DateTime.Now,
  72. OrderNo = ChangeRecordNo,
  73. ToUserId = ToUserId,
  74. FromUserId = UserId,
  75. PosCouponId = SnIdNum,
  76. }).Entity;
  77. pos.BuyUserId = ToUserId;
  78. pos.UserId = ToUserId;
  79. }
  80. maindb.SaveChanges();
  81. int SnCount = SnIdList.Length;
  82. PosCouponOrders tostat = maindb.PosCouponOrders.Add(new PosCouponOrders()
  83. {
  84. CreateDate = DateTime.Now,
  85. ChangeCount = SnCount,
  86. OrderNo = ChangeRecordNo,
  87. ToUserId = ToUserId,
  88. FromUserId = UserId,
  89. }).Entity;
  90. PublicFunction.StatUserMachineData(UserId, 0, -SnCount);
  91. PublicFunction.StatUserMachineData(ToUserId, 0, SnCount);
  92. maindb.SaveChanges();
  93. }
  94. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  95. }
  96. #endregion
  97. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  98. /// <summary>
  99. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  100. /// </summary>
  101. /// <param name="value">请求的参数(json字符串)</param>
  102. /// <param name="signField">要签名的字段</param>
  103. /// <returns></returns>
  104. private string CheckSign(string value, string[] signField)
  105. {
  106. JsonData json = JsonMapper.ToObject(value);
  107. Dictionary<string, string> dic = new Dictionary<string, string>();
  108. for (int i = 0; i < signField.Length; i++)
  109. {
  110. dic.Add(signField[i], json[signField[i]].ToString());
  111. }
  112. string sign = json["sign"].ToString(); //客户端签名字符串
  113. return new Sign().sign(dic, sign);
  114. }
  115. #endregion
  116. }
  117. }