Эх сурвалжийг харах

创建订单,查询订单接口

DOVER-GLOBAL\11047086 6 сар өмнө
parent
commit
e6c9304d5d

+ 6 - 1
FuelCloud/Fuel.Application/Service/ITransactionsService.cs

@@ -1,4 +1,6 @@
-using System;
+using Fuel.Core.Transactions.Dto;
+using FuelServer.Core.Entity;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -8,5 +10,8 @@ namespace Fuel.Application.Service
 {
     public interface ITransactionsService
     {
+         Task<transactions> CreateTransactions(UploadTransactions uploadTransactions);
+
+        Task<List<transactions>> GetTransactionsAsync(TransactionsInput input);
     }
 }

+ 6 - 0
FuelCloud/Fuel.Application/Service/NozzleService.cs

@@ -5,6 +5,8 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Transactions;
+using static FreeSql.Internal.GlobalFilter;
 
 namespace Fuel.Application.Service
 {
@@ -22,6 +24,10 @@ namespace Fuel.Application.Service
         /// <returns></returns>
         public async Task<bool> 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 = Guid.Parse("12345678-9abc-def0-1234-56789abcdef0");
             if (uploadNozzle.type == 1)
             {

+ 112 - 2
FuelCloud/Fuel.Application/Service/TransactionsService.cs

@@ -1,13 +1,123 @@
-using System;
+using Fuel.Core.Transactions.Dto;
+using Org.BouncyCastle.Asn1.Ocsp;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using static FreeSql.Internal.GlobalFilter;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+using FuelServer.Core.Entity;
+using Fuel.Core.Nozzle.Dto;
+using Org.BouncyCastle.Asn1.X509;
+using System.Linq.Expressions;
 
 namespace Fuel.Application.Service
 {
-    public class TransactionsService
+    public class TransactionsService : ITransactionsService
     {
+        private readonly EntityHelper _entityHelper;
+        private readonly IHttpContextAccessor _httpContextAccessor;
+        public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor)
+        {
+            _entityHelper = entityHelper;
+            _httpContextAccessor = httpContextAccessor;
+        }
+
+        public async Task<transactions> CreateTransactions(UploadTransactions uploadTransactions)
+        {
+            string Buid  = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
+            Guid guid = Guid.Parse(Buid);
+            string key = string.Empty;
+            if (uploadTransactions.Type == 1)//预支付
+            {
+                key = uploadTransactions.NozzleId + ":" +
+                         uploadTransactions.OriginalAmount + ":" +
+                         uploadTransactions.Qty + ":" +
+                         uploadTransactions.MiniProgramID + ":" +
+                         Buid;
+            }
+            else//后支付
+            {
+                key = uploadTransactions.NozzleId + ":" +
+                       uploadTransactions.OriginalAmount + ":" +
+                       uploadTransactions.Qty + ":" +
+                       uploadTransactions.MiniProgramID + ":" +
+                       uploadTransactions.FuelItemTransactionEndTime + ":" +
+                        uploadTransactions.TransactionNumber + ":" +
+                       Buid;
+            }
+            transactions output = await GetRedisTransactions(uploadTransactions, key);
+            if (output != null)
+            {
+                return output;
+            }
+            var _product = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == guid && _.ProductName == uploadTransactions.Product);
+            var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == uploadTransactions.NozzleId);
+            var transactions = uploadTransactions.ToTransactions(uploadTransactions, guid, _product.FirstOrDefault(), _nozzle.FirstOrDefault());
+            var respond = await _entityHelper.InsertEntityAsync(transactions);
+            string jsonString = JsonConvert.SerializeObject(respond);
+
+            RedisHelper.SetAsync(key, jsonString, 3600);
+            return respond;
+        }
+        public async Task<List<transactions>> GetTransactionsAsync(TransactionsInput input)
+        {
+            string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
+            Guid guid = Guid.Parse(Buid);
+            Expression<Func<transactions, bool>> where = p => p.Buid == guid;
+            if (input.TransactionID != null)
+            {
+                where = CombineExpressions(where, p => p.Id == input.TransactionID);
+            }
+            if (input.type != null)
+            {
+                var status = (transactionsORDERSTATUS)input.type.Value;
+                where = CombineExpressions(where, p => p.OrderStatus == status);
+            }
+            if (input.MiniProgramID != null)
+            {
+                where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID);
+            }
+            if (input.TransactionSTime != null)
+            {
+                where = CombineExpressions(where, p => p.TransactionTime >= input.TransactionSTime);
+            }
+            if (input.TransactionETime != null)
+            {
+                where = CombineExpressions(where, p => p.TransactionTime == input.TransactionETime);
+            }
+            if (!string.IsNullOrEmpty(input.Product))
+            {
+                where = CombineExpressions(where, p => p.ProductName == input.Product);
+            }
+
+            return await _entityHelper.GetEntitiesAsync<transactions>(where);
+        }
+        /// <summary>
+        /// 查询redis订单缓存
+        /// </summary>
+        /// <returns></returns>
+        public async Task<transactions> GetRedisTransactions(UploadTransactions uploadTransactions,string key)
+        {
+            string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
+            var respond = RedisHelper.GetAsync(key).Result;
+            if (respond == null)
+            {
+                return null;
+            }
+            transactions transactions = JsonConvert.DeserializeObject<transactions>(respond);
+            return transactions;
+        }
+        // 辅助方法:组合两个表达式
+        private static Expression<Func<T, bool>> CombineExpressions<T>(Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
+        {
+            ParameterExpression param = expr1.Parameters[0];
+            Expression body = Expression.AndAlso(expr1.Body, Expression.Invoke(expr2, param));
+            return Expression.Lambda<Func<T, bool>>(body, param);
+        }
 
     }
 }

+ 49 - 0
FuelCloud/Fuel.Infrastructure/CoreRedisHelper.cs

@@ -0,0 +1,49 @@
+using CSRedis;
+using System.Collections.Concurrent;
+using System.Linq;
+
+namespace Fuel.Infrastructure
+{
+    /// <summary>
+    /// redis 多个database 客户端
+    /// </summary>
+    public class CoreRedisHelper
+    {
+        public static ConcurrentDictionary<string, CSRedisClient> redisHelpers
+        {
+            get;
+            private set;
+        }
+
+
+        public static bool AddClient(string clientName, CSRedisClient redisHelper)
+        {
+            if (redisHelpers == null)
+            {
+                redisHelpers = new ConcurrentDictionary<string, CSRedisClient>();
+            }
+            if (string.IsNullOrWhiteSpace(clientName))
+            {
+                return false;
+            }
+            return redisHelpers.TryAdd(clientName, redisHelper);
+        }
+
+        /// <summary>
+        /// 获取redis客户端名称
+        /// </summary>
+        /// <param name="clientName"></param>
+        /// <returns></returns>
+        public static CSRedisClient GetRedisHelper(string clientName)
+        {
+            if (redisHelpers.Any())
+            {
+                if (redisHelpers.Keys.Any(p => p == clientName))
+                {
+                    return redisHelpers[clientName];
+                }
+            }
+            return null;
+        }
+    }
+}

+ 12 - 0
FuelCloud/Fuel.Infrastructure/Extension/Class1.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Fuel.Infrastructure.Extension
+{
+    internal class Class1
+    {
+    }
+}

+ 74 - 0
FuelCloud/Fuel.Infrastructure/Extension/RedisListOptions.cs

@@ -0,0 +1,74 @@
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Fuel.Infrastructure.Extension
+{
+    public class RedisListOptions : IOptions<RedisListOptions>
+    {
+        public List<RedisConfig> RedisConfigs { get; set; }
+
+        public RedisListOptions Value => this;
+
+        public RedisListOptions()
+        {
+            RedisConfigs = new List<RedisConfig>();
+        }
+
+        /// <summary>
+        /// 添加的redis 
+        /// </summary>
+        /// <param name="redisConfig"></param>
+        public void TryAdd(RedisConfig redisConfig)
+        {
+            if (!RedisConfigs.Any(p => p.Connection == redisConfig.Connection && p.DataBase== redisConfig.DataBase))
+            {
+                RedisConfigs.Add(redisConfig);
+            }
+        }
+
+        public void Set(RedisConfig redisConfig)
+        {
+            if (!RedisConfigs.Any(p => p.Connection == redisConfig.Connection && p.DataBase == redisConfig.DataBase))
+            {
+                RedisConfigs.Remove(redisConfig);
+            }
+            RedisConfigs.Add(redisConfig);
+        }
+
+    }
+
+
+    /// <summary>
+    /// redis config 配置
+    /// </summary>
+    public class RedisConfig
+    {
+        /// <summary>
+        /// url 地址
+        /// </summary>
+        public string Connection { get; set; }
+
+        /// <summary>
+        /// 数据库编号
+        /// </summary>
+        public int DataBase { get; set; }
+
+        /// <summary>
+        /// 链接名称
+        /// </summary>
+        public string KeyName { get; set; }
+
+        /// <summary>
+        /// 缓存key 的前缀
+        /// </summary>
+        public string PrefixKey { get; set; }
+
+        /// <summary>
+        /// 哨兵地址
+        /// </summary>
+        public string[] Sentinels {get;set;}
+    }
+}

