123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- using Cksoft.Data;
- using Cksoft.Data.Repository;
- using DllEapEntity;
- using DllEapEntity.Dtos;
- using DllEapEntity.OFILM;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- namespace DllEapDal
- {
- public class DaSampleDal
- {
- private readonly IDatabase Db = null;
- public DaSampleDal(IDatabase db)
- {
- Db=db;
- }
- /// <summary>
- /// 获取DA sample校验参数数据列表以生成时间倒序排列
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="macnum"></param>
- /// <param name="type"></param>
- /// <param name="result"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="total"></param>
- /// <returns></returns>
- public IEnumerable<Sample> Get(string maccode, string macnum, string type, string result, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
- {
- string str = $@"f.FName Factory,
- e.FName Plant,
- d.FName Floor,
- g.PCode PCode,
- b.FCode MacCode,
- h.MacNumber MacNum,
- a.Time Time,
- a.Type Type,
- a.Result Result,
- a.X,
- a.Y,
- a.T,
- a.RecCode,
- i.FName NAME ";
-
- var datas = Db.FindList<Sample>(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize,str)+$"limit { (pageIndex - 1) * pageSize},{ pageSize}");
- var sampleValue = MesGet();
- foreach (var item in datas)
- {
- var temp = sampleValue.Where(c => c.macId == item.MacCode && c.materialNo == item.Type)?.FirstOrDefault();
- /* item.aXOffset = temp.aXOffset == null ?"0" : temp.aXOffset;
- item.sXOffset = temp.aXOffset == null ? "0" : temp.sXOffset;
- item.wXOffset = temp.wXOffset == null ? "0" : temp.wXOffset;
- item.aYOffset = temp.aYOffset == null ? "0" : temp.aYOffset;
- item.sYOffset = temp.sYOffset == null ? "0" : temp.sYOffset;
- item.wYOffset = temp.wYOffset == null ? "0" : temp.wYOffset;
- item.aTOffset = temp.aTOffset == null ? "0" : temp.aTOffset;
- item.sTOffset = temp.sTOffset == null ? "0" : temp.sTOffset;
- item.wTOffset = temp.wTOffset == null ? "0" : temp.wTOffset;*/
- item.aXOffset = temp.aXOffset ;
- item.sXOffset = temp.sXOffset;
- item.wXOffset = temp.wXOffset ;
- item.aYOffset = temp.aYOffset ;
- item.sYOffset = temp.sYOffset ;
- item.wYOffset = temp.wYOffset;
- item.aTOffset = temp.aTOffset;
- item.sTOffset = temp.sTOffset ;
- item.wTOffset = temp.wTOffset;
- }
- str = "count(1)";
- total = Convert.ToInt32(Db.FindObject(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize, str)));
- return datas;
-
- }
- public IEnumerable<QisCheck> MesGet()
- {
- using(IDatabase db = DbFactory.Base("qis"))
- {
- return db.FindList<QisCheck>($@"select mac_id macId,Material_NO materialNo,STANDARD_XOFFSET sXOffset,STANDARD_YOFFSET sYOffset,STANDARD_ROTATE sTOffset,WARNING_XOFFSET wXOffset,WARNING_YOFFSET wYOffset,WARNING_ROTATE wTOffset,ABNORMAL_XOFFSET aXOffset,ABNORMAL_YOFFSET aYOffset,ABNORMAL_ROTATE aTOffset FROM ofg_qis.view_mfg_sample_check where 1=1");
- }
- }
- /// <summary>
- /// 构建DA Sample 数据 sql语句
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="macnum"></param>
- /// <param name="type"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="str"></param>
- /// <returns></returns>
- public string Sql(string maccode, string macnum, string type,string result,DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
- {
- string sql = $@"select {str} FROM
- dasample a
- LEFT JOIN machine b ON b.id = a.MacId
- INNER JOIN factoryregion c ON b.RegionId = c.Id
- INNER JOIN factoryregion d ON d.id = c.ParentId
- INNER JOIN factoryregion e ON e.id = d.ParentId
- INNER JOIN factoryregion f ON f.Id = e.ParentId
- LEFT JOIN mactprocess g ON g.MacID = b.ID
- LEFT JOIN machinenumber h ON h.MacId = b.id
- LEFT JOIN staff i ON i.FCode = a.RecCode where 1=1 ";
- if (!string.IsNullOrEmpty(maccode))
- {
- sql += $" and b.fcode like '%{maccode}%'";
- }
- if (!string.IsNullOrEmpty(macnum))
- {
- sql += $" and h.MacNumber like '%{macnum}%'";
- }
- if (!string.IsNullOrEmpty(type))
- {
- sql += $" and a.type like '%{type}%'";
- }
- if (!string.IsNullOrEmpty(result))
- {
- sql += $" and a.result like '%{result}%'";
- }
- if (start.HasValue)
- {
- sql += $" and a.Time > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
- }
- if (end.HasValue)
- {
- sql += $" and a.Time < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
- }
- sql += $"order By a.Time desc ";
- return sql;
- }
- /// <summary>
- /// 从数据获取DA parameter 数据列表以时间倒序排列
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="macnum"></param>
- /// <param name="type"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="total"></param>
- /// <returns></returns>
- public IEnumerable<Sample> GetParameter(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
- {
- string str = $@"f.FName Factory,
- e.FName Plant,
- d.FName Floor,
- g.PCode PCode,
- b.FCode MacCode,
- h.MacNumber MacNum,
- a.RecTime Time,
- a.Type Type,
- a.X,
- a.Y,
- a.T,a.MDP,a.ADP,a.RecCode";
- var data = Db.FindList<Sample>(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str));
- str = "count(1)";
- total = Convert.ToInt32(Db.FindObject(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str)));
- return data;
-
- }
- /// <summary>
- /// 生成DA parameter的sql语句
- /// </summary>
- /// <param name="maccode"></param>
- /// <param name="macnum"></param>
- /// <param name="type"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <param name="pageIndex"></param>
- /// <param name="pageSize"></param>
- /// <param name="str"></param>
- /// <returns></returns>
- public string GetSql(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
- {
- string sql = $@"select {str}
- FROM
- daparameter a
- LEFT JOIN machine b ON b.id = a.MacId
- INNER JOIN factoryregion c ON b.RegionId = c.Id
- INNER JOIN factoryregion d ON d.id = c.ParentId
- INNER JOIN factoryregion e ON e.id = d.ParentId
- INNER JOIN factoryregion f ON f.Id = e.ParentId
- LEFT JOIN mactprocess g ON g.MacID = b.ID
- LEFT JOIN machinenumber h ON h.MacId = b.id
- where 1=1 ";
- if (!string.IsNullOrEmpty(maccode))
- {
- sql += $" and b.fcode like '%{maccode}%'";
- }
- if (!string.IsNullOrEmpty(macnum))
- {
- sql += $" and h.MacNumber like '%{macnum}%'";
- }
- if (!string.IsNullOrEmpty(type))
- {
- sql += $" and a.type like '%{type}%'";
- }
- if (start.HasValue)
- {
- sql += $" and a.RecTime > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
- }
- if (end.HasValue)
- {
- sql += $" and a.RecTime < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
- }
- sql += $"order By a.RecTime desc ";
- if (str != "count(1)")
- {
- sql += $"limit { (pageIndex - 1) * pageSize},{ pageSize}";
- }
- return sql;
- }
- /// <summary>
- /// 从DA机台获取参数
- /// </summary>
- /// <param name="m"></param>
- /// <returns></returns>
- public async Task<object> GetParams(MacInfo m)
- {
- try
- {
-
- string url = "http://192.168.124.93:8606/eap/api/equipment/getOriginEquipmentParams";
-
- HttpClient client = new HttpClient(new HttpClientHandler
- {
- AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
- });
- client.DefaultRequestHeaders.Add("Method", "POST");
- client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
- var content = new MultipartFormDataContent();
- var uri = new Uri(url);
- var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(m.EquipmentID));
- content.Add(equipmentId, "EquipmentID");
- for (int i = 0; i < m.parameters.Count(); i++)
- {
- var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(m.parameters.ElementAt(i)));
- content.Add(temp, $"ParamsList[{i}]");
- }
- client.Timeout = new TimeSpan(0, 0, 600);
-
- var result = await client.PostAsync(uri, content);
- if (result != null && result.IsSuccessStatusCode)
- {
- var bytes = result.Content.ReadAsByteArrayAsync().Result;
- return Deal(Encoding.UTF8.GetString(bytes),m.MacId);
- }
- return "请求超时" ;
- }
- catch (Exception ex)
- {
-
- return "请求超时"+ex;
- }
- }
- /// <summary>
- /// 将机台获取到的参数存放到数据库中
- /// </summary>
- /// <param name="str"></param>
- /// <param name="id"></param>
- /// <returns></returns>
- private object Deal(string str,int id)
- {
- DAParameter sample = new DAParameter();
- SampleRes s = JsonConvert.DeserializeObject<SampleRes>(str);
- if (s.code == "ok")
- {
- //var d = Db.FindObject($@"select id from machine where FCode='{s.data.equipmentID}'");
- sample.MacId = id;
- sample.RecTime = DateTime.Now;
- foreach (var item in s.data.parameters)
- {
- if (item.paramName == "560")
- {
- sample.X = item.paramValue;
- }
- if (item.paramName == "561")
- {
- sample.Y = item.paramValue;
- }
- if (item.paramName == "562")
- {
- sample.T = item.paramValue;
- }
- if (item.paramName == "1070")
- {
- sample.MDP = item.paramValue;
- }
- if (item.paramName == "1060")
- {
- sample.ADP = item.paramValue;
- }
- if (item.paramName == "19")
- {
- string type = item.paramValue;
- type = type.Replace(" ", "-");
- int i = type.IndexOf("-");
- sample.Type = type.Substring(0, i);
- }
- }
- if (Db.InsertFor<DAParameter>(sample, "自动采集") > 0)
- {
- return "新增成功";
- }
- else
- {
- return "增加失败";
- }
- }
- return sample;
- }
- }
-
- }
|