12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409 |
- 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<string>("ProgramDir");
- }
- public MacProgramDal(IDatabase db, string userCode)
- {
- CurrDb = db;
- this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue<string>("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<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号{maccode}的信息。";
- return null;
- }
- List<OrderData> 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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<OrderData> datas = CurrDb.FindListForCondition<OrderData>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(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<MacProgram>(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<MacProgram>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- //if (macs.Count <= 0)
- //{
- // errorinfo = $"未找到机台编号{maccode}的信息。";
- // return null;
- //}
- //读取机型
- string condition = $" and a.id={mac.MModeID}";
- List<MacModel> macmodel = CurrDb.FindListForCondition<MacModel>(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<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(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<MacModelProgram>(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<MacModelProgram>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<BusinessFileRelation> templists = CurrDb.FindListForCondition<BusinessFileRelation>(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<TFileInfo>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(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<MacProgram> macprograms = CurrDb.FindListForCondition<MacProgram>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号{maccode}的信息。";
- return -1;
- }
- //读取机型
- condition = $" and a.id={macs[0].MModeID}";
- List<MacModel> macmodels = CurrDb.FindListForCondition<MacModel>(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<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(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<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号{maccode}的信息。";
- return null;
- }
- //读取机型
- condition = $" and a.id={macs[0].MModeID}";
- List<MacModel> macmodels = CurrDb.FindListForCondition<MacModel>(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<MacModelProgram> macprograms = CurrDb.FindListForCondition<MacModelProgram>(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<MacProgram> Get()
- {
- var models = CurrDb.FindList<MacProgram>();
- return models;
- }
- public IEnumerable<MacProgram> Get(int start, int length, string order, string sort, string filter, string errorinfo)
- {
- var sql = new BusinessFileRelation().GetSelectSql();
- var versions = CurrDb.FindListForCondition<BusinessFileRelation>($"{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<BusinessFileRelation>($"{filter}", ref errorinfo);
- return versions.Count();
- }
- public IEnumerable<MacProgramDto> 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<MacProgramDto>(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<string>(sql).FirstOrDefault() ?? "0");
- }
- public int Delete(int id, ref string msg)
- {
- if (CurrDb.DeleteFor<BusinessFile>(id) < 0)
- {
- msg = "删除失败";
- return -1;
- }
- msg = string.Empty;
- return 1;
- }
- public BusinessFile getBusinessFile(int id)
- {
- return CurrDb.FindEntityFor<BusinessFile>(id);
- }
- public IEnumerable<string> 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;
- }
- /// <summary>
- /// 设置默认版次
- /// </summary>
- /// <param name="id"></param>
- /// <param name="isDefault"></param>
- /// <param name="macId"></param>
- /// <param name="programName"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<BusinessFileRelation>(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);
- }
- /// <summary>
- /// 修改程序名称,先修改数据库程序名称(macprogram,macmodelprogram),然后重命名文件夹
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="orgname"></param>
- /// <param name="desname"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int ModifyProgramName(string maccode, string orgname, string desname, ref string errorinfo)
- {
- try
- {
- string condition = $" and a.fcode='{maccode}'";
- List<Machine> macs = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList();
- if (macs.Count <= 0)
- {
- errorinfo = $"未找到机台编号{maccode}的信息。";
- return -1;
- }
- //检查目的程序名称是否存在
- string sqlstr = $" and a.OrgId={macs[0].ID} and OrgTypeID=1 ";
- List<BusinessFile> programs = CurrDb.FindListForCondition<BusinessFile>(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<BusinessFile>(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<BusinessFile> modelprograms = CurrDb.FindList<BusinessFile>(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<BusinessFile>(sqlstr, ref errorinfo).Where(c => c.FName == orgname).ToList();
- if (programs.Count > 0)
- {
- foreach (var item in programs)
- item.FName = desname;
- if (CurrDb.UpdateFor<BusinessFile>(programs, "") < 0)
- {
- errorinfo = "更新机台程序失败";
- return -1;
- }
- }
- if (modelprograms.Count() > 0)
- {
- foreach (var item in modelprograms)
- item.FName = desname;
- if (CurrDb.UpdateFor<BusinessFile>(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;
- }
- }
- /// <summary>
- /// 拷贝机台版次
- /// </summary>
- /// <param name="macCodes"></param>
- /// <param name="proid"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int CopyProgramVersion(IEnumerable<string> macCodes, int proid, string userCode, ref string errorinfo)
- {
- if (macCodes == null || macCodes.Count() <= 0)
- {
- errorinfo = "需要复制的机台不能为空";
- return -1;
- }
- var program = CurrDb.FindEntityFor<BusinessFileRelation>(proid);
- // program.ProgramName = program.ProgramName.Trim();
- if (program == null)
- {
- errorinfo = "待复制的程序不存在或已被删除";
- return -1;
- }
- var macId = CurrDb.FindEntityFor<BusinessFile>(program.BusinessFileID).OrgID;
- var oriMac = CurrDb.FindEntityFor<Machine>(macId);
- foreach (var item in macCodes)
- {
- var error = string.Empty;
- var res = this.CopySingleVersion(oriMac.FCode, item, program, userCode, ref error);
- errorinfo += error + "<br />";
- }
- return 1;
- }
- /// <summary>
- /// 复制单个版次程序
- /// </summary>
- /// <param name="oriMacCode"></param>
- /// <param name="macCode"></param>
- /// <param name="macProgram"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<Machine>($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault();
- var proVersion = CurrDb.FindListForCondition<BusinessFileRelation>($" 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<BusinessFile>($" 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<string>(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<BusinessFileRelation>($" 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<BusinessFileRelation>(obj, userCode) < 0)
- {
- errorinfo = $"机台号【{macCode}】的机台复制程序失败,原因是【插入数据库表记录失败】";
- CurrDb.Rollback();
- return -1;
- }
- }
- var program = CurrDb.FindListForCondition<ProgramMst>($" 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;
- }
- }
- /// <summary>
- /// 拷贝整个机台的程序到另一个机台
- /// </summary>
- /// <param name="macCodes"></param>
- /// <param name="oriMacCode"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int CopyWholePrograms(IEnumerable<string> macCodes, string oriMacCode, string userCode, ref string errorinfo)
- {
- if (macCodes == null || macCodes.Count() <= 0)
- {
- errorinfo = "需要复制的机台不能为空";
- return -1;
- }
- var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode='{oriMacCode}'", ref errorinfo).FirstOrDefault();
- if (machine == null)
- {
- errorinfo = "机台不存在";
- return -1;
- }
- var toCopyPros = CurrDb.FindListForCondition<BusinessFile>($" 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<BusinessFileRelation>($" 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 + "<br />";
- }
- return 1;
- }
- /// <summary>
- /// 拷贝单个程序
- /// </summary>
- /// <param name="macCode"></param>
- /// <param name="oriMacCode"></param>
- /// <param name="pros"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- private int CopyWholeSinglePro(string macCode, string oriMacCode, IEnumerable<BusinessFile> pros, string userCode, ref string errorinfo)
- {
- try
- {
- CurrDb = DbFactory.Base("eap");
- CurrDb.BeginTrans();
- var machine = CurrDb.FindListForCondition<Machine>($" and a.FCode='{macCode}'", ref errorinfo).FirstOrDefault();
- var existPros = CurrDb.FindListForCondition<BusinessFile>($" 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<BusinessFile>(item, userCode) < 0)
- {
- errorinfo = $"机台【{machine.FCode}】复制失败,原因是:插入数据库失败";
- CurrDb.Rollback();
- return -1;
- }
- var sql = "select @@identity;";
- var id = Convert.ToInt32(CurrDb.FindList<string>(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<ProgramMst>($" 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<BusinessFileRelation>(id);
- }
- public IEnumerable<BusinessFile> 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<BusinessFile>(sql);
- }
- /// <summary>
- /// 删除单个版次
- /// </summary>
- /// <param name="id"></param>
- /// <param name="macCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int DeleteSingVersion(int id, string macCode, ref string errorinfo)
- {
- var macProgram = CurrDb.FindEntityFor<BusinessFileRelation>(id);
- if (macProgram != null)
- {
- var sql = $"delete from businessfilerelation where id={id}";
- if (CurrDb.ExecuteBySql(sql) < 0)
- {
- errorinfo = "删除数据库中程序版次失败";
- return -1;
- }
- var macPros = CurrDb.FindListForCondition<BusinessFileRelation>($" 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<BusinessFileRelation> getbusinessfilerelation(string proName, string macCode, ref string errorinfo)
- {
- var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
- var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" 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<BusinessFileRelation>(sql);
- }
- public BusinessFile getBusinessFile(string proName, string macCode, ref string errorinfo)
- {
- var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
- var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgTypeId=1 and OrgId={machine.ID} and a.FName='{proName}'",
- ref errorinfo).FirstOrDefault();
- if (busineeFile == null)
- {
- errorinfo = "当前程序不存在或已被删除";
- return null;
- }
- return CurrDb.FindEntity<BusinessFile>(busineeFile.ID);
- }
- /// <summary>
- /// 删除整个程序
- /// </summary>
- /// <param name="proName"></param>
- /// <param name="macCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int DeleteWholeProgram(string proName, string macCode, ref string errorinfo)
- {
- var machine = CurrDb.FindListForCondition<Machine>($" and a.Fcode='{macCode}'", ref errorinfo).FirstOrDefault();
- var busineeFile = CurrDb.FindListForCondition<BusinessFile>($" 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<BusinessFile>(busineeFile.ID) < 0)
- {
- errorinfo = "删除程序主表失败";
- return -1;
- }
- return 1;
- }
- }
- }
|