using ChangKeTec.Wms.Models.Wms; using CK.SCP.Common; using CK.SCP.Models; using CK.SCP.Models.AppBoxEntity; using System; using System.Collections.Generic; using System.Data; using System.Data.Entity.Migrations; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace CK.SCP.UniApi.Controller { public static class MailController { public class MailVend { public string Email { get; set; } public string VendId { get; set; } } public static void GetNewCtrlList() { AppBoxContext idb = EntitiesFactory.CreateAppBoxInstance(); WmsEntities wmsdb = EntitiesFactory.CreateWmsInstance(); StringBuilder _buffer = new StringBuilder(); _buffer.Append("SELECT b.Email ,a.VenderId as VendId "); _buffer.Append(" FROM VenderUsers AS a INNER JOIN "); _buffer.Append(" Users AS b ON a.UserId = b.ID INNER JOIN "); _buffer.Append(" FactoryUsers AS c ON a.UserId = c.UserID INNER JOIN "); _buffer.Append(" TA_FACTORY AS d ON c.FACTORY_ID = d.ID INNER JOIN "); _buffer.Append(" Users AS f ON c.UserID = f.ID INNER JOIN "); _buffer.Append(" RoleUsers AS g ON a.UserId = g.UserID INNER JOIN "); _buffer.Append(" Roles AS h ON g.RoleID = h.ID "); _buffer.AppendFormat(" WHERE(h.Name = '供应商' and b.Enabled=1) "); var _userList = idb.Database.SqlQuery(_buffer.ToString()).ToList(); var grouplist = _userList.GroupBy(r => r.VendId); if (_userList != null && _userList.Count > 0) { foreach (var itm in grouplist) { var maillist = itm.Select(r => r.Email); string mails = ""; foreach (var mail in maillist) { mails += mail + ","; } if (mails != "") { mails = mails.Substring(0, mails.Length - 1); } var stocklist = wmsdb.VIEW_STOCK_VEND.Where(r => r.VendId == itm.Key).ToList(); if (stocklist.Count() == 0) { continue; } var dt_stocklist = ToDataTable(stocklist); var html_stocklist = GetHtmlString(dt_stocklist); MyWebSmtpMail webmail = new MyWebSmtpMail(); webmail.Send(mails, html_stocklist, "库存"); Console.WriteLine($"发送邮件:供应商编码为{ itm.Key}"); } } } #region 将list转换为datatable /// /// 将list转换为datatable /// public static DataTable ToDataTable(List items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) { var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } /// /// Determine of specified type is nullable /// public static bool IsNullable(Type t) { return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } /// /// Return underlying type if type is Nullable otherwise return the type /// public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) { return t; } else { return Nullable.GetUnderlyingType(t); } } else { return t; } } #endregion public static string GetHtmlString(DataTable dt) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append("Excel转换为Table"); sb.Append(" "); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); foreach (DataColumn column in dt.Columns) { sb.Append(""); } sb.Append(""); int iColsCount = dt.Columns.Count; int rowsCount = dt.Rows.Count - 1; for (int j = 0; j <= rowsCount; j++) { sb.Append(""); sb.Append(""); for (int k = 0; k <= iColsCount - 1; k++) { sb.Append(""); } sb.Append(""); } sb.Append("
" + column.ColumnName + "
" + ((int)(j + 1)).ToString() + ""); object obj = dt.Rows[j][k]; if (obj == DBNull.Value) { obj = " ";//如果是NULL则在HTML里面使用一个空格替换之 } if (obj.ToString() == "") { obj = " "; } string strCellContent = obj.ToString().Trim(); sb.Append("" + strCellContent + ""); sb.Append("
"); //点击单元格 输出 行和列 //sb.Append(""); //sb.Append(""); sb.Append(""); return sb.ToString(); } /// /// 发送材料订单邮件 /// public static void SendOrderMail() { ScpEntities SDB = EntitiesFactory.CreateScpInstance(); var emailList = SDB.TS_Email.Where(q => q.EmailSendState == 0).ToList(); try { foreach (var item in emailList) { try { MailManager.SendMailOrders(item.EmailAddress, item.EmailString, "材料订单提醒"); Console.WriteLine($"发送邮件:供应商为{ item.EmailToUser}"); item.EmailSendState = 1; item.UpdateTime = DateTime.Now.ToString(); } catch (Exception ex) { item.EmailSendState = 2; item.EmailSendFailReason = ex.Message; continue; } } SDB.TS_Email.AddOrUpdate(q => q.UID, emailList.ToArray()); SDB.SaveChanges(); } catch(Exception ex) { //emailList.ForEach(q => //{ // q.EmailSendState = 2; // q.EmailSendFailReason = ex.Message; //}); Console.WriteLine(ex.Message); if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } //SDB.TS_Email.AddOrUpdate(q => q.UID, emailList.ToArray()); //SDB.SaveChanges(); } } } }