DaSampleDal.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using DllEapEntity;
  4. using DllEapEntity.Dtos;
  5. using DllEapEntity.OFILM;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Http;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace DllEapDal
  16. {
  17. public class DaSampleDal
  18. {
  19. private readonly IDatabase Db = null;
  20. public DaSampleDal(IDatabase db)
  21. {
  22. Db=db;
  23. }
  24. /// <summary>
  25. /// 获取DA sample校验参数数据列表以生成时间倒序排列
  26. /// </summary>
  27. /// <param name="maccode"></param>
  28. /// <param name="macnum"></param>
  29. /// <param name="type"></param>
  30. /// <param name="result"></param>
  31. /// <param name="start"></param>
  32. /// <param name="end"></param>
  33. /// <param name="pageIndex"></param>
  34. /// <param name="pageSize"></param>
  35. /// <param name="total"></param>
  36. /// <returns></returns>
  37. public IEnumerable<Sample> Get(string maccode, string macnum, string type, string result, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
  38. {
  39. string str = $@"f.FName Factory,
  40. e.FName Plant,
  41. d.FName Floor,
  42. g.PCode PCode,
  43. b.FCode MacCode,
  44. h.MacNumber MacNum,
  45. a.Time Time,
  46. a.Type Type,
  47. a.Result Result,
  48. a.X,
  49. a.Y,
  50. a.T,
  51. a.RecCode,
  52. i.FName NAME ";
  53. var datas = Db.FindList<Sample>(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize,str)+$"limit { (pageIndex - 1) * pageSize},{ pageSize}");
  54. var sampleValue = MesGet();
  55. foreach (var item in datas)
  56. {
  57. var temp = sampleValue.Where(c => c.macId == item.MacCode && c.materialNo == item.Type)?.FirstOrDefault();
  58. /* item.aXOffset = temp.aXOffset == null ?"0" : temp.aXOffset;
  59. item.sXOffset = temp.aXOffset == null ? "0" : temp.sXOffset;
  60. item.wXOffset = temp.wXOffset == null ? "0" : temp.wXOffset;
  61. item.aYOffset = temp.aYOffset == null ? "0" : temp.aYOffset;
  62. item.sYOffset = temp.sYOffset == null ? "0" : temp.sYOffset;
  63. item.wYOffset = temp.wYOffset == null ? "0" : temp.wYOffset;
  64. item.aTOffset = temp.aTOffset == null ? "0" : temp.aTOffset;
  65. item.sTOffset = temp.sTOffset == null ? "0" : temp.sTOffset;
  66. item.wTOffset = temp.wTOffset == null ? "0" : temp.wTOffset;*/
  67. item.aXOffset = temp.aXOffset ;
  68. item.sXOffset = temp.sXOffset;
  69. item.wXOffset = temp.wXOffset ;
  70. item.aYOffset = temp.aYOffset ;
  71. item.sYOffset = temp.sYOffset ;
  72. item.wYOffset = temp.wYOffset;
  73. item.aTOffset = temp.aTOffset;
  74. item.sTOffset = temp.sTOffset ;
  75. item.wTOffset = temp.wTOffset;
  76. }
  77. str = "count(1)";
  78. total = Convert.ToInt32(Db.FindObject(Sql(maccode, macnum, type, result, start, end, pageIndex, pageSize, str)));
  79. return datas;
  80. }
  81. public IEnumerable<QisCheck> MesGet()
  82. {
  83. using(IDatabase db = DbFactory.Base("qis"))
  84. {
  85. 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");
  86. }
  87. }
  88. /// <summary>
  89. /// 构建DA Sample 数据 sql语句
  90. /// </summary>
  91. /// <param name="maccode"></param>
  92. /// <param name="macnum"></param>
  93. /// <param name="type"></param>
  94. /// <param name="start"></param>
  95. /// <param name="end"></param>
  96. /// <param name="pageIndex"></param>
  97. /// <param name="pageSize"></param>
  98. /// <param name="str"></param>
  99. /// <returns></returns>
  100. public string Sql(string maccode, string macnum, string type,string result,DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
  101. {
  102. string sql = $@"select {str} FROM
  103. dasample a
  104. LEFT JOIN machine b ON b.id = a.MacId
  105. INNER JOIN factoryregion c ON b.RegionId = c.Id
  106. INNER JOIN factoryregion d ON d.id = c.ParentId
  107. INNER JOIN factoryregion e ON e.id = d.ParentId
  108. INNER JOIN factoryregion f ON f.Id = e.ParentId
  109. LEFT JOIN mactprocess g ON g.MacID = b.ID
  110. LEFT JOIN machinenumber h ON h.MacId = b.id
  111. LEFT JOIN staff i ON i.FCode = a.RecCode where 1=1 ";
  112. if (!string.IsNullOrEmpty(maccode))
  113. {
  114. sql += $" and b.fcode like '%{maccode}%'";
  115. }
  116. if (!string.IsNullOrEmpty(macnum))
  117. {
  118. sql += $" and h.MacNumber like '%{macnum}%'";
  119. }
  120. if (!string.IsNullOrEmpty(type))
  121. {
  122. sql += $" and a.type like '%{type}%'";
  123. }
  124. if (!string.IsNullOrEmpty(result))
  125. {
  126. sql += $" and a.result like '%{result}%'";
  127. }
  128. if (start.HasValue)
  129. {
  130. sql += $" and a.Time > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  131. }
  132. if (end.HasValue)
  133. {
  134. sql += $" and a.Time < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  135. }
  136. sql += $"order By a.Time desc ";
  137. return sql;
  138. }
  139. /// <summary>
  140. /// 从数据获取DA parameter 数据列表以时间倒序排列
  141. /// </summary>
  142. /// <param name="maccode"></param>
  143. /// <param name="macnum"></param>
  144. /// <param name="type"></param>
  145. /// <param name="start"></param>
  146. /// <param name="end"></param>
  147. /// <param name="pageIndex"></param>
  148. /// <param name="pageSize"></param>
  149. /// <param name="total"></param>
  150. /// <returns></returns>
  151. public IEnumerable<Sample> GetParameter(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int total)
  152. {
  153. string str = $@"f.FName Factory,
  154. e.FName Plant,
  155. d.FName Floor,
  156. g.PCode PCode,
  157. b.FCode MacCode,
  158. h.MacNumber MacNum,
  159. a.RecTime Time,
  160. a.Type Type,
  161. a.X,
  162. a.Y,
  163. a.T,a.MDP,a.ADP,a.RecCode";
  164. var data = Db.FindList<Sample>(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str));
  165. str = "count(1)";
  166. total = Convert.ToInt32(Db.FindObject(GetSql(maccode, macnum, type, start, end, pageIndex, pageSize, str)));
  167. return data;
  168. }
  169. /// <summary>
  170. /// 生成DA parameter的sql语句
  171. /// </summary>
  172. /// <param name="maccode"></param>
  173. /// <param name="macnum"></param>
  174. /// <param name="type"></param>
  175. /// <param name="start"></param>
  176. /// <param name="end"></param>
  177. /// <param name="pageIndex"></param>
  178. /// <param name="pageSize"></param>
  179. /// <param name="str"></param>
  180. /// <returns></returns>
  181. public string GetSql(string maccode, string macnum, string type, DateTime? start, DateTime? end, int pageIndex, int pageSize, string str)
  182. {
  183. string sql = $@"select {str}
  184. FROM
  185. daparameter a
  186. LEFT JOIN machine b ON b.id = a.MacId
  187. INNER JOIN factoryregion c ON b.RegionId = c.Id
  188. INNER JOIN factoryregion d ON d.id = c.ParentId
  189. INNER JOIN factoryregion e ON e.id = d.ParentId
  190. INNER JOIN factoryregion f ON f.Id = e.ParentId
  191. LEFT JOIN mactprocess g ON g.MacID = b.ID
  192. LEFT JOIN machinenumber h ON h.MacId = b.id
  193. where 1=1 ";
  194. if (!string.IsNullOrEmpty(maccode))
  195. {
  196. sql += $" and b.fcode like '%{maccode}%'";
  197. }
  198. if (!string.IsNullOrEmpty(macnum))
  199. {
  200. sql += $" and h.MacNumber like '%{macnum}%'";
  201. }
  202. if (!string.IsNullOrEmpty(type))
  203. {
  204. sql += $" and a.type like '%{type}%'";
  205. }
  206. if (start.HasValue)
  207. {
  208. sql += $" and a.RecTime > '{start.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  209. }
  210. if (end.HasValue)
  211. {
  212. sql += $" and a.RecTime < '{end.Value.ToString("yyyy-MM-dd HH:mm:ss")}'";
  213. }
  214. sql += $"order By a.RecTime desc ";
  215. if (str != "count(1)")
  216. {
  217. sql += $"limit { (pageIndex - 1) * pageSize},{ pageSize}";
  218. }
  219. return sql;
  220. }
  221. /// <summary>
  222. /// 从DA机台获取参数
  223. /// </summary>
  224. /// <param name="m"></param>
  225. /// <returns></returns>
  226. public async Task<object> GetParams(MacInfo m)
  227. {
  228. try
  229. {
  230. string url = "http://192.168.124.93:8606/eap/api/equipment/getOriginEquipmentParams";
  231. HttpClient client = new HttpClient(new HttpClientHandler
  232. {
  233. AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
  234. });
  235. client.DefaultRequestHeaders.Add("Method", "POST");
  236. client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
  237. var content = new MultipartFormDataContent();
  238. var uri = new Uri(url);
  239. var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(m.EquipmentID));
  240. content.Add(equipmentId, "EquipmentID");
  241. for (int i = 0; i < m.parameters.Count(); i++)
  242. {
  243. var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(m.parameters.ElementAt(i)));
  244. content.Add(temp, $"ParamsList[{i}]");
  245. }
  246. client.Timeout = new TimeSpan(0, 0, 600);
  247. var result = await client.PostAsync(uri, content);
  248. if (result != null && result.IsSuccessStatusCode)
  249. {
  250. var bytes = result.Content.ReadAsByteArrayAsync().Result;
  251. return Deal(Encoding.UTF8.GetString(bytes),m.MacId);
  252. }
  253. return "请求超时" ;
  254. }
  255. catch (Exception ex)
  256. {
  257. return "请求超时"+ex;
  258. }
  259. }
  260. /// <summary>
  261. /// 将机台获取到的参数存放到数据库中
  262. /// </summary>
  263. /// <param name="str"></param>
  264. /// <param name="id"></param>
  265. /// <returns></returns>
  266. private object Deal(string str,int id)
  267. {
  268. DAParameter sample = new DAParameter();
  269. SampleRes s = JsonConvert.DeserializeObject<SampleRes>(str);
  270. if (s.code == "ok")
  271. {
  272. //var d = Db.FindObject($@"select id from machine where FCode='{s.data.equipmentID}'");
  273. sample.MacId = id;
  274. sample.RecTime = DateTime.Now;
  275. foreach (var item in s.data.parameters)
  276. {
  277. if (item.paramName == "560")
  278. {
  279. sample.X = item.paramValue;
  280. }
  281. if (item.paramName == "561")
  282. {
  283. sample.Y = item.paramValue;
  284. }
  285. if (item.paramName == "562")
  286. {
  287. sample.T = item.paramValue;
  288. }
  289. if (item.paramName == "1070")
  290. {
  291. sample.MDP = item.paramValue;
  292. }
  293. if (item.paramName == "1060")
  294. {
  295. sample.ADP = item.paramValue;
  296. }
  297. if (item.paramName == "19")
  298. {
  299. string type = item.paramValue;
  300. type = type.Replace(" ", "-");
  301. int i = type.IndexOf("-");
  302. sample.Type = type.Substring(0, i);
  303. }
  304. }
  305. if (Db.InsertFor<DAParameter>(sample, "自动采集") > 0)
  306. {
  307. return "新增成功";
  308. }
  309. else
  310. {
  311. return "增加失败";
  312. }
  313. }
  314. return sample;
  315. }
  316. }
  317. }