using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllEapEntity; using DllEapEntity.Dtos; using DllEapEntity.Rms; using DllHsms; using DllHsmsWeb; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace DllEapDal { public class MacProgramDal { private string programBaseDir; private IDatabase CurrDb; private string commonFilter = null; public MacProgramDal(IDatabase db) { CurrDb = db; this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue("ProgramDir"); } public MacProgramDal(IDatabase db, string userCode) { CurrDb = db; this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue("ProgramDir"); //var smDal = new StaffMachineDal(CurrDb); //string errorinfo = string.Empty; //var idFilter = smDal.GetFilter(userCode, ref errorinfo); // commonFilter = // idFilter.Replace("a.id", "b.id"); } public MacProgram UpProgram(OrderBlock sendentity, string maccode, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return null; } List datas = sendentity.Datalists.Where(t => t.ParentID == 0).OrderBy(t => t.FNum).ToList(); int parentid = datas[0].ID;//获取L的ID值 datas = sendentity.Datalists.Where(t => t.ParentID == parentid).OrderBy(t => t.FNum).ToList(); string programname = datas[0].FContent.Trim(); byte[] filedatas = datas[1].OrgDatas; if (macs[0].MModeCode.ToLower() == "dad3350" || macs[0].MModeCode.ToLower() == "dfd6341") { //从文件中分析文件描述 byte[] tempbytes = new byte[200]; if (filedatas.Length < 200) { tempbytes = filedatas; } else { Array.Copy(filedatas, 0, tempbytes, 0, 200); } //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); programname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); if (!string.IsNullOrEmpty(errorinfo)) { return null; } } if (macs[0].MModeCode.ToLower() == "bg") { //从文件中分析文件描述 byte[] tempbytes = new byte[200]; if (filedatas.Length < 200) { tempbytes = filedatas; } else { Array.Copy(filedatas, 0, tempbytes, 0, 200); } //string orgname = DiodesProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); programname = Bg8761ProgramFileHelper.GetProgramName(tempbytes, ref errorinfo); if (!string.IsNullOrEmpty(errorinfo)) { return null; } } //获取程序文件存放路径 string filedir = AppConfigurtaionServices.Configuration["ProgramDir"]; MacProgram macprogram = IMacProgram(macs[0], programname, filedir, filedatas, ref errorinfo); if (macprogram == null) { return null; } string macmodelfiledir = AppConfigurtaionServices.Configuration["MacModelProgramDir"]; //添加机型程序 MacModelProgram macmodel = IMacModelProgram(macs[0], programname, macmodelfiledir, filedatas, ref errorinfo); if (macmodel == null) { return null; } datas[1].OrgDatas = null;//清空文件数据 datas[1].FContent = macprogram.ID.ToString();//保存文件存放路径 return macprogram; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public int ClearProgram(string maccode, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } //if (macs[0].MModeCode.ToLower() != "dad3350") //{ // return 1; //} int result = ClearProgram(macs[0], ref errorinfo); return result; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public int ClearProgram(Machine mac, ref string errorinfo) { try { MacOrderSendDal dal = new MacOrderSendDal(CurrDb); OrderDetail order = dal.ReadMachineOrderDetail(mac.ID, 7, 17, "S7F17ClearProgram", ref errorinfo); if (order == null) return -1; string condition = $" and a.preid={order.ID}"; List datas = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (datas == null) return -1; HsmsWeb accessmac = new HsmsWeb(); OrderBlock rec = accessmac.SendOrderFor(mac.FCode, order, datas, ref errorinfo); if (rec == null) return -1; if (int.Parse(rec.Datalists[0].FContent) != 0) { errorinfo = $"delete program has occ:{rec.Datalists[1].FContent}。"; return -1; } return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public byte[] ModifyProgramName(byte[] datas, string maccode, string programname, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return null; } if (macs[0].MModeCode.ToLower() != "dad3350") { return datas; } byte[] filedatas = DiodesProgramFileHelper.ReplaceProgramName(datas, programname, ref errorinfo); if (filedatas == null) { return null; } return filedatas; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public MacProgram IMacProgram(Machine mac, string programname, string mstdir, byte[] filedatas, ref string errorinfo) { try { //主目录+机台编号目录+程序名称目录 string filedir = $"{mstdir}\\{mac.FCode}\\{programname}"; if (!Directory.Exists(@filedir)) { Directory.CreateDirectory(@filedir); } //string condition = $" and a.fcode='{maccode}'"; //List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //if (macs.Count <= 0) //{ // errorinfo = $"未找到机台编号{maccode}的信息。"; // return null; //} MacProgram entity = new MacProgram(); entity.MacID = mac.ID; entity.ProgramName = programname; entity.IsDefault = -1; string condition = $" and a.MacID={mac.ID} and a.ProgramName='{programname}'"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { entity.Version = 1; } else { entity.Version = macprograms.Max(t => t.Version) + 1; } entity.FileOrgName = $"{programname}_{entity.Version}"; CurrDb.InsertFor(entity, ""); //读取记录 object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); //检查programmst里是否存在此程序,如果不存在则添加 ProgramMstDal pmdal = new ProgramMstDal(CurrDb); int tempid = pmdal.IUProgramMst(mac, programname, ref errorinfo); if (tempid <= 0) return null; //写入文件 int result = UnityHelper.WriteFile(filedir, programname + $"_{entity.Version}", filedatas, ref errorinfo); if (result <= 0) return null; entity = CurrDb.FindEntityFor(id); return entity; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public MacModelProgram IMacModelProgram(Machine mac, string programname, string mstdir, byte[] filedatas, ref string errorinfo) { try { //string condition = $" and a.fcode='{maccode}'"; //List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); //if (macs.Count <= 0) //{ // errorinfo = $"未找到机台编号{maccode}的信息。"; // return null; //} //读取机型 string condition = $" and a.id={mac.MModeID}"; List macmodel = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macmodel.Count <= 0) { errorinfo = $"未找到机台编号{mac.FCode}对应的机型信息。"; return null; } //主目录+机台编号目录+程序名称目录 string filedir = $"{mstdir}\\{macmodel[0].FCode}\\{programname}"; if (!Directory.Exists(@filedir)) { Directory.CreateDirectory(@filedir); } MacModelProgram entity = new MacModelProgram(); entity.MacID = mac.ID; entity.ProgramName = programname; entity.IsDefault = -1; entity.MacModelID = macmodel[0].ID; condition = $" and a.MacModelID={macmodel[0].ID} and a.ProgramName='{programname}'"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { entity.Version = 1; } else { entity.Version = macprograms.Max(t => t.Version) + 1; } entity.FileOrgName = $"{programname}_{entity.Version}"; CurrDb.InsertFor(entity, ""); //写入文件 int result = UnityHelper.WriteFile(filedir, programname + $"_{entity.Version}", filedatas, ref errorinfo); if (result <= 0) return null; //读取记录 object objid = CurrDb.FindObject("select @@IDENTITY"); if (objid.ToString() == "") { return null; } int id = int.Parse(objid.ToString()); entity = CurrDb.FindEntityFor(id); return entity; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } //public byte[] ReadMacProgram(string maccode, string programname, string mstdir, ref string errorinfo) //{ // try // { // string condition = $" and a.fcode='{maccode}'"; // List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); // if (macs.Count <= 0) // { // errorinfo = $"未找到机台编号{maccode}的信息。"; // return null; // } // condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1"; // List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); // if (macprograms.Count <= 0) // { // errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。"; // return null; // } // if (macprograms.Count > 1) // { // errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。"; // return null; // } // string filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}"; // //写入文件 // byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); // return filedatas; // } // catch (Exception ex) // { // errorinfo = ex.Message.ToString(); // return null; // } //} public int ReadMacProgram(string maccode, string programname, string mstdir, ref int filelen, ref string filepath, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。"; return -1; } if (macprograms.Count > 1) { errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。"; return -1; } //filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}"; filepath = $"{mstdir}\\{maccode}\\{programname}\\{macprograms[0].FileOrgName}"; //写入文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); if (filedatas == null) return -1; filelen = filedatas.Length; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public byte[] ReadMacProgram(Machine mac, string programname, int orgtypeid, string mstdir, ref string errorinfo) { try { string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.IsDefault=1"; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。"; return null; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return null; } condition = $" and a.id={templists[0].FileInfoID}"; TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; if (files.FilePath.Contains("\\")) { filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}"; } else { filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; } //读取文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); if (filedatas == null) return null; return filedatas; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public byte[] ReadMacProgramD(Machine mac, string programname, int orgtypeid, string mstdir, ref string errorinfo) { try { string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1 "; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。"; return null; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return null; } condition = $" and a.id={templists[0].FileInfoID}"; TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; if (files.FilePath.Contains("\\")) { filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}"; } else { filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; } //读取文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); if (filedatas == null) return null; return filedatas; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public int ReadMacProgram(Machine mac, string programname, int orgtypeid, string mstdir, ref string ppGroup, ref string errorinfo) { try { string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1"; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。"; return -1; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return -1; } ppGroup = templists[0].FileOrgName.Replace($"@{programname}.zip", ""); // condition = $" and a.id={templists[0].FileInfoID}"; //TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; //string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; //if (files.FilePath.Contains("\\")) //{ // filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}"; //} //else //{ // filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; //} //读取文件 // byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); //if (filedatas == null) // return null; // System.IO.File.Copy(filepath, toPath, true); return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public byte[] ReadMacProgram(Machine mac, string programname, int ver, int orgtypeid, string mstdir, ref string errorinfo) { //try //{ string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.Version={ver}"; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的标准程序。"; return null; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return null; } condition = $" and a.id={templists[0].FileInfoID}"; TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; //读取文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); if (filedatas == null) return null; return filedatas; //} //catch (Exception ex) //{ // errorinfo = ex.Message.ToString(); // return null; //} } public TFileInfo ReadMacProgramForFile(Machine mac, string programname, int ver, int orgtypeid, string mstdir, ref string errorinfo) { try { string condition = $" and b.OrgTypeID={orgtypeid} and b.FName='{programname}' and a.Version={ver}"; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的程序。"; return null; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个程序。"; return null; } condition = $" and a.id={templists[0].FileInfoID}"; TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; files.FilePath = filepath; return files; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public TFileInfo ReadMacProgramForFile(Machine mac, string programname, int orgtypeid, string mstdir, ref string ppGroup, ref string errorinfo) { try { string condition = $" and b.OrgTypeID={orgtypeid} and b.FName like '%@{programname}.zip' and a.IsDefault=1 "; if (orgtypeid == 1) { condition = $"{condition} and b.OrgID={mac.ID}"; } else { condition = $"{condition} and b.OrgID={mac.MModeID}"; } List templists = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (templists.Count <= 0) { errorinfo = $"未找到机台【{mac.FCode}】,程序名称【{programname}】,对应的程序。"; return null; } if (templists.Count > 1) { errorinfo = $"机台【{mac.FCode}】,程序名称【{programname}】,对应了多个程序。"; return null; } ppGroup = templists[0].FileOrgName.Replace($"@{programname}.zip", ""); condition = $" and a.id={templists[0].FileInfoID}"; TFileInfo files = CurrDb.FindListForCondition(condition, ref errorinfo).ToList()[0]; string filepath = $"{mstdir}{Path.DirectorySeparatorChar}{files.FilePath}{Path.DirectorySeparatorChar}{files.ID}"; files.FilePath = filepath; return files; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public byte[] ReadMacProgram(string maccode, string programname, string mstdir, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return null; } condition = $" and a.MacID={macs[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { errorinfo = $"未找到机台【{maccode}】,程序名称【{programname}】,对应的标准程序。"; return null; } if (macprograms.Count > 1) { errorinfo = $"机台【{maccode}】,程序名称【{programname}】,对应了多个标准程序。"; return null; } //filepath = $"{mstdir}\\{maccode}\\{programname}\\{programname}_{macprograms[0].Version}"; string filepath = $"{mstdir}\\{maccode}\\{programname}\\{macprograms[0].FileOrgName}"; //写入文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); if (filedatas == null) return null; return filedatas; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public int ReadMacModelProgram(string maccode, string programname, string mstdir, ref int filelen, ref string filepath, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } //读取机型 condition = $" and a.id={macs[0].MModeID}"; List macmodels = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macmodels.Count <= 0) { errorinfo = $"未找到机台【{maccode}】,对应的机型。"; return -1; } condition = $" and a.MacModelID={macmodels[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应的标准程序。"; return -1; } if (macprograms.Count > 1) { errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return -1; } //filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{programname}_{macprograms[0].Version}"; filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{macprograms[0].FileOrgName}"; //写入文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); filelen = filedatas.Length; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } public byte[] ReadMacModelProgram(string maccode, string programname, string mstdir, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return null; } //读取机型 condition = $" and a.id={macs[0].MModeID}"; List macmodels = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macmodels.Count <= 0) { errorinfo = $"未找到机台【{maccode}】,对应的机型。"; return null; } condition = $" and a.MacModelID={macmodels[0].ID} and a.ProgramName='{programname}' and a.IsDefault=1"; List macprograms = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macprograms.Count <= 0) { errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应的标准程序。"; return null; } if (macprograms.Count > 1) { errorinfo = $"未找到机型【{macmodels[0].FCode}】,程序名称【{programname}】,对应了多个标准程序。"; return null; } //filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{programname}_{macprograms[0].Version}"; string filepath = $"{mstdir}\\{macmodels[0].FCode}\\{programname}\\{macprograms[0].FileOrgName}"; //写入文件 byte[] filedatas = UnityHelper.ReadFile(filepath, ref errorinfo); return filedatas; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return null; } } public IEnumerable Get() { var models = CurrDb.FindList(); return models; } public IEnumerable Get(int start, int length, string order, string sort, string filter, string errorinfo) { var sql = new BusinessFileRelation().GetSelectSql(); var versions = CurrDb.FindListForCondition($"{filter} order by {sort} {order} limit {start - 1},{length} ", ref errorinfo); return versions.Select(c => new MacProgram { ID = c.ID, FileOrgName = c.FileOrgName, IsDefault = c.IsDefault, ModCode = c.ModCode, MacID = c.OrgID, Version = c.Version, ModTime = c.ModTime, RecCode = c.RecCode, ProgramName = c.ProgramName, RecTime = c.RecTime, Remark = c.remark, FileInfoRemark = c.FileInfoRemark, UploadMacCode = c.UploadMacCode }); } public int GetVersionCount(string filter) { string errorinfo = string.Empty; var versions = CurrDb.FindListForCondition($"{filter}", ref errorinfo); return versions.Count(); } public IEnumerable GetGrouped(int start, int length, string order, string sort, string filter, string errorinfo) { if (!string.IsNullOrEmpty(filter)) filter = filter.Replace("%2B", "+"); var sql = $"select a.Id,a.FName as ProgramName,b.FCode MacCode," + $"b.FName MacName,c.FCode MacModelCode,c.FName MacModelName," + $"b.Id MacId,c.Id MacModelId,b.RegionId " + $"from businessfile a " + $"inner join Machine b on a.OrgId=b.Id and a.OrgTypeID=1 " + $"left join MacModel c on b.MModeID=c.id " + $"where 1=1 {filter + commonFilter} order by {sort} {order} limit {start - 1},{length} "; var datas = CurrDb.FindList(sql); if (datas != null && datas.Count() > 0) { var regionDal = new FactoryRegionDal(CurrDb); var allRegions = regionDal.Get(1, 10000, "ID", "asc", string.Empty, errorinfo); foreach (var item in datas) { var regionName = string.Empty; if (item.RegionId != null) { regionName = regionDal.GetFullRegionName(item.RegionId.Value, allRegions); } item.RegionName = regionName; } } return datas; } public int GetGroupedCount(string filter) { if (!string.IsNullOrEmpty(filter)) filter = filter.Replace("%2B", "+"); string sql = $"select count(1) " + $"from businessfile a " + $"inner join Machine b on a.OrgId=b.Id and a.OrgTypeID=1 " + $"left join MacModel c on b.MModeID=c.id " + $"where 1=1 {filter + commonFilter}"; return Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "0"); } public int Delete(int id, ref string msg) { if (CurrDb.DeleteFor(id) < 0) { msg = "删除失败"; return -1; } msg = string.Empty; return 1; } public BusinessFile getBusinessFile(int id) { return CurrDb.FindEntityFor(id); } public IEnumerable GetProgramFiles(string macCode, string programName, ref string errorinfo) { string path = $@"{programBaseDir}\{macCode}\{programName}"; if (!Directory.Exists(path)) { errorinfo = "程序目录不存在,请确认"; return null; } var programs = Directory.EnumerateFiles(path).Select(c => { var arr = c.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries); var temp = arr.Last(); var name = temp.Split(new char[] { '.' }).FirstOrDefault(); return name; }); return programs; } /// /// 设置默认版次 /// /// /// /// /// /// /// public int UpdateField(int id, int isDefault, int macId, string programName, ref string errorinfo) { if (isDefault != -1) { var filter = $" and a.BusinessFileId=(select id from businessfile where orgId={macId} and OrgTypeID=1 and FName='{programName}'" + $" limit 0,1) and a.isDefault=1"; var entity = CurrDb.FindListForCondition(filter, ref errorinfo).FirstOrDefault(); if (entity != null) { if (entity.ID != id) { errorinfo = "当前机台已经存在默认程序,如需重新设置请先取消原来的默认程序"; } else { errorinfo = "当前程序版次已经是默认版次,无需重复设置"; } return -1; } } string sql = $"update BusinessFileRelation set isDefault={isDefault} where id={id}"; return CurrDb.ExecuteBySql(sql); } /// /// 修改程序名称,先修改数据库程序名称(macprogram,macmodelprogram),然后重命名文件夹 /// /// /// /// /// /// public int ModifyProgramName(string maccode, string orgname, string desname, ref string errorinfo) { try { string condition = $" and a.fcode='{maccode}'"; List macs = CurrDb.FindListForCondition(condition, ref errorinfo).ToList(); if (macs.Count <= 0) { errorinfo = $"未找到机台编号{maccode}的信息。"; return -1; } //检查目的程序名称是否存在 string sqlstr = $" and a.OrgId={macs[0].ID} and OrgTypeID=1 "; List programs = CurrDb.FindListForCondition(sqlstr, ref errorinfo).Where(c => c.FName == desname).ToList(); if (programs.Count > 0) { errorinfo = $"机台{maccode}的目的程序名称{desname}已存在,不能修改。"; return -1; } //读取机台原程序信息 sqlstr = $" and a.OrgId={macs[0].ID} and OrgTypeID=1"; programs = CurrDb.FindListForCondition(sqlstr, ref errorinfo).Where(c => c.FName == orgname).ToList(); sqlstr = $@"select a.* from BusinessFile a left join businessfilerelation b on a.id=b.businessfileid left join TFileInfo c on b.FileInfoId=c.Id where 1=1 and a.OrgTypeID=2 and a.OrgId={macs[0].MModeID}"; IEnumerable modelprograms = CurrDb.FindList(sqlstr).Where(c => c.FName == desname); if (modelprograms.Count() > 0) { errorinfo = $"机型{macs[0].MModeCode}目的程序名称{desname}已存在,不能修改。"; return -1; } sqlstr = $" and OrgTypeId=2 and a.OrgId={macs[0].MModeID}"; modelprograms = CurrDb.FindListForCondition(sqlstr, ref errorinfo).Where(c => c.FName == orgname).ToList(); if (programs.Count > 0) { foreach (var item in programs) item.FName = desname; if (CurrDb.UpdateFor(programs, "") < 0) { errorinfo = "更新机台程序失败"; return -1; } } if (modelprograms.Count() > 0) { foreach (var item in modelprograms) item.FName = desname; if (CurrDb.UpdateFor(modelprograms, "") < 0) { errorinfo = "更新机型程序失败"; return -1; } } var programmstDal = new ProgramMstDal(CurrDb); if (programmstDal.IUProgramMst(macs[0], desname, ref errorinfo) < 0) return -1; return 1; } catch (Exception ex) { errorinfo = ex.Message.ToString(); return -1; } } /// /// 拷贝机台版次 /// /// /// /// /// /// public int CopyProgramVersion(IEnumerable macCodes, int proid, string userCode, ref string errorinfo) { if (macCodes == null || macCodes.Count() <= 0) { errorinfo = "需要复制的机台不能为空"; return -1; } var program = CurrDb.FindEntityFor(proid); // program.ProgramName = program.ProgramName.Trim(); if (program == null) { errorinfo = "待复制的程序不存在或已被删除"; return -1; } var macId = CurrDb.FindEntityFor(program.BusinessFileID).OrgID; var oriMac = CurrDb.FindEntityFor(macId); foreach (var item in macCodes) { var error = string.Empty; var res = this.CopySingleVersion(oriMac.FCode, item, program, userCode, ref error); errorinfo += error + "
"; } return 1; } /// /// 复制单个版次程序 /// /// /// /// /// /// /// private int CopySingleVersion(string oriMacCode, string macCode, BusinessFileRelation macProgram, string userCode, ref string errorinfo) { using (IDatabase db = DbFactory.Base("eap")) { CurrDb = db; CurrDb.BeginTrans(); var machine = CurrDb.FindListForCondition($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault(); var proVersion = CurrDb.FindListForCondition($" and b.OrgId={machine.ID} " + $"and b.OrgTypeID={macProgram.OrgTypeID} and b.FName='{macProgram.ProgramName}'" + $"and a.Version='{macProgram.Version}'", ref errorinfo).FirstOrDefault(); if (proVersion != null && proVersion.IsDefault == 1) { CurrDb.Rollback(); errorinfo = $"机台【{macCode}】当前程序版次已存在,并且该版次为默认程序,不能覆盖"; return 1; } var bf = new BusinessFile { OrgID = machine.ID, OrgTypeID = 1, FName = macProgram.ProgramName, RecCode = userCode, RecTime = DateTime.Now, ModCode = userCode, ModTime = DateTime.Now, }; var existBf = CurrDb.FindListForCondition($" and a.FName='{macProgram.ProgramName}' and a.OrgTypeID=1 " + $"and a.OrgId={machine.ID}", ref errorinfo).FirstOrDefault(); var businessFileId = 0; if (existBf != null) { businessFileId = existBf.ID; } else { if (CurrDb.InsertFor(bf, userCode) < 0) { CurrDb.Rollback(); errorinfo = "插入主表失败"; return -1; } string sql = "select @@identity;"; businessFileId = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); } var obj = new BusinessFileRelation(); obj.IsDefault = macProgram.IsDefault; obj.BusinessFileID = businessFileId; obj.ModCode = userCode; obj.ModTime = DateTime.Now; obj.Version = macProgram.Version; obj.RecCode = userCode; obj.RecTime = DateTime.Now; obj.FileInfoID = macProgram.FileInfoID; var existPros = CurrDb.FindListForCondition($" and b.OrgId={machine.ID} and a.IsDefault=1 " + $"and b.OrgTypeID=1", ref errorinfo); if (existPros != null && existPros.Count() > 0) { obj.IsDefault = -1; } if (proVersion != null) // 当待复制的程序不是默认程序且目标机台有同名程序时,更新数据库数据并覆盖原程序 { obj.ID = proVersion.ID; if (CurrDb.UpdateFor(obj, userCode) < 0) { errorinfo = $"机台号【{macCode}】的机台覆盖程序失败,原因是【更新数据库表记录失败】"; CurrDb.Rollback(); return -1; } } else { if (CurrDb.InsertFor(obj, userCode) < 0) { errorinfo = $"机台号【{macCode}】的机台复制程序失败,原因是【插入数据库表记录失败】"; CurrDb.Rollback(); return -1; } } var program = CurrDb.FindListForCondition($" and a.FName='{macProgram.ProgramName}' " + $"and a.ModelID='{machine.MModeID}' and a.ProcessCode='{machine.PCode}' ", ref errorinfo).FirstOrDefault(); if (program == null) { var temp = new ProgramMst { FName = macProgram.ProgramName, ModelID = machine.MModeID, ProcessCode = machine.PCode, RecCode = userCode, RecTime = DateTime.Now, ModCode = userCode, ModTime = DateTime.Now, Remark = $"由机台【{machine.FCode}】上复制而来" }; if (CurrDb.InsertFor(temp, userCode) < 0) { errorinfo = "程序表新增数据失败"; return -1; } } errorinfo = $"机台【{macCode}】复制版次成功"; CurrDb.Commit(); return 1; } } /// /// 拷贝整个机台的程序到另一个机台 /// /// /// /// /// /// public int CopyWholePrograms(IEnumerable macCodes, string oriMacCode, string userCode, ref string errorinfo) { if (macCodes == null || macCodes.Count() <= 0) { errorinfo = "需要复制的机台不能为空"; return -1; } var machine = CurrDb.FindListForCondition($" and a.FCode='{oriMacCode}'", ref errorinfo).FirstOrDefault(); if (machine == null) { errorinfo = "机台不存在"; return -1; } var toCopyPros = CurrDb.FindListForCondition($" and a.OrgId={machine.ID} and a.OrgTypeId=1", ref errorinfo); if (toCopyPros == null || toCopyPros.Count() <= 0) { errorinfo = "待复制的程序为空"; return -1; } foreach (var item in toCopyPros) { var versions = CurrDb.FindListForCondition($" and a.BusinessFileId={item.ID}", ref errorinfo); item.BusinessFileRelations = versions; } foreach (var item in macCodes) { var error = string.Empty; var res = this.CopyWholeSinglePro(item, oriMacCode, toCopyPros, userCode, ref error); errorinfo += error + "
"; } return 1; } /// /// 拷贝单个程序 /// /// /// /// /// /// /// private int CopyWholeSinglePro(string macCode, string oriMacCode, IEnumerable pros, string userCode, ref string errorinfo) { try { CurrDb = DbFactory.Base("eap"); CurrDb.BeginTrans(); var machine = CurrDb.FindListForCondition($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault(); var existPros = CurrDb.FindListForCondition($" and a.OrgId={machine.ID} and a.OrgTypeId=1", ref errorinfo); foreach (var item in pros) { if (existPros.FirstOrDefault(c => c.OrgTypeID == item.OrgTypeID && c.OrgID == machine.ID && c.FName == item.FName) == null) { item.OrgID = machine.ID; if (CurrDb.InsertFor(item, userCode) < 0) { errorinfo = $"机台【{machine.FCode}】复制失败,原因是:插入数据库失败"; CurrDb.Rollback(); return -1; } var sql = "select @@identity;"; var id = Convert.ToInt32(CurrDb.FindList(sql).FirstOrDefault() ?? "-1"); if (item.BusinessFileRelations != null && item.BusinessFileRelations.Count() > 0) { foreach (var r in item.BusinessFileRelations) { var temp = new BusinessFileRelation { BusinessFileID = id, FileInfoID = r.FileInfoID, IsDefault = r.IsDefault, Version = r.Version, RecCode = userCode, RecTime = DateTime.Now, ModCode = userCode, ModTime = DateTime.Now }; if (CurrDb.InsertFor(temp, userCode) < 0) { CurrDb.Rollback(); errorinfo = "插入子表失败"; return -1; } } } var program = CurrDb.FindListForCondition($" and a.FName='{item.FName}' " + $"and a.ModelID='{machine.MModeID}' and a.ProcessCode='{machine.PCode}' ", ref errorinfo).FirstOrDefault(); if (program == null) { var temp = new ProgramMst { FName = item.FName, ModelID = machine.MModeID, ProcessCode = machine.PCode, RecCode = userCode, RecTime = DateTime.Now, ModCode = userCode, ModTime = DateTime.Now, Remark = $"由机台【{machine.FCode}】上复制而来" }; if (CurrDb.InsertFor(temp, userCode) < 0) { errorinfo = "程序表新增数据失败"; return -1; } } } } errorinfo = $"机台【{macCode}】程序复制成功"; CurrDb.Commit(); return 1; } catch (Exception e) { errorinfo = e.Message; CurrDb.Rollback(); return -1; } finally { if (CurrDb != null) CurrDb.Close(); } } public BusinessFileRelation getBusinessFileRelation(int id) { return CurrDb.FindEntityFor(id); } public IEnumerable getBusinessFile(BusinessFileRelation macProgram) { string sql = $"select * from BusinessFile where OrgId='{macProgram.OrgID}' and OrgTypeId=1 and Fname='{macProgram.ProgramName}' and OrgTypeID =1 "; return CurrDb.FindList(sql); } /// /// 删除单个版次 /// /// /// /// /// public int DeleteSingVersion(int id, string macCode, ref string errorinfo) { var macProgram = CurrDb.FindEntityFor(id); if (macProgram != null) { var sql = $"delete from businessfilerelation where id={id}"; if (CurrDb.ExecuteBySql(sql) < 0) { errorinfo = "删除数据库中程序版次失败"; return -1; } var macPros = CurrDb.FindListForCondition($" and b.OrgId='{macProgram.OrgID}' and b.OrgTypeID=1 " + $"and b.FName='{macProgram.ProgramName}'", ref errorinfo); if (macPros == null || macPros.Count() <= 0) { sql = $"delete from BusinessFile where OrgId='{macProgram.OrgID}' and OrgTypeId=1 and Fname='{macProgram.ProgramName}' and OrgTypeID =1 "; if (CurrDb.ExecuteBySql(sql) < 0) { errorinfo = "删除程序版次失败"; return -1; } } } return 1; } public IEnumerable getbusinessfilerelation(string proName, string macCode, ref string errorinfo) { var machine = CurrDb.FindListForCondition($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault(); var busineeFile = CurrDb.FindListForCondition($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'", ref errorinfo).FirstOrDefault(); if (busineeFile == null) { errorinfo = "当前程序不存在或已被删除"; return null; } var sql = $"select * from businessfilerelation where BusinessFileId={busineeFile.ID}"; return CurrDb.FindList(sql); } public BusinessFile getBusinessFile(string proName, string macCode, ref string errorinfo) { var machine = CurrDb.FindListForCondition($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault(); var busineeFile = CurrDb.FindListForCondition($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'", ref errorinfo).FirstOrDefault(); if (busineeFile == null) { errorinfo = "当前程序不存在或已被删除"; return null; } return CurrDb.FindEntity(busineeFile.ID); } /// /// 删除整个程序 /// /// /// /// /// public int DeleteWholeProgram(string proName, string macCode, ref string errorinfo) { var machine = CurrDb.FindListForCondition($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault(); var busineeFile = CurrDb.FindListForCondition($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'", ref errorinfo).FirstOrDefault(); if (busineeFile == null) { errorinfo = "当前程序不存在或已被删除"; return -1; } var sql = $"delete from businessfilerelation where BusinessFileId={busineeFile.ID}"; if (CurrDb.ExecuteBySql(sql) < 0) { errorinfo = "删除机台程序表失败"; return -1; } if (CurrDb.DeleteFor(busineeFile.ID) < 0) { errorinfo = "删除程序主表失败"; return -1; } return 1; } } }