You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

179 lines
7.4 KiB

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.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<MailVend>(_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
/// <summary>
/// 将list转换为datatable
/// </summary>
public static DataTable ToDataTable<T>(List<T> 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;
}
/// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
/// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
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("<html><head>");
sb.Append("<title>Excel转换为Table</title>");
sb.Append("<meta http-equiv='content-type' content='text/html; charset=GB2312'> ");
sb.Append("<style type=text/css>");
sb.Append("td{font-size: 9pt;border:solid 1 #000000;}");
sb.Append("table{padding:3 0 3 0;border:solid 1 #000000;margin:0 0 0 0;BORDER-COLLAPSE: collapse;}");
sb.Append("</style>");
sb.Append("</head>");
sb.Append("<body>");
sb.Append("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'>");
sb.Append("<tr valign='middle'>");
sb.Append("<td><b></b></td>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td><b><span>" + column.ColumnName + "</span></b></td>");
}
sb.Append("</tr>");
int iColsCount = dt.Columns.Count;
int rowsCount = dt.Rows.Count - 1;
for (int j = 0; j <= rowsCount; j++)
{
sb.Append("<tr>");
sb.Append("<td>" + ((int)(j + 1)).ToString() + "</td>");
for (int k = 0; k <= iColsCount - 1; k++)
{
sb.Append("<td>");
object obj = dt.Rows[j][k];
if (obj == DBNull.Value)
{
obj = "&nbsp;";//如果是NULL则在HTML里面使用一个空格替换之
}
if (obj.ToString() == "")
{
obj = "&nbsp;";
}
string strCellContent = obj.ToString().Trim();
sb.Append("<span>" + strCellContent + "</span>");
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
//点击单元格 输出 行和列
//sb.Append("<script src='https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js'></script>");
//sb.Append("<script type='text/javascript'>");
//sb.Append("$('table tbody').on('click', 'td', function (e) {");
//sb.Append("var row = $(this).parent().prevAll().length-1 ;");
//sb.Append("var column = $(this).prevAll().length-1 ;");
//sb.Append("var str = 'dt.Rows[' + row + '][' + column + '].ToString()';");
//sb.Append("console.log(str);alert(str);");
//sb.Append("});");
//sb.Append("</script>");
sb.Append("</body></html>");
return sb.ToString();
}
}
}