This commit is contained in:
2021-10-19 06:29:11 +08:00
commit 60131e4608
131 changed files with 46479 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
using _2021_backend.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace _2021_backend.Utils
{
public abstract class ResponseDto
{
[DataMember]
public string Status;
}
[DataContract]
public class SuccessResponseDto : ResponseDto
{
[DataMember]
public new string Status { get; set; }
[DataMember(EmitDefaultValue = false)]
public object Data { get; set; }
public SuccessResponseDto()
{
Status = "success";
}
}
[DataContract]
public class ErrorResponseDto : ResponseDto
{
[DataMember]
public new string Status { get; set; }
[DataMember(EmitDefaultValue = false)]
public string ErrorMsg { get; set; }
public ErrorResponseDto()
{
Status = "error";
}
}
public static class ApiResponse
{
public static SuccessResponseDto Success(object data) => new SuccessResponseDto { Data = data };
public static ErrorResponseDto Error(string errorMsg) => new ErrorResponseDto { ErrorMsg = errorMsg };
public static ErrorResponseDto Error(string errorMsg, status stat)
{
return new ErrorResponseDto { ErrorMsg = errorMsg, Status = Enum.GetName(typeof(status), stat) };
}
}
}
+112
View File
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using _2021_backend.Data;
using _2021_backend.Models;
using System.IO;
using System.Threading.Tasks;
namespace _2021_backend.Utils
{
public class Arranger
{
static BackendContext Context;
static List<Student> students;
static List<Session> sessions;
public static bool Arrange(BackendContext context)
{
Context = context;
students = context.Students.Where(it => it.Status != status.不通过 && it.Status != status.已确认时间 && it.Timelist.Count != 0 && it.InterviewTime == Guid.Empty && it.Status != status.需调整时间).OrderBy(it => it.Timelist.Count).ToList();
sessions = context.Sessions.OrderBy(it => it.Day).ThenByDescending(it => it.Capacity - it.Students.Count).ThenBy(it => it.BeginTime).ThenBy(it => it.Place).ToList();
students.Sort((Student a, Student b) =>
{
var capa = 0;
var capb = 0;
foreach (var tm in a.Timelist)
{
var t = sessions.Find(x => x.Guid == tm);
capa += t.Capacity - t.Students.Count;
}
foreach (var tm in b.Timelist)
{
var t = sessions.Find(x => x.Guid == tm);
capb += t.Capacity - t.Students.Count;
}
return capa - capb;
});
#if DEBUG
var str = "";
foreach(var st in students)
{
var cap = 0;
var tmstr = "";
foreach (var tm in st.Timelist)
{
var t = sessions.Find(x => x.Guid == tm);
cap += t.Capacity - t.Students.Count;
tmstr += t.Day.ToString("dd") + "-" + t.BeginTime.ToString("HH:mm-") + t.Place + " ";
}
str += st.Name + "\t" + st.Timelist.Count.ToString() + "\t" + cap.ToString() + "\t"+ tmstr+ "\n";
}
File.WriteAllText("./students.txt", str);
#endif
var res = DFS(0);
context.SaveChanges();
return res;
}
static bool DFS(int m)
{
if (m >= students.Count) return true;
var lst = students[m].Timelist;
var stu = students[m];
if (stu.Timelist.Contains(stu.InterviewTime) || stu.Timelist.Count() == 0 || stu.InterviewTime != Guid.Empty || stu.Status != status.已选时间)
{
return m >= students.Count - 1 ? true : DFS(m + 1);
}
else
{
var tm = Context.Sessions.Find(stu.InterviewTime);
if (tm != null)
{
tm.Students.RemoveAll(it => it == stu.Guid);
}
stu.InterviewTime = Guid.Empty;
}
bool depth = false;
foreach (var i in lst)
{
var tm = sessions.Find(s => s.Guid == i);
if (tm.Students.Count < tm.Capacity && tm.Day.CompareTo(DateTime.Now.AddHours(-14)) > 0)
{
status prev = students[m].Status;
Guid prevs = students[m].InterviewTime;
students[m].Status = status.已选时间;
students[m].InterviewTime = tm.Guid;
tm.Students.Add(students[m].Guid);
if (m == students.Count - 1)
{
return true;
}
else
{
depth = true;
if (!DFS(m + 1))
{
students[m].Status = prev;
students[m].InterviewTime = prevs;
tm.Students.Remove(students[m].Guid);
continue;
}
else return true;
}
}
}
if (!depth) Console.WriteLine("Unable to find an arrangement. Stack trace:");
Console.WriteLine(stu.Name + " " + stu.Stuid);
return false;
}
}
}
+85
View File
@@ -0,0 +1,85 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace _2021_backend.Utils
{
public static class EvaClaimTypes
{
public const string IsManager = "IsManager";
}
public static class LoginHelper
{
public static string PostMoths(string url, object obj_model)
{
string param = JsonConvert.SerializeObject(obj_model);
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
/* if (dic != null && dic.Count != 0)
{
foreach (var item in dic)
{
request.Headers.Add(item.Key, item.Value);
}
}*/
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = payload.Length;
string strValue = "";
try
{
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
}
}
catch (Exception e)
{
strValue = e.Message;
}
return strValue;
}
}
public static class EvaCryptoHelper
{
public static string SHA1(string content)
{
try
{
using (var sha1 = new SHA1CryptoServiceProvider())
{
byte[] bytes_in = Encoding.UTF8.GetBytes(content);
byte[] bytes_out = sha1.ComputeHash(bytes_in);
string result = BitConverter.ToString(bytes_out);
result = result.Replace("-", "");
return result;
}
}
catch (Exception)
{
return string.Empty;
}
}
public static string Password2Secret(string password)
{
return EvaCryptoHelper.SHA1("W5D1" + EvaCryptoHelper.SHA1("EVa" + password + "n13@!@") + "D1X1AnsH3ng");
}
}
}
+75
View File
@@ -0,0 +1,75 @@
using _2021_backend.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace _2021_backend.Utils
{
public class DateHelper
{
public static bool CheckDate()
{
if (DateTime.Now.CompareTo(new DateTime(2021, 9, 28)) < 0) return true;
return false;
}
public static string GetDay(int day)
{
switch (day)
{
case 1:
return "24日";
case 2:
return "25日";
case 3:
return "26日";
case 4:
return "27日";
case 5:
return "28日";
case 6:
return "29日";
case 7:
return "30日";
default:
return "暂无";
}
}
public static string GetGrade(int grade)
{
switch (grade)
{
case 1:
return "大一";
case 2:
return "大二";
case 3:
return "大三";
case 4:
return "大四";
default:
return "暂缺";
}
}
public static string GetEtc(int etc)
{
switch (etc)
{
case 1:
return "第一场";
case 2:
return "第二场";
case 3:
return "第三场";
case 4:
return "第四场";
case 5:
return "第五场";
default:
return "暂无";
}
}
}
}
+168
View File
@@ -0,0 +1,168 @@
using _2021_backend.Data;
using _2021_backend.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20210111;
using TencentCloud.Sms.V20210111.Models;
namespace _2021_backend.Utils
{
public class TencentSMS
{
public static string Tencent_id { get; set; }
public static string Tencent_key { get; set; }
public static string SMS_appid { get; set; }
public static string SMSID_accept { get; set; }
public static string SMSID_reject { get; set; }
public static string SMSID_timeSet { get; set; }
public static string SMSID_signed { get; set; }
private static bool Initialized = false;
private static string SMSID_timeSelect { get; set; }
public static void Init(string tencentid, string tencentKey, string appid, string ID_accept, string ID_reject, string ID_timeSet, string ID_submitted, string ID_timeSelect)
{
Tencent_id = tencentid;
Tencent_key = tencentKey;
SMS_appid = appid;
SMSID_accept = ID_accept;
SMSID_reject = ID_reject;
SMSID_timeSet = ID_timeSet;
SMSID_signed = ID_submitted;
SMSID_timeSelect = ID_timeSelect;
Initialized = true;
}
public static async Task<bool> Pull(BackendContext Context, Student stu, bool fullPull)
{
if (!Initialized) return false;
try
{
Credential cred = new Credential
{
SecretId = Tencent_id,
SecretKey = Tencent_key
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("sms.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
PullSmsReplyStatusByPhoneNumberRequest req = new PullSmsReplyStatusByPhoneNumberRequest();
//var it = Context.SMS.Find(stu.Messages.LastOrDefault()).SendTime;
req.BeginTime = (ulong)DateTimeOffset.UtcNow.AddDays(-5).ToUnixTimeSeconds();
req.EndTime = (ulong)DateTimeOffset.UtcNow.AddSeconds(-1).ToUnixTimeSeconds();
req.Offset = 0;
req.Limit = 100;
req.PhoneNumber = "+86" + stu.Tel;
req.SmsSdkAppId = SMS_appid;
foreach (var msg in stu.Messages)
{
var sms = Context.SMS.Find(msg);
if (sms.SendTime.CompareTo(DateTime.Now.AddDays(-5)) > 0)
{
stu.Messages.Remove(msg);
Context.SMS.Remove(sms);
}
}
Context.SaveChanges();
PullSmsReplyStatusByPhoneNumberResponse resp = client.PullSmsReplyStatusByPhoneNumberSync(req);
var str = AbstractModel.ToJsonString(resp);
Console.WriteLine(resp);
SMSResponseDto obj = JsonSerializer.Deserialize<SMSResponseDto>(str, new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
var lst = obj.PullSmsReplyStatusSet;
foreach (var item in lst)
{
var msg = new SMS(item, stu.Guid);
Context.Add<SMS>(msg);
stu.Messages.Add(msg.Guid);
}
Context.SaveChanges();
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
public static async Task<bool> Send(BackendContext Context, SMSType type, Student stu, string sender)
{
if (!Initialized) return false;
try
{
Credential cred = new Credential
{
SecretId = Tencent_id,
SecretKey = Tencent_key
};
ClientProfile cp = new ClientProfile();
HttpProfile hp = new HttpProfile();
hp.Endpoint = ("sms.tencentcloudapi.com");
cp.HttpProfile = hp;
SmsClient client = new SmsClient(cred, "ap-nanjing", cp);
SendSmsRequest req = new SendSmsRequest();
req.PhoneNumberSet = new string[] { "" };
req.PhoneNumberSet[0] = "+86" + stu.Tel;
req.SmsSdkAppId = SMS_appid;
req.SignName = "ZJUEVA";
Guid index = stu.InterviewTime;
SMS sms = new SMS();
switch (type)
{
case SMSType.Accept:
sms.Type = SMSType.Accept;
req.TemplateId = SMSID_accept;
req.TemplateParamSet = new string[] { stu.Name };
break;
case SMSType.Reject:
sms.Type = SMSType.Reject;
req.TemplateId = SMSID_reject;
req.TemplateParamSet = new string[] { stu.Name };
break;
case SMSType.TimeSelect:
sms.Type = SMSType.TimeSelect;
req.TemplateId = SMSID_timeSelect;
req.TemplateParamSet = new string[] { stu.Name, stu.Stuid };
break;
case SMSType.TimeSet:
sms.Type = SMSType.TimeSet;
var q = from e in Context.Sessions where e.Guid == index select e;
var time = await q.FirstOrDefaultAsync();
req.TemplateParamSet = new string[] { stu.Name, time.Day.ToString("MM-dd") + " " + time.BeginTime.ToString("HH:mm"), time.Place };
req.TemplateId = SMSID_timeSet;
break;
case SMSType.Signed:
sms.Type = SMSType.Signed;
req.TemplateParamSet = new string[] { stu.Name, stu.Stuid };
req.TemplateId = SMSID_signed;
break;
}
sms.Tel = stu.Tel;
sms.Host = stu.Guid;
var query = from e in Context.Users where e.stuID == sender select e;
var usr = await query.FirstOrDefaultAsync();
sms.Sender = usr == null ? "null" : usr.Name;
sms.Data = req.TemplateParamSet.ToList<string>();
SendSmsResponse resp = client.SendSmsSync(req);
Console.WriteLine(AbstractModel.ToJsonString(resp));
Context.Add(sms);
Context.SaveChanges();
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
}
}