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 = SQLiteHelper.Instance; private SerilogHelper _log = SerilogHelper.Instance; /// /// 绑定信息根据托盘号删除 /// /// public bool TrayCodeDelete(string trayCode) { try { int res = _helper.DeleteRange(trayCode); return res != 0; } catch(Exception ex) { _log.Error("托盘码删除错误", ex); return false; } } /// /// 托盘二维码重新绑定 /// /// 托盘码 /// 产品码 /// public bool TrayBindingRefresh(string trayCode, string[] prodCode) { try { //先删除原来的 _helper.DeleteRange(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 }; entities.Add(entity); } _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; } } } }