using Sln.Iot.Repository.dao; using Sln.Iot.Serilog; using SQLitePCL; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sln.Iot.Repository.service { /// /// 托盘绑定服务类 /// public class TrayBindingService { private static readonly Lazy lazy = new Lazy(() => new TrayBindingService()); public static TrayBindingService Instance { get { return lazy.Value; } } private SQLiteHelper _helper = new SQLiteHelper(); private SerilogHelper _log = SerilogHelper.Instance; /// /// 获取托盘绑定的产品码 /// /// /// public string[] ProdCodeGet(string trayCode) { try { string[] res = _helper.Query(t => t.TrayCode == trayCode).Select(x => x.ProductionCode).ToArray(); return res; } catch(Exception ex) { _log.Error("托盘绑定产品码获取错误", ex); return Array.Empty(); } } /// /// 获取托盘绑定的产品码(获取第一个) /// /// /// public string TidGet() { try { var res = _helper.QuerySingle(); if(res == null) { return string.Empty; } return res.TransactionID ?? string.Empty; } catch(Exception ex) { _log.Error("托盘绑定产品码获取错误", ex); return string.Empty; } } /// /// 获取托盘绑定的产品码 /// /// /// public string TidGet(string trayCode) { try { var res = _helper.Query(t => t.TrayCode == trayCode).FirstOrDefault(); if (res == null) { return string.Empty; } return res.TransactionID ?? string.Empty; } catch (Exception ex) { _log.Error("托盘绑定产品码获取错误", ex); return string.Empty; } } /// /// 绑定信息根据托盘号删除 /// /// public bool TrayCodeDelete(string trayCode) { try { int res = _helper.DeleteTrayRange(trayCode); return res != 0; } catch (Exception ex) { _log.Error("托盘码删除错误", ex); return false; } } /// /// 托盘二维码重新绑定 /// /// 托盘码 /// 产品码 /// public bool TrayBindingRefresh(Guid tid, string trayCode, string[] prodCode) { try { //先删除原来的 _helper.DeleteTrayRange(trayCode); //再绑定(插入)新的 var entities = new List(); foreach (string code in prodCode) { if (string.IsNullOrWhiteSpace(code)) { continue; } TrayRfidBinding entity = new TrayRfidBinding() { GUID = Guid.NewGuid().ToString("N"), TrayCode = trayCode, ProductionCode = code, TransactionID = tid.ToString(), }; entities.Add(entity); } if(entities.Count == 0 || entities == null) { return false; } _helper.InsertRange(entities); return true; } catch (Exception ex) { _log.Error("托盘二维码重新绑定错误", ex); return false; } } /// /// 存储对应托盘码的时间节点 /// /// 时间字符串 /// 托盘码 /// 时间参数编号 /// public bool UpDateTime(string timeString, string trayCode, string timeCount) { try { int res = _helper.SqlExcute($@"update TrayRfidBinding set time{timeCount} = '{timeString}' where TrayCode = '{trayCode}' "); if (res <= 0 || res > 6) { return false; } return true; } catch (Exception ex) { _log.Error("时间更新错误", ex); return false; } } /// /// 获取一个时间 /// /// /// public string GetTime2ByTrayCode(string trayCode) { string time2 = string.Empty; try { var res = _helper.Query(t => t.TrayCode == trayCode).FirstOrDefault(); if (res != null) { time2 = res.Time2; } return time2; } catch (Exception ex) { _log.Error("根据托盘码获取time2错误", ex); return time2; } } } }