123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- 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<BusinessFile>(id);
- return mst;
- }
- catch (Exception e)
- {
- errorinfo = e.Message;
- return null;
- }
- }
- /// <summary>
- /// 根据MD5添加程序文件,如果MD5不存在则不添加文件关系
- /// </summary>
- /// <param name="md5"></param>
- /// <param name="filename"></param>
- /// <param name="maccode"></param>
- /// <param name="remark"></param>
- /// <param name="usercode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<Machine>($" and a.fcode='{maccode}'", ref errorinfo).FirstOrDefault();
- if(mac==null)
- {
- errorinfo = $"未找到编号为={maccode}的机台信息。";
- return null;
- }
- TFileInfoDal dal = new TFileInfoDal(CurrDb);
- TFileInfo fileinfo = CurrDb.FindListForCondition<TFileInfo>($" 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;
- }
- }
- /// <summary>
- /// 供文件方式上传程序使用
- /// </summary>
- /// <param name="filedatas"></param>
- /// <param name="filename"></param>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public BusinessFile UpdownFileForFile(byte[] filedatas, string filename,string maccode, ref string errorinfo)
- {
- try
- {
- string filedir = AppConfigurtaionServices.Configuration["ProgramDir"];
- Machine mac = CurrDb.FindListForCondition<Machine>($" 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;
- }
- }
- /// <summary>
- /// 程序上次
- /// </summary>
- /// <param name="filedatas">程序数据</param>
- /// <param name="filedir">程序存放目录</param>
- /// <param name="filename">程序名称</param>
- /// <param name="mac">机台信息</param>
- /// <param name="remark">备注</param>
- /// <param name="usercode">操作员代码</param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- 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<BusinessFile> busifiles = CurrDb.FindListForCondition<BusinessFile>(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}";
- }
- }
- }
|