NozzleService.cs 18 KB

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