NozzleService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using DFS.Core.Abstractions.Models;
  2. using DFS.Infrastructure;
  3. using Fuel.Application.MqttService;
  4. using Fuel.Core;
  5. using Fuel.Core.Models;
  6. using Fuel.Core.Nozzle.Dto;
  7. using Fuel.Core.Transactions.Dto;
  8. using FuelServer.Core.Entity;
  9. using Newtonsoft.Json;
  10. using Org.BouncyCastle.Asn1.Ocsp;
  11. using Org.BouncyCastle.Ocsp;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Transactions;
  19. using System.Xml;
  20. using static FreeSql.Internal.GlobalFilter;
  21. namespace Fuel.Application.Service
  22. {
  23. public class NozzleService : INozzleService
  24. {
  25. private readonly EntityHelper _entityHelper;
  26. public readonly IFreeSql _fsql;
  27. private readonly IMqttClientService _mqttService;
  28. public NozzleService(EntityHelper entityHelper, IFreeSql fsql, IMqttClientService mqttService)
  29. {
  30. _entityHelper = entityHelper;
  31. _fsql = fsql;
  32. _mqttService = mqttService;
  33. }
  34. #region 油品
  35. /// <summary>
  36. /// 上传油品
  37. /// </summary>
  38. /// <param name="uploadProduct"></param>
  39. /// <returns></returns>
  40. public async Task<ServiceResponse> UploadProduct(UploadProduct uploadProduct)
  41. {
  42. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  43. var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
  44. if (_product != null)
  45. {
  46. return ServiceResponse.Error("油品已存在,请勿重复上传");
  47. }
  48. product product = new product();
  49. product.Buid = guid;
  50. product.ProductId = uploadProduct.ProductId;
  51. product.ProductCode = uploadProduct.ProductCode;
  52. product.ProductName = uploadProduct.ProductName;
  53. product.ProductPrice = uploadProduct.ProductPrice;
  54. int affectedRows = _fsql.Insert<product>().AppendData(product).ExecuteAffrows();
  55. if (affectedRows <= 0)
  56. {
  57. return ServiceResponse.Error("油品信息插入失败");
  58. }
  59. return ServiceResponse.Ok(product);
  60. }
  61. /// <summary>
  62. /// 更新油品信息
  63. /// </summary>
  64. /// <param name="uploadNozzle"></param>
  65. /// <returns></returns>
  66. public async Task<ServiceResponse> UpdateProduct(UploadProduct uploadProduct)
  67. {
  68. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  69. var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
  70. if (_product == null)
  71. {
  72. return await UploadProduct(uploadProduct);
  73. }
  74. _product.ProductCode = uploadProduct.ProductCode;
  75. _product.ProductName = uploadProduct.ProductName;
  76. _product.ProductPrice = uploadProduct.ProductPrice;
  77. int affectedRows = _fsql.Update<product>().SetSource(_product).ExecuteAffrows();
  78. if (affectedRows <= 0)
  79. {
  80. return ServiceResponse.Error("油品信息更新失败");
  81. }
  82. return ServiceResponse.Ok(_product);
  83. }
  84. /// <summary>
  85. /// 删除油品信息
  86. /// </summary>
  87. /// <param name="uploadNozzle"></param>
  88. /// <returns></returns>
  89. public async Task<ServiceResponse> DeleteProduct(UploadProduct uploadProduct)
  90. {
  91. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  92. var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
  93. if (_product == null)
  94. {
  95. return ServiceResponse.Error("未找到油品");
  96. }
  97. int affectedRows = _fsql.Delete<product>()
  98. .Where(p => p.ProductId == uploadProduct.ProductId)
  99. .ExecuteAffrows();
  100. if (affectedRows <= 0)
  101. {
  102. return ServiceResponse.Error("油品信息删除失败");
  103. }
  104. return ServiceResponse.Ok();
  105. }
  106. #endregion
  107. #region 油罐
  108. /// <summary>
  109. /// 上传油罐
  110. /// </summary>
  111. /// <param name="uploadProduct"></param>
  112. /// <returns></returns>
  113. public async Task<ServiceResponse> UploadTanks(UploadTanks uploadTanks)
  114. {
  115. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  116. var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
  117. if (_tanks != null)
  118. {
  119. return ServiceResponse.Error("油灌已存在,请勿重复上传");
  120. }
  121. tanks tanks = new tanks();
  122. tanks.Buid = guid;
  123. tanks.ProductId = (long)uploadTanks.ProductId;
  124. tanks.TankCapacity = uploadTanks.TankCapacity;
  125. tanks.ProductName = uploadTanks.ProductName;
  126. tanks.TankNumber = uploadTanks.TankNumber;
  127. tanks.TankID = uploadTanks.TankID;
  128. int affectedRows = _fsql.Insert<tanks>().AppendData(tanks).ExecuteAffrows();
  129. if (affectedRows <= 0)
  130. {
  131. return ServiceResponse.Error("油灌信息插入失败");
  132. }
  133. return ServiceResponse.Ok(tanks);
  134. }
  135. /// <summary>
  136. /// 更新油罐
  137. /// </summary>
  138. /// <param name="uploadNozzle"></param>
  139. /// <returns></returns>
  140. public async Task<ServiceResponse> UpdateTanks(UploadTanks uploadTanks)
  141. {
  142. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  143. var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
  144. if (_tanks == null)
  145. {
  146. return await UploadTanks(uploadTanks);
  147. }
  148. _tanks.ProductId = (long)uploadTanks.ProductId;
  149. _tanks.TankCapacity = uploadTanks.TankCapacity;
  150. _tanks.ProductName = uploadTanks.ProductName;
  151. _tanks.TankNumber = uploadTanks.TankNumber;
  152. int affectedRows = _fsql.Update<tanks>().SetSource(_tanks).ExecuteAffrows();
  153. if (affectedRows <= 0)
  154. {
  155. return ServiceResponse.Error("油灌信息更新失败");
  156. }
  157. return ServiceResponse.Ok(_tanks);
  158. }
  159. /// <summary>
  160. /// 删除油罐
  161. /// </summary>
  162. /// <param name="uploadNozzle"></param>
  163. /// <returns></returns>
  164. public async Task<ServiceResponse> DeleteTanks(UploadTanks uploadTanks)
  165. {
  166. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  167. var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
  168. if (_tanks == null)
  169. {
  170. return ServiceResponse.Error("油灌不存在,删除失败");
  171. }
  172. int affectedRows = _fsql.Delete<tanks>()
  173. .Where(p => p.TankID == uploadTanks.TankID)
  174. .ExecuteAffrows();
  175. if (affectedRows <= 0)
  176. {
  177. return ServiceResponse.Error("油灌信息删除失败");
  178. }
  179. return ServiceResponse.Ok();
  180. }
  181. #endregion
  182. #region 油枪
  183. /// <summary>
  184. /// 上传油枪
  185. /// </summary>
  186. /// <param name="uploadNozzle"></param>
  187. /// <returns></returns>
  188. public async Task<ServiceResponse> UploadNozzle(UploadNozzle uploadNozzle)
  189. {
  190. //RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232");
  191. //RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600);
  192. // var fsdds = RedisHelper.GetAsync("33:22:33:44");
  193. //var das = RedisHelper.ExpireAsync("33:22:33:44", 10);
  194. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  195. var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadNozzle.ProductId).First();
  196. var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadNozzle.TankID).First();
  197. var isproduct = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
  198. if (isproduct != null)
  199. {
  200. return ServiceResponse.Error("油枪已存在");
  201. }
  202. if (_product == null || _tanks == null)
  203. {
  204. return ServiceResponse.Error("油品或油罐信息为空");
  205. }
  206. nozzle _nozzle = new nozzle();
  207. _nozzle.Buid = guid;
  208. _nozzle.PumpId = uploadNozzle.PumpID;
  209. _nozzle.TankId = _tanks.Id;
  210. _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber;
  211. _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber;
  212. _nozzle.ProductID = _product.Id;
  213. _nozzle.NozzleId = uploadNozzle.NozzleId;
  214. long id = _fsql.Insert<nozzle>().AppendData(_nozzle).ExecuteIdentity();
  215. if (id <= 0)
  216. {
  217. return ServiceResponse.Error("油枪信息插入失败");
  218. }
  219. _nozzle.Id = id;
  220. return ServiceResponse.Ok(_nozzle);
  221. }
  222. /// <summary>
  223. /// 更新油枪信息
  224. /// </summary>
  225. /// <param name="uploadNozzle"></param>
  226. /// <returns></returns>
  227. public async Task<ServiceResponse> UpdateNozzle(UploadNozzle uploadNozzle)
  228. {
  229. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  230. var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
  231. if (_nozzle == null)
  232. {
  233. return await UploadNozzle(uploadNozzle);
  234. }
  235. _nozzle.PumpId = uploadNozzle.PumpID;
  236. _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber;
  237. _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber;
  238. _nozzle.TankId = (long)uploadNozzle.TankID;
  239. _nozzle.ProductID = (long)uploadNozzle.ProductId;
  240. int affectedRows = _fsql.Update<nozzle>().SetSource(_nozzle).ExecuteAffrows();
  241. if (affectedRows <= 0)
  242. {
  243. return ServiceResponse.Error("油枪信息更新失败");
  244. }
  245. return ServiceResponse.Ok(_nozzle);
  246. }
  247. /// <summary>
  248. /// 删除油枪信息
  249. /// </summary>
  250. /// <param name="uploadNozzle"></param>
  251. /// <returns></returns>
  252. public async Task<ServiceResponse> DeleteNozzle(UploadNozzle uploadNozzle)
  253. {
  254. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  255. var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
  256. if (_nozzle == null)
  257. {
  258. return ServiceResponse.Error("未找到油枪");
  259. }
  260. int affectedRows = _fsql.Delete<nozzle>()
  261. .Where(p => p.NozzleId == uploadNozzle.NozzleId)
  262. .ExecuteAffrows();
  263. if (affectedRows <= 0)
  264. {
  265. return ServiceResponse.Error("油枪信息删除失败");
  266. }
  267. return ServiceResponse.Ok();
  268. }
  269. public async Task<ServiceResponse> UpdateNozzleStatus(List<UploadNozzleStatus> uploadNozzleStatuses)
  270. {
  271. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  272. List<nozzle> nozzleStatuses = new List<nozzle>();
  273. var nozzleList = _fsql.Select<nozzle>().Where(_ => _.Buid == guid).ToList();
  274. foreach (var n in uploadNozzleStatuses)
  275. {
  276. var _nozzle = nozzleList.Where(_ => _.Id == n.NozzleId).First();
  277. if (_nozzle != null)
  278. {
  279. _nozzle.Status = n.Status;
  280. nozzleStatuses.Add(_nozzle);
  281. }
  282. }
  283. int affectedRows = _fsql.InsertOrUpdate<nozzle>().SetSource(nozzleStatuses).ExecuteAffrows();
  284. return ServiceResponse.Ok();
  285. }
  286. public async Task<List<NozzleInfo>> GetNozzleInfo(int Nozzleid)
  287. {
  288. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  289. return _entityHelper._fsql.Select<nozzle, tanks, product>()
  290. .LeftJoin((a, b, c) => a.TankId == b.Id)
  291. .LeftJoin((a, b, c) => a.ProductID == c.Id)
  292. .Where((a, b, c) => a.Buid == guid && a.Id == Nozzleid)
  293. .ToList((a, b, c) => new NozzleInfo()
  294. {
  295. Nozzleid = a.Id,
  296. ProductName = c.ProductName,
  297. ProductPrice = c.ProductPrice,
  298. Status = a.Status,
  299. TankNumber = b.TankNumber,
  300. ExternalGunNumber = a.ExternalGunNumber
  301. });
  302. }
  303. /// <summary>
  304. /// 根据外部枪号获取油枪id
  305. /// </summary>
  306. /// <param name="Nozzleid"></param>
  307. /// <returns></returns>
  308. public async Task<NozzleInfo> GetNozzleID(int Nozzleid)
  309. {
  310. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  311. return _entityHelper._fsql.Select<nozzle>()
  312. .Where((a) => a.ExternalGunNumber == Nozzleid)
  313. .ToList((a) => new NozzleInfo()
  314. {
  315. Nozzleid = a.Id
  316. }).FirstOrDefault();
  317. }
  318. #endregion
  319. /// <summary>
  320. /// 更新授权状态
  321. /// </summary>
  322. /// <param name="nozzleAuthorization"></param>
  323. /// <returns></returns>
  324. public async Task<ServiceResponse> UpdateNozzleAuthorization(NozzleAuthorization nozzleAuthorization)
  325. {
  326. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  327. string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key
  328. KeyValueCache.AddOrUpdate("authGun", key, nozzleAuthorization);//更新授权结果
  329. return ServiceResponse.Ok();
  330. }
  331. /// <summary>
  332. /// 向fcc发起油枪授权
  333. /// </summary>
  334. /// <param name="nozzleAuthorization"></param>
  335. /// <returns></returns>
  336. public async Task<ServiceResponse> NozzleAuthorizationAsync(int trxId)
  337. {
  338. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  339. var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
  340. if (trx == null)
  341. {
  342. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
  343. }
  344. var user = _fsql.Select<miniprogramusers>().Where(_ => _.Id == trx.MiniProgramID).First();
  345. string key = guid + "_" + trx.NozzleId;//授权结果的key
  346. string jsonString = JsonConvert.SerializeObject(trx);
  347. await _mqttService.SubscribeAsync("authorization/" + guid);
  348. await Task.Delay(2000);
  349. var sendjson = new { data = jsonString, UserName = user.UserName, UserPhoneNumber = user.UserPhoneNumber };
  350. await _mqttService.PublishAsync("authorization/" + guid, JsonConvert.SerializeObject(sendjson));
  351. KeyValueCache.AddOrUpdate("authGun", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果
  352. bool changed = await KeyValueCache.MonitorDictionaryChanges("authGun", key, AuthorizationStatus.WaitAuthorization);
  353. if (!changed)
  354. {
  355. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败");
  356. }
  357. var auth = KeyValueCache.GetValueOrDefault<NozzleAuthorization>("authGun", key);
  358. if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success)
  359. {
  360. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败");
  361. }
  362. trx.TransactionNumber = auth.TransactionNumber;
  363. trx.authorizationStatus = AuthorizationStatus.Authorized;//将订单授权状态更改成已授权
  364. int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
  365. return ServiceResponse.Ok("授权成功");
  366. }
  367. /// <summary>
  368. /// 向fcc发起取消油枪授权
  369. /// </summary>
  370. /// <param name="nozzleAuthorization"></param>
  371. /// <returns></returns>
  372. public async Task<ServiceResponse> CancelNozzleAuthorizationAsync(int trxId)
  373. {
  374. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  375. var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
  376. if (trx == null)
  377. {
  378. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
  379. }
  380. string key = guid + "_" + trx.NozzleId;//授权结果的key
  381. string jsonString = JsonConvert.SerializeObject(trx);
  382. await _mqttService.SubscribeAsync("unAuthorization/" + guid);
  383. await Task.Delay(2000);
  384. var sendjson = new { data = jsonString };
  385. await _mqttService.PublishAsync("unAuthorization/" + guid, JsonConvert.SerializeObject(sendjson));
  386. KeyValueCache.AddOrUpdate("cancelAuth", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果
  387. bool changed = await KeyValueCache.MonitorDictionaryChanges("cancelAuth", key, AuthorizationStatus.WaitAuthorization);
  388. if (!changed)
  389. {
  390. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败");
  391. }
  392. var auth = KeyValueCache.GetValueOrDefault<NozzleAuthorization>("cancelAuth", key);
  393. if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success)
  394. {
  395. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败");
  396. }
  397. trx.TransactionNumber = auth.TransactionNumber;
  398. trx.authorizationStatus = AuthorizationStatus.Unauthorized;//将订单授权状态更改成已授权
  399. int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
  400. return ServiceResponse.Ok("取消授权成功");
  401. }
  402. /// <summary>
  403. /// 更新取消授权状态
  404. /// </summary>
  405. /// <param name="nozzleAuthorization"></param>
  406. /// <returns></returns>
  407. public async Task<ServiceResponse> UpdateCancelNozzleAuthorization(NozzleAuthorization nozzleAuthorization)
  408. {
  409. Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
  410. string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key
  411. KeyValueCache.AddOrUpdate("cancelAuth", key, nozzleAuthorization);//更新授权结果
  412. if (nozzleAuthorization.OilMachineStatus == OilMachineStatus.Success)
  413. {
  414. var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == nozzleAuthorization.NozzleId);
  415. if (_nozzle.Count() <= 0)
  416. {
  417. return ServiceResponse.Error("油枪查询失败,油枪名称:" + nozzleAuthorization.NozzleId);
  418. }
  419. var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Buid == guid && _.NozzleId == _nozzle.First().Id).Result.FirstOrDefault();
  420. if (trx != null)
  421. {
  422. trx.authorizationStatus = AuthorizationStatus.Unauthorized;
  423. int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
  424. }
  425. }
  426. return ServiceResponse.Ok();
  427. }
  428. }
  429. }