using Operater.DAL;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using CommonUtil;
using Operater.DbModel;
namespace ScheduledTasks.Handle
{
public class DataStatisticsHandle : IJob
{
///
/// 数据统计
///
///
///
public async Task Execute(IJobExecutionContext context)
{
await Synchronize(context);
}
public static async Task Synchronize(IJobExecutionContext context)
{
try
{
//获取上次统计时间
string timeSql = $"select max(static_time) as static_time from data_static;";
DataTable timeDt = new YzOrderinfoDAL().SearchBySqlDataTable("", timeSql);
DateTime staticTime = DateTime.Now.AddDays(-1).Date;
//if (!timeDt.IsNull() && !timeDt.Rows[0]["static_time"].IsNull() && !timeDt.Rows[0]["static_time"].ToString().IsNullOrEmpty())
//{
// staticTime = Convert.ToDateTime(timeDt.Rows[0]["static_time"].ToString()).Date;
//}
//if (staticTime == DateTime.Now.AddDays(-1).Date)
//{
// Console.WriteLine($"已经统计过,跳过!");
// return;
//}
DataStatic dataStatic = new DataStatic()
{
Id = Guid.NewGuid().ToString(),
StaticTime = staticTime
};
string relaStr = $"SELECT yz_tid as tid FROM trade_create_relation where run_time>='{staticTime.ToString("yyyy-MM-dd 00:00:00")}' and run_time<='{staticTime.ToString("yyyy-MM-dd 23:59:59")}' and run_status='2'";
string staticCount = $"SELECT count(1) as yz_count,sum(payment) as yz_total_fee from yz_tradeinfo where tid in({relaStr}) ";
DataTable staticCountDt = new YzOrderinfoDAL().SearchBySqlDataTable("", staticCount);
if (!staticCountDt.IsNull())
{
//有赞订单量
dataStatic.YzTradeCount = int.Parse(staticCountDt.Rows[0]["yz_count"].ToString());
//有赞订单总金额
dataStatic.YzTotalFee = staticCountDt.Rows[0]["yz_total_fee"].ToString().IsNullOrEmpty()?0:decimal.Parse(staticCountDt.Rows[0]["yz_total_fee"].ToString());
}
staticCount = $"SELECT count(1) as yz_count,sum(real_pay) as realpay from yz_tradeinfo where tid in({relaStr}) and real_pay>0";
staticCountDt = new YzOrderinfoDAL().SearchBySqlDataTable("", staticCount);
if (!staticCountDt.IsNull())
{
//有赞付款订单量
dataStatic.YzPayCount = int.Parse(staticCountDt.Rows[0]["yz_count"].ToString());
//有赞付款金额
dataStatic.YzPayment = staticCountDt.Rows[0]["realpay"].ToString().IsNullOrEmpty()?0: decimal.Parse(staticCountDt.Rows[0]["realpay"].ToString());
}
relaStr = $"SELECT ali_tid as tid FROM trade_create_relation where run_time>='{staticTime.ToString("yyyy-MM-dd 00:00:00")}' and run_time<='{staticTime.ToString("yyyy-MM-dd 23:59:59")}' and run_status='2'";
staticCount = $"select sum(sum_product_payment+shipping_fee) as payment from ali_tradeinfo where tid in({relaStr})";
staticCountDt = new YzOrderinfoDAL().SearchBySqlDataTable("", staticCount);
if (!staticCountDt.IsNull())
{
//1688订单金额
dataStatic.AliTotalFee = staticCountDt.Rows[0]["payment"].ToString().IsNullOrEmpty()?0: decimal.Parse(staticCountDt.Rows[0]["payment"].ToString());
dataStatic.ProfitPay = dataStatic.YzPayment - dataStatic.AliTotalFee;
}
//获取时间内交易成功的订单
string yzFeeStr = $"select sum(payment) as yz_total_fee from yz_tradeinfo where success_time>='{staticTime.ToString("yyyy-MM-dd 00:00:00")}' and success_time<='{staticTime.ToString("yyyy-MM-dd 23:59:59")}'";
staticCountDt = new YzOrderinfoDAL().SearchBySqlDataTable("", yzFeeStr);
if (!staticCountDt.IsNull() && !staticCountDt.Rows[0]["yz_total_fee"].ToString().IsNullOrEmpty() && decimal.Parse(staticCountDt.Rows[0]["yz_total_fee"].ToString()) > 0)
{//有交易成功的订单
string successYzStr = $"select tid from yz_tradeinfo where success_time>='{staticTime.ToString("yyyy-MM-dd 00:00:00")}' and success_time<='{staticTime.ToString("yyyy-MM-dd 23:59:59")}'";
string relaALiTid = $"select ali_tid as tid FROM trade_create_relation where yz_tid in({successYzStr})";
relaALiTid = $"select sum(sum_product_payment+shipping_fee) as payment from ali_tradeinfo where tid in({relaALiTid})";
DataTable aliSucDt = new YzOrderinfoDAL().SearchBySqlDataTable("", relaALiTid);
if (!aliSucDt.IsNull())
{
dataStatic.ProfitSuccess = decimal.Parse(staticCountDt.Rows[0]["yz_total_fee"].ToString()) - decimal.Parse(aliSucDt.Rows[0]["payment"].ToString());
}
}
else
{
dataStatic.ProfitSuccess = 0;
}
new DataStaticDAL().Insert(dataStatic);
}
catch (Exception e)
{
Console.WriteLine($"数据统计错误:{e.ToString()}");
LogUtil.Info(e.ToString(), "数据统计错误");
}
}
}
}