diff --git a/New_College.Api/Controllers/HealthCheckController.cs b/New_College.Api/Controllers/HealthCheckController.cs index 6d34264..04f5ed9 100644 --- a/New_College.Api/Controllers/HealthCheckController.cs +++ b/New_College.Api/Controllers/HealthCheckController.cs @@ -2,6 +2,7 @@ using New_College.Common; using New_College.Common.Helper; using New_College.IServices; +using New_College.Services; using System.Threading.Tasks; namespace New_College.Controllers @@ -16,11 +17,13 @@ namespace New_College.Controllers private readonly IT_EnrollmentPlaneServices t_EnrollmentPlane; private readonly ID_LongIdMapServices d_LongIdMapServices; - - public HealthCheckController(IT_EnrollmentPlaneServices t_EnrollmentPlaneServices, ID_LongIdMapServices d_LongIdMapServices) + + private readonly IV_CustomerInfoServices v_CustomerInfoServices; + public HealthCheckController(IT_EnrollmentPlaneServices t_EnrollmentPlaneServices, ID_LongIdMapServices d_LongIdMapServices, IV_CustomerInfoServices _CustomerInfoServices) { t_EnrollmentPlane = t_EnrollmentPlaneServices; this.d_LongIdMapServices = d_LongIdMapServices; + v_CustomerInfoServices = _CustomerInfoServices; } /// @@ -52,10 +55,10 @@ namespace New_College.Controllers /// /// [HttpPost] - public Task Post() + public async Task Post() { // return t_EnrollmentPlane.Import(); - // return t_EnrollmentPlane.Importuniverbaseinfo(); + // return t_EnrollmentPlane.Importuniverbaseinfo(); // return t_EnrollmentPlane.universitydetailupdate(); @@ -64,10 +67,11 @@ namespace New_College.Controllers // return t_EnrollmentPlane.categoryupdate(); - // return d_LongIdMapServices.UpdateUniveristyInf(); - // return d_LongIdMapServices.Import(); + // return d_LongIdMapServices.UpdateUniveristyInf(); + // return d_LongIdMapServices.Import(); - return d_LongIdMapServices.UpdatePlanProInf(); + await v_CustomerInfoServices.CustomeBillExport(); + return true; } } diff --git a/New_College.Api/Controllers/OAuthController.cs b/New_College.Api/Controllers/OAuthController.cs index 7d3d072..640c182 100644 --- a/New_College.Api/Controllers/OAuthController.cs +++ b/New_College.Api/Controllers/OAuthController.cs @@ -95,7 +95,8 @@ namespace New_College.Controllers return new MessageModel() { msg = "code已失效", - success = false + success = false, + status = 401, }; } response.servicetoken = getaccesstoken.access_token; @@ -118,6 +119,7 @@ namespace New_College.Controllers } } baseuser.UUID = userinfo.sub; + user = baseuser; await _CustomerInfoServices.Update(baseuser); } } @@ -144,7 +146,7 @@ namespace New_College.Controllers single.NickName = user.UserCode; } await _CustomerInfoServices.Update(single); - } + } } TokenModelJwt tokenModel = new TokenModelJwt { Uid = user.Id, Role = "users" }; jwtStr = JwtHelper.IssueJwt(tokenModel); diff --git a/New_College.Api/New_College.Api.csproj b/New_College.Api/New_College.Api.csproj index b1dc16f..be5e17a 100644 --- a/New_College.Api/New_College.Api.csproj +++ b/New_College.Api/New_College.Api.csproj @@ -57,6 +57,8 @@ + + @@ -102,6 +104,7 @@ + diff --git a/New_College.Api/New_College.Model.xml b/New_College.Api/New_College.Model.xml index 9c5b912..ee1542a 100644 --- a/New_College.Api/New_College.Model.xml +++ b/New_College.Api/New_College.Model.xml @@ -5755,6 +5755,36 @@ 未完成人数 + + + 性别 1男 2女 + + + + + 省市区名称 + + + + + 学生数据是否初始化(省份,选科等) + + + + + 考生预计分数 + + + + + 是否为VIP + + + + + 学届 + + 返回状态1成功 2验证码错误 3失败 diff --git a/New_College.Api/Startup.cs b/New_College.Api/Startup.cs index 8d89cb5..9c40a5e 100644 --- a/New_College.Api/Startup.cs +++ b/New_College.Api/Startup.cs @@ -69,6 +69,16 @@ namespace New_College SHanDongTel.Tel = Appsettings.app(new string[] { "SHanDongTel", "Tel" }); + SHanDongTel.Mail = Appsettings.app(new string[] { "SHanDongTel", "Mail" }); + //邮箱 + MailKitConfig.DisplayName = Appsettings.app(new string[] { "MailKit", "DisplayName" }).ToString(); + MailKitConfig.Password = Appsettings.app(new string[] { "MailKit", "Password" }).ToString(); + MailKitConfig.MailAddress = Appsettings.app(new string[] { "MailKit", "MailAddress" }).ToString(); + MailKitConfig.IsSsl = Appsettings.app(new string[] { "MailKit", "IsSsl" }).ObjToBool(); + MailKitConfig.MailServer = Appsettings.app(new string[] { "MailKit", "MailServer" }).ToString(); + MailKitConfig.SmtpPort = Appsettings.app(new string[] { "MailKit", "SmtpPort" }).ObjToInt(); + + //阿里云oss配置 AliYunOssConfig.wendpoint = Appsettings.app(new string[] { "AliYunOss", "wendpoint" }).ObjToString(); AliYunOssConfig.accessKeyId = Appsettings.app(new string[] { "AliYunOss", "accessKeyId" }).ObjToString(); diff --git a/New_College.Api/appsettings.Development.json b/New_College.Api/appsettings.Development.json index 63e5a61..99adffa 100644 --- a/New_College.Api/appsettings.Development.json +++ b/New_College.Api/appsettings.Development.json @@ -211,17 +211,17 @@ "wendpoint": "https://static-data.ycymedu.com", "accessKeyId": "LTAI5tKs3TXSbt7E4WMDcxwR", "accessKeySecret": "EvC8MjRaQC1kHubgU4MtecZnofOb0v", - "bucket": "static-data-ycymedu", + "bucket": "static-ycymedu-minpro", "endpoint": "https://oss-cn-shanghai.aliyuncs.com" }, "Casdoor": { - "Endpoint": "http://192.168.103.119:8000", + "Endpoint": "http://192.168.104.107:8000", "OrganizationName": "六纬生涯", "ApplicationName": "六纬生涯", "ApplicationType": "webapi", "ClientId": "776d639918192c449537", "ClientSecret": "d556f0692052a2df56614282dd86895b04783262", - "CallbackPath": "http://test.sso.ycymedu.com/login/oauth/authorize?client_id=4c6d761417dbbdd665be&response_type=code&redirect_uri=http://192.168.100.129:8081/Home/FirstPage&scope=read&state=casdoor", + "CallbackPath": "http://test.sso.ycymedu.com/login/oauth/authorize?client_id=776d639918192c449537&response_type=code&redirect_uri=http://192.168.100.106:8080/Home/FirstPage&scope=read&state=casdoor", "RequireHttpsMetadata": false }, "WeChatPay": { @@ -250,7 +250,17 @@ "Url": "http://api.carrerwebapi.com/api/oauth/SyncUpdateStudentInfo" }, "SHanDongTel": { - "Tel": "15662627992|18724728603|17362186261|15264182912" + "Tel": "15662627992|18724728603|17362186261|15264182912", + "Mail": "156663459@qq.com" + + }, + "MailKit": { + "DisplayName": "六纬技术团队", + "Password": "Vs..127134", + "MailAddress": "yly@ycymedu.com", + "IsSsl": "true", + "MailServer": "smtp.ym.163.com", + "SmtpPort": "465" }, "XiaoETONG": { diff --git a/New_College.Api/appsettings.json b/New_College.Api/appsettings.json index bbbbe77..d3070f6 100644 --- a/New_College.Api/appsettings.json +++ b/New_College.Api/appsettings.json @@ -215,13 +215,13 @@ "endpoint": "https://oss-cn-shanghai.aliyuncs.com" }, "Casdoor": { - "Endpoint": "http://192.168.103.119:8000", + "Endpoint": "http://192.168.104.107:8000", "OrganizationName": "六纬生涯", "ApplicationName": "六纬生涯", "ApplicationType": "webapi", "ClientId": "4c6d761417dbbdd665be", "ClientSecret": "cc385a5d0efd5f7bf693d8c4574126158f7b0a8f", - "CallbackPath": "http://192.168.103.119:8000/login/oauth/authorize?client_id=ae6bdccc3a7821232b31&response_type=code&redirect_uri=http://192.168.103.100:8083/callback&scope=read&state=casdoor", + "CallbackPath": "http://192.168.104.107:8000/login/oauth/authorize?client_id=ae6bdccc3a7821232b31&response_type=code&redirect_uri=http://192.168.103.100:8083/callback&scope=read&state=casdoor", "RequireHttpsMetadata": false }, "WeChatPay": { @@ -246,7 +246,17 @@ "SerialNumber": "1B304596B953D156B37863FF52FCAE40B458C9F2" //(新)证书序列号 }, "SHanDongTel": { - "Tel": "15662627992|18724728603|17362186261|15264182912" + "Tel": "15662627992|18724728603|17362186261|15264182912", + "Mail": "2461966073@qq.com" + }, + "MailKit": { + "DisplayName": "六纬技术团队", + "Password": "Vs..127134", + "MailAddress": "yly@ycymedu.com", + "IsSsl": "true", + "MailServer": "smtp.ym.163.com", + "SmtpPort": "465" + }, "XiaoETONG": { "app_id": "appn9m3yups9098", diff --git a/New_College.Common/Excel/ListExportExcelUtil.cs b/New_College.Common/Excel/ListExportExcelUtil.cs new file mode 100644 index 0000000..b95ba7c --- /dev/null +++ b/New_College.Common/Excel/ListExportExcelUtil.cs @@ -0,0 +1,104 @@ +using NPOI.XSSF.UserModel; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace New_College.Common +{ + public static class ListExportExcelUtil + { + + public static void ExportToExcel(List data, string filePath) + { + // 创建一个新的XSSFWorkbook(用于.xlsx格式) + using var workbook = new XSSFWorkbook(); + + // 在工作簿中创建一个工作表 + var sheet = workbook.CreateSheet("Sheet1"); + + // 定义标题行(根据YourModel属性) + var headerRow = sheet.CreateRow(0); + headerRow.CreateCell(0).SetCellValue("性别"); + headerRow.CreateCell(1).SetCellValue("手机号"); + headerRow.CreateCell(2).SetCellValue("高考年份"); + headerRow.CreateCell(3).SetCellValue("学校"); + headerRow.CreateCell(4).SetCellValue("省份"); + headerRow.CreateCell(5).SetCellValue("否初始化"); + headerRow.CreateCell(6).SetCellValue("学生分数"); + headerRow.CreateCell(7).SetCellValue("选科组合"); + headerRow.CreateCell(8).SetCellValue("是否vip"); + headerRow.CreateCell(9).SetCellValue("注册时间"); + + // 遍历数据列表并写入Excel + for (var i = 0; i < data.Count; i++) + { + var item = data[i]; + var row = sheet.CreateRow(i + 1); // 从第1行开始写数据 + + // 将模型属性值写入对应的单元格 + row.CreateCell(0).SetCellValue(item.Gender); + row.CreateCell(1).SetCellValue(item.Phone); + row.CreateCell(2).SetCellValue(item.Year); + row.CreateCell(3).SetCellValue(item.SchoolName); + row.CreateCell(4).SetCellValue(item.AreaName); + row.CreateCell(5).SetCellValue(item.Datainit); + row.CreateCell(6).SetCellValue(item.Expectedscore.Value); + row.CreateCell(7).SetCellValue(item.subjectgroupName); + row.CreateCell(8).SetCellValue(item.IsVIP); + row.CreateCell(9).SetCellValue(item.CreateTime); + // ... 对其他属性进行相同操作 + } + + // 保存到指定文件路径 + using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write); + workbook.Write(fileStream); + } + } + + public class CustomeExportModel + { + + /// + /// 性别 1男 2女 + /// + public string Gender { get; set; } + /// + /// 省市区名称 + /// + public string AreaName { get; set; } + + /// + ///学生数据是否初始化(省份,选科等) + /// + public string Datainit { get; set; } + + /// + /// 考生预计分数 + /// + public double? Expectedscore { get; set; } = 0; + + public string subjectgroupName { get; set; } + + /// + /// 是否为VIP + /// + public string IsVIP { get; set; } + + public string Phone { get; set; } + + /// + /// 学届 + /// + public int Year { get; set; } + + public string SchoolName { get; set; } + + + public string CreateTime { get; set; } + + } + +} diff --git a/New_College.Common/GlobalVar/WeiXinConfig.cs b/New_College.Common/GlobalVar/WeiXinConfig.cs index e2a0557..44d7ba9 100644 --- a/New_College.Common/GlobalVar/WeiXinConfig.cs +++ b/New_College.Common/GlobalVar/WeiXinConfig.cs @@ -18,6 +18,30 @@ { public static string Tel { get; set; } + public static string Mail { get; set; } + + } + + /// + /// 邮箱配置 + /// + public static class MailKitConfig + { + /// + /// 发送邮件人别名 + /// + public static string DisplayName { get; set; } + /// + /// 发送邮件id + /// + public static string MailAddress { get; set; } + public static bool IsSsl { get; set; } + /// + /// + /// + public static string MailServer { get; set; } + public static int SmtpPort { get; set; } + public static string Password { get; set; } } public static class WeixinConfig diff --git a/New_College.Common/Helper/AliYunOssHelper.cs b/New_College.Common/Helper/AliYunOssHelper.cs index ffd62cc..6e664aa 100644 --- a/New_College.Common/Helper/AliYunOssHelper.cs +++ b/New_College.Common/Helper/AliYunOssHelper.cs @@ -2,6 +2,7 @@ using Aliyun.OSS.Common; using System; using System.Collections.Generic; +using System.IO; using System.Text; @@ -9,7 +10,44 @@ namespace New_College.Common { public class AliYunOssHelper { - // private readonly static AliYunOssConfig ossConfig = GlobalData.AliYunOss; + // private readonly static AliYunOssConfig ossConfig = GlobalData.AliYunOss; + + /// + /// OSS下载 + /// + /// + /// + /// + public static string DownloadFile(string key, string filepath) + { + // 创建OssClient实例。 + var client = new OssClient(AliYunOssConfig.endpoint, AliYunOssConfig.accessKeyId, AliYunOssConfig.accessKeySecret); + try + { + // 下载文件到流。OssObject 包含了文件的各种信息,如文件所在的存储空间、文件名、元信息以及一个输入流。 + var obj = client.GetObject(AliYunOssConfig.bucket, filepath); + using (var requestStream = obj.Content) + { + byte[] buf = new byte[1024]; + var fs = File.Open(key, FileMode.OpenOrCreate); + var len = 0; + // 通过输入流将文件的内容读取到文件或者内存中。 + while ((len = requestStream.Read(buf, 0, 1024)) != 0) + { + fs.Write(buf, 0, len); + } + fs.Close(); + } + Console.WriteLine("Get object succeeded"); + } + catch (Exception ex) + { + Console.WriteLine("Get object failed. {0}", ex.Message); + } + return ""; + + } + public static string UploadFile(string key, string filepath) diff --git a/New_College.Common/Helper/FileZipHelper.cs b/New_College.Common/Helper/FileZipHelper.cs new file mode 100644 index 0000000..39fb1d7 --- /dev/null +++ b/New_College.Common/Helper/FileZipHelper.cs @@ -0,0 +1,112 @@ +using Microsoft.AspNetCore.Hosting; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace New_College.Common.Helper +{ + public static class FileZipHelper + { + /// + /// 批量压缩上传 + /// + /// + /// + /// + /// + /// + public static string FileMultipZipUpload(IWebHostEnvironment webHost, List files, string mangeName, bool isuploadOss = true) + { + string zipstr = string.Empty; + try + { + + var tmplist = new List(); + string rootdir = webHost.WebRootPath + "/" + mangeName; + string zipname = mangeName + ".zip"; + string ossfileUrl = webHost.WebRootPath + "/" + zipname; + if (!Directory.Exists(rootdir)) + { + Directory.CreateDirectory(rootdir); + } + int tmpid = 0; + files.ForEach(a => + { + string userdir = rootdir + "/" + a.UserName; + if (!Directory.Exists(userdir)) + { + Directory.CreateDirectory(userdir); + } + tmpid++; + int k = 0; + a.fileItems.ForEach(s => + { + if (s.ImgUrl.Contains("http")) + { + k++; + string filename = string.Format("{0}/{1}.{2}-{3}-{4}{5}", userdir, tmpid, s.typeName, a.UserName, k, Path.GetExtension(s.ImgUrl)); + // Wb_Client(s.ImgUrl, filename); + tmplist.Add(filename); + } + }); + }); + + if (File.Exists(ossfileUrl)) + { + File.Delete(ossfileUrl); + } + ZipFile.CreateFromDirectory(rootdir, ossfileUrl); + + if (isuploadOss) + { + zipstr = AliYunOssHelper.UploadFile(zipname, ossfileUrl); + if (!string.IsNullOrEmpty(zipstr)) + { + tmplist.Add(ossfileUrl); + tmplist.ForEach(d => + { + File.Delete(d); + }); + Directory.Delete(rootdir, true); + } + return zipstr; + } + else + { + tmplist.ForEach(d => + { + File.Delete(d); + }); + Directory.Delete(rootdir, true); + return zipname; + } + } + catch (Exception ex) + { + DingHookHelper.DingTalkHookMessage("zip压缩错误", ex.Message); + return "zip压缩错误"; + } + + } + + } + + + public class FileModel + { + public string UserName { get; set; } + + public List fileItems { get; set; } + + } + + public class FileItemModel + { + public string typeName { get; set; } + public string ImgUrl { get; set; } + } +} diff --git a/New_College.Common/Helper/MailKitHelper.cs b/New_College.Common/Helper/MailKitHelper.cs new file mode 100644 index 0000000..f228c69 --- /dev/null +++ b/New_College.Common/Helper/MailKitHelper.cs @@ -0,0 +1,101 @@ +using Microsoft.Extensions.Options; +using MimeKit; +using MimeKit.Text; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading.Tasks; + +namespace New_College.Common.Helper +{ + + + public static class MailKitManagement + { + + + /// + /// send email + /// + /// + /// + /// + /// + /// + /// + /// + public static async Task SendMessageAsync(List toMailAddressList, List ccAddresList, string subject, string body, List attachmentList = null, bool isHtml = false) + { + var mailMessage = new MimeMessage(); + mailMessage.From.Add(new MailboxAddress(MailKitConfig.DisplayName, MailKitConfig.MailAddress)); + foreach (var mailAddressItem in toMailAddressList) + { + mailMessage.To.Add(MailboxAddress.Parse(mailAddressItem)); + } + if (ccAddresList != null) + { + ccAddresList.ForEach(p => + { + mailMessage.Cc.Add(MailboxAddress.Parse(p)); + }); + } + + mailMessage.Subject = subject; + TextPart messageBody = null; + if (isHtml) + { + messageBody = new TextPart(TextFormat.Html) + { + Text = body, + }; + } + else + { + messageBody = new TextPart(TextFormat.Plain) + { + Text = body, + }; + } + var mulitiPart = new Multipart("mixed") + { + }; + mulitiPart.Add(messageBody); + if (attachmentList != null && attachmentList.Count > 0) + { + foreach (var attatchItem in attachmentList) + { + using (var stream = File.OpenRead(attatchItem)) + { + var attachment = new MimePart() + { + Content = new MimeContent(stream, ContentEncoding.Default), + ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), + ContentTransferEncoding = ContentEncoding.Base64, + FileName = Path.GetFileName(attatchItem) + }; + mulitiPart.Add(attachment); + } + } + } + mailMessage.Body = mulitiPart; + using (var client = new MailKit.Net.Smtp.SmtpClient()) + { + if (MailKitConfig.IsSsl) + { + client.ServerCertificateValidationCallback = (s, c, h, e) => true; + } + + client.Connect(MailKitConfig.MailServer, MailKitConfig.SmtpPort, MailKitConfig.IsSsl); + + // Disable the XOAUTH2 authentication mechanism. + //client.AuthenticationMechanisms.Remove("XOAUTH2"); + + // Note: only needed if the SMTP server requires authentication + client.Authenticate(MailKitConfig.MailAddress, MailKitConfig.Password); + await client.SendAsync(mailMessage); + client.Disconnect(true); + } + } + } +} diff --git a/New_College.Common/New_College.Common.csproj b/New_College.Common/New_College.Common.csproj index d0cafa5..dcd62b9 100644 --- a/New_College.Common/New_College.Common.csproj +++ b/New_College.Common/New_College.Common.csproj @@ -18,6 +18,7 @@ + diff --git a/New_College.Extensions/ServiceExtensions/JobSetup.cs b/New_College.Extensions/ServiceExtensions/JobSetup.cs index 4bb2240..9120c52 100644 --- a/New_College.Extensions/ServiceExtensions/JobSetup.cs +++ b/New_College.Extensions/ServiceExtensions/JobSetup.cs @@ -18,6 +18,7 @@ namespace New_College.Extensions //services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); + services.AddHostedService(); services.AddSingleton(); services.AddTransient();//Job使用瞬时依赖注入 services.AddSingleton(); diff --git a/New_College.IServices/IV_CustomerInfoServices.cs b/New_College.IServices/IV_CustomerInfoServices.cs index 204d1fb..043ab48 100644 --- a/New_College.IServices/IV_CustomerInfoServices.cs +++ b/New_College.IServices/IV_CustomerInfoServices.cs @@ -71,5 +71,9 @@ namespace New_College.IServices /// Task CustomerSave(DecryptUserInfoRequest register); + + + Task CustomeBillExport(); + } } \ No newline at end of file diff --git a/New_College.Model/ViewModels/Result/CustomerInfoResult.cs b/New_College.Model/ViewModels/Result/CustomerInfoResult.cs index 51c659a..4275014 100644 --- a/New_College.Model/ViewModels/Result/CustomerInfoResult.cs +++ b/New_College.Model/ViewModels/Result/CustomerInfoResult.cs @@ -22,6 +22,49 @@ namespace New_College.Model.ViewModels public string nickName { get; set; } } + + public class CustomerExportModel + { + + /// + /// 性别 1男 2女 + /// + public string Gender { get; set; } + /// + /// 省市区名称 + /// + public string AreaName { get; set; } + + /// + ///学生数据是否初始化(省份,选科等) + /// + public string Datainit { get; set; } + + /// + /// 考生预计分数 + /// + public double? Expectedscore { get; set; } = 0; + + public string subjectgroupName { get; set; } + + /// + /// 是否为VIP + /// + public string IsVIP { get; set; } + + public string Phone { get; set; } + + /// + /// 学届 + /// + public int Year { get; set; } + + public string SchoolName { get; set; } + + public DateTime? CreateTime { get; set; } + + } + public class CustomerInfoResult { diff --git a/New_College.Services/V_CustomerInfoServices.cs b/New_College.Services/V_CustomerInfoServices.cs index 5f8434a..2c45ad8 100644 --- a/New_College.Services/V_CustomerInfoServices.cs +++ b/New_College.Services/V_CustomerInfoServices.cs @@ -19,6 +19,10 @@ using New_College.Common.HttpRestSharp; using StackExchange.Profiling.Internal; using New_College.Repository; using System.Xml.Linq; +using NPOI.HPSF; +using System.IO; +using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Hosting; namespace New_College.Services { @@ -29,7 +33,8 @@ namespace New_College.Services private readonly ICasdoorUserRepository _casdoorUserRepository; private readonly ID_UserSettingBaseServices _userSetting; private readonly ID_XiaoEtongUserRepository _XiaoEtongUserRepository; - public V_CustomerInfoServices(IBaseRepository dal, IT_BatchlineRepository t_Batchline, ICasdoorUserRepository casdoorUserRepository, ID_UserSettingBaseServices userSetting, ID_XiaoEtongUserRepository d_XiaoEtongUserRepository) + private readonly IWebHostEnvironment environment; + public V_CustomerInfoServices(IBaseRepository dal, IT_BatchlineRepository t_Batchline, ICasdoorUserRepository casdoorUserRepository, ID_UserSettingBaseServices userSetting, ID_XiaoEtongUserRepository d_XiaoEtongUserRepository, IWebHostEnvironment webHostEnvironment) { this._dal = dal; base.BaseDal = dal; @@ -37,6 +42,7 @@ namespace New_College.Services _casdoorUserRepository = casdoorUserRepository; _userSetting = userSetting; _XiaoEtongUserRepository = d_XiaoEtongUserRepository; + environment = webHostEnvironment; } @@ -355,7 +361,7 @@ namespace New_College.Services string typename = "综合"; if (info.subjectgroupName != null) { - typename = info.subjectgroupName.Length > 2 ? "综合" : info.subjectgroupName; + typename = info.subjectgroupName.Length > 2 ? "综合" : info.subjectgroupName; } var batchlist = await t_BatchlineRepository.Query(c => c.Year == years && c.AreaName == info.AreaName && c.Type_name == typename); @@ -684,5 +690,54 @@ namespace New_College.Services { throw new NotImplementedException(); } + + + /// + /// 定期抓取数据 + /// + /// + public async Task CustomeBillExport() + { + var customer = await _dal.Query(c => c.IsDelete == false && c.CreateTime >= DateTime.Now.AddDays(-7)); + + if (customer.Any()) + { + var clist = new List(); + customer.ForEach(c => + { + clist.Add(new CustomeExportModel() + { + AreaName = c.AreaName, + Datainit = c.Datainit.Value ? "是" : "否", + Expectedscore = c.Expectedscore, + Gender = c.Gender == 1 ? "男" : c.Gender == 2 ? "女" : "未填写", + IsVIP = c.IsVIP ? "是" : "否", + Phone = c.Phone, + SchoolName = c.SchoolName, + subjectgroupName = c.subjectgroupName, + Year = c.Year, + CreateTime = c.CreateTime.Value.ToString() + }); + }); + + var outputPath = string.Format("output_{0}.xlsx", DateTime.Now.ToString("yyyyMMddHHssfff")); // 或指定完整的文件路径 + var path = environment.WebRootPath; + string urlPath = $"/Content/Upload/"; + var newpath = path + urlPath + outputPath; + string DirectoryName = Path.GetDirectoryName(path + urlPath); + if (!Directory.Exists(DirectoryName)) + Directory.CreateDirectory(DirectoryName); + ListExportExcelUtil.ExportToExcel(clist, newpath); + + string outputres = "Excel/"+outputPath; + string outputurl = AliYunOssHelper.UploadFile(outputres, newpath); + + await MailKitManagement.SendMessageAsync(new List() { SHanDongTel.Mail }, new List() { }, "六纬志愿技术团队", string.Format("文件下载地址:上周用户数据

