Commit a0927245 by jianshuqin

增加功能:同步资金系统亚马逊交易流水

parent e7c62d23
......@@ -54,5 +54,10 @@ namespace Bailun.DC.Common
/// </summary>
public static string ConnectionString_Pro = "Server=gz-cdb-oxt89knc.sql.tencentcdb.com;port=62967;database=bailun_bltpro;uid=root;password=#7kfnymAM$Y9-Ntf;Convert Zero Datetime=True;Allow User Variables=True;pooling=true";
/// <summary>
/// 财务资金
/// </summary>
public static string ConnectionString_Fund = "Server=gz-cdb-lnrmt5zh.sql.tencentcdb.com;port=61369;database=bailun_caiwu;uid=root;password=#7kfnymAM$Y9-Ntf;Convert Zero Datetime=True;SslMode=none;";
}
}
......@@ -3,7 +3,7 @@
namespace Bailun.DC.Models.Component.Enum
{
/// <summary>
/// 数据库:1: 数据中心, 2: 交易流水, 3: 产品库
/// 数据库:1: 数据中心, 2: 交易流水, 3: 产品库, 4: 财务资金
/// </summary>
public enum DBEnum
{
......@@ -23,6 +23,12 @@ namespace Bailun.DC.Models.Component.Enum
/// 产品库
/// </summary>
[Description("产品库")]
Pro = 3
Pro = 3,
/// <summary>
/// 财务资金
/// </summary>
[Description("财务资金")]
Fund = 4
}
}
namespace Bailun.DC.Models.DataWareHouse.DTO
{
public class FundAmazonTransactionDTO
{
/// <summary>
/// ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 平台
/// </summary>
public string Platform { get; set; }
/// <summary>
/// 账号
/// </summary>
public string AccountName { get; set; }
/// <summary>
/// 站点
/// </summary>
public string Website { get; set; }
/// <summary>
/// 年月
/// </summary>
public string Month { get; set; }
/// <summary>
/// 流水数据
/// </summary>
public string DataJson { get; set; }
/// <summary>
/// 报告数量
/// </summary>
public int ReportCount { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Bailun.DC.Models.DataWareHouse
{
......@@ -31,6 +29,11 @@ namespace Bailun.DC.Models.DataWareHouse
public string createuser { get; set; }
public int has_error { get; set; }
public string md5code { get; set; }
public int? delstatus { get; set; } = 0;
}
}
using System;
namespace Bailun.DC.Models.DataWareHouse
{
/// <summary>
/// 交易流水进程
/// </summary>
public class flowing_sales_progress
{
/// <summary>
/// ID
/// </summary>
public int id { get; set; }
/// <summary>
/// 平台
/// </summary>
public string platform { get; set; }
/// <summary>
/// 帐号名称
/// </summary>
public string account_name { get; set; }
/// <summary>
/// 站点
/// </summary>
public string website { get; set; }
/// <summary>
/// 月份
/// </summary>
public string month { get; set; }
/// <summary>
/// 资金流水数量
/// </summary>
public int fund_flowing_count { get; set; }
/// <summary>
/// 同步流水数量
/// </summary>
public int sync_flowing_count { get; set; }
/// <summary>
/// 同步时间数量
/// </summary>
public int sync_time_count { get; set; }
/// <summary>
/// 同步状态
/// </summary>
public int sync_status { get; set; }
/// <summary>
/// 是否删除
/// </summary>
public bool delstatus { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime create_time { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime update_time { get; set; }
}
}
......@@ -4,6 +4,7 @@ using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.Streaming;
using System;
using System.Collections.Generic;
using System.Data;
......@@ -71,6 +72,24 @@ namespace Bailun.DC.Services.Component
}
}
private MySqlConnection _FUND_DB;
public MySqlConnection FUND_DB
{
get
{
if (_FUND_DB == null)
{
_FUND_DB = new MySqlConnection(GlobalConfig.ConnectionString_Fund);
}
if (_FUND_DB.State == ConnectionState.Closed)
{
_FUND_DB.Open();
}
return _FUND_DB;
}
}
public IEnumerable<T> DeserializeCollection<T>(string jsonStr)
{
IEnumerable<T> list = default(IEnumerable<T>);
......@@ -224,8 +243,8 @@ namespace Bailun.DC.Services.Component
MemoryStream ms = null;
try
{
workbook = new HSSFWorkbook();
int excelMaxRowCount = 65535;
workbook = dataCount <= 65535 ? (IWorkbook)new HSSFWorkbook() : new SXSSFWorkbook();
int excelMaxRowCount = dataCount <= 65535 ? 65535 : 1048576;
for (int i = 0; (i * excelMaxRowCount < dataCount); i++)
{
ISheet sheet = workbook.CreateSheet($"Sheet{i + 1}");
......
......@@ -44,6 +44,9 @@ namespace Bailun.DC.Services.Component
case DBEnum.Pro:
db = PRO_DB;
break;
case DBEnum.Fund:
db = FUND_DB;
break;
default:
db = DB;
break;
......
using Bailun.DC.Models.Component.DTO;
using Bailun.DC.Models.Component.Entity;
using Bailun.DC.Models.Component.Enum;
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bailun.DC.Services.Component
......
......@@ -464,6 +464,7 @@ namespace Bailun.DC.Services.Component
{
byte[] b = default(byte[]);
string name = default(string);
int dataCount = default(int);
if (queryFilter != null)
{
TableDTO entity = this.Get(queryFilter.Code);
......@@ -482,6 +483,7 @@ namespace Bailun.DC.Services.Component
if (!string.IsNullOrWhiteSpace(entity.DataValue) && dataTypes.Contains(entity.DataType))
{
(int, IDataReader) reader = this.GetListDataReader(queryFilter, entity);
dataCount = reader.Item1;
using (reader.Item2)
{
b = DataReaderToExcel(listColumn, reader.Item2, reader.Item1);
......@@ -500,7 +502,7 @@ namespace Bailun.DC.Services.Component
}
if (b != null)
{
name = $"{(name ?? "Export")}_{DateTime.Now.ToString("yyyyMMddhhmm")}.xls";
name = $"{(name ?? "Export")}_{DateTime.Now.ToString("yyyyMMddhhmm")}.{(dataCount <= 65535 ? "xls" : "xlsx")}";
}
return (name, b);
......@@ -757,6 +759,9 @@ namespace Bailun.DC.Services.Component
case DBEnum.Pro:
db = PRO_DB;
break;
case DBEnum.Fund:
db = FUND_DB;
break;
default:
db = DB;
break;
......
using Bailun.DC.Models.Component.DTO;
using Bailun.DC.Models.DataWareHouse;
using Bailun.DC.Models.DataWareHouse.DTO;
using Bailun.DC.Services.Component;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Bailun.DC.Services.DataWareHouse
{
/// <summary>
/// 同步组件
/// </summary>
public class SyncService : BaseService
{
/// <summary>
/// 加载资金系统亚马逊交易流水
/// </summary>
/// <param name="month">年月</param>
/// <returns>加载结果</returns>
public ResultDTO LoadFundAmazonTransaction(string month)
{
ResultDTO result = new ResultDTO();
if (!string.IsNullOrWhiteSpace(month))
{
IList<FundAmazonTransactionDTO> list = default(IList<FundAmazonTransactionDTO>);
string sql = $@"SELECT
t1.account_name AS AccountName
,t1.market as Website
,DATE_FORMAT(report_month,'%Y-%m') AS `Month`
,count(*) AS `ReportCount`
FROM
`amazon_transaction_detail` t1
WHERE
report_month = CONCAT(@month,'-01 00:00:00')
GROUP BY
t1.market
,t1.account_name
,`month`";
using (var db = FUND_DB)
{
list = db.Query<FundAmazonTransactionDTO>(sql, new { Month = month }).ToList();
}
if (list?.Count > 0)
{
using (var db = DW_DB)
{
if (db.State == System.Data.ConnectionState.Closed)
{
db.Open();
}
foreach (FundAmazonTransactionDTO dto in list)
{
flowing_sales_progress entity = new flowing_sales_progress()
{
platform = "亚马逊",
account_name = dto.AccountName,
website = dto.Website,
month = dto.Month,
fund_flowing_count = dto.ReportCount,
sync_flowing_count = 0,
sync_time_count = 0,
sync_status = 0,
delstatus = false,
update_time = DateTime.Now,
};
sql = $@"UPDATE flowing_sales_progress
SET
fund_flowing_count = @fund_flowing_count
WHERE 1 = 1
AND platform = @platform
AND account_name = @account_name
AND website = @website
AND month = @month";
int count = db.Execute(sql, entity);
if (count == 0)
{
entity.create_time = DateTime.Now;
db.Insert(entity);
}
}
}
result.Result = true;
result.Message = "加载成功!";
}
else
{
result.Message = "没数据加载!";
}
}
else
{
result.Message = "年月不能为空!";
}
return result;
}
/// <summary>
/// 加载资金系统亚马逊交易流水
/// </summary>
/// <param name="month">年月</param>
/// <returns>加载结果</returns>
public ResultDTO FundAmazonTransaction(string month, IEnumerable<FundAmazonTransactionDTO> listDto)
{
ResultDTO result = new ResultDTO();
if (!string.IsNullOrWhiteSpace(month) || listDto?.Count() > 0)
{
IList<FundAmazonTransactionDTO> list = default(IList<FundAmazonTransactionDTO>);
DateTime[] months = !string.IsNullOrWhiteSpace(month) ? new DateTime[] { DateTime.Parse(month) } : listDto.Where(l => !string.IsNullOrWhiteSpace(l.Month)).Select(l => DateTime.Parse(l.Month)).Distinct().ToArray();
string sql = $@"SELECT
id as Id
,'亚马逊' AS Platform
,t1.account_name AS AccountName
,t1.market as Website
,DATE_FORMAT(report_month,'%Y-%m') AS `Month`
,data_json AS `DataJson`
FROM
`amazon_transaction_detail` t1
WHERE
report_month in @months";
using (var db = FUND_DB)
{
list = db.Query<FundAmazonTransactionDTO>(sql, new { months = months }).ToList();
}
if (list?.Count > 0)
{
using (var db = DW_DB)
{
sql = $@"UPDATE flowing_sales_progress
SET
sync_status = @sync_status
WHERE 1 = 1
AND sync_status < 1
AND {(!string.IsNullOrWhiteSpace(month) ? "month = @month" : "id in @id")}";
//同步为进行中状态
int count = db.Execute(sql, new { sync_status = 1, month = month, id = listDto.Select(l => l.Id).ToArray() });
}
Task task = Task.Factory.StartNew(() =>
{
int count = 0;
if (listDto?.Count() > 0)
{
list = list.Where(l => listDto.Select(l2 => l2.Month).Distinct().Contains(l.Month) && listDto.Select(l2 => l2.AccountName).Distinct().Contains(l.AccountName) && listDto.Select(l2 => l2.Website).Distinct().Contains(l.Website)).ToList();
}
using (var db = DW_DB)
{
if (db.State == System.Data.ConnectionState.Closed)
{
db.Open();
}
//同步流水
foreach (FundAmazonTransactionDTO dto in list)
{
flowing_sales entity = new flowing_sales
{
accountname = dto.AccountName,
jsondata = dto.DataJson,
month = dto.Month,
orderno = "",
website = dto.Website ?? "",
platform = dto.Platform,
platformsku = "",
createuser = "web",
delstatus = 0,
has_error = 0,
md5code = dto.Id,
};
sql = $@"UPDATE flowing_sales
SET
accountname = @accountname
,jsondata = @jsondata
,month = @month
,website = @website
,platform = @platform
,jsondata = @jsondata
WHERE 1 = 1
AND md5code = @md5code";
count = db.Execute(sql, entity);
if (count == 0)
{
entity.createtime = DateTime.Now;
db.Insert(entity);
}
}
//同步状态
sql = $@"UPDATE flowing_sales_progress t1
INNER JOIN (
SELECT t2.accountname,t2.website,t2.`month`,count(*) AS sync_flowing_count FROM flowing_sales t2
WHERE 1 = 1
AND t2.platform = '亚马逊'
AND `month` IN @month
GROUP BY
t2.accountname
,t2.website
,t2.`month`
)
AS t2
ON t1.account_name = t2.accountname
AND t1.website = t2.website
AND t1.`month` = t2.`month`
SET
t1.sync_flowing_count = t2.sync_flowing_count,
t1.sync_status = 2
WHERE t1.platform = '亚马逊'
AND t1.`month` IN @month
AND t1.sync_status < 2";
count = db.Execute(sql, new { month = months.Select(l => l.ToString("yyyy-MM")).ToArray() });
}
});
result.Result = true;
result.Message = "正在同步!";
}
else
{
result.Message = "没数据加载!";
}
}
else
{
result.Message = "年月不能为空!";
}
return result;
}
}
}
using Bailun.DC.Models.Component.DTO;
using Bailun.DC.Services.Component;
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
namespace Bailun.DC.Web.Areas.Component.Controllers
{
......
using Bailun.DC.Models.Component.DTO;
using Bailun.DC.Models.DataWareHouse.DTO;
using Bailun.DC.Services.DataWareHouse;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace Bailun.DC.Web.Areas.DataWareHouse.Controllers
{
[Area("DataWareHouse")]
public class SyncController : Base.BaseController
{
[HttpPost]
public JsonResult LoadFundAmazonTransaction([FromForm] string month)
{
ResultDTO result = default(ResultDTO);
try
{
result = new SyncService().LoadFundAmazonTransaction(month);
}
catch (Exception ex)
{
result = new ResultDTO() { Message = ex.Message };
}
return Json(result);
}
[HttpPost]
public JsonResult FundAmazonTransaction([FromForm] string month, [FromForm] IEnumerable<FundAmazonTransactionDTO> list)
{
ResultDTO result = default(ResultDTO);
try
{
result = new SyncService().FundAmazonTransaction(month, list);
}
catch (Exception ex)
{
result = new ResultDTO() { Message = ex.Message };
}
return Json(result);
}
}
}
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