+ 46 - 0
FuelCloud/Fuel.Infrastructure/Extension/RedisOptions.cs

@@ -0,0 +1,46 @@
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Fuel.Infrastructure.Extension
+{
+    public class RedisOptions 
+    {
+        public RedisOptions Value =>this;
+
+        /// <summary>
+        /// url 地址
+        /// </summary>
+        public string ConnectionString { get; set; }
+
+        /// <summary>
+        /// 密码
+        /// </summary>
+        public string Password { get; set; }
+
+        /// <summary>
+        /// 数据库编号
+        /// </summary>
+        public int DataBase { get; set; }
+
+        /// <summary>
+        /// 链接名称
+        /// </summary>
+        public string KeyName { get; set; }
+
+        /// <summary>
+        /// 缓存key 的前缀
+        /// </summary>
+        public string PrefixKey { get; set; }
+
+        public int Default { get; set; }
+
+        /// <summary>
+        /// 哨兵地址
+        /// </summary>
+        public string[] Sentinels { get; set; }
+
+
+    }
+}

+ 86 - 0
FuelCloud/Fuel.Infrastructure/Extension/RedisServiceCollectionExtension.cs

@@ -0,0 +1,86 @@
+using CSRedis;
+using Microsoft.Extensions.Caching.Distributed;
+using Microsoft.Extensions.Caching.Redis;
+using Microsoft.Extensions.DependencyInjection;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Fuel.Infrastructure.Extension
+{
+    /// <summary>
+    /// redis 扩展方法
+    /// </summary>
+    public static class RedisServiceCollectionExtension
+    {
+        public static IServiceCollection AddRedisClients(this IServiceCollection services, RedisOptions options)
+        {
+            var redisConfig = options;
+            CSRedisClient redisClient = null;
+            List<string> panams = new List<string>();
+            if (redisConfig != null)
+            {
+                //暂时不支持多个数据库连接
+                //var config = redis.FirstOrDefault();
+                if (redisConfig.Sentinels != null && redisConfig.Sentinels.Any())
+                {
+                    //"127.0.0.1:6379,password=123,defaultDatabase=13,prefix=key前辍"
+                    redisClient =
+                        new CSRedisClient(
+                            $"{redisConfig.ConnectionString},defaultDatabase={redisConfig.DataBase},prefix={redisConfig.PrefixKey}",
+                            redisConfig.Sentinels);
+                }
+                else
+                {
+                    //"127.0.0.1:6379,password=123,defaultDatabase=13,prefix=key前辍"
+                    redisClient =
+                        new CSRedisClient(
+                            $"{redisConfig.ConnectionString},password={redisConfig.Password},defaultDatabase={redisConfig.DataBase},prefix={redisConfig.PrefixKey}");
+                }
+            }
+
+            CoreRedisHelper.AddClient(options.KeyName, redisClient);
+            if (CoreRedisHelper.redisHelpers.Any() && CoreRedisHelper.redisHelpers.Count <= 1)
+            {
+                RedisHelper.Initialization(redisClient);
+                services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
+            }
+
+            return services;
+        }
+
+        /// <summary>
+        /// 单个redis 兼容旧版本
+        /// </summary>
+        /// <param name="services"></param>
+        /// <returns></returns>
+        public static IServiceCollection UseRedisClient(this IServiceCollection services, RedisOptions redisOptions )
+        {
+            //var redisOptions = new RedisOptions();
+            //configuration.GetSection("Redis").Get<RedisOptions>();
+            services.AddRedisClients(redisOptions);
+            return services;
+        }
+
+        /// <summary>
+        /// 多个redis database 的问题
+        /// </summary>
+        /// <param name="services"></param>
+        /// <returns></returns>
+        public static IServiceCollection UseRedisClients(this IServiceCollection services,List<RedisOptions> redisClients)
+        {
+            if (redisClients.Any())
+            {
+                redisClients = redisClients.OrderByDescending(p => p.Default).ToList();
+                foreach (var client in redisClients)
+                {
+                    if (!string.IsNullOrWhiteSpace(client.KeyName))
+                    {
+                        services.AddRedisClients(client);
+                    }
+                }
+            }
+
+            return services;
+        }
+    }
+}

