using Cksoft.Data;
using Cksoft.Unity;
using DllEapEntity;
using DllEapEntity.Dtos;
using DllEapEntity.Onsemi;
using DllSocketFile;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace DllEapDal.OFILM
{
public class OfilmProgramDal
{
private IDatabase CurrDb;
private IConfiguration Configuration;
private string mesApiPrefix;
private ILogger logger;
public OfilmProgramDal(IDatabase db)
{
CurrDb = db;
}
public OfilmProgramDal(IDatabase db, IConfiguration configuration,
ILogger logger)
{
CurrDb = db;
this.Configuration = configuration;
this.mesApiPrefix = Configuration["mesurl:url"];
this.logger = logger;
}
///
/// 获取程序列表
///
///
///
///
///
public IEnumerable GetProgramPaths(int macId, string proName, ref string errorinfo)
{
var machine = CurrDb.FindEntityFor(macId);
var uploadedPros = CurrDb.FindListForCondition($" and c.UploadMacID={machine.ID}", ref errorinfo);
if (machine.MModeCode == "ADT7900DUO")
{
string reError = "";
var res = new MacOrderSendDal(this.CurrDb).SendOrder(machine, 7, 19, "S7F19", ref reError);
if (proName != null && proName.Length > 0)
{
res = res.Where(v => v.FContent.Contains(proName)).ToList();
}
return res.Where(v => v.FContent.Trim().Length > 0).Select(v =>
{
var fileOrgName = Regex.Match(v.FContent, @"(?<=\\).*").Value;
return new OnsemiProgramDto()
{
Name = fileOrgName,
Path = v.FContent,
State = uploadedPros.Any(t => t.FileOrgName == fileOrgName) ? "已上传" : "未上传"
};
});
};
IEnumerable pros = null;
var socketFile = new SocketFile();
IEnumerable fileNames = null;
var customer = AppConfigurtaionServices.Configuration["Client"];
if (customer.ToUpper() == "OFILM")
{
fileNames = socketFile.GetMacFileList(machine, proName, ref errorinfo);
if (fileNames != null && fileNames.Count() > 0)
{
pros = fileNames.Select(c => new OnsemiProgramDto
{
Path = c,
Name = c,
State = uploadedPros.Any(t => t.FileOrgName == c) ? "已上传" : "未上传"
});
}
}
else if (customer.ToUpper() == "TRS")
{
var dtos = socketFile.GetMacFileListForTRS(machine, proName, ref errorinfo);
if (dtos != null && dtos.Count() > 0)
{
pros = dtos.Select(c => new OnsemiProgramDto
{
Path = c.Path,
Name = c.Name,
State = uploadedPros.Any(t => t.FileOrgName == c.Name) ? "已上传" : "未上传"
});
}
}
else
{
var dtos = socketFile.GetMacFileListForTRS(machine, proName, ref errorinfo);
if (dtos != null && dtos.Count() > 0)
{
pros = dtos.Select(c => new OnsemiProgramDto
{
Path = c.Path,
Name = c.Name,
State = uploadedPros.Any(t => t.FileOrgName == c.Name) ? "已上传" : "未上传"
});
}
}
return pros;
}
public int Upload(int macId, string proPath, string proName, string userCode, ref string errorinfo)
{
var machine = CurrDb.FindEntityFor(macId);
//if (machine.MModeCode == "ADT7900DUO")
//{
// // ADT7900程序上传
// return new Adt7900ProgramDal(this.CurrDb, this.logger).UploadProgram(machine, proName, proPath, userCode);
//}
var socketFile = new SocketFile();
var customer = AppConfigurtaionServices.Configuration["Client"];
if (customer.ToUpper() == "OFILM")
{
var flag = socketFile.UploadFile(machine, proName, ref errorinfo);
if (flag == null)
return -1;
CallMesuploadRecipe(machine.FCode, proName, flag.Version, ref errorinfo);
}
else
{
var flag = socketFile.UploadForTRS(machine, proName, proPath, ref errorinfo);
if (flag == null)
return -1;
}
return 1;
}
private int CallMesuploadRecipe(string maccode, string programname, int version, ref string errorinfo)
{
try
{
//处理程序名称,传给MES的程序名称不能含.RCP扩展名称
if (programname.Length > 4)
{
string extname = programname.Substring(programname.Length - 4, 4);
if (extname == ".rcp")
programname = programname.Substring(0, programname.Length - 4);
}
string actionName = "uploadRecipe";
string fullUrl = mesApiPrefix + actionName;
IDictionary dic = new Dictionary();
dic.Add("equipmentID", maccode);
dic.Add("recipeName", programname);
dic.Add("version", version.ToString());
var response = HttpRequestHelper.Get(fullUrl, dic, ref errorinfo);
string logText = $"调用MES上传程序接口:{fullUrl},传入的参数:{JsonConvert.SerializeObject(dic)}," +
$"接收到的值为:{JsonConvert.SerializeObject(response)}";
this.logger.LogError(logText);
return 1;
}
catch (Exception ex)
{
errorinfo = ex.ToString();
return -1;
}
}
}
}