", outputurl), null, true); + System.IO.File.Delete(newpath); + } + + + + } } } \ No newline at end of file diff --git a/New_College.Tasks/HostedService/ExcelExportJobTimedService.cs b/New_College.Tasks/HostedService/ExcelExportJobTimedService.cs new file mode 100644 index 0000000..3dee53b --- /dev/null +++ b/New_College.Tasks/HostedService/ExcelExportJobTimedService.cs @@ -0,0 +1,121 @@ +using New_College.Common.Helper; +using New_College.IServices; +using Microsoft.Extensions.Hosting; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace New_College.Tasks +{ + public class ExcelExportJobTimedService : IHostedService, IDisposable + { + private Timer _timer; + private readonly IV_CustomerInfoServices _customerInfoServices; + // private readonly Action _taskAction; + + // 这里可以注入 + public ExcelExportJobTimedService(IV_CustomerInfoServices v_CustomerInfoServices) + { + _customerInfoServices = v_CustomerInfoServices; + + // _taskAction = taskAction ?? throw new ArgumentNullException(nameof(taskAction)); + + // 初始化定时器 + _timer = new Timer(async state => + { + // 执行任务 + await ExecuteTaskAsync(); + + // 计算下一次执行时间 + DateTime nextExecutionTime = CalculateNextMondayMorning(8); + + // 重新设置定时器间隔,确保在下周一早上8点触发 + TimeSpan timeUntilNextExecution = nextExecutionTime - DateTime.Now; + if (timeUntilNextExecution.TotalMilliseconds > 0) + { + _timer.Change(timeUntilNextExecution, TimeSpan.FromMilliseconds(-1)); + } + }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(-1)); + + } + + + + private async Task ExecuteTaskAsync() + { + await Task.Run(async () => + { + try + { + await _customerInfoServices.CustomeBillExport(); + Console.WriteLine($"ExportJob 启动成功"); + } + catch (Exception ex) + { + Console.WriteLine($"Error:{ex.Message}"); + } + + ConsoleHelper.WriteSuccessLine($"Job ExportJob: {DateTime.Now}"); + }); + } + + private static DateTime CalculateNextMondayMorning(int hour) + { + DateTime now = DateTime.Now; + DateTime nextMonday = GetNextMonday(now); + return new DateTime(nextMonday.Year, nextMonday.Month, nextMonday.Day, hour, 0, 0, DateTimeKind.Local); + } + + private static DateTime GetNextMonday(DateTime input) + { + DayOfWeek dayOfWeek = input.DayOfWeek; + int daysUntilMonday = ((int)DayOfWeek.Monday - (int)dayOfWeek + 7) % 7; + return input.AddDays(daysUntilMonday); + } + + + + + + + public Task StartAsync(CancellationToken cancellationToken) + { + Console.WriteLine("ExcelExportJob is starting."); + + // 启动定时器 + _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(-1)); + + return Task.CompletedTask; + } + + //private async void DoWork(object state) + //{ + // try + // { + // await _customerInfoServices.CustomeBillExport(); + // Console.WriteLine($"ExportJob 启动成功"); + // } + // catch (Exception ex) + // { + // Console.WriteLine($"Error:{ex.Message}"); + // } + + // ConsoleHelper.WriteSuccessLine($"Job ExportJob: {DateTime.Now}"); + //} + + public Task StopAsync(CancellationToken cancellationToken) + { + Console.WriteLine("ExportJob is stopping."); + + // 停止定时器 + _timer.Change(Timeout.Infinite, Timeout.Infinite); + + return Task.CompletedTask; + } + + public void Dispose() + { + _timer?.Dispose(); + } + } +}