+ 160 - 0
FuelCloud/Fuel.Infrastructure/Extension/TimeExtension.cs

@@ -0,0 +1,160 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Fuel.Infrastructure.Extension
+{
+    public static class TimeExtension
+    {
+        /// <summary>
+        /// 返回 yyyy-dd-mm hh:mm:ss
+        /// </summary>
+        /// <param name="dateTime"></param>
+        /// <returns></returns>
+        public static string ToDateTimeString(this DateTime dateTime)
+        {
+            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
+        }
+
+        /// <summary>
+        /// 返回 yyyy-dd-mm hh:mm:ss
+        /// </summary>
+        /// <param name="dateTime"></param>
+        /// <returns></returns>
+        public static string ToDateTimeString(this DateTime? dateTime)
+        {
+
+            if (dateTime == null)
+            {
+                return "";
+            }
+            return ((DateTime)dateTime).ToString("yyyy-MM-dd HH:mm:ss");
+
+        }
+
+        /// <summary>
+        /// 获取本地时间的世界时间戳
+        /// </summary>
+        /// <returns></returns>
+        public static long GetNowTimeStamp(bool isInMillisecond = false)
+        {
+            int unit = isInMillisecond ? 10000 : 10000000;
+            var TimeStamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / unit;
+            return TimeStamps;
+        }
+
+
+        /// <summary>
+        /// 将时间转换为时间戳
+        /// </summary>
+        /// <param name="dateTime"></param>
+        /// <returns></returns>
+        public static long ToTimeStamp(DateTime dateTime, bool isInMillisecond = false)
+        {
+            int unit = isInMillisecond ? 10000 : 10000000;
+            var TimeStamps = (dateTime.Ticks - 621355968000000000) / unit;
+            return TimeStamps;
+        }
+
+        /// <summary>
+        /// 获取本地时间的世界时间的偏移量
+        /// </summary>
+        /// <returns></returns>
+        public static long GetUtcOffsets()
+        {
+            var UtcOffsets = TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime()).Ticks / 10000;
+            return UtcOffsets;
+        }
+
+        /// <summary>
+        /// 将时间戳转换为时间
+        /// </summary>
+        /// <returns></returns>
+        public static DateTime ToDateTime(this long TimeStamps, bool millisecond = true)
+        {
+            if (millisecond)
+            {
+                return new DateTime(1970, 1, 1).AddMilliseconds(TimeStamps);
+            }
+            else
+            {
+                return new DateTime(1970, 1, 1).AddSeconds(TimeStamps);
+            }
+        }
+
+        public static DateTime ToDateTime(long TimeStamps)
+        {
+            var date = new DateTime(1970, 1, 1).AddMilliseconds(TimeStamps);
+            return date;
+        }
+
+        /// <summary>
+        /// 将时间戳转换为时间
+        /// </summary>
+        /// <returns></returns>
+        public static DateTime ToTime(this string TimeStamps)
+        {
+            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+            try
+            {
+                if (TimeStamps.Length >= 13)
+                {
+                    return start.AddMilliseconds(Convert.ToInt64(TimeStamps)).AddHours(8);
+                }
+                else
+                {
+                    return start.AddSeconds(Convert.ToInt64(TimeStamps)).AddHours(8);
+                }
+            }
+            catch { }
+            return DateTime.Now.AddYears(-1);
+        }
+
+        /// <summary>
+        /// 将时间戳转换为时间
+        /// </summary>
+        /// <returns></returns>
+        public static DateTime ToUTCTime(this string TimeStamps)
+        {
+            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
+            try
+            {
+                if (TimeStamps.Length >= 13)
+                {
+                    return start.AddMilliseconds(Convert.ToInt64(TimeStamps));
+                }
+                else
+                {
+                    return start.AddSeconds(Convert.ToInt64(TimeStamps));
+                }
+            }
+            catch { }
+            return DateTime.Now.AddYears(-1);
+        }
+
+
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的秒数
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static double GetTotalSeconds(this in DateTime dt) => new DateTimeOffset(dt).ToUnixTimeSeconds();
+
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的毫秒数
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static double GetTotalMilliseconds(this in DateTime dt) => new DateTimeOffset(dt).ToUnixTimeMilliseconds();
+
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static long GetTotalMicroseconds(this in DateTime dt) => new DateTimeOffset(dt).Ticks / 10;
+
+    }
+}

