NozzleService.cs 21 KB

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