using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapDal.OFILM; using DllEapEntity; using DllEapEntity.OFILM; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace WebUpload.Controllers { public class MachineMaterialService { /// /// 存储单个文件 /// /// /// /// /// public int UploadFile(IDatabase db, EapMacFile macFile, ref string errorinfo) { var mstDal = new MachineMaterialMstDal(db); var fileNameWithoutExt = Path.GetFileNameWithoutExtension(macFile.FileName); var date = fileNameWithoutExt.Substring(0, 8); var shift = fileNameWithoutExt.Substring(8, 1); var recipe = fileNameWithoutExt.Substring(9); if (string.IsNullOrEmpty(recipe)) { errorinfo = "文件名不合法"; LogHelper.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," + $"原因为[{errorinfo}]", "AA文件解析", string.Empty); return -1; } date = Convert.ToDateTime(date.Insert(4, "-").Insert(7, "-")).ToString("yyyy-MM-dd"); var mac = db.FindListForCondition($" and a.FCode='{macFile.MacCode}'", ref errorinfo).FirstOrDefault(); if (mac == null) { errorinfo = "机台未录入EAP系统"; return -1; } var exist = mstDal.Get($" and b.FCode='{macFile.MacCode}' and a.FDate='{date}' and a.Shift='{shift}' " + $"and a.recipe = '{recipe}'", ref errorinfo).FirstOrDefault(); if (exist != null && exist.Md5Val == macFile.Md5) { errorinfo = $"机台[{macFile.MacCode}]的文件[{macFile.FileName}]已存在"; LogHelper.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," + $"原因为{errorinfo}", "AA文件解析", string.Empty); return 1; } // 测试环境 var rootPath = AppConfigurtaionServices.Configuration["AAMaterialWinSavePath"]; //正式环境 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { rootPath = AppConfigurtaionServices.Configuration["AAMaterialSavePath"]; } if (string.IsNullOrEmpty(rootPath)) { errorinfo = "文件保存路径未设置"; return -1; } var savePath = $"{rootPath}/{macFile.MacCode}"; if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } var filePath = $"{savePath}/{macFile.FileName}"; FileStream fs = new FileStream(filePath, FileMode.Create); var bytes = Encoding.UTF8.GetBytes(macFile.FileContent); fs.Write(bytes, 0, bytes.Length); fs.Dispose(); fs.Close(); if (exist != null && exist.Md5Val != macFile.Md5) { var sql = $"DELETE FROM MachineMaterialDetail where mstid={exist.Id}"; if (db.ExecuteBySql(sql) < 0) { errorinfo = "删除原数据失败"; return -1; } sql = $"DELETE FROM MaterialErrorMessage where MstId={exist.Id}"; if (db.ExecuteBySql(sql) < 0) { errorinfo = "删除原数据失败"; return -1; } sql = $"DELETE FROM MachineMaterialMst WHERE ID={exist.Id} "; if (db.ExecuteBySql(sql) < 0) { errorinfo = "删除原数据失败"; LogHelper.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," + $"原因为[{errorinfo}]", "AA文件解析", string.Empty); return -1; } } var mst = new MachineMaterialMst { MacId = mac.ID, FDate = Convert.ToDateTime(date), FilePath = filePath, IsAnalysised = -1, Shift = shift, Md5Val = macFile.Md5, Recipe = recipe }; if (mstDal.Insert(mst, ref errorinfo) < 0) return -1; LogHelper.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存成功", "AA文件解析", string.Empty); return 1; } /// /// 批量存储 /// /// /// /// public int UploadTrans(EapMacUploadModel model, ref string errorinfo) { using (IDatabase db = DbFactory.Base("eap")) { db.BeginTrans(); foreach (var item in model.Files) { if (this.UploadFile(db, item, ref errorinfo) < 0) { db.Rollback(); return -1; } } db.Commit(); return 1; } } public int UploadTableTrans(LHAMachineModel model, ref string errorinfo) { try { using (IDatabase db = DbFactory.Base("eap")) { db.BeginTrans(); var mstDal = new CodelinkDal(db); LogHelper.LogError($"准备采集", "LHA数据采集", string.Empty); //string delSql = $@"delete from Postbondview where MacCode = '{model.MacCode}' and TIME<'{DateTime.Now.ToString("yyyy-MM-dd 00:00")}' and TIME> '{DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00")}'"; //LogHelper.LogError("删除:"+delSql, "LHA数据采集", string.Empty); //db.ExecuteBySql(delSql); foreach (var item in model.Postbondviews) { if (mstDal.Insert(item, model.MacCode, ref errorinfo) < 0) { db.Rollback(); return -1; } } db.Commit(); LogHelper.LogError($"采集完成", "LHA数据采集", string.Empty); return 1; } } catch (Exception ex) { LogHelper.LogError(ex.ToString() + ex.StackTrace, "LHA数据采集", string.Empty); return -1; } } } }