+ 75 - 0
FuelCloud/Fuel.Infrastructure/RedisLockerHelper.cs

@@ -0,0 +1,75 @@
+using Fuel.Infrastructure.Extension;
+using System;
+
+namespace Fuel.Infrastructure
+{
+    /// <summary>
+    /// Redis实现锁帮助类
+    /// </summary>
+    public class RedisLockerHelper
+    {
+        /// <summary>
+        /// 获取redis分布式锁
+        /// </summary>
+        /// <param name="lockKeyName"></param>
+        /// <param name="lockTimeout">自动过期时间(毫秒)</param>
+        /// <returns></returns>
+        public static bool GetRedisDistributedLock(string lockKeyName, long lockTimeout)
+        {
+            if (SetNx(lockKeyName, TimeExtension.GetNowTimeStamp(true) + lockTimeout, lockTimeout))
+            {
+                return true;
+            }
+            else
+            {
+                //未获取到锁,继续判断,判断时间戳看看是否可以重置并获取锁
+                long lockValue = RedisHelper.Get<long>(lockKeyName);
+                long time = TimeExtension.GetNowTimeStamp(true);
+
+                if (time > lockValue)
+                {
+                    //再次用当前时间戳getset
+                    //返回固定key的旧值,旧值判断是否可以获取锁
+                    long getsetResult = RedisHelper.GetSet<long>(lockKeyName, time);
+                    if (getsetResult == lockValue)
+                    {
+                        RedisHelper.Expire(lockKeyName, TimeSpan.FromMilliseconds(lockTimeout));
+                        return true;
+                    }
+                    else
+                    {
+                        return false;
+                    }
+                }
+                else
+                {
+                    return false;
+                }
+            }
+
+        }
+
+        private static bool SetNx(string key, long time, double expireMS)
+        {
+
+            if (expireMS > 0)
+            {
+                if (RedisHelper.Set(key, time, TimeSpan.FromMilliseconds(expireMS), CSRedis.RedisExistence.Nx))
+                    return true;
+            }
+            else
+            {
+                if (RedisHelper.SetNx(key, time))
+                {
+                    RedisHelper.Expire(key, TimeSpan.FromMilliseconds(expireMS));
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+    }
+
+
+}

+ 0 - 15
FuelCloud/src/Fuel.Payment.Server/Controllers/Transactions.cs

@@ -1,15 +0,0 @@
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-
-namespace Fuel.PaymentServer.Controllers
-{
-    [Route("api/[controller]")]
-    [ApiController]
-    public class Transactions : ControllerBase
-    {
-        public Transactions()
-        { 
-            
-        }
-    }
-}

+ 42 - 0
FuelCloud/src/Fuel.Payment.Server/Controllers/TransactionsController.cs

@@ -0,0 +1,42 @@
+using Fuel.Application.Service;
+using Fuel.Core.Nozzle.Dto;
+using Fuel.Core.Transactions.Dto;
+using FuelServer.Core.Entity;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Fuel.PaymentServer.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class TransactionsController : ControllerBase
+    {
+        private readonly ITransactionsService _transactionsService;
+        public TransactionsController(ITransactionsService transactionsService)
+        { 
+             _transactionsService = transactionsService;
+        }
+        /// <summary>
+        /// 创建订单
+        /// </summary>
+        /// <param name="uploadTransactions"></param>
+        /// <returns></returns>
+        [Route("CreateTransactions")]
+        [HttpPost]
+        public async Task<transactions> CreateTransactionsAsync(UploadTransactions uploadTransactions)
+        {
+            return await _transactionsService.CreateTransactions(uploadTransactions);
+        }
+        /// <summary>
+        /// 查询订单
+        /// </summary>
+        /// <param name="input"></param>
+        /// <returns></returns>
+        [Route("GetTransactions")]
+        [HttpPost]
+        public async Task<List<transactions>> GetTransactionsAsync(TransactionsInput input)
+        {
+            return await _transactionsService.GetTransactionsAsync(input);
+        }
+    }
+}

+ 7 - 2
FuelCloud/src/Fuel.Payment.Server/Program.cs

@@ -1,6 +1,7 @@
 
 
 using DFS.Core.Mvc;
+using DFS.Infrastructure.Redis.Extension;
 using FreeSql;
 using Fuel.Application;
 using Fuel.Application.Repositories;
@@ -12,6 +13,8 @@ using Fuel.Payment.Service.Factory;
 using Fuel.Payment.Service.Pay;
 using Fuel.Payment.Service.UnionPayProcessor;
 using Fuel.PaymentServer.MicServer;
+using Fuel.Infrastructure.Extension;
+using CSRedis;
 
 var builder = WebApplication.CreateBuilder(args);
 builder.Services.AddScoped<IPayService, PayService>();
@@ -34,15 +37,17 @@ builder.Services.AddFreeSql(builder.Configuration);
 //³õʼ»¯DFS Server
 builder.Services.AddMicService(builder.Environment);
 
-
+builder.Services.AddHttpContextAccessor();
 builder.Services.AddScoped<INozzleRepository, NozzleRepository>();
 builder.Services.AddScoped<INozzleService, NozzleService>();
-
+builder.Services.AddScoped<ITransactionsService, TransactionsService>();
 
 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
 
+Fuel.Infrastructure.Extension.RedisOptions redisOptions = builder.Configuration.GetSection("Redis").Get<Fuel.Infrastructure.Extension.RedisOptions>();
+builder.Services.UseRedisClient(redisOptions);
 var app = builder.Build();
 
 

+ 6 - 0
FuelCloud/src/Fuel.Payment.Server/appsettings.json

@@ -8,5 +8,11 @@
       "Microsoft.AspNetCore": "Warning"
     }
   },
