MachineChangeController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/pos/[controller]/[action]")]
  17. public class MachineChangeController : BaseController
  18. {
  19. public MachineChangeController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 创客-首页-仓库管理-售后单列表
  23. [Authorize]
  24. public JsonResult OrderList(string value)
  25. {
  26. value = DesDecrypt(value);
  27. JsonData data = JsonMapper.ToObject(value);
  28. List<Dictionary<string, object>> dataList = OrderListDo(value);
  29. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  30. }
  31. public List<Dictionary<string, object>> OrderListDo(string value)
  32. {
  33. JsonData data = JsonMapper.ToObject(value);
  34. int OutStoreId = int.Parse(function.CheckInt(data["OutStoreId"].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<MachineChange> query = maindb.MachineChange.Where(m => m.OutStoreId == OutStoreId);
  39. query = query.OrderByDescending(m => m.Id);
  40. if (PageNum == 1)
  41. {
  42. query = query.Take(PageSize);
  43. }
  44. else
  45. {
  46. int skipNum = PageSize * (PageNum - 1);
  47. query = query.Skip(skipNum).Take(PageSize);
  48. }
  49. foreach (var subdata in query.ToList())
  50. {
  51. Dictionary<string, object> curData = new Dictionary<string, object>();
  52. curData.Add("UserId", subdata.UserId); //创客
  53. curData.Add("BackProductType", RelationClass.GetKqProductBrandInfo(subdata.BackProductType)); //退回产品类型
  54. curData.Add("ChangeTime", subdata.ChangeTime == null ? "" : subdata.ChangeTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //转换时间
  55. curData.Add("AuditResult", subdata.AuditResult); //审核结果
  56. curData.Add("ChangeSnExpand", subdata.ChangeSnExpand); //转换机器SN来源
  57. curData.Add("Id", subdata.Id); //Id
  58. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == subdata.QueryCount) ?? new Orders();
  59. curData.Add("ReceiveName", order.RealName); //收货人
  60. curData.Add("Address", order.Address); //收货地址
  61. curData.Add("Mobile", order.Mobile); //联系电话
  62. curData.Add("DeliveryType", order.DeliveryType); //提货方式
  63. curData.Add("Reason", subdata.SeoKeyword); //换新原因
  64. curData.Add("OrderStatus", order.Status);
  65. curData.Add("NewSnNo", order.SnNos); //新机具SN
  66. dataList.Add(curData);
  67. }
  68. return dataList;
  69. }
  70. #endregion
  71. #region 创客-首页-售后换新-换新记录
  72. [Authorize]
  73. public JsonResult List(string value)
  74. {
  75. value = DesDecrypt(value);
  76. JsonData data = JsonMapper.ToObject(value);
  77. List<Dictionary<string, object>> dataList = ListDo(value);
  78. return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
  79. }
  80. public List<Dictionary<string, object>> ListDo(string value)
  81. {
  82. JsonData data = JsonMapper.ToObject(value);
  83. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  84. int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
  85. int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
  86. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  87. IQueryable<MachineChange> query = maindb.MachineChange.Where(m => m.UserId == UserId);
  88. query = query.OrderByDescending(m => m.Id);
  89. if (PageNum == 1)
  90. {
  91. query = query.Take(PageSize);
  92. }
  93. else
  94. {
  95. int skipNum = PageSize * (PageNum - 1);
  96. query = query.Skip(skipNum).Take(PageSize);
  97. }
  98. foreach (var subdata in query)
  99. {
  100. Dictionary<string, object> curData = new Dictionary<string, object>();
  101. curData.Add("AuditResult", subdata.AuditResult); //审核结果
  102. curData.Add("ChangeSnExpand", subdata.ChangeSnExpand); //转换机器SN来源
  103. curData.Add("Id", subdata.Id); //Id
  104. curData.Add("CreateDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //CreateDate
  105. curData.Add("Reason", subdata.SeoKeyword); //换机原因
  106. dataList.Add(curData);
  107. }
  108. return dataList;
  109. }
  110. #endregion
  111. #region 创客-首页-售后换新-换新记录-详情
  112. [Authorize]
  113. public JsonResult Detail(string value)
  114. {
  115. value = DesDecrypt(value);
  116. JsonData data = JsonMapper.ToObject(value);
  117. Dictionary<string, object> Obj = DetailDo(value);
  118. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  119. }
  120. public Dictionary<string, object> DetailDo(string value)
  121. {
  122. JsonData data = JsonMapper.ToObject(value);
  123. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  124. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  125. Dictionary<string, object> Obj = new Dictionary<string, object>();
  126. MachineChange query = maindb.MachineChange.FirstOrDefault(m => m.Id == Id && m.UserId == UserId) ?? new MachineChange();
  127. StoreHouse store = maindb.StoreHouse.FirstOrDefault(m => m.Id == query.OutStoreId) ?? new StoreHouse();
  128. Obj.Add("BackProductType", RelationClass.GetKqProductBrandInfo(query.BackProductType)); //退回产品类型
  129. Obj.Add("ChangeDeviceNum", query.ChangeDeviceNum); //转换机具数量
  130. Obj.Add("ChangeTime", query.ChangeTime == null ? "" : query.ChangeTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //转换时间
  131. Obj.Add("AuditResult", query.AuditResult); //审核结果, 0:待审核,1:通过,2:驳回
  132. Obj.Add("ChangeSnExpand", query.ChangeSnExpand); //转换机器SN来源
  133. Obj.Add("OutStoreId", store.StoreName); //出库仓库
  134. Obj.Add("OutStoreManager", query.OutStoreManager); //出库仓库联系人
  135. Obj.Add("ChangeDate", query.CreateDate == null ? "" : query.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //申请时间
  136. Obj.Add("Reason", query.SeoKeyword); //换机原因
  137. Orders order = maindb.Orders.FirstOrDefault(m => m.Id == query.QueryCount) ?? new Orders();
  138. Obj.Add("DeliveryType", order.DeliveryType); //提货方式
  139. Obj.Add("Note", query.Remark); //备注
  140. Obj.Add("OrderId", order.Id); //订单Id
  141. Obj.Add("ReceiveName", order.RealName); //收货人
  142. Obj.Add("Address", order.Areas + order.Address); //收货地址
  143. Obj.Add("Mobile", order.Mobile); //联系电话
  144. Obj.Add("NewSnNo", order.SnNos); //新机具SN
  145. Obj.Add("OrderStatus", order.Status); //订单状态, 2:已发货(成功),1:待发货,-1:已驳回
  146. List<string> PosPhoto = new List<string>();
  147. if (!string.IsNullOrEmpty(query.SeoDescription))
  148. {
  149. string[] Photos = query.SeoDescription.Split(',');
  150. foreach (string Photo in Photos)
  151. {
  152. PosPhoto.Add(SourceHost + Photo);
  153. }
  154. }
  155. Obj.Add("PosPhoto", PosPhoto); //机具照片(数组)
  156. return Obj;
  157. }
  158. #endregion
  159. #region 创客-首页-售后换新-提交
  160. [Authorize]
  161. public JsonResult Add(string value)
  162. {
  163. value = DesDecrypt(value);
  164. JsonData data = JsonMapper.ToObject(value);
  165. AppResultJson result = AddDo(value);
  166. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  167. }
  168. public AppResultJson AddDo(string value)
  169. {
  170. JsonData data = JsonMapper.ToObject(value);
  171. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  172. int BrandId = int.Parse(function.CheckInt(data["BackProductType"].ToString())); //退回产品类型
  173. string ChangeSnExpand = data["ChangeSnExpand"].ToString(); //转换机器SN来源
  174. string Remark = data["Remark"].ToString(); //订单备注
  175. int OutStoreId = int.Parse(function.CheckInt(data["OutStoreId"].ToString())); //出库仓库
  176. string PosPhoto = data["PosPhoto"].ToString(); //机具照片
  177. int AddressId = int.Parse(function.CheckInt(data["AddressId"].ToString())); //收货地址
  178. int DeliveryType = int.Parse(function.CheckInt(data["DeliveryType"].ToString()));//提货方式
  179. string Reason = data["Reason"].ToString(); //换机原因
  180. string Note = data["Note"].ToString(); //备注
  181. if (OutStoreId == 0)
  182. {
  183. return new AppResultJson() { Status = "-1", Info = "请选择出库仓库" };
  184. }
  185. if (string.IsNullOrEmpty(ChangeSnExpand))
  186. {
  187. return new AppResultJson() { Status = "-1", Info = "请填写机具SN号" };
  188. }
  189. MachineForSnNo forSnNo = maindb.MachineForSnNo.FirstOrDefault(m => m.SnNo == ChangeSnExpand);
  190. if (forSnNo == null)
  191. {
  192. return new AppResultJson() { Status = "-1", Info = "机具SN号不正确" };
  193. }
  194. string ChangeNo = "BC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  195. KqProducts protype = KqProductsDbconn.Instance.GetList().FirstOrDefault(m => m.Id == BrandId) ?? new KqProducts();
  196. Users user = UsersDbconn.Instance.Get(UserId) ?? new Users();
  197. UserAddress address = UserAddressDbconn.Instance.Get(AddressId) ?? new UserAddress();
  198. StoreHouse store = StoreHouseDbconn.Instance.Get(OutStoreId) ?? new StoreHouse();
  199. if (store.BrandId != BrandId.ToString())
  200. {
  201. return new AppResultJson() { Status = "-1", Info = "你选择的品牌和仓库不一致" };
  202. }
  203. Users storeuser = UsersDbconn.Instance.Get(store.UserId) ?? new Users();
  204. Users manager = UsersDbconn.Instance.Get(store.ManageUserId) ?? new Users();
  205. string OrderNo = "BM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  206. bool check = maindb.MachineChange.Any(m => m.ChangeSnExpand == ChangeSnExpand && m.AuditResult < 2);
  207. if (check)
  208. {
  209. return new AppResultJson() { Status = "-1", Info = "申请已提交,请勿重复申请" };
  210. }
  211. Dictionary<string, object> Obj = new Dictionary<string, object>();
  212. RedisDbconn.Instance.GetLock("MachineChange:" + UserId);
  213. MachineChange query = maindb.MachineChange.Add(new MachineChange()
  214. {
  215. CreateDate = DateTime.Now,
  216. UpdateDate = DateTime.Now,
  217. SeoKeyword = Reason,
  218. SeoDescription = PosPhoto,
  219. ChangeNo = ChangeNo, //转换单号
  220. UserId = UserId, //创客
  221. BackProductType = BrandId, //退回产品类型
  222. BackProductName = protype.Name, //退回产品名称
  223. ChangeDeviceNum = 1, //转换机具数量
  224. ChangeTime = DateTime.Now, //转换时间
  225. ChangeSnExpand = ChangeSnExpand, //转换机器SN来源
  226. BackStoreId = OutStoreId, //退回仓库
  227. BackStoreName = store.StoreName, //退回仓库名称
  228. Remark = Note, //订单备注
  229. BackStoreUserId = store.UserId, //退回仓库归属人
  230. OutProductType = BrandId, //出库产品类型
  231. OutProductName = protype.Name, //出库产品名称
  232. OutStoreId = OutStoreId, //出库仓库
  233. OutStoreName = store.StoreName, //出库仓库名称
  234. OutStoreAreas = store.Areas, //出库仓库所在地区
  235. OutStoreAddress = store.Address, //出库仓库地址
  236. OutStoreManager = storeuser.RealName, //出库仓库联系人
  237. OutStoreManagerMobile = store.ManageMobile, //出库仓库联系人手机号
  238. }).Entity;
  239. maindb.SaveChanges();
  240. RedisDbconn.Instance.ReleaseLock("MachineChange:" + UserId);
  241. Orders order = maindb.Orders.Add(new Orders()
  242. {
  243. OrderNo = OrderNo,
  244. RealName = address.RealName,
  245. Mobile = address.Mobile,
  246. Areas = address.Areas,
  247. Address = address.Address,
  248. StoreContact = manager.RealName,
  249. StoreContactMobile = store.ManageMobile,
  250. StoreUserId = store.UserId,
  251. StoreType = store.StoreType,
  252. CreateDate = DateTime.Now, //创建时间
  253. UserId = UserId, //创客
  254. StoreId = OutStoreId, //仓库
  255. TopUserId = PublicFunction.GetTopUserId(user.ParentNav), //顶级创客
  256. DeliveryType = DeliveryType, //提货类型
  257. BuyCount = 1,
  258. TotalPrice = 0, //订单总额
  259. Remark = Remark, //订单备注
  260. Status = 1,
  261. PayStatus = 1,
  262. PayDate = DateTime.Now,
  263. QueryCount = 2, //申请换新机标记
  264. Sort = query.Id,
  265. }).Entity;
  266. maindb.SaveChanges();
  267. MachineChange edit = maindb.MachineChange.FirstOrDefault(m => m.Id == query.Id);
  268. if (edit != null)
  269. {
  270. edit.QueryCount = order.Id;
  271. maindb.SaveChanges();
  272. }
  273. OrderForNo orderFor = maindb.OrderForNo.Add(new OrderForNo()
  274. {
  275. OrderNo = OrderNo,
  276. OrderIds = order.Id.ToString(),
  277. }).Entity;
  278. maindb.SaveChanges();
  279. Obj.Add("Id", query.Id); //Id
  280. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  281. }
  282. #endregion
  283. #region 创客-首页-售后换新-查询
  284. [Authorize]
  285. public JsonResult Check(string value)
  286. {
  287. value = DesDecrypt(value);
  288. JsonData data = JsonMapper.ToObject(value);
  289. AppResultJson result = CheckDo(value);
  290. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  291. }
  292. public AppResultJson CheckDo(string value)
  293. {
  294. JsonData data = JsonMapper.ToObject(value);
  295. int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客
  296. int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //品牌
  297. string PosSn = data["PosSn"].ToString(); //转换机器SN来源
  298. PosMachinesTwo pos = maindb.PosMachinesTwo.FirstOrDefault(m => m.Status > -1 && m.PosSn == PosSn && m.UserId == UserId);
  299. if (pos == null)
  300. {
  301. return new AppResultJson() { Status = "-1", Info = "SN不存在" };
  302. }
  303. if (pos.BrandId != BrandId)
  304. {
  305. return new AppResultJson() { Status = "-1", Info = "选择的品牌和SN号不一致" };
  306. }
  307. bool check = maindb.MachineChange.Any(m => m.ChangeSnExpand == PosSn && m.AuditResult < 2);
  308. if (check)
  309. {
  310. return new AppResultJson() { Status = "-1", Info = "此SN已提交,请勿重复申请" };
  311. }
  312. return new AppResultJson() { Status = "1", Info = "" };
  313. }
  314. #endregion
  315. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  316. /// <summary>
  317. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  318. /// </summary>
  319. /// <param name="value">请求的参数(json字符串)</param>
  320. /// <param name="signField">要签名的字段</param>
  321. /// <returns></returns>
  322. private string CheckSign(string value, string[] signField)
  323. {
  324. JsonData json = JsonMapper.ToObject(value);
  325. Dictionary<string, string> dic = new Dictionary<string, string>();
  326. for (int i = 0; i < signField.Length; i++)
  327. {
  328. dic.Add(signField[i], json[signField[i]].ToString());
  329. }
  330. string sign = json["sign"].ToString(); //客户端签名字符串
  331. return new Sign().sign(dic, sign);
  332. }
  333. #endregion
  334. }
  335. }