Browse Source

初步完成

lcl 1 year ago
parent
commit
198223e8e9

BIN
.DS_Store


+ 133 - 0
AppStart/Helper/ExportService.cs

@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using Library;
+using LitJson;
+using System.Data;
+using System.Threading;
+using NPOI.HSSF.UserModel;
+using NPOI.XSSF.UserModel;
+using NPOI.SS.UserModel;
+using NPOI.HSSF.Util;
+
+namespace MySystem
+{
+    public class ExportService
+    {
+        public readonly static ExportService Instance = new ExportService();
+        private ExportService()
+        { }
+
+
+
+
+        public void Start()
+        {
+            Thread th = new Thread(StartDo);
+            th.IsBackground = true;
+            th.Start();
+        }
+        public void StartDo()
+        {
+            while (true)
+            {
+                string content = RedisDbconn.Instance.RPop<string>("ExportQueue");
+                if(!string.IsNullOrEmpty(content))
+                {
+                    StartExport(content);
+                }
+                else
+                {
+                    Thread.Sleep(5000);
+                }
+            }
+        }
+        public void StartExport(string content)
+        {
+            JsonData jsonObj = JsonMapper.ToObject(content);
+            string Operater = jsonObj["Operater"].ToString();
+            string SqlString = jsonObj["SqlString"].ToString();
+            string FileName = jsonObj["FileName"].ToString();
+            int MaxCount = int.Parse(jsonObj["MaxCount"].ToString());
+
+            //创建工作薄
+            var workbook = new XSSFWorkbook();
+
+            //单元格样式
+            ICellStyle style = workbook.CreateCellStyle();
+            style.BorderBottom = BorderStyle.Thin;
+            style.BorderTop = BorderStyle.Thin;
+            style.BorderLeft = BorderStyle.Thin;
+            style.BorderRight = BorderStyle.Thin;
+            style.BottomBorderColor = HSSFColor.Black.Index;
+            style.TopBorderColor = HSSFColor.Black.Index;
+            style.LeftBorderColor = HSSFColor.Black.Index;
+            style.RightBorderColor = HSSFColor.Black.Index;
+
+            //创建表
+            var table = workbook.CreateSheet("Sheet1");
+            string FilePath = function.ReadInstance("/WebRootPath.txt");
+
+            //填充数据
+            int i = 0;
+            int page = 0;
+            int pageSize = 200;
+            bool op = true;
+            while(op)
+            {
+                var fs = System.IO.File.OpenWrite(FilePath + "/exportfile/" + FileName + ".xlsx");
+                int skip = page * pageSize;
+                DataTable dt = CustomerSqlConn.dtable(SqlString + " limit " + skip + "," + pageSize, MysqlConn.connstr);
+                if(dt.Rows.Count > 0)
+                {
+                    foreach (DataRow dr in dt.Rows)
+                    {
+                        i += 1;
+                        if(i <= MaxCount || MaxCount == 0)
+                        {
+                            if(i == 1)
+                            {
+                                //创建表头
+                                var title = table.CreateRow(0);
+                                int j = 0;
+                                foreach (DataColumn dc in dt.Columns)
+                                {
+                                    var cell = title.CreateCell(j);
+                                    cell.CellStyle = style;
+                                    cell.SetCellValue(dc.ColumnName);
+                                    j += 1;
+                                }
+                            }
+                            var row = table.CreateRow(i);
+                            int index = 0;
+                            foreach (DataColumn dc in dt.Columns)
+                            {
+                                var cell = row.CreateCell(index);
+                                string val = dr[dc.ColumnName].ToString();
+                                cell.CellStyle = style;
+                                cell.SetCellValue(val);
+                                index += 1;
+                            }
+                        }
+                        else
+                        {
+                            op = false;
+                        }
+                    }
+                    //打开xls文件,如没有则创建,如存在则在创建是不要打开该文件
+                    workbook.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
+                    page += 1;
+                }
+                else
+                {
+                    op = false;
+                }
+                fs.Dispose();
+            }
+            if(i > 0)
+            {
+                string FileUrl = MysqlConn.sourceHost + "exportfile/" + FileName + ".xlsx";
+                CustomerSqlConn.op("insert into ExportExcels (CreateDate,FileName,FileUrl,SysId) values ('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + FileName + "','" + FileUrl + "'," + Operater + ")", MysqlConn.connstr);
+            }
+        }
+    }
+}

+ 0 - 157
AppStart/Helper/StatService.cs

@@ -1,157 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Library;
-using LitJson;
-using System.Linq;
-using System.Data;
-using System.Threading;
-
-namespace MySystem
-{
-    public class StatService
-    {
-        public readonly static StatService Instance = new StatService();
-        private StatService()
-        { }
-
-
-
-
-        public void Start()
-        {
-            Thread th = new Thread(StartDo);
-            th.IsBackground = true;
-            th.Start();
-        }
-        public void StartDo()
-        {
-            while (true)
-            {
-                string content = RedisDbconn.Instance.RPop<string>("DoTableEveryMonthQueue");
-                if(!string.IsNullOrEmpty(content) && DateTime.Now.Day < 15)
-                {
-                    GetApMysqlData();
-                }
-                Thread.Sleep(60000);
-            }
-        }
-        public void GetApMysqlData()
-        {
-            string Month = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
-            string Start = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
-            string End = DateTime.Now.ToString("yyyy-MM");
-            string check = function.ReadInstance("/MakeTable/" + Month + ".txt");
-            if (!string.IsNullOrEmpty(check))
-            {
-                return;
-            }
-            function.WritePage("/MakeTable/", Month + ".txt", DateTime.Now.ToString());
-            try
-            {
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "抓取数据");
-
-                Dictionary<string, string> fields = new Dictionary<string, string>();
-                string createstr = "";
-                string sqlstr = "";
-
-                //创客表
-                fields.Add("Id", "Id");
-                fields.Add("ParentUserId", "ParentUserId");
-                fields.Add("ParentNav", "'ParentNav'");
-                fields.Add("UserLevel", "UserLevel");
-                fields.Add("PreUserLevel", "PreUserLevel");
-                createstr = "Id int NOT NULL AUTO_INCREMENT,ParentUserId int not null default 0,ParentNav longtext,UserLevel int not null default 0,PreUserLevel int not null default 0,PRIMARY KEY(Id)";
-                sqlstr = "select Id,ParentUserId,ParentNav,UserLevel,(case when PreUserLevel is null then 0 else PreUserLevel end) PreUserLevel from (select Id,ParentUserId,ParentNav,UserLevel,(select Rank from UserRankWhite where Id=u.Id and CreateDate<'" + End + "-01 00:00:00' and UpdateDate>'" + End + "-01 00:00:00') PreUserLevel from Users u where AuthFlag=1) tb";
-                AddTable(Month, "Users", createstr, sqlstr, fields);
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "抓取数据");
-
-                //交易统计表-贷记卡
-                fields = new Dictionary<string, string>();
-                fields.Add("UserId", "UserId");
-                fields.Add("QrPayFlag", "QrPayFlag");
-                fields.Add("BrandId", "BrandId");
-                fields.Add("HelpDirectTradeAmt", "HelpDirectTradeAmt");
-                fields.Add("NotHelpDirectTradeAmt", "NotHelpDirectTradeAmt");
-                fields.Add("ProfitDirectTradeAmt", "ProfitDirectTradeAmt");
-                createstr = "Id int NOT NULL AUTO_INCREMENT,UserId int not null default 0,QrPayFlag int not null default 0,BrandId int not null default 0,HelpDirectTradeAmt decimal(18,2) not null default 0,NotHelpDirectTradeAmt decimal(18,2) not null default 0,ProfitDirectTradeAmt decimal(18,2) not null default 0,PRIMARY KEY(Id)";
-                sqlstr = "select UserId,QueryCount QrPayFlag,BrandId,sum(HelpDirectTradeAmt) HelpDirectTradeAmt,sum(NotHelpDirectTradeAmt) NotHelpDirectTradeAmt,sum(ProfitDirectTradeAmt) ProfitDirectTradeAmt from TradeDaySummary where TradeMonth='" + Month + "' and SeoTitle='self' group by UserId,QueryCount,BrandId having sum(HelpDirectTradeAmt)>0 or sum(NotHelpDirectTradeAmt)>0 or sum(ProfitDirectTradeAmt)>0 order by UserId";
-                AddTable(Month, "CreditTradeDaySummary", createstr, sqlstr, fields);
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "抓取数据");
-
-                //交易统计表-借记卡
-                fields = new Dictionary<string, string>();
-                fields.Add("UserId", "UserId");
-                fields.Add("QrPayFlag", "QrPayFlag");
-                fields.Add("BrandId", "BrandId");
-                fields.Add("HelpDirectDebitTradeAmt", "HelpDirectDebitTradeAmt");
-                fields.Add("NotHelpDirectDebitTradeAmt", "NotHelpDirectDebitTradeAmt");
-                fields.Add("HelpDirectDebitCapTradeAmt", "HelpDirectDebitCapTradeAmt");
-                fields.Add("NotHelpDirectDebitCapTradeAmt", "NotHelpDirectDebitCapTradeAmt");
-                fields.Add("HelpDirectDebitCapNum", "HelpDirectDebitCapNum");
-                fields.Add("NotHelpDirectDebitCapNum", "NotHelpDirectDebitCapNum");
-                fields.Add("ProfitDirectDebitTradeAmt", "ProfitDirectDebitTradeAmt");
-                fields.Add("ProfitDirectDebitCapTradeAmt", "ProfitDirectDebitCapTradeAmt");
-                fields.Add("ProfitDirectDebitCapNum", "ProfitDirectDebitCapNum");
-                createstr = "Id int NOT NULL AUTO_INCREMENT,UserId int not null default 0,QrPayFlag int not null default 0,BrandId int not null default 0,HelpDirectDebitTradeAmt decimal(18,2) not null default 0,NotHelpDirectDebitTradeAmt decimal(18,2) not null default 0,HelpDirectDebitCapTradeAmt decimal(18,2) not null default 0,NotHelpDirectDebitCapTradeAmt decimal(18,2) not null default 0,HelpDirectDebitCapNum decimal(18,2) not null default 0,NotHelpDirectDebitCapNum decimal(18,2) not null default 0,ProfitDirectDebitTradeAmt decimal(18,2) not null default 0,ProfitDirectDebitCapTradeAmt decimal(18,2) not null default 0,ProfitDirectDebitCapNum decimal(18,2) not null default 0,PRIMARY KEY(Id)";
-                sqlstr = "select UserId,QueryCount QrPayFlag,BrandId,sum(HelpDirectDebitTradeAmt) HelpDirectDebitTradeAmt,sum(NotHelpDirectDebitTradeAmt) NotHelpDirectDebitTradeAmt,sum(HelpDirectDebitCapTradeAmt) HelpDirectDebitCapTradeAmt,sum(NotHelpDirectDebitCapTradeAmt) NotHelpDirectDebitCapTradeAmt,sum(HelpDirectDebitCapNum) HelpDirectDebitCapNum,sum(NotHelpDirectDebitCapNum) NotHelpDirectDebitCapNum,sum(ProfitDirectDebitTradeAmt) ProfitDirectDebitTradeAmt,sum(ProfitDirectDebitCapTradeAmt) ProfitDirectDebitCapTradeAmt,sum(ProfitDirectDebitCapNum) ProfitDirectDebitCapNum from TradeDaySummary where TradeMonth='" + Month + "' and SeoTitle='self' group by UserId,QueryCount,BrandId having sum(HelpDirectDebitTradeAmt)>0 or sum(NotHelpDirectDebitTradeAmt)>0 or sum(HelpDirectDebitCapTradeAmt)>0 or sum(NotHelpDirectDebitCapTradeAmt)>0 order by UserId";
-                AddTable(Month, "DebitTradeDaySummary", createstr, sqlstr, fields);
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "抓取数据");
-
-                //交易记录表-补贴
-                fields = new Dictionary<string, string>();
-                fields.Add("SnNo", "'SnNo'");
-                fields.Add("UserId", "UserId");
-                fields.Add("QrPayFlag", "QrPayFlag");
-                fields.Add("MerHelpFlag", "MerHelpFlag");
-                fields.Add("BrandId", "BrandId");
-                fields.Add("Version", "Version");
-                fields.Add("TradeAmount", "TradeAmount");
-                createstr = "Id int NOT NULL AUTO_INCREMENT,SnNo varchar(50),UserId int not null default 0,QrPayFlag int not null default 0,MerHelpFlag int not null default 0,Version int not null default 0,BrandId int not null default 0,TradeAmount decimal(18,2) not null default 0,PRIMARY KEY(Id)";
-                sqlstr = "select SnNo,UserId,QrPayFlag,MerHelpFlag,BrandId,Version,sum(TradeAmount) TradeAmount from TradeRecord where Id>3593404 and CreateDate>='" + Start + "-01 00:00:00' and CreateDate<'" + End + "-01 00:00:00' and BankCardType!=0 group by SnNo,UserId,QrPayFlag,MerHelpFlag,BrandId,Version";
-                AddTable(Month, "TradeRecord", createstr, sqlstr, fields);
-
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "抓取数据");
-            }
-            catch(Exception ex)
-            {
-                function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n" + ex.ToString() + "\n\n", "抓取数据异常");
-            }
-
-            RedisDbconn.Instance.AddList("DoProfitQueue", "1");
-        }
-
-        public void AddTable(string Month, string TableName, string createstr, string sqlstr, Dictionary<string, string> fields)
-        {
-            string connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
-            CustomerSqlConn.op("CREATE table KxsStatServer." + TableName + Month + "(" + createstr + ")ENGINE=InnoDB DEFAULT charset=utf8;", connstr);
-            string fieldstrs = "";
-            foreach(string field in fields.Keys)
-            {
-                fieldstrs += field.Trim(',') + ",";
-            }
-            CustomerSqlConn.op("insert into KxsStatServer." + TableName + Month + " (" + fieldstrs.TrimEnd(',') + ") " + sqlstr, connstr);
-            // string sql = "";
-            // int index = 0;
-            // DataTable dt = CustomerSqlConn.dtable(sqlstr, connstr);
-            // foreach(DataRow dr in dt.Rows)
-            // {
-            //     index += 1;
-            //     string fieldstrs = "";
-            //     string vals = "";
-            //     foreach(string field in fields.Keys)
-            //     {
-            //         fieldstrs += field + ",";
-            //         vals += fields[field].Replace(field, dr[field].ToString()) + ",";
-            //     }
-            //     sql += "insert into " + TableName + Month + " (" + fieldstrs.TrimEnd(',') + ") values (" + vals.TrimEnd(',') + ");\n";
-            //     if(index % 1000 == 0)
-            //     {
-            //         CustomerSqlConn.op(sql, mainconnstr);
-            //         sql = "";
-            //     }
-            // }
-            // function.WriteLog(dt.Rows.Count.ToString(), "抓取数据");
-            // CustomerSqlConn.op(sql, mainconnstr);
-        }
-    }
-}

