123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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
- {
- /// <summary>
- /// 存储单个文件
- /// </summary>
- /// <param name="db"></param>
- /// <param name="macFile"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<MachineMaterialService>.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<Machine>($" 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<MachineMaterialService>.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<MachineMaterialService>.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<MachineMaterialService>.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存成功", "AA文件解析", string.Empty);
- return 1;
- }
- /// <summary>
- /// 批量存储
- /// </summary>
- /// <param name="model"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<MachineMaterialService>.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<MachineMaterialService>.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<MachineMaterialService>.LogError($"采集完成", "LHA数据采集", string.Empty);
- return 1;
- }
- }
- catch (Exception ex)
- {
- LogHelper<MachineMaterialService>.LogError(ex.ToString() + ex.StackTrace, "LHA数据采集", string.Empty);
- return -1;
- }
- }
- }
- }
|