+  "Redis": {
+    "ConnectionString": "127.0.0.1:6379",
+    "DataBase": 2,
+    "PrefixKey": "TransServer_",
+    "KeyName": "Trans"
+  },
   "AllowedHosts": "*"
 }

+ 52 - 10
FuelCloud/src/FuelServer.Core/Entity/transactions.cs

@@ -50,13 +50,13 @@ namespace FuelServer.Core.Entity
 		/// 实际支付金额
 		/// </summary>
 		[JsonProperty]
-		public decimal ActualPaymentAmount { get; set; }
+		public decimal? ActualPaymentAmount { get; set; }
 
 		/// <summary>
 		/// 授权时间
 		/// </summary>
 		[JsonProperty, Column(DbType = "timestamp", InsertValueSql = "0000-00-00 00:00:00")]
-		public DateTime AuthorizationTime { get; set; }
+		public DateTime? AuthorizationTime { get; set; }
 
 		[JsonProperty, Column(StringLength = 100)]
 		public string CreateBy { get; set; }
@@ -68,7 +68,7 @@ namespace FuelServer.Core.Entity
 		/// 挂枪时间
 		/// </summary>
 		[JsonProperty, Column(DbType = "timestamp", InsertValueSql = "CURRENT_TIMESTAMP")]
-		public DateTime FuelItemTransactionEndTime { get; set; }
+		public DateTime? FuelItemTransactionEndTime { get; set; }
 
 		/// <summary>
 		/// 是否删除
@@ -92,7 +92,7 @@ namespace FuelServer.Core.Entity
 		/// 原金额
 		/// </summary>
 		[JsonProperty]
-		public decimal OriginalAmount { get; set; }
+		public decimal? OriginalAmount { get; set; }
 
 		/// <summary>
 		/// 支付方式
@@ -132,10 +132,52 @@ namespace FuelServer.Core.Entity
 
 	}
 
