using DFS.Core.Abstractions.Models; using DFS.Infrastructure; using Fuel.Application.MqttService; using Fuel.Core; using Fuel.Core.Models; using Fuel.Core.Nozzle.Dto; using Fuel.Core.Transactions.Dto; using FuelServer.Core.Entity; using Newtonsoft.Json; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Ocsp; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Transactions; using System.Xml; using static FreeSql.Internal.GlobalFilter; namespace Fuel.Application.Service { public class NozzleService : INozzleService { private readonly EntityHelper _entityHelper; public readonly IFreeSql _fsql; private readonly IMqttClientService _mqttService; public NozzleService(EntityHelper entityHelper, IFreeSql fsql, IMqttClientService mqttService) { _entityHelper = entityHelper; _fsql = fsql; _mqttService = mqttService; } #region 油品 /// /// 上传油品 /// /// /// public async Task UploadProduct(UploadProduct uploadProduct) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _product = _fsql.Select().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First(); if (_product != null) { return ServiceResponse.Error("油品已存在,请勿重复上传"); } product product = new product(); product.Buid = guid; product.ProductId = uploadProduct.ProductId; product.ProductCode = uploadProduct.ProductCode; product.ProductName = uploadProduct.ProductName; product.ProductPrice = uploadProduct.ProductPrice; int affectedRows = _fsql.Insert().AppendData(product).ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油品信息插入失败"); } return ServiceResponse.Ok(product); } /// /// 更新油品信息 /// /// /// public async Task UpdateProduct(UploadProduct uploadProduct) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _product = _fsql.Select().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First(); if (_product == null) { return await UploadProduct(uploadProduct); } _product.ProductCode = uploadProduct.ProductCode; _product.ProductName = uploadProduct.ProductName; _product.ProductPrice = uploadProduct.ProductPrice; int affectedRows = _fsql.Update().SetSource(_product).ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油品信息更新失败"); } return ServiceResponse.Ok(_product); } /// /// 删除油品信息 /// /// /// public async Task DeleteProduct(UploadProduct uploadProduct) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _product = _fsql.Select().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First(); if (_product == null) { return ServiceResponse.Error("未找到油品"); } int affectedRows = _fsql.Delete() .Where(p => p.ProductId == uploadProduct.ProductId) .ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油品信息删除失败"); } return ServiceResponse.Ok(); } #endregion #region 油罐 /// /// 上传油罐 /// /// /// public async Task UploadTanks(UploadTanks uploadTanks) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _tanks = _fsql.Select().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First(); if (_tanks != null) { return ServiceResponse.Error("油灌已存在,请勿重复上传"); } tanks tanks = new tanks(); tanks.Buid = guid; tanks.ProductId = (long)uploadTanks.ProductId; tanks.TankCapacity = uploadTanks.TankCapacity; tanks.ProductName = uploadTanks.ProductName; tanks.TankNumber = uploadTanks.TankNumber; tanks.TankID = uploadTanks.TankID; int affectedRows = _fsql.Insert().AppendData(tanks).ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油灌信息插入失败"); } return ServiceResponse.Ok(tanks); } /// /// 更新油罐 /// /// /// public async Task UpdateTanks(UploadTanks uploadTanks) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _tanks = _fsql.Select().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First(); if (_tanks == null) { return await UploadTanks(uploadTanks); } _tanks.ProductId = (long)uploadTanks.ProductId; _tanks.TankCapacity = uploadTanks.TankCapacity; _tanks.ProductName = uploadTanks.ProductName; _tanks.TankNumber = uploadTanks.TankNumber; int affectedRows = _fsql.Update().SetSource(_tanks).ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油灌信息更新失败"); } return ServiceResponse.Ok(_tanks); } /// /// 删除油罐 /// /// /// public async Task DeleteTanks(UploadTanks uploadTanks) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _tanks = _fsql.Select().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First(); if (_tanks == null) { return ServiceResponse.Error("油灌不存在,删除失败"); } int affectedRows = _fsql.Delete() .Where(p => p.TankID == uploadTanks.TankID) .ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油灌信息删除失败"); } return ServiceResponse.Ok(); } #endregion #region 油枪 /// /// 上传油枪 /// /// /// public async Task UploadNozzle(UploadNozzle uploadNozzle) { //RedisHelper.HSetAsync("Transaction", "11:22:33:44", "3232"); //RedisHelper.SetAsync("33:22:33:44", "qweqweqwe", 3600); // var fsdds = RedisHelper.GetAsync("33:22:33:44"); //var das = RedisHelper.ExpireAsync("33:22:33:44", 10); Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _product = _fsql.Select().Where(_ => _.Buid == guid && _.ProductId == uploadNozzle.ProductId).First(); var _tanks = _fsql.Select().Where(_ => _.Buid == guid && _.TankID == uploadNozzle.TankID).First(); var isproduct = _fsql.Select().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First(); if (isproduct != null) { return ServiceResponse.Error("油枪已存在"); } if (_product == null || _tanks == null) { return ServiceResponse.Error("油品或油罐信息为空"); } nozzle _nozzle = new nozzle(); _nozzle.Buid = guid; _nozzle.PumpId = uploadNozzle.PumpID; _nozzle.TankId = _tanks.Id; _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber; _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber; _nozzle.ProductID = _product.Id; _nozzle.NozzleId = uploadNozzle.NozzleId; long id = _fsql.Insert().AppendData(_nozzle).ExecuteIdentity(); if (id <= 0) { return ServiceResponse.Error("油枪信息插入失败"); } _nozzle.Id = id; return ServiceResponse.Ok(_nozzle); } /// /// 更新油枪信息 /// /// /// public async Task UpdateNozzle(UploadNozzle uploadNozzle) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _nozzle = _fsql.Select().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First(); if (_nozzle == null) { return await UploadNozzle(uploadNozzle); } _nozzle.PumpId = uploadNozzle.PumpID; _nozzle.InternalGunNumber = uploadNozzle.InternalGunNumber; _nozzle.ExternalGunNumber = uploadNozzle.ExternalGunNumber; _nozzle.TankId = (long)uploadNozzle.TankID; _nozzle.ProductID = (long)uploadNozzle.ProductId; int affectedRows = _fsql.Update().SetSource(_nozzle).ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油枪信息更新失败"); } return ServiceResponse.Ok(_nozzle); } /// /// 删除油枪信息 /// /// /// public async Task DeleteNozzle(UploadNozzle uploadNozzle) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var _nozzle = _fsql.Select().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First(); if (_nozzle == null) { return ServiceResponse.Error("未找到油枪"); } int affectedRows = _fsql.Delete() .Where(p => p.NozzleId == uploadNozzle.NozzleId) .ExecuteAffrows(); if (affectedRows <= 0) { return ServiceResponse.Error("油枪信息删除失败"); } return ServiceResponse.Ok(); } public async Task UpdateNozzleStatus(List uploadNozzleStatuses) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id List nozzleStatuses = new List(); var nozzleList = _fsql.Select().Where(_ => _.Buid == guid).ToList(); foreach (var n in uploadNozzleStatuses) { var _nozzle = nozzleList.Where(_ => _.Id == n.NozzleId).First(); if (_nozzle != null) { _nozzle.Status = n.Status; nozzleStatuses.Add(_nozzle); } } int affectedRows = _fsql.InsertOrUpdate().SetSource(nozzleStatuses).ExecuteAffrows(); return ServiceResponse.Ok(); } public async Task> GetNozzleInfo(int Nozzleid) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id return _entityHelper._fsql.Select() .LeftJoin((a, b, c) => a.TankId == b.Id) .LeftJoin((a, b, c) => a.ProductID == c.Id) .Where((a, b, c) => a.Buid == guid && a.Id == Nozzleid) .ToList((a, b, c) => new NozzleInfo() { Nozzleid = a.Id, ProductName = c.ProductName, ProductPrice = c.ProductPrice, Status = a.Status, TankNumber = b.TankNumber, ExternalGunNumber = a.ExternalGunNumber }); } /// /// 根据外部枪号获取油枪id /// /// /// public async Task GetNozzleID(int Nozzleid) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id return _entityHelper._fsql.Select() .Where((a) => a.ExternalGunNumber == Nozzleid) .ToList((a) => new NozzleInfo() { Nozzleid = a.Id }).FirstOrDefault(); } #endregion /// /// 更新授权状态 /// /// /// public async Task UpdateNozzleAuthorization(NozzleAuthorization nozzleAuthorization) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key KeyValueCache.AddOrUpdate("authGun", key, nozzleAuthorization);//更新授权结果 return ServiceResponse.Ok(); } /// /// 向fcc发起油枪授权 /// /// /// public async Task NozzleAuthorizationAsync(int trxId) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var trx = _entityHelper.GetEntitiesAsync(_ => _.Id == trxId).Result.FirstOrDefault(); if (trx == null) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!"); } var user = _fsql.Select().Where(_ => _.Id == trx.MiniProgramID).First(); string key = guid + "_" + trx.NozzleId;//授权结果的key string jsonString = JsonConvert.SerializeObject(trx); await _mqttService.SubscribeAsync("authorization/" + guid); await Task.Delay(2000); var sendjson = new { data = jsonString, UserName = user.UserName, UserPhoneNumber = user.UserPhoneNumber }; await _mqttService.PublishAsync("authorization/" + guid, JsonConvert.SerializeObject(sendjson)); KeyValueCache.AddOrUpdate("authGun", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果 bool changed = await KeyValueCache.MonitorDictionaryChanges("authGun", key, AuthorizationStatus.WaitAuthorization); if (!changed) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败"); } var auth = KeyValueCache.GetValueOrDefault("authGun", key); if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "授权失败"); } trx.TransactionNumber = auth.TransactionNumber; trx.authorizationStatus = AuthorizationStatus.Authorized;//将订单授权状态更改成已授权 int affectedRows = _fsql.Update().SetSource(trx).ExecuteAffrows(); return ServiceResponse.Ok("授权成功"); } /// /// 向fcc发起取消油枪授权 /// /// /// public async Task CancelNozzleAuthorizationAsync(int trxId) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id var trx = _entityHelper.GetEntitiesAsync(_ => _.Id == trxId).Result.FirstOrDefault(); if (trx == null) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!"); } string key = guid + "_" + trx.NozzleId;//授权结果的key string jsonString = JsonConvert.SerializeObject(trx); await _mqttService.SubscribeAsync("unAuthorization/" + guid); await Task.Delay(2000); var sendjson = new { data = jsonString }; await _mqttService.PublishAsync("unAuthorization/" + guid, JsonConvert.SerializeObject(sendjson)); KeyValueCache.AddOrUpdate("cancelAuth", key, AuthorizationStatus.WaitAuthorization);//添加字典,用于监听授权结果 bool changed = await KeyValueCache.MonitorDictionaryChanges("cancelAuth", key, AuthorizationStatus.WaitAuthorization); if (!changed) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败"); } var auth = KeyValueCache.GetValueOrDefault("cancelAuth", key); if (auth == null || auth.OilMachineStatus != OilMachineStatus.Success) { return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "取消授权失败"); } trx.TransactionNumber = auth.TransactionNumber; trx.authorizationStatus = AuthorizationStatus.Unauthorized;//将订单授权状态更改成已授权 int affectedRows = _fsql.Update().SetSource(trx).ExecuteAffrows(); return ServiceResponse.Ok("取消授权成功"); } /// /// 更新取消授权状态 /// /// /// public async Task UpdateCancelNozzleAuthorization(NozzleAuthorization nozzleAuthorization) { Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key KeyValueCache.AddOrUpdate("cancelAuth", key, nozzleAuthorization);//更新授权结果 if (nozzleAuthorization.OilMachineStatus == OilMachineStatus.Success) { var _nozzle = await _entityHelper.GetEntitiesAsync(_ => _.Buid == guid && _.ExternalGunNumber == nozzleAuthorization.NozzleId); if (_nozzle.Count() <= 0) { return ServiceResponse.Error("油枪查询失败,油枪名称:" + nozzleAuthorization.NozzleId); } var trx = _entityHelper.GetEntitiesAsync(_ => _.Buid == guid && _.NozzleId == _nozzle.First().Id).Result.FirstOrDefault(); if (trx != null) { trx.authorizationStatus = AuthorizationStatus.Unauthorized; int affectedRows = _fsql.Update().SetSource(trx).ExecuteAffrows(); } } return ServiceResponse.Ok(); } } }