123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- 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 油品
- /// <summary>
- /// 上传油品
- /// </summary>
- /// <param name="uploadProduct"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UploadProduct(UploadProduct uploadProduct)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _product = _fsql.Select<product>().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<product>().AppendData(product).ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油品信息插入失败");
- }
- return ServiceResponse.Ok(product);
- }
- /// <summary>
- /// 更新油品信息
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UpdateProduct(UploadProduct uploadProduct)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _product = _fsql.Select<product>().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<product>().SetSource(_product).ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油品信息更新失败");
- }
- return ServiceResponse.Ok(_product);
- }
- /// <summary>
- /// 删除油品信息
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> DeleteProduct(UploadProduct uploadProduct)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _product = _fsql.Select<product>().Where(_ => _.Buid == guid && _.ProductId == uploadProduct.ProductId).First();
- if (_product == null)
- {
- return ServiceResponse.Error("未找到油品");
- }
- int affectedRows = _fsql.Delete<product>()
- .Where(p => p.ProductId == uploadProduct.ProductId)
- .ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油品信息删除失败");
- }
- return ServiceResponse.Ok();
- }
- #endregion
- #region 油罐
- /// <summary>
- /// 上传油罐
- /// </summary>
- /// <param name="uploadProduct"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UploadTanks(UploadTanks uploadTanks)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _tanks = _fsql.Select<tanks>().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<tanks>().AppendData(tanks).ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油灌信息插入失败");
- }
- return ServiceResponse.Ok(tanks);
- }
- /// <summary>
- /// 更新油罐
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UpdateTanks(UploadTanks uploadTanks)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _tanks = _fsql.Select<tanks>().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<tanks>().SetSource(_tanks).ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油灌信息更新失败");
- }
- return ServiceResponse.Ok(_tanks);
- }
- /// <summary>
- /// 删除油罐
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> DeleteTanks(UploadTanks uploadTanks)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadTanks.TankID).First();
- if (_tanks == null)
- {
- return ServiceResponse.Error("油灌不存在,删除失败");
- }
- int affectedRows = _fsql.Delete<tanks>()
- .Where(p => p.TankID == uploadTanks.TankID)
- .ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油灌信息删除失败");
- }
- return ServiceResponse.Ok();
- }
- #endregion
- #region 油枪
- /// <summary>
- /// 上传油枪
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> 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<product>().Where(_ => _.Buid == guid && _.ProductId == uploadNozzle.ProductId).First();
- var _tanks = _fsql.Select<tanks>().Where(_ => _.Buid == guid && _.TankID == uploadNozzle.TankID).First();
- var isproduct = _fsql.Select<nozzle>().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<nozzle>().AppendData(_nozzle).ExecuteIdentity();
- if (id <= 0)
- {
- return ServiceResponse.Error("油枪信息插入失败");
- }
- _nozzle.Id = id;
- return ServiceResponse.Ok(_nozzle);
- }
- /// <summary>
- /// 更新油枪信息
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UpdateNozzle(UploadNozzle uploadNozzle)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _nozzle = _fsql.Select<nozzle>().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<nozzle>().SetSource(_nozzle).ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油枪信息更新失败");
- }
- return ServiceResponse.Ok(_nozzle);
- }
- /// <summary>
- /// 删除油枪信息
- /// </summary>
- /// <param name="uploadNozzle"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> DeleteNozzle(UploadNozzle uploadNozzle)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Buid == guid && _.NozzleId == uploadNozzle.NozzleId).First();
- if (_nozzle == null)
- {
- return ServiceResponse.Error("未找到油枪");
- }
- int affectedRows = _fsql.Delete<nozzle>()
- .Where(p => p.NozzleId == uploadNozzle.NozzleId)
- .ExecuteAffrows();
- if (affectedRows <= 0)
- {
- return ServiceResponse.Error("油枪信息删除失败");
- }
- return ServiceResponse.Ok();
- }
- public async Task<ServiceResponse> UpdateNozzleStatus(List<UploadNozzleStatus> uploadNozzleStatuses)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- List<nozzle> nozzleStatuses = new List<nozzle>();
- var nozzleList = _fsql.Select<nozzle>().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<nozzle>().SetSource(nozzleStatuses).ExecuteAffrows();
- return ServiceResponse.Ok();
- }
- public async Task<List<NozzleInfo>> GetNozzleInfo(int Nozzleid)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- return _entityHelper._fsql.Select<nozzle, tanks, product>()
- .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
- });
- }
- /// <summary>
- /// 根据外部枪号获取油枪id
- /// </summary>
- /// <param name="Nozzleid"></param>
- /// <returns></returns>
- public async Task<NozzleInfo> GetNozzleID(int Nozzleid)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- return _entityHelper._fsql.Select<nozzle>()
- .Where((a) => a.ExternalGunNumber == Nozzleid)
- .ToList((a) => new NozzleInfo()
- {
- Nozzleid = a.Id
- }).FirstOrDefault();
- }
- #endregion
- /// <summary>
- /// 更新授权状态
- /// </summary>
- /// <param name="nozzleAuthorization"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> UpdateNozzleAuthorization(NozzleAuthorization nozzleAuthorization)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- string key = guid + "_" + nozzleAuthorization.NozzleId;//授权结果的key
- KeyValueCache.AddOrUpdate("authGun", key, nozzleAuthorization);//更新授权结果
- return ServiceResponse.Ok();
- }
- /// <summary>
- /// 向fcc发起油枪授权
- /// </summary>
- /// <param name="nozzleAuthorization"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> NozzleAuthorizationAsync(int trxId)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
- if (trx == null)
- {
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
- }
- var user = _fsql.Select<miniprogramusers>().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<NozzleAuthorization>("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<transactions>().SetSource(trx).ExecuteAffrows();
- return ServiceResponse.Ok("授权成功");
- }
- /// <summary>
- /// 向fcc发起取消油枪授权
- /// </summary>
- /// <param name="nozzleAuthorization"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> CancelNozzleAuthorizationAsync(int trxId)
- {
- Guid guid = HttpRequestReader.GetCurrentBuId(); //站点id
- var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.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<NozzleAuthorization>("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<transactions>().SetSource(trx).ExecuteAffrows();
- return ServiceResponse.Ok("取消授权成功");
- }
- /// <summary>
- /// 更新取消授权状态
- /// </summary>
- /// <param name="nozzleAuthorization"></param>
- /// <returns></returns>
- public async Task<ServiceResponse> 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<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == nozzleAuthorization.NozzleId);
- if (_nozzle.Count() <= 0)
- {
- return ServiceResponse.Error("油枪查询失败,油枪名称:" + nozzleAuthorization.NozzleId);
- }
- var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Buid == guid && _.NozzleId == _nozzle.First().Id).Result.FirstOrDefault();
- if (trx != null)
- {
- trx.authorizationStatus = AuthorizationStatus.Unauthorized;
- int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
- }
- }
- return ServiceResponse.Ok();
- }
- }
- }
|