using Cksoft.Data; using Cksoft.Unity; using DllEapEntity; using System; using System.Collections.Generic; using System.Linq; using DllEapEntity.Dtos; namespace DllEapDal { public class BusinessFileDal { private IDatabase CurrDb = null; private string commonFilter = null; public BusinessFileDal(IDatabase db) { CurrDb = db; } public BusinessFileDal(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 BusinessFile IUBusinessFile(BusinessFile 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; } } /// /// 根据MD5添加程序文件,如果MD5不存在则不添加文件关系 /// /// /// /// /// /// /// /// public BusinessFile UpdownFileForMd5(string md5, string filename, string maccode, string remark, string usercode, ref string errorinfo) { try { string filedir = AppConfigurtaionServices.Configuration["ProgramDir"]; Machine mac = CurrDb.FindListForCondition($" and a.fcode='{maccode}'", ref errorinfo).FirstOrDefault(); if(mac==null) { errorinfo = $"未找到编号为={maccode}的机台信息。"; return null; } TFileInfoDal dal = new TFileInfoDal(CurrDb); TFileInfo fileinfo = CurrDb.FindListForCondition($" and a.Md5Val='{md5}'", ref errorinfo).FirstOrDefault(); if (fileinfo == null) { errorinfo = $"md5={md5}文件不存在。"; return null; } BusinessFile busifile = UpdownFileFor(fileinfo, filename, 1, mac.ID, usercode, ref errorinfo); if (busifile == null) return null; //处理机台程序 busifile = UpdownFileFor(fileinfo, filename, 2, mac.MModeID, usercode, ref errorinfo); if (busifile == null) return null; return busifile; } catch (Exception e) { errorinfo = e.Message; return null; } } /// /// 供文件方式上传程序使用 /// /// /// /// /// /// public BusinessFile UpdownFileForFile(byte[] filedatas, string filename,string maccode, ref string errorinfo) { try { string filedir = AppConfigurtaionServices.Configuration["ProgramDir"]; Machine mac = CurrDb.FindListForCondition($" and a.fcode='{maccode}'", ref errorinfo).FirstOrDefault(); if (mac == null) { errorinfo = $"未找到编号为={maccode}的机台信息。"; return null; } return UpdownFile(filedatas, filedir, filename, mac, "文件方式上传程序", "", ref errorinfo); } catch (Exception e) { errorinfo = e.Message; return null; } } /// /// 程序上次 /// /// 程序数据 /// 程序存放目录 /// 程序名称 /// 机台信息 /// 备注 /// 操作员代码 /// /// public BusinessFile UpdownFile(byte[] filedatas, string filedir, string filename, Machine mac, string remark, string usercode, ref string errorinfo) { try { TFileInfoDal dal = new TFileInfoDal(CurrDb); TFileInfo fileinfo = dal.AddTFileInfo(filedatas, filedir, filename, mac.ID, remark, usercode, ref errorinfo); if (fileinfo == null) return null; BusinessFile busifile = UpdownFileFor(fileinfo, filename, 1, mac.ID, usercode, ref errorinfo); if (busifile == null) return null; //处理机台程序 busifile = UpdownFileFor(fileinfo, filename, 2, mac.MModeID, usercode, ref errorinfo); if (busifile == null) return null; return busifile; } catch (Exception e) { errorinfo = e.Message; return null; } } private BusinessFile UpdownFileFor(TFileInfo fileinfo, string filename, int orgtypeid, int orgid, string usercode, ref string errorinfo) { try { //判断机台是否有此文件,如果没有则添加 BusinessFile busifile = null; string condition = $" and a.FName='{filename}' and a.OrgTypeID={orgtypeid} and a.OrgID={orgid}"; List busifiles = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (busifiles.Count <= 0) { //说明机台没有此文件名称 busifile = new BusinessFile(); busifile.OrgTypeID = orgtypeid; busifile.OrgID = orgid; busifile.FName = filename; busifile = IUBusinessFile(busifile, usercode, ref errorinfo); if (busifile == null) return null; } else { busifile = busifiles[0]; } //直接添加文件关系 BusinessFileRelationDal reladal = new BusinessFileRelationDal(CurrDb); BusinessFileRelation rela = reladal.IUBusinessFileRelation(busifile.ID, fileinfo.ID, usercode, ref errorinfo); if (rela == null) return null; busifile.Version = rela.Version; return busifile; } catch (Exception e) { errorinfo = e.Message; return null; } } private string ComputerPath(TFileInfo entity) { int dir = entity.ID / 1000; return $"{dir}"; } } }