-	public enum transactionsORDERSTATUS {
-		未支付 = 1, 已支付, 支付中, 退全款, 退余额, 卡支付
-	}
-	public enum transactionsORDERTYPE {
-		预支付 = 1, 后支付
-	}
+    public enum transactionsORDERSTATUS
+    {
+        /// <summary>
+        /// 订单未支付。
+        /// </summary>
+        Unpaid,
+
+        /// <summary>
+        /// 订单已支付。
+        /// </summary>
+        Paid,
+
+        /// <summary>
+        /// 订单正在支付中。
+        /// </summary>
+        Paying,
+
+        /// <summary>
+        /// 订单已全额退款。
+        /// </summary>
+        FullyRefunded,
+
+        /// <summary>
+        /// 订单已部分退款。
+        /// </summary>
+        PartiallyRefunded,
+
+        /// <summary>
+        /// 订单通过卡支付。
+        /// </summary>
+        CardPayment
+    }
+
+    /// <summary>
+    /// 表示交易订单的类型。
+    /// </summary>
+    public enum transactionsORDERTYPE
+    {
+        /// <summary>
+        /// 预支付订单。
+        /// </summary>
+        Prepayment,
+
+        /// <summary>
+        /// 后支付订单。
+        /// </summary>
+        Postpayment
+    }
 }

+ 1 - 0
FuelCloud/src/FuelServer.Core/Fuel.Core.csproj

@@ -7,6 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="DFS.Infrastructure.Redis" Version="8.0.0" />
     <PackageReference Include="FreeSql" Version="3.2.833" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
   </ItemGroup>

+ 54 - 0
FuelCloud/src/FuelServer.Core/Transactions/Dto/TransactionsInput.cs

@@ -0,0 +1,54 @@
+using FuelServer.Core.Entity;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Fuel.Core.Transactions.Dto
+{
+    public class TransactionsInput
+    {
+        /// <summary>
+        /// 订单id
+        /// </summary>
+        public int? TransactionID { get; set; }
+        /// <summary>
+        /// 订单状态
+        /// </summary>
+        public int? type { get; set; }
+        /// <summary>
+        /// 小程序用户id
+        /// </summary>
+        public int? MiniProgramID { get; set; }
+        /// <summary>
+        /// 开始时间
+        /// </summary>
+        public DateTime? TransactionSTime { get; set; }
+        /// <summary>
+        /// 结束时间
+        /// </summary>
+        public DateTime? TransactionETime { get; set; }
+        /// <summary>
+        /// 油品
+        /// </summary>
+        public string Product { get; set; }
+        /// <summary>
+        /// 支付类型
+        /// </summary>
+        public string paytype { get; set; }
+
+        public static transactionsORDERSTATUS ToTransactionsORDERSTATUS(int value)
+        {
+            if (Enum.IsDefined(typeof(transactionsORDERSTATUS), value))
+            {
+                return (transactionsORDERSTATUS)value;
+            }
+            else
+            {
+                throw new ArgumentException("Invalid order type value.", nameof(value));
+            }
+        }
+
+    }
+}

+ 81 - 0
FuelCloud/src/FuelServer.Core/Transactions/Dto/TransactionsOutput.cs

@@ -0,0 +1,81 @@
+using FuelServer.Core.Entity;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Fuel.Core.Transactions.Dto
+{
+    public class TransactionsOutput
+    {
+        /// <summary>
+        /// 订单唯一标识符
+        /// </summary>
+        public int Id { get; set; }
+        /// <summary>
+        /// 油站唯一标识符
+        /// </summary>
+        public Guid Buid { get; set; }
+        /// <summary>
+        /// 用户ID
+        /// </summary>
+        public int MiniProgramID { get; set; }
+        /// <summary>
+        /// 枪号
+        /// </summary>
+        public int NozzleId { get; set; }
+        /// <summary>
+        /// 油品名称
+        /// </summary>
+        public string ProductName { get; set; }
+        /// <summary>
+        /// 实际支付金额
+        /// </summary>
+        public decimal? ActualPaymentAmount { get; set; }
+        /// <summary>
+        /// 授权时间
+        /// </summary>
+        public DateTime? AuthorizationTime { get; set; }
+        /// <summary>
+        /// 挂枪时间
+        /// </summary>
+        public DateTime? FuelItemTransactionEndTime { get; set; }
+        /// <summary>
+        /// 订单状态
+        /// </summary>
+        public transactionsORDERSTATUS OrderStatus { get; set; }
+        /// <summary>
+        /// 订单类型
+        /// </summary>
+        public transactionsORDERTYPE OrderType { get; set; }
+
+        /// <summary>
+        /// 原金额
+        /// </summary>
+        public decimal OriginalAmount { get; set; }
+        /// <summary>
+        /// 支付方式
+        /// </summary>
+        public string PaymentMethod { get; set; }
+
+        /// <summary>
+        /// 升数
+        /// </summary>
+        public decimal Qty { get; set; }
+        /// <summary>
+        /// 退款金额
+        /// </summary>
+        public decimal? RefundAmount { get; set; }
+        /// <summary>
+        /// 订单流水号
+        /// </summary>
+        public string TransactionNumber { get; set; }
+
+        /// <summary>
+        /// 交易时间
+        /// </summary>
+        public DateTime TransactionTime { get; set; }
+    }
+}

