using DBI.SaaS.Web.Models.Args;
using Rafy;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace DBI.SaaS.Web.Common
{
/// <summary>
/// Redis帮助类
/// </summary>
public class RedisHelper
{
private static ConnectionMultiplexer _connectionMultiplexer;
/// <summary>
/// 连接多工
/// </summary>
public static ConnectionMultiplexer ConnectionMultiplexer
{
get
{
if (_connectionMultiplexer == null||_connectionMultiplexer.IsConnected==false)
{
var server = ConfigurationHelper.GetAppSettingOrDefault("RedisUri");
var port = ConfigurationHelper.GetAppSettingOrDefault("RedisPort");
var configString = $"{server}:{port}";
var config = ConfigurationOptions.Parse(configString);
config.Password = ConfigurationHelper.GetAppSettingOrDefault("RedisPassword");
_connectionMultiplexer = ConnectionMultiplexer.Connect(config);
}
return _connectionMultiplexer;
}
}
public const string CustomerInfoHeaderKey = "RCIHK";
/// <summary>
/// 添加Redis数据库中key下的值
/// </summary>
/// <param name="redisKey"></param>
/// <param name="customer"></param>
public static void AddCustomerInfoToKey(string redisKey, PMSCustomerInfo customer)
{
customer.SetTime = DateTime.Now.ToString();
customer.Identity = string.Empty;//RedisHelper.GetNextCustomerIdentity(redisKey);
customer.IsUsed = "false";
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
var value = RedisHelper.ConvertRedisValueFromCustomerInfo(customer);
rdb.ListRightPush(redisKey, value);
}
private static string GetNextCustomerIdentity(string redisKey)
{
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
var value = rdb.ListRightPop(redisKey).ToString();
if (string.IsNullOrEmpty(value)) return "1";
rdb.ListRightPush(redisKey, value);
var valueArray = value.Split(',');
if (valueArray.Count() != 10) return "1";
var identity = Convert.ToInt64(valueArray[9]) + 1;
return identity.ToString();
}
/// <summary>
/// 将当前推送客户信息转为Redis值
/// </summary>
/// <param name="customer"></param>
/// <returns></returns>
private static string ConvertRedisValueFromCustomerInfo(PMSCustomerInfo customer)
{
var sb = new StringBuilder();
sb.Append(customer.CustomerAddressPhone).Append(",");
sb.Append(customer.CustomerBankAccount).Append(",");
sb.Append(customer.CustomerName).Append(",");
sb.Append(customer.CustomerTaxCode).Append(",");
sb.Append(customer.ALipayId).Append(",");
sb.Append(customer.WechatId).Append(",");
sb.Append(customer.PhoneNumber).Append(",");
sb.Append(customer.SetTime).Append(",");
sb.Append(customer.IsUsed).Append(",");
sb.Append(customer.Identity);
return sb.ToString();
}
/// <summary>
/// 根据key获取客户信息
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public static List<PMSCustomerInfo> GetCustomerListByKey(string redisKey)
{
var customerList = new List<PMSCustomerInfo>();
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
//RedisHelper.CheckCustomerExpire(redisKey);
var values = rdb.ListRange(redisKey).ToStringArray().ToList();
foreach (var value in values)
{
var valueArray = value.Split(',');
if (valueArray.Count() != 10) continue;
var customer = new PMSCustomerInfo
{
CustomerAddressPhone = valueArray[0],
CustomerBankAccount = valueArray[1],
CustomerName = valueArray[2],
CustomerTaxCode = valueArray[3],
ALipayId = valueArray[4],
WechatId = valueArray[5],
PhoneNumber = valueArray[6],
SetTime = valueArray[7],
IsUsed = valueArray[8],
Identity = valueArray[9]
};
customerList.Add(customer);
}
var returnList = customerList.OrderByDescending(e =>Convert.ToDateTime(e.SetTime)).ToList();
return returnList;
}
private static void CheckCustomerExpire(string redisKey)
{
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
while (true)
{
var value = rdb.ListLeftPop(redisKey).ToString();
if (string.IsNullOrEmpty(value)) return;
var valueArray = value.Split(',');
if (valueArray.Count() != 10) continue;
var setTime = Convert.ToDateTime(valueArray[7]);
if (setTime.AddDays(2) > DateTime.Now)
{
rdb.ListLeftPush(redisKey, value);
break;
}
}
}
public static void UpdateCustomerIsUsed(PMSCustomerInfo customer, string redisKey)
{
var list = new List<string>();
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
while (true)
{
var redisValue = rdb.ListLeftPop(redisKey).ToString();
if (string.IsNullOrEmpty(redisValue)) break;
var customerValue = ConvertRedisValueFromCustomerInfo(customer);
if (customerValue == redisValue)
{
customer.IsUsed = "true";
redisValue = ConvertRedisValueFromCustomerInfo(customer);
}
list.Add(redisValue);
}
for (int i = list.Count - 1; i >= 0; i--)
{
rdb.ListLeftPush(redisKey, list[i]);
}
}
public static void RemoveCustomerFromRedis(PMSCustomerInfo customer, string redisKey)
{
var list = new List<string>();
var rdb = RedisHelper.ConnectionMultiplexer.GetDatabase();
while (true)
{
var redisValue = rdb.ListLeftPop(redisKey).ToString();
if (string.IsNullOrEmpty(redisValue)) break;
var customerValue = ConvertRedisValueFromCustomerInfo(customer);
if (customerValue == redisValue) break;
list.Add(redisValue);
}
for (int i = list.Count - 1; i >= 0; i--)
{
rdb.ListLeftPush(redisKey, list[i]);
}
}
}
}