diff --git a/ICSSoft.FromERP/ICSSoft.FromERP/ICSInventorySql.cs b/ICSSoft.FromERP/ICSSoft.FromERP/ICSInventorySql.cs index 6302eae..d25722a 100644 --- a/ICSSoft.FromERP/ICSSoft.FromERP/ICSInventorySql.cs +++ b/ICSSoft.FromERP/ICSSoft.FromERP/ICSInventorySql.cs @@ -7,6 +7,7 @@ using Quartz; using System.Data; using ICSSoft.Frame.Data.Entity; using ICSSoft.Base.Config.AppConfig; +using Newtonsoft.Json; namespace ICSSoft.FromERP { @@ -38,6 +39,7 @@ namespace ICSSoft.FromERP { try { + log.Info("开始……………………………………………………………………"); string conStr = ICSHelper.GetConnectString(); string WHCode = ICSHelper.GetConfigString()["WHCode"]; //string[] WHCodeList= ICSHelper.GetConfigString()["WHCode"].Split(','); @@ -46,7 +48,6 @@ namespace ICSSoft.FromERP try { foreach (DataRow dr in dt.Rows) - //foreach(string WHCode in WHCodeList) { string sql = ""; int Count = 0; @@ -74,6 +75,7 @@ namespace ICSSoft.FromERP SAPGetINVZS.ZWMS_SK_MARAResponse result = new SAPGetINVZS.ZWMS_SK_MARAResponse(); client.Timeout = 600000; result = client.CallZWMS_SK_MARA(Info); + //log.Info(JsonConvert.SerializeObject(result)); foreach (SAPGetINVZS.ZWMS_MARA item in result.T_MARA) { TotalCount++; @@ -92,9 +94,9 @@ namespace ICSSoft.FromERP ,InvIQC,InvFQC,EffectiveEnable,EffectiveDays,BatchEnable,LotEnable,PrintEnable ,MUSER,MUSERName,MTIME,WorkPoint,DrawingNo,Version) values - ('" + AppConfig.GetGuid() + "','" + item.MATNR.TrimStart('0') + "','" + item.MAKTX.Replace('\'','"') + "','" + item.DESCR.Replace('\'', '"') + @"' + ('" + AppConfig.GetGuid() + "','" + item.MATNR.TrimStart('0') + "','" + item.MAKTX.Replace('\'', '"') + "','" + item.DESCR.Replace('\'', '"') + @"' ,'" + item.MEINS + "','" + item.MATKL + "','" + item.EXTWG + "','" + item.NTGEW + "','" + item.MTART + @"' - ,0,0,0,0,0,0,0 + ,0,0,0,0,0,1,0 ,'JOB','JOB','" + DateTime.Now + "','" + item.WERKS + "','" + item.TEST1 + "','" + item.TEST2 + @"') END"; @@ -122,7 +124,10 @@ namespace ICSSoft.FromERP sql = ""; } } - ICSHelper.ExecuteDate(conStr, sql); + if (!string.IsNullOrWhiteSpace(sql)) + { + ICSHelper.ExecuteDate(conStr, sql); + } } } catch (Exception ex) diff --git a/ICSSoft.FromERP/ICSSoft.FromERP/ICSSendEmail.cs b/ICSSoft.FromERP/ICSSoft.FromERP/ICSSendEmail.cs index cec7819..5dd3e94 100644 --- a/ICSSoft.FromERP/ICSSoft.FromERP/ICSSendEmail.cs +++ b/ICSSoft.FromERP/ICSSoft.FromERP/ICSSendEmail.cs @@ -176,7 +176,12 @@ namespace ICSSoft.FromERP { try { - string text = "INSERT INTO ICSSendMail(ID, SendHost, SendPort, SendDisplayName, SendAddress, \r\n SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, \r\n Body, Retry, Status, Message, MUSER, MUSERName, CreatTIME, ModifyTIME)\r\n VALUES(NEWID(),'{0}','{1}','{2}','{3}',\r\n '{4}','{5}','{6}','{7}','{8}',\r\n '{9}','0','{10}','{11}','','',GETDATE(),GETDATE())"; + string text = @"INSERT INTO ICSSendMail(ID, SendHost, SendPort, SendDisplayName, SendAddress, + SendPassword, TOAddress, CCAddress, Subject, IsBodyHtml, + Body, Retry, Status, Message, MUSER, MUSERName, CreatTIME, ModifyTIME) + VALUES(NEWID(),'{0}','{1}','{2}','{3}', + '{4}','{5}','{6}','{7}','{8}', + '{9}','0','{10}','{11}','','',GETDATE(),GETDATE())"; text = string.Format(text, new object[] { SendHost, diff --git a/ICSSoft.FromERP/ICSSoft.FromERP/ICSSoft.FromERP.csproj b/ICSSoft.FromERP/ICSSoft.FromERP/ICSSoft.FromERP.csproj index b22772e..2e62bd4 100644 --- a/ICSSoft.FromERP/ICSSoft.FromERP/ICSSoft.FromERP.csproj +++ b/ICSSoft.FromERP/ICSSoft.FromERP/ICSSoft.FromERP.csproj @@ -42,6 +42,9 @@ false + + ..\Root\Aspose.Cells.dll + False bin\Debug\ICSSoft.Base.Config.AppConfig.dll @@ -55,6 +58,18 @@ ..\..\..\50-job\Scheduler_4.0\Scheduler\JOB\Newtonsoft.Json.DLL + + ..\Root\NPOI.dll + + + ..\Root\NPOI.OOXML.dll + + + ..\Root\NPOI.OpenXml4Net.dll + + + ..\Root\NPOI.OpenXmlFormats.dll + ..\..\..\50-job\Scheduler_4.0\Scheduler\JOB\Quartz.dll @@ -115,6 +130,8 @@ + + diff --git a/ICSSoft.FromERP/ICSSoft.FromERP/ICSWHSendMail.cs b/ICSSoft.FromERP/ICSSoft.FromERP/ICSWHSendMail.cs new file mode 100644 index 0000000..9b98d14 --- /dev/null +++ b/ICSSoft.FromERP/ICSSoft.FromERP/ICSWHSendMail.cs @@ -0,0 +1,282 @@ +using Aspose.Cells; +using NPOI.SS.UserModel; +using Quartz; +using System; +using System.Data; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using System.Text; + +namespace ICSSoft.FromERP +{ + /// + /// 库存清单发送邮件 + /// + public class ICSWHSendMail : IJob + { + private static object key = new object(); + private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + public void Execute(IJobExecutionContext context) + { + try + { + lock (key) + { + log.Info("开始……………………………………………………………………"); + Execute(); + log.Info("结束……………………………………………………………………"); + } + } + catch (Exception ex) + { + log.Error(ex.ToString()); + } + } + + public void Execute() + { + + log.Info("发送邮件开始……………………………………………………………………"); + string sql = ""; + try + { + string conStr = ICSHelper.GetConnectString(); + string Namespace = this.GetType().Namespace; + string Class = this.GetType().Name; + + string host = ICSHelper.GetConfigString()["Host"];//邮件服务器 + string port = ICSHelper.GetConfigString()["Port"];//端口 默认25 + string send = ICSHelper.GetConfigString()["SendDisplayName"];//发件人显示名称 + string sendAccount = ICSHelper.GetConfigString()["SendAccount"];//发送邮箱账号 + string sendPassWord = ICSHelper.GetConfigString()["SendPassWord"];//发送邮箱密码 + string to = ICSHelper.GetConfigString()["ToAddress"].ToString();//接受邮箱地址 + string cc = ICSHelper.GetConfigString()["CC"].ToString();//抄送邮箱地址 + #region sql + sql = @" + +SELECT DISTINCT c.ID,bb.oldlotno LotNO,b.INVCode ,b.INVNAME,b.INVPARSETYPE,b.INVSTD,b.INVMODELCODE InvType +,ISNULL(c.BatchNO, a.VenderLotNO) BatchNO, 1 rowNo +,ISNULL(c.ProductionDate, a.PRODUCTDATE) ProductionDate,ISNULL(c.ExpiryDate , DATEADD(DAY, b.INVShelfLife, a.PRODUCTDATE))ExpiryDate +FROM ICSDataCollectINVUP aa with(nolock) +inner JOIN #lotTable1 bb ON aa.mainlotno=bb.LOTNO2 +INNER JOIN ICSITEMLot a with(nolock) ON aa.SubLotNo=a.LotNo +LEFT JOIN ICSINVENTORY b with(nolock) ON a.ItemCODE=b.invcode +LEFT JOIN ICSIsolationWareHouseLine c with(nolock) ON b.invcode=c.INVCode and bb.oldlotno=c.lotno +WHERE b.INVMODELCODE IS NOT NULL AND bb.oldlotno IN (SELECT * FROM #TABLE3) + +) a order by rowNo + +DROP TABLE #TABLE1 +DROP TABLE #TABLE2 +DROP TABLE #TABLE3 +DROP TABLE #lotTable1"; + #endregion + sql = string.Format(sql); + DataTable dt = new DataTable();// ICSHelper.GetDataTable(conStr, sql); + + string html = @" + + "; + + for (int i = 0; i < dt.Rows.Count; i++) + { + html += string.Format(@" " +, dt.Rows[i]["LotNO"].ToString(), dt.Rows[i]["INVCode"].ToString(), dt.Rows[i]["INVNAME"].ToString(), dt.Rows[i]["INVPARSETYPE"].ToString() +, dt.Rows[i]["LotQty"].ToString(), dt.Rows[i]["ReceiveDate"].ToString(), dt.Rows[i]["ExpiryDate"].ToString(), dt.Rows[i]["StorageName"].ToString()); + } + html += "
产品条码产品编码产品名称产品代码数量入库时间失效日期仓库名称
{0}{1}{2}{3}{4}{5}{6}{7}

"; + + string Subject = "产品/材料过期提醒"; + sql = string.Format(@"INSERT INTO ICSSendMail +VALUES (newid(), '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', NULL, 'job', 'job', getdate(), NULL);" +, host, port, send, sendAccount, sendPassWord, to, cc, Subject, 1, html, 0, 0); + ICSHelper.ExecuteDate(conStr, sql); + + + try + { + string Files = System.IO.Directory.GetCurrentDirectory() + @"\邮件\"; + string File = ZLFExcel(dt, Files); + SendEmail(host, Convert.ToInt32(port), "", sendAccount, sendPassWord, to, cc, Subject, true, html, File); + } + catch (Exception ex) + { + throw ex; + } + } + + catch (Exception ex) + { + log.Error(ex.ToString()); + log.Error(sql); + } + + + log.Info("发送邮件结束……………………………………………………………………"); + } + + #region 发邮件 + public static void SendEmail(string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body, string File) + { + try + { + SmtpClient smtpClient = new SmtpClient + { + //EnableSsl = false, + UseDefaultCredentials = false, + Host = SendHost, + Port = SendPort, + Credentials = new NetworkCredential(SendAddress, SendPassword) + }; + + MailMessage mailMessage = new MailMessage + { + Subject = Subject, + SubjectEncoding = Encoding.GetEncoding("utf-8"), + BodyEncoding = Encoding.GetEncoding("utf-8"), + From = new MailAddress(SendAddress, SendDisplayName), + IsBodyHtml = IsBodyHtml, + Body = Body + }; + string SUpFile = File; + Attachment data = new Attachment(SUpFile, MediaTypeNames.Application.Octet); + //附件资料 + System.Net.Mime.ContentDisposition disposition = data.ContentDisposition; + disposition.CreationDate = System.IO.File.GetCreationTime(SUpFile); + disposition.ModificationDate = System.IO.File.GetLastWriteTime(SUpFile); + disposition.ReadDate = System.IO.File.GetLastAccessTime(SUpFile); + //加入邮件附件 + mailMessage.Attachments.Add(data); + string[] array = TOAddress.Split(new char[] + { + ',' + }); + string[] array2 = array; + for (int i = 0; i < array2.Length; i++) + { + string text = array2[i]; + if (!string.IsNullOrEmpty(text)) + { + mailMessage.To.Add(text); + } + } + string[] array3 = CCAddress.Split(new char[] + { + ',' + }); + array2 = array3; + for (int i = 0; i < array2.Length; i++) + { + string text2 = array2[i]; + if (!string.IsNullOrEmpty(text2)) + { + mailMessage.CC.Add(text2); + } + } + ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true); + smtpClient.Send(mailMessage); + } + catch (Exception ex) + { + throw; + } + } + #endregion + + + public string ZLFExcel(DataTable dt1, string Files) + { + string TempletFileName = System.IO.Directory.GetCurrentDirectory() + @"\邮件\产品or材料过期提醒.xls"; + + + IWorkbook workbook = null; + using (FileStream fs = System.IO.File.Open(TempletFileName, FileMode.Open, + FileAccess.Read, FileShare.ReadWrite)) + { + //把xls文件读入workbook变量里,之后就可以关闭了 + workbook = WorkbookFactory.Create(fs); + fs.Close(); + } + ISheet sheet = workbook.GetSheetAt(0); + + ICellStyle cellStyle = workbook.CreateCellStyle(); + + //设置单元格上下左右边框线 + cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + //文字水平和垂直对齐方式 + cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; + cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; + + ICell Cell = null; + IRow row = null;//创建行 + int start = 1;//从第2行开始写入数据 + + //var line = db.SAL_AccountStatementLine.GetList(x => x.AccountStatement == ID).ToList(); + int index = 1; + decimal CostMoney = 0; + + for (int i = 0; i < dt1.Rows.Count; i++) + { + row = sheet.CreateRow(start); + for (int j = 0; j < 8; j++) + { + Cell = row.CreateCell(j); + Cell.CellStyle = cellStyle; + if (j == 0) + Cell.SetCellValue(dt1.Rows[i]["LotNO"].ToString()); + else if (j == 1) + Cell.SetCellValue(dt1.Rows[i]["INVCode"].ToString()); + else if (j == 2) + Cell.SetCellValue(dt1.Rows[i]["INVNAME"].ToString()); + else if (j == 3) + Cell.SetCellValue(dt1.Rows[i]["INVPARSETYPE"].ToString()); + else if (j == 4) + Cell.SetCellValue(dt1.Rows[i]["LotQty"].ToString()); + else if (j == 5) + Cell.SetCellValue(dt1.Rows[i]["ReceiveDate"].ToString()); + else if (j == 6) + Cell.SetCellValue(dt1.Rows[i]["ExpiryDate"].ToString()); + else if (j == 7) + Cell.SetCellValue(dt1.Rows[i]["StorageName"].ToString()); + } + start++; + index++; + string where = string.Format(" LotNO = '{0}' ", dt1.Rows[i]["LotNO"].ToString()); + } + IFont font = workbook.CreateFont(); + font.IsBold = false; + font.FontHeightInPoints = 16; + + string fileName = "产品or材料过期提醒" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; + string excelPath = System.IO.Directory.GetCurrentDirectory() + @"\邮件\" + fileName; + //string pdf2 = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; + //string pdf = HttpContext.Current.Server.MapPath("/File/" + pdf2); + //保存 + using (NpoiMemoryStream ms = new NpoiMemoryStream()) + { + using (FileStream fs = new FileStream(excelPath, FileMode.Create, FileAccess.Write)) + { + ms.AllowClose = false; + workbook.Write(ms); + ms.Flush(); + ms.Seek(0, SeekOrigin.Begin); + ms.AllowClose = true; + byte[] datas = ms.ToArray(); + fs.Write(datas, 0, datas.Length); + fs.Flush(); + + } + } + Workbook workbooks = new Workbook(excelPath); + return excelPath; + } + } +} diff --git a/ICSSoft.FromERP/ICSSoft.FromERP/NpoiMemoryStream.cs b/ICSSoft.FromERP/ICSSoft.FromERP/NpoiMemoryStream.cs new file mode 100644 index 0000000..40a4e15 --- /dev/null +++ b/ICSSoft.FromERP/ICSSoft.FromERP/NpoiMemoryStream.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICSSoft.FromERP +{ + public class NpoiMemoryStream : MemoryStream + { + public NpoiMemoryStream() + { + AllowClose = true; + } + + public bool AllowClose { get; set; } + + public override void Close() + { + if (AllowClose) + base.Close(); + } + } +} diff --git a/ICSSoft.FromERP/Root/Aspose.Cells.dll b/ICSSoft.FromERP/Root/Aspose.Cells.dll new file mode 100644 index 0000000..374abae Binary files /dev/null and b/ICSSoft.FromERP/Root/Aspose.Cells.dll differ diff --git a/ICSSoft.FromERP/Root/ICSSoft.Frame.Data.Entity.dll b/ICSSoft.FromERP/Root/ICSSoft.Frame.Data.Entity.dll index f72dd58..b52d715 100644 Binary files a/ICSSoft.FromERP/Root/ICSSoft.Frame.Data.Entity.dll and b/ICSSoft.FromERP/Root/ICSSoft.Frame.Data.Entity.dll differ diff --git a/ICSSoft.FromERP/Root/NPOI.OOXML.dll b/ICSSoft.FromERP/Root/NPOI.OOXML.dll new file mode 100644 index 0000000..4844c6c Binary files /dev/null and b/ICSSoft.FromERP/Root/NPOI.OOXML.dll differ diff --git a/ICSSoft.FromERP/Root/NPOI.OpenXml4Net.dll b/ICSSoft.FromERP/Root/NPOI.OpenXml4Net.dll new file mode 100644 index 0000000..c6581e3 Binary files /dev/null and b/ICSSoft.FromERP/Root/NPOI.OpenXml4Net.dll differ diff --git a/ICSSoft.FromERP/Root/NPOI.OpenXmlFormats.dll b/ICSSoft.FromERP/Root/NPOI.OpenXmlFormats.dll new file mode 100644 index 0000000..2fba873 Binary files /dev/null and b/ICSSoft.FromERP/Root/NPOI.OpenXmlFormats.dll differ diff --git a/ICSSoft.FromERP/Root/NPOI.dll b/ICSSoft.FromERP/Root/NPOI.dll new file mode 100644 index 0000000..2415072 Binary files /dev/null and b/ICSSoft.FromERP/Root/NPOI.dll differ