+ 13 - 0
Config/MysqlConn.cs

@@ -0,0 +1,13 @@
+
+/// <summary>
+/// MySql链接
+/// </summary>
+namespace MySystem
+{
+    public class MysqlConn
+    {
+        public static string connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
+        public static string sourceHost = Library.ConfigurationManager.AppSettings["SourceHost"].ToString();
+
+    }
+}

+ 12 - 1
Controllers/HomeController.cs

@@ -45,6 +45,17 @@ namespace MySystem.Controllers
                 }
             }
             return View();
-        }        
+        }
+
+        public string test()
+        {
+            Dictionary<string, string> dic = new Dictionary<string, string>();
+            dic.Add("Operater", "testman");
+            dic.Add("SqlString", "select * from HelpProfitExchange");
+            dic.Add("FileName", "测试文件");
+            dic.Add("MaxCount", "0");
+            RedisDbconn.Instance.AddList("ExportQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
+            return "ok";
+        }
     }
 }

+ 1 - 0
MySystem.csproj

@@ -50,6 +50,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="DotNetCore.NPOI" Version="1.2.3" />
     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
     <PackageReference Include="RabbitMQ.Client" Version="6.2.2" />
     <PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />

+ 2 - 0
Startup.cs

@@ -65,6 +65,7 @@ namespace MySystem
                 app.UseExceptionHandler("/Home/Error");
                 app.UseHsts();
             }
+            Library.ConfigurationManager.EnvironmentFlag = 2;
             Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
             app.UseStaticFiles();
             app.UseStaticFiles(new StaticFileOptions
@@ -89,6 +90,7 @@ namespace MySystem
             });
             
             //必须打开的
+            ExportService.Instance.Start();
             //必须打开的
 
         }

+ 1 - 1
appsettings.json

@@ -15,7 +15,7 @@
     "WebServiceUrl": "",
     "DbSchemeUrl": "",
     "Host": "http://localhost:5051/",
-    "SourceHost": "",
+    "SourceHost": "http://test.export.kexiaoshuang.com/",
     "Database": "KxsExportServer",
     "SqlConnStr": "server=47.109.31.237;port=3306;user=KxsMainServer;password=FrW8ZfxlcaVdm1r0;database=KxsMainServer;charset=utf8;",
     "RedisConnStr": "47.109.31.237:6379,password=klm@redis,DefaultDatabase=1,poolsize=500,preheat=50,asyncPipeline=true",

BIN
wwwroot/admin/.DS_Store


BIN
wwwroot/测试文件.xlsx