PosMachinesController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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
  14. {
  15. [Area("Api")]
  16. [Route("Api/v1/[controller]/[action]")]
  17. public class PosMachinesController : BaseController
  18. {
  19. public PosMachinesController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 商户-二维码关联音箱
  23. [Authorize]
  24. public JsonResult List(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = ListDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> ListDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].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. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new MerchantInfo();
  39. List<PosMachines> query = RedisDbconn.Instance.GetList<PosMachines>("PosMachines:" + BindMerchantId, PageNum, PageSize);
  40. foreach (var subdata in query)
  41. {
  42. Dictionary<string, object> curData = new Dictionary<string, object>();
  43. curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  44. curData.Add("PosSn", subdata.PosSn); //SN编号
  45. curData.Add("Id", subdata.Id); //Id
  46. curData.Add("MerchantName", merchant.Name); //商户名称
  47. string CheckKey = "_" + subdata.Id;
  48. int BindCount = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId).Count(m => m.DataId.EndsWith(CheckKey));
  49. curData.Add("BindCount", BindCount); //关联数量
  50. dataList.Add(curData);
  51. }
  52. return dataList;
  53. }
  54. #endregion
  55. #region 商户-已绑音箱
  56. [Authorize]
  57. public JsonResult BindedMachines(string value)
  58. {
  59. value = DesDecrypt(value);
  60. JsonData data = JsonMapper.ToObject(value);
  61. List<Dictionary<string, object>> dataList = BindedMachinesDo(value);
  62. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  63. }
  64. public List<Dictionary<string, object>> BindedMachinesDo(string value)
  65. {
  66. JsonData data = JsonMapper.ToObject(value);
  67. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  68. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  69. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  70. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  71. MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new MerchantInfo();
  72. List<int> query = PosMachinesDbconn.Instance.GetList(BindMerchantId, PageNum, PageSize);
  73. foreach (int MachineId in query)
  74. {
  75. PosMachines subdata = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
  76. Dictionary<string, object> curData = new Dictionary<string, object>();
  77. curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd")); //绑定时间
  78. curData.Add("PosSn", subdata.PosSn); //SN编号
  79. curData.Add("Id", subdata.Id); //Id
  80. curData.Add("MerchantName", merchant.Name); //商户名称
  81. string CheckKey = "_" + subdata.Id;
  82. List<MachineForQrCode> QrCodes = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId);
  83. QrCodes = QrCodes.Where(m => m.DataId.EndsWith(CheckKey)).ToList();
  84. curData.Add("BindCount", QrCodes.Count); //关联数量
  85. List<Dictionary<string, object>> QrCodeList = new List<Dictionary<string, object>>();
  86. foreach (MachineForQrCode QrCode in QrCodes)
  87. {
  88. Dictionary<string, object> item = new Dictionary<string, object>();
  89. string DataId = QrCode.DataId;
  90. if (DataId.Contains("_"))
  91. {
  92. DataId = DataId.Split('_')[0];
  93. }
  94. item.Add("Id", DataId);
  95. item.Add("Type", 1);
  96. item.Add("BindDate", QrCode.BindDate == null ? "" : QrCode.BindDate.Value.ToString("yyyy-MM-dd"));
  97. item.Add("Sn", QrCode.SnNo);
  98. QrCodeList.Add(item);
  99. }
  100. curData.Add("QrCodeList", QrCodeList); //二维码列表
  101. dataList.Add(curData);
  102. }
  103. return dataList;
  104. }
  105. #endregion
  106. #region 商户-替换音箱码
  107. [Authorize]
  108. public JsonResult ReplaceMachines(string value)
  109. {
  110. value = DesDecrypt(value);
  111. JsonData data = JsonMapper.ToObject(value);
  112. List<Dictionary<string, object>> dataList = ReplaceMachinesDo(value);
  113. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  114. }
  115. public List<Dictionary<string, object>> ReplaceMachinesDo(string value)
  116. {
  117. JsonData data = JsonMapper.ToObject(value);
  118. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  119. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  120. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  121. string CheckKey = "_" + BindMerchantId;
  122. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  123. List<int> query = RedisDbconn.Instance.GetList<int>("MerchantQrCodeList:" + BindMerchantId, 1, 1000);
  124. if (PageNum == 1)
  125. {
  126. query = query.Take(PageSize).ToList();
  127. }
  128. else
  129. {
  130. int skipNum = PageSize * (PageNum - 1);
  131. query = query.Skip(skipNum).Take(PageSize).ToList();
  132. }
  133. foreach (int MerchantQrCodeId in query)
  134. {
  135. MerchantQrCode subdata = RedisDbconn.Instance.Get<MerchantQrCode>("MerchantQrCode:" + MerchantQrCodeId) ?? new MerchantQrCode();
  136. Dictionary<string, object> curData = new Dictionary<string, object>();
  137. curData.Add("BindingTime", subdata.UpdateDate == null ? "" : subdata.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  138. curData.Add("PosSn", subdata.SnNo); //SN编号
  139. curData.Add("Id", subdata.Id); //Id
  140. dataList.Add(curData);
  141. }
  142. return dataList;
  143. }
  144. #endregion
  145. #region 商户-确认关联音箱
  146. [Authorize]
  147. public JsonResult Add(string value)
  148. {
  149. value = DesDecrypt(value);
  150. JsonData data = JsonMapper.ToObject(value);
  151. AppResultJson result = AddDo(value);
  152. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  153. }
  154. public AppResultJson AddDo(string value)
  155. {
  156. JsonData data = JsonMapper.ToObject(value);
  157. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  158. string QrCodeId = data["QrCodeId"].ToString(); //二维码Id
  159. string MachineId = data["MachineId"].ToString(); //音箱Id
  160. Dictionary<string, object> Obj = new Dictionary<string, object>();
  161. MerchantQrCode qrcode = RedisDbconn.Instance.Get<MerchantQrCode>("MerchantQrCode:" + QrCodeId) ?? new MerchantQrCode();
  162. PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines();
  163. MachineForQrCode query = new MachineForQrCode()
  164. {
  165. DataId = QrCodeId + "_" + MachineId,
  166. MerchantId = BindMerchantId,
  167. BindDate = DateTime.Now,
  168. SnNo = qrcode.SnNo,
  169. MachineSnNo = machine.PosSn,
  170. };
  171. machine.BindingState = 1;
  172. qrcode.MachineId = int.Parse(MachineId);
  173. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  174. }
  175. #endregion
  176. #region 商户-绑定列表
  177. [Authorize]
  178. public JsonResult BindList(string value)
  179. {
  180. value = DesDecrypt(value);
  181. JsonData data = JsonMapper.ToObject(value);
  182. List<Dictionary<string, object>> dataList = BindListDo(value);
  183. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  184. }
  185. public List<Dictionary<string, object>> BindListDo(string value)
  186. {
  187. JsonData data = JsonMapper.ToObject(value);
  188. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  189. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  190. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  191. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  192. List<PosMachines> query = RedisDbconn.Instance.GetList<PosMachines>("PosMachines", PageNum, PageSize);
  193. if (!string.IsNullOrEmpty(data["BindMerchantId"].ToString()))
  194. {
  195. query = query.Where(m => m.BindMerchantId == BindMerchantId).ToList();
  196. }
  197. foreach (var subdata in query)
  198. {
  199. Dictionary<string, object> curData = new Dictionary<string, object>();
  200. curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  201. curData.Add("PosSn", subdata.PosSn); //SN编号
  202. curData.Add("MerchantName", ""); //商户名称
  203. curData.Add("MerchantLogo", ""); //商户Logo
  204. dataList.Add(curData);
  205. }
  206. return dataList;
  207. }
  208. #endregion
  209. #region 商户-绑定机具
  210. [Authorize]
  211. public JsonResult Bind(string value)
  212. {
  213. value = DesDecrypt(value);
  214. JsonData data = JsonMapper.ToObject(value);
  215. AppResultJson result = BindDo(value);
  216. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  217. }
  218. public AppResultJson BindDo(string value)
  219. {
  220. JsonData data = JsonMapper.ToObject(value);
  221. string PosSn = data["PosSn"].ToString(); //SN编号
  222. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  223. Dictionary<string, object> Obj = new Dictionary<string, object>();
  224. PosMachines query = new PosMachines();
  225. query = new PosMachines()
  226. {
  227. CreateDate = DateTime.Now, //创建时间
  228. UpdateDate = DateTime.Now, //修改时间
  229. PosSn = PosSn, //SN编号
  230. BindMerchantId = BindMerchantId, //绑定商户
  231. };
  232. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  233. }
  234. #endregion
  235. #region 商户-已绑音箱解除关联
  236. [Authorize]
  237. public JsonResult Remove(string value)
  238. {
  239. value = DesDecrypt(value);
  240. JsonData data = JsonMapper.ToObject(value);
  241. AppResultJson result = RemoveDo(value);
  242. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  243. }
  244. public AppResultJson RemoveDo(string value)
  245. {
  246. JsonData data = JsonMapper.ToObject(value);
  247. int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户
  248. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  249. Dictionary<string, object> Obj = new Dictionary<string, object>();
  250. string CheckKey = "_" + Id;
  251. var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == BindMerchantId && m.DataId.EndsWith(CheckKey)).ToList();
  252. foreach (var sub in list)
  253. {
  254. MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId);
  255. if (edit != null)
  256. {
  257. maindb.MachineForQrCode.Remove(edit);
  258. int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1]));
  259. PosMachines machine = maindb.PosMachines.FirstOrDefault(m => m.BindMerchantId == MachineId);
  260. if (machine != null)
  261. {
  262. machine.BindMerchantId = 0;
  263. }
  264. }
  265. }
  266. maindb.SaveChanges();
  267. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  268. }
  269. #endregion
  270. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  271. /// <summary>
  272. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  273. /// </summary>
  274. /// <param name="value">请求的参数(json字符串)</param>
  275. /// <param name="signField">要签名的字段</param>
  276. /// <returns></returns>
  277. private string CheckSign(string value, string[] signField)
  278. {
  279. JsonData json = JsonMapper.ToObject(value);
  280. Dictionary<string, string> dic = new Dictionary<string, string>();
  281. for (int i = 0; i < signField.Length; i++)
  282. {
  283. dic.Add(signField[i], json[signField[i]].ToString());
  284. }
  285. string sign = json["sign"].ToString(); //客户端签名字符串
  286. return new Sign().sign(dic, sign);
  287. }
  288. #endregion
  289. }
  290. }