using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using DllEapEntity.Dtos; using System.IO; namespace DllEapDal { public class TFileInfoDal { private IDatabase CurrDb = null; private string commonFilter = null; public TFileInfoDal(IDatabase db) { CurrDb = db; } public TFileInfoDal(IDatabase db, string userCode) { CurrDb = db; var smDal = new StaffMachineDal(CurrDb); string errorinfo = string.Empty; var idFilter = smDal.GetFilter(userCode, ref errorinfo); commonFilter = idFilter; } public TFileInfo IUTFileInfo(TFileInfo mst, string usercode, ref string errorinfo) { try { int result = 0; int id = mst.ID; if (mst.EntityStatusID == 1) { mst.RecCode = usercode; mst.RecTime = DateTime.Now; mst.ModCode = usercode; mst.ModTime = DateTime.Now; result = CurrDb.InsertFor(mst, usercode); if (result < 0) { return null; } object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } id = int.Parse(objid.ToString()); } else { mst.ModCode = usercode; mst.ModTime = DateTime.Now; result = CurrDb.UpdateFor(mst, usercode); if (result < 0) { return null; } } mst = CurrDb.FindEntityFor(id); return mst; } catch (Exception e) { errorinfo = e.Message; return null; } } public TFileInfo AddTFileInfo(byte[] filedatas,string filedir,string filename,int macid,string remark, string usercode, ref string errorinfo) { try { string md5 = FileHelper.GetMD5HashFromFile(filedatas, ref errorinfo); if (md5 == "") return null; string condition = $" and a.Md5Val='{md5}'"; List list = CurrDb.FindListForCondition< TFileInfo>(condition, ref errorinfo).ToList(); if (list.Count > 0) return list[0]; //没有此文件,则新增 TFileInfo entity = new TFileInfo(); entity.FName = filename; entity.FileSize = filedatas.Length; entity.Md5Val = md5; entity.UploadMacID = macid; entity.remark = remark; entity = IUTFileInfo(entity, usercode, ref errorinfo); if (entity == null) return null; string dir = ComputerPath(entity);//文件相对路径 //考虑到linux系统,这里只保存目录 entity.FilePath = $"{dir}"; entity.ModifyEntity(); entity = IUTFileInfo(entity, usercode, ref errorinfo); if (entity == null) return null; //添加实体文件 string tempfiledir = $"{filedir}{Path.DirectorySeparatorChar}{dir}"; int result = UnityHelper.WriteFile(tempfiledir, entity.ID.ToString(), filedatas, ref errorinfo); if (result <= 0) return null; return entity; } catch (Exception e) { errorinfo = e.Message; return null; } } private string ComputerPath(TFileInfo entity) { int dir = entity.ID / 1000; return $"{dir}"; } } }