yz_server/Server/Timers/FinishOrderTimer.cs

365 lines
20 KiB
C#
Raw Permalink Normal View History

2022-04-16 07:48:12 +00:00
using Common.DbExtends.Extends;
using Common.Models.SubTables;
using Common.Models.UnqTables;
using Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using Common.Models.Enums;
using Common.Models.JsonModels;
using Common.Models.SubCountTables;
using Common.DbExtends.Others;
using Common.Utils.Extends;
namespace Server.Timers
{
public class FinishOrderTimer : MyTimer
{
Client Client = Client.SingleClient;
private SqlSugarClient Db { get { return Client.Db; } }
protected override void Run(object state, bool timedOut)
{
try
{
///读取已经解冻的订单
var Orders = Client.Db.Queryable<FinishOrder>().Where(f => f.ThawingTime <= DateTime.Now).OrderBy(f => f.CreateTime, OrderByType.Asc).Take(30).ToList();
if (Orders.Count > 0)
{
foreach (var item in Orders)
{
switch (item.LianmengType)
{
case LianmengType.:
{
var tabName = Db.GetTableNameById<TbOrder>(item.OrderId);
var order = Db.Queryable<TbOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
if (order.total_commission_fee != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = order.pub_share_pre_fee;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.item_id, LianmengType., order.LianmengId, $"{order.site_id}_{order.adzone_id}", order.pub_share_pre_fee);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.item_title, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
var tabName = Db.GetTableNameById<JdOrder>(item.OrderId);
var order = Db.Queryable<JdOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
if (order.actualFee != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = order.actualFee;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.skuId, LianmengType., order.LianmengId, $"{order.positionId}", order.actualFee);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.skuName, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
var tabName = Db.GetTableNameById<PddOrder>(item.OrderId);
var order = Db.Queryable<PddOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
var totalComm = (double)order.promotion_amount.Divided100();//分转元
if (totalComm != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = totalComm;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.goods_id.ToString(), LianmengType., order.LianmengId, $"{order.p_id}", totalComm);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.goods_name, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
var tabName = Db.GetTableNameById<WphOrder>(item.OrderId);
var order = Db.Queryable<WphOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
if (order.commission != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = order.commission;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.goodsId, LianmengType., order.LianmengId, $"{order.pid}", order.commission);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.goodsName, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
var tabName = Db.GetTableNameById<SnOrder>(item.OrderId);
var order = Db.Queryable<SnOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
var totalComm = double.Parse(order.prePayCommission);
if (totalComm != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = totalComm;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.goodsNum, LianmengType., order.LianmengId, $"", totalComm);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.productName, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
var tabName = Db.GetTableNameById<DyOrder>(item.OrderId);
var order = Db.Queryable<DyOrder>().Where(f => f.Id == item.OrderId).SplitTable(tab => tab.InTableNames(tabName)).First();
//如果佣金发生了变化
var totalComm = (double)order.estimated_comission.Divided100();
if (totalComm != order.QueryCache.TotalCommission)
{
//再不改变比例的情况下,重新计算
order.QueryCache.TotalCommission = totalComm;
order.QueryCache.CalculationUserCommission();
order.QueryCache.CalculationInviteeCommission();
}
var user = Db.GetUserData(order.UserId);
//没有查询记录,就自动创建一个
var cache = order.QueryCache;
if (cache == null)
{
var robotData = Db.GetRobot(user.RobotId);
if (robotData != null)
{
var rConfig = Db.GetRebateConfigById(robotData.ConfigRebateId);
var query = new QueryHist(user, order.product_id, LianmengType., order.LianmengId, $"", totalComm);
if (Db.CalculationData(query, rConfig))
{
order.QueryCache = query;
var bConfig = Db.GetBaseConfigById(robotData.ConfigBaseId);
if (bConfig != null)
{
Db.CalculationData(query, bConfig);
}
}
}
}
if (cache != null)
{
Db.PayPoint(order, cache, order.Id, order.product_name, true);
}
Db.Updateable(order).SplitTable(tab => tab.InTableNames(tabName)).ExecuteCommand();
Db.Deleteable(item).ExecuteCommand();
Client.SendClientMsg(cache.RobotId, DeviceMessageType., new { OrderId = order.Id, TableName = tabName });
}
break;
case LianmengType.:
{
//TODO 美团是否有?
}
break;
case LianmengType.:
break;
default:
break;
}
}
}
}
catch (Exception ex)
{
Client.Db.OnLog("订单结算", ex);
}
}
/// <summary>
/// 百分之probability的中奖概率
/// </summary>
/// <param name="probability"></param>
/// <returns></returns>
private bool IsGetHb(int probability)
{
int a = new Random(Guid.NewGuid().GetHashCode()).Next(1, 100); // [1,100]
if (a <= probability)
{
return true;
}
return false;
}
}
}