+ 101 - 0
FuelCloud/src/FuelServer.Core/Transactions/Dto/UploadTransactions.cs

@@ -0,0 +1,101 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using FuelServer.Core.Entity;
+using Newtonsoft.Json;
+
+namespace Fuel.Core.Transactions.Dto
+{
+    public class UploadTransactions
+    {
+        /// <summary>
+        /// 油枪
+        /// </summary>
+        public int NozzleId { get; set; }
+        /// <summary>
+        /// 实际加油金额
+        /// </summary>
+        public decimal? OriginalAmount { get; set; }
+        /// <summary>
+        /// 实际支付金额
+        /// </summary>
+        public decimal? ActualPaymentAmount { get; set; }
+        /// <summary>
+        /// 升数
+        /// </summary>
+        public decimal Qty { get; set; }
+        /// <summary>
+        /// 挂枪时间
+        /// </summary>
+        public DateTime? FuelItemTransactionEndTime { get; set; }
+        /// <summary>
+        /// 交易时间
+        /// </summary>
+        public DateTime? TransactionTime { get; set; }
+        /// <summary>
+        /// 油品
+        /// </summary>
+        public string Product { get; set; }
+        /// <summary>
+        /// 用户id
+        /// </summary>
+        public int MiniProgramID { get; set; }
+        /// <summary>
+        /// 订单流水号
+        /// </summary>
+        public string TransactionNumber { get; set; }
+        /// <summary>
+        /// 1预支付、2后支付 
+        /// </summary>
+        public int Type { get; set; }
+        /// <summary>
+        /// 支付方式
+        /// </summary>
+        public string PaymentMethod { get; set; }
+
+        public transactions ToTransactions(UploadTransactions upload, Guid Buid, product product, nozzle nozzle)
+        {
+            return new transactions()
+            {
+                Buid = Buid,
+                NozzleId = upload.NozzleId,
+                ProductId = product.Id,
+                ActualPaymentAmount = upload.ActualPaymentAmount,
+                FuelItemTransactionEndTime = upload.FuelItemTransactionEndTime,
+                OrderStatus = transactionsORDERSTATUS.Unpaid,
+                OrderType = ToTransactionsOrderType(upload.Type),
+                OriginalAmount = upload.ActualPaymentAmount,
+                PaymentMethod = upload.PaymentMethod,
+                ProductName = product.ProductName,
+                Qty = upload.Qty,
+                TransactionNumber = upload.TransactionNumber,
+                TransactionTime = DateTime.Now
+            };
+        }
+        public static transactionsORDERTYPE ToTransactionsOrderType(int value)
+        {
+            if (Enum.IsDefined(typeof(transactionsORDERTYPE), value))
+            {
+                return (transactionsORDERTYPE)value;
+            }
+            else
+            {
+                throw new ArgumentException("Invalid order type value.", nameof(value));
+            }
+        }
+        public static transactionsORDERSTATUS ToTransactionsORDERSTATUS(int value)
+        {
+            if (Enum.IsDefined(typeof(transactionsORDERSTATUS), value))
+            {
+                return (transactionsORDERSTATUS)value;
+            }
+            else
+            {
+                throw new ArgumentException("Invalid order type value.", nameof(value));
+            }
+        }
+    }
+}
+}