Commit f0c0bf81 by guanzhenshan

增加保存订单明细数据

parent 72452de9
using System;
using System.Collections.Generic;
using System.Text;
namespace Bailun.DC.Models
{
/// <summary>
/// 新 月销售利润报表 订单明细
/// </summary>
public class dc_month_sales_profit_orderdetail
{
/// <summary>
///
/// </summary>
public int id { get; set; }
/// <summary>
/// 月份
/// </summary>
public string month { get; set; }
/// <summary>
/// dc_base_oms_pick的id
/// </summary>
public int pickid { get; set; }
/// <summary>
/// dc_base_oms_sku的id
/// </summary>
public int orderskuid { get; set; }
/// <summary>
/// 百伦sku
/// </summary>
public string bailun_sku { get; set; }
/// <summary>
/// 产品销售额 原币
/// </summary>
public decimal amount_sales { get; set; }
/// <summary>
/// 产品成本 rmb
/// </summary>
public decimal cost_product { get; set; }
/// <summary>
/// 平台费 原币 ,ebay 取other汇率
/// </summary>
public decimal cost_platform_fee { get; set; }
/// <summary>
/// paypal费 原币
/// </summary>
public decimal cost_paypal_fee { get; set; }
/// <summary>
/// 头程费 rmb
/// </summary>
public decimal cost_first { get; set; }
/// <summary>
/// 尾程费 rmb
/// </summary>
public decimal cost_logistics { get; set; }
/// <summary>
/// 平台操作费 rmb
/// </summary>
public decimal cost_handle_platform { get; set; }
/// <summary>
/// 物流总费用
/// </summary>
public decimal total_fee { get; set; }
/// <summary>
/// 物流运输费
/// </summary>
public decimal shipping { get; set; }
/// <summary>
/// 订单汇率
/// </summary>
public decimal seller_order_exchange_rate { get; set; }
/// <summary>
/// 订单其他汇率
/// </summary>
public decimal seller_other_exchange_rate { get; set; }
/// <summary>
/// 物流汇率
/// </summary>
public decimal wms_to_cny_exchange_rate { get; set; }
/// <summary>
/// 发货数量
/// </summary>
public int quantity_shipped { get; set; }
/// <summary>
/// 发货时间,FBA的是创建时间
/// </summary>
public DateTime shipping_time { get; set; }
/// <summary>
/// 平台类型
/// </summary>
public string platform_type { get; set; }
/// <summary>
/// fba费 原币
/// </summary>
public decimal cost_fba_fee { get; set; }
/// <summary>
/// 财务分类
/// </summary>
public string financecategoryname { get; set; }
/// <summary>
///
/// </summary>
public DateTime createtime { get; set; }
}
}
...@@ -7,26 +7,26 @@ namespace Bailun.DC.MonthSaleProfit ...@@ -7,26 +7,26 @@ namespace Bailun.DC.MonthSaleProfit
{ {
class Program class Program
{ {
static async Task Main(string[] args) //static async Task Main(string[] args)
{
Console.WriteLine("启动服务 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
var builder = new HostBuilder().ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Services>();
});
await builder.RunConsoleAsync();
}
//static void Main(string[] args)
//{ //{
// var _services = new Services(); // Console.WriteLine("启动服务 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
// var builder = new HostBuilder().ConfigureServices((hostContext, services) =>
// {
// services.AddHostedService<Services>();
// });
// var start = DateTime.Parse("2020-12-01"); // await builder.RunConsoleAsync();
// //_services.Init(start);
// _services.InitNew(start);
// //_services.InitNew4K(start);
// //_services.InitNew4K(start);
//} //}
static void Main(string[] args)
{
var _services = new Services();
var start = DateTime.Parse("2021-1-01");
//_services.Init(start);
_services.InitNew(start);
//_services.InitNew4K(start);
//_services.InitNew4K(start);
}
} }
} }
...@@ -456,8 +456,8 @@ namespace Bailun.DC.MonthSaleProfit ...@@ -456,8 +456,8 @@ namespace Bailun.DC.MonthSaleProfit
public void InitNew(DateTime day) public void InitNew(DateTime day)
{ {
var objOrder = new List<Models.mOrder>(); var objOrder = new List<dc_month_sales_profit_orderdetail>();
var objFBA = new List<Models.mOrder>(); var objFBA = new List<dc_month_sales_profit_orderdetail>();
var sql = ""; var sql = "";
var m = new dc_month_sales_profit var m = new dc_month_sales_profit
...@@ -522,6 +522,9 @@ namespace Bailun.DC.MonthSaleProfit ...@@ -522,6 +522,9 @@ namespace Bailun.DC.MonthSaleProfit
rate_profit_actual=0, rate_profit_actual=0,
}; };
//先保存订单明细,为了下次更新时,订单数据能不变
SaveOrderDetail(day);
using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString_read)) using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString_read))
{ {
if (cn.State == System.Data.ConnectionState.Closed) if (cn.State == System.Data.ConnectionState.Closed)
...@@ -537,22 +540,13 @@ namespace Bailun.DC.MonthSaleProfit ...@@ -537,22 +540,13 @@ namespace Bailun.DC.MonthSaleProfit
//手续费 //手续费
var listHandleFee = new Bailun.DC.Services.FinanceReportServices().ListFinanceHandleFee(day, day.AddMonths(1).AddDays(-1), 1); var listHandleFee = new Bailun.DC.Services.FinanceReportServices().ListFinanceHandleFee(day, day.AddMonths(1).AddDays(-1), 1);
sql = $@"select sum(t2.amount_sales*t1.quantity_shipped*t2.seller_order_exchange_rate) as amount_sales,sum(t2.cost_product*t1.quantity_shipped) as cost_product,sum(t2.cost_platform_fee*(if(t2.platform_type='Ebay',t2.seller_other_exchange_rate,t2.seller_order_exchange_rate))*t1.quantity_shipped) as cost_platform_fee,sum(t2.cost_paypal_fee*t2.seller_order_exchange_rate*t1.quantity_shipped) as cost_paypal_fee,sum(t2.cost_first*t1.quantity_shipped) as cost_first,sum(t2.cost_logistics*t1.quantity_shipped) as cost_logistics,sum(t2.cost_handle_platform*t1.quantity_shipped) as cost_handle_platform,if(t6.financecategoryname is null,'',t6.financecategoryname) as financecategoryname,((t2.total_fee-t2.shipping)*t2.wms_to_cny_exchange_rate*t1.quantity_shipped) as total_fee from dc_base_oms_pick t1 //自发货订单明细
join dc_base_oms_sku t2 on t1.bailun_order_id = t2.bailun_order_id and t1.bailun_sku=t2.bailun_sku and t2.bailun_order_status != 'Canceled' and t2.has_scalp = 0 and ((t2.platform_type!='FBA' and t2.bailun_order_status!='CantHandle') or t2.platform_type='FBA') and t2.has_scalp = 0 and t2.has_innersale = 0 and t2.has_delete=0 and t2.company_id=1 sql = $@"select sum(amount_sales*quantity_shipped*seller_order_exchange_rate) as amount_sales,sum(cost_product*quantity_shipped) as cost_product,sum(cost_platform_fee*(if(platform_type='Ebay',seller_other_exchange_rate,seller_order_exchange_rate))*quantity_shipped) as cost_platform_fee,sum(cost_paypal_fee*seller_order_exchange_rate*quantity_shipped) as cost_paypal_fee,sum(cost_first*quantity_shipped) as cost_first,sum(cost_logistics*quantity_shipped) as cost_logistics,sum(cost_handle_platform*quantity_shipped) as cost_handle_platform,if(financecategoryname is null,'',financecategoryname) as financecategoryname,((total_fee-shipping)*wms_to_cny_exchange_rate*quantity_shipped) as total_fee from dc_month_sales_profit_orderdetail where month='{day.ToString("yyyy-MM")}' and platform_type!='FBA' group by financecategoryname";
left join dc_base_sku t5 on t2.bailun_sku=t5.bailun_sku objOrder = cn.Query<dc_month_sales_profit_orderdetail>(sql).ToList();
left join dc_base_sku_finance_category t6 on t6.bailuncategoryid=t5.bailun_category_id
where t1.has_delete=0 and t1.shipping_status = 'TotalShipping' and t1.company_id=1 and t1.shipping_time>='{day.ToString("yyyy-MM-dd")}' and t1.shipping_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}'
group by t6.financecategoryname
";
objOrder = cn.Query<Models.mOrder>(sql,null,null,true,6*60).ToList();
sql = $@"select sum(amount_sales*seller_order_exchange_rate*bailun_sku_quantity_ordered) as amount_sales,sum(cost_product*bailun_sku_quantity_ordered) as cost_product,sum(cost_platform_fee*seller_order_exchange_rate*bailun_sku_quantity_ordered) as cost_platform_fee,sum(cost_first*bailun_sku_quantity_ordered) as cost_first,sum(cost_logistics*bailun_sku_quantity_ordered) as cost_logistics,sum(cost_fba_fee*seller_order_exchange_rate*bailun_sku_quantity_ordered) cost_fba_fee,if(t6.financecategoryname is null,'',t6.financecategoryname) as financecategoryname from dc_base_oms_sku tb left join dc_base_sku t5 on tb.bailun_sku=t5.bailun_sku //FBA订单明细
left join dc_base_sku_finance_category t6 on t6.bailuncategoryid=t5.bailun_category_id sql = $@"select sum(amount_sales*quantity_shipped*seller_order_exchange_rate) as amount_sales,sum(cost_product*quantity_shipped) as cost_product,sum(cost_platform_fee*(if(platform_type='Ebay',seller_other_exchange_rate,seller_order_exchange_rate))*quantity_shipped) as cost_platform_fee,sum(cost_paypal_fee*seller_order_exchange_rate*quantity_shipped) as cost_paypal_fee,sum(cost_first*quantity_shipped) as cost_first,sum(cost_logistics*quantity_shipped) as cost_logistics,sum(cost_handle_platform*quantity_shipped) as cost_handle_platform,if(financecategoryname is null,'',financecategoryname) as financecategoryname,((total_fee-shipping)*wms_to_cny_exchange_rate*quantity_shipped) as total_fee from dc_month_sales_profit_orderdetail where month='{day.ToString("yyyy-MM")}' and platform_type='FBA' group by financecategoryname";
where tb.company_id=1 and tb.bailun_order_status!='Canceled' and tb.has_scalp=0 and tb.has_innersale=0 and tb.bailun_interception_status in ('None','Failed') and tb.platform_type='FBA' and tb.purchase_create_time>='{day.ToString("yyyy-MM-dd")}' and tb.purchase_create_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}' objFBA = cn.Query<dc_month_sales_profit_orderdetail>(sql).ToList();
group by t6.financecategoryname
";
objFBA = cn.Query<Models.mOrder>(sql,null,null,true,6*60).ToList();
decimal? obj = 0; decimal? obj = 0;
...@@ -580,10 +574,10 @@ namespace Bailun.DC.MonthSaleProfit ...@@ -580,10 +574,10 @@ namespace Bailun.DC.MonthSaleProfit
//平台费=平台费+paypal费+处理费+广告费+上架费 //平台费=平台费+paypal费+处理费+广告费+上架费
m.fee_platform = objOrder.Sum(a => a.cost_platform_fee??0+a.cost_paypal_fee??0+a.cost_handle_platform??0) + objFBA.Sum(a => a.cost_platform_fee??0); m.fee_platform = objOrder.Sum(a => a.cost_platform_fee+a.cost_paypal_fee+a.cost_handle_platform) + objFBA.Sum(a => a.cost_platform_fee);
//FBA费 //FBA费
m.fee_fba = objFBA.Sum(a => a.cost_fba_fee??0); m.fee_fba = objFBA.Sum(a => a.cost_fba_fee);
//平台退款 //平台退款
sql = $@"select sum(t1.amount_refund_rmb) from dc_base_crm_refund t1 where t1.shipping_status in ('TotalShipping','PartShipping') and t1.is_deleted=0 and is_freeze=0 and t1.company_id=1 and t1.refund_time>='{day.ToString("yyyy-MM-dd")}' and t1.refund_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}' and t1.shipping_status!='UnShipping'"; sql = $@"select sum(t1.amount_refund_rmb) from dc_base_crm_refund t1 where t1.shipping_status in ('TotalShipping','PartShipping') and t1.is_deleted=0 and is_freeze=0 and t1.company_id=1 and t1.refund_time>='{day.ToString("yyyy-MM-dd")}' and t1.refund_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}' and t1.shipping_status!='UnShipping'";
...@@ -636,7 +630,7 @@ join dc_base_warehouse t3 on t1.warehouse_code=t3.warehouse_code ...@@ -636,7 +630,7 @@ join dc_base_warehouse t3 on t1.warehouse_code=t3.warehouse_code
m.fee_logistics_first = objFirst; //objOrder.Sum(a => a.cost_first??0) + objFBA.Sum(a => a.cost_first??0); m.fee_logistics_first = objFirst; //objOrder.Sum(a => a.cost_first??0) + objFBA.Sum(a => a.cost_first??0);
m.fee_logistics_direct = objLogistics.Where(a => a.hq_type == "国内仓")?.Sum(b => b.cost_logistics) ?? 0; //objOrder.Sum(a => a.cost_logistics??0); m.fee_logistics_direct = objLogistics.Where(a => a.hq_type == "国内仓")?.Sum(b => b.cost_logistics) ?? 0; //objOrder.Sum(a => a.cost_logistics??0);
m.fee_logistics_tail = objLogistics.Where(a => a.hq_type != "国内仓")?.Sum(b => b.cost_logistics) ?? 0; m.fee_logistics_tail = objLogistics.Where(a => a.hq_type != "国内仓")?.Sum(b => b.cost_logistics) ?? 0;
m.fee_storage = objOrder.Sum(a => a.total_fee??0); m.fee_storage = objOrder.Sum(a => a.total_fee);
m.fee_logistics_storage = m.fee_logistics_first + m.fee_logistics_direct+m.fee_logistics_tail + m.fee_storage+m.fee_storage_incidentals; m.fee_logistics_storage = m.fee_logistics_first + m.fee_logistics_direct+m.fee_logistics_tail + m.fee_storage+m.fee_storage_incidentals;
...@@ -867,5 +861,94 @@ join dc_base_warehouse t3 on t1.warehouse_code=t3.warehouse_code ...@@ -867,5 +861,94 @@ join dc_base_warehouse t3 on t1.warehouse_code=t3.warehouse_code
} }
private void SaveOrderDetail(DateTime day)
{
var objOrder = new List<dc_month_sales_profit_orderdetail>();
var objFBA = new List<dc_month_sales_profit_orderdetail>();
using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString_read))
{
if(cn.State== System.Data.ConnectionState.Closed)
{
cn.Open();
}
var checkSaved = cn.QueryFirstOrDefault<dc_month_sales_profit>("select * from dc_month_sales_profit where isedit=0 and month='" + day.ToString("yyyy-MM") + "'");
if(checkSaved!=null)
{
return;
}
var sql = $@"select t1.id as pickid,t2.id as orderskuid,t2.bailun_sku,t2.amount_sales,t2.cost_product,t2.cost_platform_fee,t2.cost_paypal_fee,t2.cost_first,t2.cost_logistics,t2.cost_handle_platform,t2.total_fee,t2.seller_order_exchange_rate,t2.seller_other_exchange_rate,t2.wms_to_cny_exchange_rate,t2.shipping,t1.quantity_shipped,t1.shipping_time,t2.platform_type,t2.cost_fba_fee,if(t6.financecategoryname is null,'',t6.financecategoryname) as financecategoryname
from dc_base_oms_pick t1
join dc_base_oms_sku t2 on t1.bailun_order_id = t2.bailun_order_id and t1.bailun_sku=t2.bailun_sku and t2.bailun_order_status != 'Canceled' and t2.has_scalp = 0 and ((t2.platform_type!='FBA' and t2.bailun_order_status!='CantHandle') or t2.platform_type='FBA') and t2.has_scalp = 0 and t2.has_innersale = 0 and t2.has_delete=0 and t2.company_id=1
left join dc_base_sku t5 on t2.bailun_sku=t5.bailun_sku
left join dc_base_sku_finance_category t6 on t6.bailuncategoryid=t5.bailun_category_id
where t1.has_delete=0 and t1.shipping_status = 'TotalShipping' and t1.company_id=1 and t1.shipping_time>='{day.ToString("yyyy-MM-dd")}' and t1.shipping_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}'
";
objOrder = cn.Query<dc_month_sales_profit_orderdetail>(sql, null, null, true, 6 * 60).ToList();
var page = 0;
var pagesize = 3000;
Console.WriteLine($"开始保存自发货SKU明细 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
while (page==0||(page*pagesize)<objOrder.Count)
{
var tempList = objOrder.Skip(page*pagesize).Take(pagesize).ToList();
var str = "insert dc_month_sales_profit_orderdetail (month,pickid,orderskuid,bailun_sku,amount_sales,cost_product,cost_platform_fee,cost_paypal_fee,cost_first,cost_logistics,cost_handle_platform,total_fee,shipping,seller_order_exchange_rate,seller_other_exchange_rate,wms_to_cny_exchange_rate,quantity_shipped,shipping_time,platform_type,cost_fba_fee,financecategoryname) values ";
foreach(var item in tempList)
{
str += $"('{day.ToString("yyyy-MM")}',{item.pickid},{item.orderskuid},'{item.bailun_sku}',{item.amount_sales},{item.cost_product},{item.cost_platform_fee},{item.cost_paypal_fee},{item.cost_first},{item.cost_logistics},{item.cost_handle_platform},{item.total_fee},{item.shipping},{item.seller_order_exchange_rate},{item.seller_other_exchange_rate},{item.wms_to_cny_exchange_rate},{item.quantity_shipped},'{item.shipping_time}','{item.platform_type}',{item.cost_fba_fee},'{item.financecategoryname}'),";
}
Console.WriteLine($"总记录数:{objOrder.Count},第{(page + 1)}页,记录数:{tempList.Count} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
SaveData(str.Substring(0, str.Length - 1));
page++;
}
sql = $@"select 0 as pickid,tb.id as orderskuid,tb.bailun_sku,tb.amount_sales,tb.cost_product,tb.cost_platform_fee,tb.cost_paypal_fee,tb.cost_first,tb.cost_logistics,tb.cost_handle_platform,tb.total_fee,tb.seller_order_exchange_rate,tb.seller_other_exchange_rate,tb.wms_to_cny_exchange_rate,tb.shipping,tb.bailun_sku_quantity_ordered as quantity_shipped,tb.purchase_create_time as shipping_time,tb.platform_type,tb.cost_fba_fee,if(t6.financecategoryname is null,'',t6.financecategoryname) as financecategoryname
from dc_base_oms_sku tb left join dc_base_sku t5 on tb.bailun_sku=t5.bailun_sku
left join dc_base_sku_finance_category t6 on t6.bailuncategoryid=t5.bailun_category_id
where tb.company_id=1 and tb.bailun_order_status!='Canceled' and tb.has_scalp=0 and tb.has_innersale=0 and tb.bailun_interception_status in ('None','Failed') and tb.platform_type='FBA' and tb.purchase_create_time>='{day.ToString("yyyy-MM-dd")}' and tb.purchase_create_time<'{day.AddMonths(1).ToString("yyyy-MM-dd")}'
";
objFBA = cn.Query<dc_month_sales_profit_orderdetail>(sql, null, null, true, 6 * 60).ToList();
Console.WriteLine($"开始保存FBA明细 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
page = 0;
pagesize = 3000;
while (page == 0 || (page * pagesize) < objFBA.Count)
{
var tempList = objFBA.Skip(page*pagesize).Take(pagesize).ToList();
var str = "insert dc_month_sales_profit_orderdetail (month,pickid,orderskuid,bailun_sku,amount_sales,cost_product,cost_platform_fee,cost_paypal_fee,cost_first,cost_logistics,cost_handle_platform,total_fee,shipping,seller_order_exchange_rate,seller_other_exchange_rate,wms_to_cny_exchange_rate,quantity_shipped,shipping_time,platform_type,cost_fba_fee,financecategoryname) values ";
foreach (var item in tempList)
{
str += $"('{day.ToString("yyyy-MM")}',{item.pickid},{item.orderskuid},'{item.bailun_sku}',{item.amount_sales},{item.cost_product},{item.cost_platform_fee},{item.cost_paypal_fee},{item.cost_first},{item.cost_logistics},{item.cost_handle_platform},{item.total_fee},{item.shipping},{item.seller_order_exchange_rate},{item.seller_other_exchange_rate},{item.wms_to_cny_exchange_rate},{item.quantity_shipped},'{item.shipping_time}','{item.platform_type}',{item.cost_fba_fee},'{item.financecategoryname}'),";
}
Console.WriteLine($"总记录数:{objFBA.Count},第{(page + 1)}页,记录数:{tempList.Count} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
SaveData(str.Substring(0, str.Length - 1));
page++;
}
}
}
private string SaveData(string sql)
{
using (var cn = new MySqlConnection(Common.GlobalConfig.ConnectionString))
{
if(cn.State== System.Data.ConnectionState.Closed)
{
cn.Open();
}
cn.Execute(sql);
return "";
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment