StoreMachineApplyController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 StoreMachineApplyController : BaseController
  18. {
  19. public StoreMachineApplyController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-首页-仓库管理-申请机具-申请记录
  23. [Authorize]
  24. public JsonResult ApplyList(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = ApplyListDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> ApplyListDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
  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<StoreMachineApply> query = maindb.StoreMachineApply.Where(m => m.Status >= 0 && m.UserId == UserId);
  39. int TotalCount = query.Count();
  40. query = query.OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id);
  41. if (PageNum == 1)
  42. {
  43. query = query.Take(PageSize);
  44. }
  45. else
  46. {
  47. int skipNum = PageSize * (PageNum - 1);
  48. query = query.Skip(skipNum).Take(PageSize);
  49. }
  50. var mydata = query.ToList();
  51. foreach (var subdata in mydata)
  52. {
  53. Dictionary<string, object> curData = new Dictionary<string, object>();
  54. curData.Add("ApplyNum", subdata.ApplyNum); //申请台数
  55. curData.Add("Id", subdata.Id); //Id
  56. curData.Add("Status", subdata.Status); //Status(0 待配货 1 已发货 2 已驳回)
  57. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  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 Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  76. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
  77. Dictionary<string, object> Obj = new Dictionary<string, object>();
  78. StoreMachineApply query = new StoreMachineApply();
  79. query = maindb.StoreMachineApply.FirstOrDefault(m => m.Id == Id) ?? new StoreMachineApply();
  80. List<KqProducts> brandList = maindb.KqProducts.ToList();
  81. List<Dictionary<string, object>> brands = new List<Dictionary<string, object>>();
  82. foreach (var items in brandList)
  83. {
  84. List<StoreStockChange> mydata = maindb.StoreStockChange.Where(m => m.ToUserId == UserId && m.Sort == Id && m.BrandId == items.Id && m.Status >= 0).ToList();
  85. if (mydata.Count > 0)
  86. {
  87. Dictionary<string, object> brandInfo = new Dictionary<string, object>();
  88. KqProducts kqProducts = maindb.KqProducts.FirstOrDefault(m => m.Id == items.Id);
  89. brandInfo.Add("Id", items.Id);//产品类型Id
  90. brandInfo.Add("Name", items.Name);//产品类型名称
  91. List<Dictionary<string, object>> snList = new List<Dictionary<string, object>>();
  92. foreach (var subdata in mydata)
  93. {
  94. Dictionary<string, object> curData = new Dictionary<string, object>();
  95. curData.Add("Id", subdata.Id);//记录Id
  96. curData.Add("SnNo", subdata.SnNo); //机具SN
  97. snList.Add(curData);
  98. }
  99. brandInfo.Add("SnList", snList);
  100. brands.Add(brandInfo);
  101. }
  102. }
  103. // var StoreStockChange = maindb.StoreStockChange.Where(m => m.ToUserId == UserId);
  104. Obj.Add("CreateDate", Convert.ToDateTime(query.CreateDate).ToString("yyyy-MM-dd HH:mm:ss")); //创建时间
  105. Obj.Add("ApplyNum", query.ApplyNum); //申请台数
  106. Obj.Add("SendNum", query.SendNum); //发货台数
  107. Obj.Add("UseAmount", query.UseAmount); //使用额度
  108. Obj.Add("SendMode", query.SendMode); //发货方式
  109. Obj.Add("ErpCode", query.ErpCode); //快递单号
  110. Obj.Add("SendSn", brands); //发货SN数据
  111. return Obj;
  112. }
  113. #endregion
  114. #region 创客-首页-仓库管理-申请机具-确认申请
  115. [Authorize]
  116. public JsonResult ConfirmApply(string value)
  117. {
  118. value = DesDecrypt(value);
  119. JsonData data = JsonMapper.ToObject(value);
  120. AppResultJson result = ConfirmApplyDo(value);
  121. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  122. }
  123. public AppResultJson ConfirmApplyDo(string value)
  124. {
  125. JsonData data = JsonMapper.ToObject(value);
  126. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  127. JsonData ApplyList = data["ApplyList"];//申请数据
  128. int NeedPay = int.Parse(function.CheckInt(data["NeedPay"].ToString())); //所需金额
  129. Dictionary<string, object> Obj = new Dictionary<string, object>();
  130. int ApplyNum = 0;
  131. for (int i = 0; i < ApplyList.Count; i++)
  132. {
  133. int num = Convert.ToInt32(ApplyList[i]["ApplyNum"].ToString());
  134. ApplyNum += num;
  135. }
  136. int CheckUserId = UserId;
  137. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.UserId == UserId && m.Status > -1 && m.OpId > 0 && m.Sort == 0) ?? new StoreHouse();
  138. StoreMachineApply query = new StoreMachineApply();
  139. query = maindb.StoreMachineApply.Add(new StoreMachineApply()
  140. {
  141. CreateDate = DateTime.Now, //创建时间
  142. SendSn = ApplyList.ToJson(),//申请机具json数据
  143. ApplyNo = "FC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),//申请单号
  144. ApplyNum = ApplyNum,// 申请机具数量
  145. UserId = UserId, //创客
  146. QueryCount = store.OpId, //运营中心所属人Id
  147. }).Entity;
  148. UserAccount userAccount = maindb.UserAccount.FirstOrDefault(m => m.UserId == UserId);
  149. // if (userAccount.FixedAmount >= NeedPay)
  150. // {
  151. // userAccount.FixedAmount -= NeedPay;
  152. // }
  153. // if (userAccount.FixedAmount < NeedPay && userAccount.FixedAmount + userAccount.TempAmount >= NeedPay)
  154. // {
  155. // userAccount.FixedAmount = 0;
  156. // userAccount.TempAmount -= NeedPay - userAccount.FixedAmount;
  157. // }
  158. // if (userAccount.FixedAmount < NeedPay || userAccount.FixedAmount + userAccount.TempAmount < NeedPay)
  159. // {
  160. // return new AppResultJson() { Status = "-1", Info = "可用额度不足,请充值临时额度" };
  161. // }
  162. if (userAccount.ValidAmount >= NeedPay)
  163. {
  164. userAccount.ValidAmount -= NeedPay;
  165. query.UseAmount = NeedPay;
  166. }
  167. else
  168. {
  169. return new AppResultJson() { Status = "-1", Info = "可用额度不足,请充值临时额度" };
  170. }
  171. maindb.SaveChanges();
  172. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  173. }
  174. #endregion
  175. #region 创客-首页-仓库管理-申请机具-可选品牌列表
  176. [Authorize]
  177. public JsonResult ProductList(string value)
  178. {
  179. value = DesDecrypt(value);
  180. JsonData data = JsonMapper.ToObject(value);
  181. List<Dictionary<string, object>> ObjList = ProductListDo(value);
  182. return Json(new AppResultJson() { Status = "1", Info = "", Data = ObjList });
  183. }
  184. public List<Dictionary<string, object>> ProductListDo(string value)
  185. {
  186. JsonData data = JsonMapper.ToObject(value);
  187. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  188. List<Dictionary<string, object>> ObjList = new List<Dictionary<string, object>>();
  189. List<StoreHouse> storeHouse = maindb.StoreHouse.Where(m => m.UserId == UserId && m.BrandId != null && m.Status > 0 && m.Sort == 0).ToList();
  190. foreach (var item in storeHouse)
  191. {
  192. Dictionary<string, object> Obj = new Dictionary<string, object>();
  193. int Price = 0;
  194. KqProducts kqProducts = maindb.KqProducts.FirstOrDefault(m => m.Id == Convert.ToInt32(item.BrandId));
  195. if (kqProducts.Name.Contains("大POS"))
  196. {
  197. Price = 300;
  198. }
  199. if (kqProducts.Name.Contains("电签"))
  200. {
  201. Price = 200;
  202. }
  203. Obj.Add("BrandId", kqProducts.Id);
  204. Obj.Add("BrandName", kqProducts.Name);
  205. Obj.Add("Price", Price);
  206. ObjList.Add(Obj);
  207. }
  208. return ObjList;
  209. }
  210. #endregion
  211. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  212. /// <summary>
  213. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  214. /// </summary>
  215. /// <param name="value">请求的参数(json字符串)</param>
  216. /// <param name="signField">要签名的字段</param>
  217. /// <returns></returns>
  218. private string CheckSign(string value, string[] signField)
  219. {
  220. JsonData json = JsonMapper.ToObject(value);
  221. Dictionary<string, string> dic = new Dictionary<string, string>();
  222. for (int i = 0; i < signField.Length; i++)
  223. {
  224. dic.Add(signField[i], json[signField[i]].ToString());
  225. }
  226. string sign = json["sign"].ToString(); //客户端签名字符串
  227. return new Sign().sign(dic, sign);
  228. }
  229. #endregion
  230. }
  231. }