using DevExpress.Skins; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using ZJ_BYD.Common; using ZJ_BYD.Model; using ZJ_BYD.Untils; using ZJ_BYD.ViewModel; namespace ZJ_BYD.DB { public class MskCodeHelper { public MskCodeHelper() : base() { } /// /// 查询所有掩码表 /// /// public static ISugarQueryable QueryMskCodes() { try { return DBHelper.sqlSugarDb.Queryable(); } catch (Exception ex) { var msg = ex == null ? "执行QueryMskCodes方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMskCodes方法时异常:{msg}"); return null; } } /// /// 根据机型查询条码字符 /// /// /// public static string QueryBarCodeChar(string stationCode, string productSfcCode) { try { if (string.IsNullOrWhiteSpace(productSfcCode)) { LogHelper.WriteLog($"执行QueryBarCodeChar方法时productSfcCode参数为空!"); return ""; } return DBHelper.sqlSugarDb.Queryable() .First(m => m.StationCode == stationCode && SqlFunc.Replace(m.ProductSfcCode, "*", "") == SqlFunc.Substring(productSfcCode, 0, SqlFunc.Length(SqlFunc.Replace(m.ProductSfcCode, "*", ""))))?.BarCodeChar; } catch (Exception ex) { var msg = ex == null ? "执行QueryBarCodeChar方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryBarCodeChar方法时异常:{msg}"); return null; } } /// /// 根据主条码查询描述 /// /// /// public static string QueryMskCodeDesc(string code) { try { if (string.IsNullOrWhiteSpace(code)) { LogHelper.WriteLog($"执行QueryMskCodeDesc方法时参数code为空!"); return null; } return DBHelper.sqlSugarDb.Queryable().First(m => SqlFunc.Replace(m.ProductSfcCode, "*", "") == code)?.SfcDsp; } catch (Exception ex) { var msg = ex == null ? "执行QueryMskCodeDesc方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMskCodeDesc方法时异常:{msg}"); return null; } } /// /// 查询机型对应工位数据 /// /// /// public static ISugarQueryable QueryMaChineTypeStationVM(string machineTypeCode) { try { return DBHelper.sqlSugarDb.Queryable() .InnerJoin((a, b) => a.StationCode == b.StationCode && a.LineCode == Program.CurrentLineCode && !b.IsDeleted) .Where(a => a.ProductSfcCode == machineTypeCode && a.StationCode != Program.ActiveStatinCode) .Select((a, b) => new MaChineTypeStationVM { Id = a.Id, MachineTypeCode = a.ProductSfcCode, StationCode = a.StationCode, StationName = b.StationName, StrIsUsed = a.IsUsed ? "是" : "否", Category = a.Category, }).Distinct(); } catch (Exception ex) { var msg = ex == null ? "执行QueryMaChineTypeStationVM方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMaChineTypeStationVM方法时异常:{msg}"); return null; } } /// /// 根据工位和排序索引查询掩码表 /// /// /// /// public static T_MaskCode QueryMskCode(string stationCode, int sortIndex) { try { return DBHelper.sqlSugarDb.Queryable().First(m => m.StationCode == stationCode && m.SortIndex == sortIndex); } catch (Exception ex) { var msg = ex == null ? "执行QueryMskCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMskCode方法时异常:{msg}"); return null; } } /// /// 根据工位编码和排序索引查询掩码表记录 /// /// /// /// public static T_MaskCode QueryMakCodeByStationCodeSort(string stationCode, int sortIndex) { try { return Program.t_MaskCodes.First(m => m.StationCode == stationCode && m.SortIndex == sortIndex); } catch (Exception ex) { var msg = ex == null ? "执行QueryMakCodeByStationCodeSort方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMakCodeByStationCodeSort方法时异常:{msg}"); return null; } } /// /// 根据组件条码查询描述 /// /// /// public static string QuerySubCodeDesc(string subCode) { try { if (string.IsNullOrWhiteSpace(subCode)) { LogHelper.WriteLog($"执行QuerySubCodeDesc方法时参数subCode为空!"); return ""; } var model = new T_MaskCode(); var maskCodes = DBHelper.sqlSugarDb.Queryable(); model = maskCodes.First(m => SqlFunc.Replace(m.Sub1SfcCode, "*", "") == subCode); if (model != null) { return model.Sub1SfcDsp; } model = maskCodes.First(m => SqlFunc.Replace(m.Sub2SfcCode, "*", "") == subCode); if (model != null) { return model.Sub2SfcDsp; } model = maskCodes.First(m => SqlFunc.Replace(m.Sub3SfcCode, "*", "") == subCode); if (model != null) { return model.Sub3SfcDsp; } model = maskCodes.First(m => SqlFunc.Replace(m.Sub4SfcCode, "*", "") == subCode); if (model != null) { return model.Sub4SfcDsp; } model = maskCodes.First(m => SqlFunc.Replace(m.Sub5SfcCode, "*", "") == subCode); if (model != null) { return model.Sub5SfcDsp; } return ""; } catch (Exception ex) { var msg = ex == null ? "执行QuerySubCodeDesc方法时异常" : ex.Message; LogHelper.WriteLog($"执行QuerySubCodeDesc方法时异常:{msg}"); return null; } } /// /// 根据Ids删除掩码表 /// 物理删除 /// /// /// public static int DelMaskCodeByIds(int id) { return DBHelper.sqlSugarDb.Deleteable().Where(m => m.Id == id).ExecuteCommand(); } /// /// 批量新增掩码表 /// /// /// public static bool AddMaskCodes(List models) { var result = DBHelper.sqlSugarDb.UseTran(() => { DBHelper.sqlSugarDb.Insertable(models).ExecuteCommand(); }); return result.IsSuccess; } /// /// 新增掩码表 /// /// /// public static int AddMaskCode(T_MaskCode t_MaskCode) { return DBHelper.sqlSugarDb.Insertable(t_MaskCode).ExecuteCommand(); } /// /// 修改掩码表 /// /// /// public static int UpdateMakCode(T_MaskCode t_MaskCode) { return DBHelper.sqlSugarDb.Updateable(t_MaskCode).ExecuteCommand(); } /// /// 查询表中最大索引 /// /// public static int GetMaxSortIndex() { return DBHelper.sqlSugarDb.Queryable().Max(m => m.SortIndex); } /// /// 从掩码表校验主条码 /// /// /// /// /// public static bool CheckCodeByMakCode(string stationCode, string mskCode, string machineType) { try { if (string.IsNullOrWhiteSpace(mskCode)) { LogHelper.WriteLog($"执行CheckCodeByMakCode方法时参数mskCode为空!"); return false; } if (machineType.Contains(".")) { machineType = machineType.Substring(0, machineType.LastIndexOf(".")); } int.TryParse(machineType, out int intMachineType); var model = Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == stationCode && m.IsUsed && m.SortIndex == intMachineType); if (model != null) { var code = model.ProductSfcCode.TrimEnd('*'); int codeLen = code.Length; if (mskCode.Length < codeLen) { LogHelper.WriteLog($"执行CheckCodeByMakCode方法时参数mskCode长度异常!"); return false; } if (code == mskCode.Substring(0, codeLen)) { return true; } } return false; } catch (Exception ex) { var msg = ex == null ? "执行CheckCodeByMakCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行CheckCodeByMakCode方法时异常:{msg}"); return false; } } /// /// 从掩码表校验转子主条码 /// /// /// /// /// public static bool CheckCodeByMakCode_ZZCC(string stationCode, string mskCode, string machineType) { try { if (string.IsNullOrWhiteSpace(mskCode)) { LogHelper.WriteLog($"执行CheckCodeByMakCode方法时参数mskCode为空!"); return false; } if (machineType.Contains(".")) { machineType = machineType.Substring(0, machineType.LastIndexOf(".")); } int.TryParse(machineType, out int intMachineType); var model = Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == stationCode && m.IsUsed && m.SortIndex == intMachineType); if (model != null) { var code = model.ProductSfcCode.Trim('*'); int codeLen = code.Length; if (mskCode.Length < codeLen) { LogHelper.WriteLog($"执行CheckCodeByMakCode方法时参数mskCode长度异常!"); return false; } if (code == mskCode.Substring(0, codeLen)) { return true; } } return false; } catch (Exception ex) { var msg = ex == null ? "执行CheckCodeByMakCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行CheckCodeByMakCode方法时异常:{msg}"); return false; } } /// /// 校验组件条码 /// /// /// /// /// public static (bool isok, string msg) CheckSubCode(T_MaskCode mskCode, List subCodeVms) { try { if (subCodeVms.Count <= 0) { return (false, ""); } if (mskCode == null) { return (false, ""); } int len; string subCode; for (int i = 0; i < subCodeVms.Count; i++) { switch (subCodeVms[i].SortIndex) { case 1: if (string.IsNullOrWhiteSpace(mskCode.Sub1SfcCode)) { return (false, ""); } len = mskCode.Sub1SfcCode.Replace("*", "").Length; subCode = subCodeVms[i].SubCode.Substring(0, len); if (mskCode.Sub1SfcCode.Replace("*", "") != subCode) { return (false, subCode); } break; case 2: if (string.IsNullOrWhiteSpace(mskCode.Sub2SfcCode)) { return (false, ""); } len = mskCode.Sub2SfcCode.Replace("*", "").Length; subCode = subCodeVms[i].SubCode.Substring(0, len); if (mskCode.Sub2SfcCode.Replace("*", "") != subCode) { return (false, subCode); } break; case 3: if (string.IsNullOrWhiteSpace(mskCode.Sub3SfcCode)) { return (false, ""); } len = mskCode.Sub3SfcCode.Replace("*", "").Length; subCode = subCodeVms[i].SubCode.Substring(0, len); if (mskCode.Sub3SfcCode.Replace("*", "") != subCode) { return (false, subCode); } break; case 4: if (string.IsNullOrWhiteSpace(mskCode.Sub4SfcCode)) { return (false, ""); } len = mskCode.Sub4SfcCode.Replace("*", "").Length; subCode = subCodeVms[i].SubCode.Substring(0, len); if (mskCode.Sub4SfcCode.Replace("*", "") != subCode) { return (false, subCode); } break; case 5: if (string.IsNullOrWhiteSpace(mskCode.Sub5SfcCode)) { return (false, ""); } len = mskCode.Sub5SfcCode.Replace("*", "").Length; subCode = subCodeVms[i].SubCode.Substring(0, len); if (mskCode.Sub5SfcCode.Replace("*", "") != subCode) { return (false, subCode); } break; default: break; } } return (true, ""); } catch (Exception ex) { var msg = ex == null ? "执行CheckSubCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行CheckSubCode方法时异常:{msg}"); return (false, ""); } } /// /// 查询使用中的机型信息 /// public static ISugarQueryable QueryUsingMaChineTypeVM() { var maskcodes = DBHelper.sqlSugarDb.Queryable().ToList(); if (maskcodes.Count <= 0) { return DBHelper.sqlSugarDb.Queryable() .LeftJoin((a, b) => a.LineCode == b.LineCode && a.StationCode == b.StationCode && a.Category == b.Category && b.IsUsed && !a.IsDeleted) .Select((a, b) => new StationInfo { IpcId = a.IpcId, StationCode = a.StationCode, StationName = a.StationName, IsShowOrderBtn = a.IsShowOrderBtn, IsShowPrintBtn = a.IsShowPrintBtn, MachineType = b.ProductSfcCode, MachineTypeIndex = b.SortIndex, IsSearchAssemble = a.IsSearchAssemble, IsBindAssembleCode = a.IsBindAssembleCode, IsSearchLastStation = a.IsSearchLastStation, IsBranch = a.IsBranch, IsSfcCodeRepeat = a.IsSfcCodeRepeat, DatabaseIp = a.DatabaseIp, Site = b.Site, Resource = b.Resource, Procedure = b.Procedure, NgCode = b.NgCode, Sub1SfcCode = b.Sub1SfcCode, Sub2SfcCode = b.Sub2SfcCode, Sub3SfcCode = b.Sub3SfcCode, Sub4SfcCode = b.Sub4SfcCode, Sub5SfcCode = b.Sub5SfcCode, Category = a.Category, SortIndex = a.SortIndex, SoftVersionLen = b.SoftVersionLen }); } else { return DBHelper.sqlSugarDb.Queryable() .InnerJoin((a, b) => a.LineCode == b.LineCode && a.StationCode == b.StationCode && a.Category == b.Category && b.IsUsed && !a.IsDeleted) .Select((a, b) => new StationInfo { IpcId = a.IpcId, StationCode = a.StationCode, StationName = a.StationName, IsShowOrderBtn = a.IsShowOrderBtn, IsShowPrintBtn = a.IsShowPrintBtn, MachineType = b.ProductSfcCode, MachineTypeIndex = b.SortIndex, IsSearchAssemble = a.IsSearchAssemble, IsBindAssembleCode = a.IsBindAssembleCode, IsSearchLastStation = a.IsSearchLastStation, IsBranch = a.IsBranch, IsSfcCodeRepeat = a.IsSfcCodeRepeat, DatabaseIp = a.DatabaseIp, Site = b.Site, Resource = b.Resource, Procedure = b.Procedure, NgCode = b.NgCode, Sub1SfcCode = b.Sub1SfcCode, Sub2SfcCode = b.Sub2SfcCode, Sub3SfcCode = b.Sub3SfcCode, Sub4SfcCode = b.Sub4SfcCode, Sub5SfcCode = b.Sub5SfcCode, Category = a.Category, SortIndex = a.SortIndex, SoftVersionLen = b.SoftVersionLen }); } } /// /// 查询当前产线的所有工位及对应机型 /// /// public static ISugarQueryable QueryLocalLineStation() { return DBHelper.sqlSugarDb.Queryable() .LeftJoin((a, b) => a.LineCode == b.LineCode && !b.IsDeleted) .LeftJoin((a, b, c) => a.LineCode == c.LineCode && b.StationCode == c.StationCode && b.Category == c.Category) .Where(a => a.LineCode == Program.CurrentLineCode) .Select((a, b, c) => new LineStationVM { Id = a.Id, Ipc = b.IpcId, LineCode = a.LineCode, MachineTypeName = c.ProductSfcCode, StationCode = b.StationCode, StationName = b.StationName, IsUsed = c.IsUsed, SortIndex = c.SortIndex, StrIsUsed = c.IsUsed ? "是" : "否", Category = b.Category }); } /// /// 更新使用中机型 /// /// /// public static int UpdateMskCodeIsUsedBySortIndex(int sortIndex) { //先全部切换成False var sql = $"update T_MaskCode set IsUsed=false,UpdatedTime='{DateTime.Now}',UpdatedBy='{CurrentUser.UserName}'"; DBHelper.sqlSugarDb.Ado.ExecuteCommand(sql); var maskIds = DBHelper.sqlSugarDb.Queryable() .InnerJoin((a, b) => a.LineCode == b.LineCode && a.StationCode == b.StationCode && a.Category == b.Category) .Where((a, b) => a.SortIndex == sortIndex) .Select((a, b) => a.Id).ToList(); if (maskIds.Count > 0) { return DBHelper.sqlSugarDb.Updateable() .SetColumns(m => m.IsUsed == true) .SetColumns(m => m.UpdatedTime == DateTime.Now) .SetColumns(m => m.UpdatedBy == CurrentUser.UserName) .Where(m => maskIds.Contains(m.Id)).ExecuteCommand(); } return 0; } /// /// 修改是否启用定子支线查询 /// /// /// /// public static int UpdateMskCodeIsUsedStator(int id, bool isUseStator) { try { return DBHelper.sqlSugarDb.Updateable() .SetColumns(m => m.IsUseStator == isUseStator) .SetColumns(m => m.UpdatedTime == DateTime.Now) .SetColumns(m => m.UpdatedBy == CurrentUser.UserName) .Where(m => m.Id == id).ExecuteCommand(); } catch (Exception ex) { var msg = ex == null ? "执行UpdateMskCodeIsUsedStator方法时异常" : ex.Message; LogHelper.WriteLog($"执行UpdateMskCodeIsUsedStator方法时异常:{msg}"); return 0; } } /// /// 根据工位查询掩码表 /// /// /// public static T_MaskCode QueryMask_code(string stationCode) { return DBHelper.sqlSugarDb.Queryable().First(m => m.StationCode == stationCode && m.IsUsed); } /// /// 根据工位和机壳码查询掩码表 /// /// /// /// public static T_MaskCode QueryMask_codeByStationAndSfcCode(string stationCode, string productSfcCode) { try { return Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == stationCode && SqlFunc.Replace(m.ProductSfcCode, "*", "") == SqlFunc.Substring(productSfcCode, 0, SqlFunc.Length(SqlFunc.Replace(m.ProductSfcCode, "*", "")))); } catch (Exception ex) { var msg = ex == null ? "执行QueryMask_codeByStationAndSfcCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMask_codeByStationAndSfcCode方法时异常:{msg}"); return null; } } /// /// 根据工位和线路查询掩码表 /// /// /// /// public static T_MaskCode QueryMask_codeByStationAndCategory(string stationCode, string category) { try { return Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == stationCode && m.Category == category); } catch (Exception ex) { var msg = ex == null ? "执行QueryMask_codeByStationAndSfcCode方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryMask_codeByStationAndSfcCode方法时异常:{msg}"); return null; } } /// /// 用于查询上工位的资源和工序等信息 /// /// /// /// public static T_MaskCode QueryMaskCodeByStationCodeAndLastStationCode(string stationCode, string lastStationCode) { var model = Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == stationCode && m.IsUsed); if (model != null) { return Program.t_MaskCodes.FirstOrDefault(m => m.StationCode == lastStationCode && m.Category == model.Category); } else { LogHelper.WriteLog($"查询{lastStationCode}工位的资源工序等信息时异常,工位{stationCode}的信息未查询到!"); } return null; } /// /// 根据工位编码和机壳码修改启用定子支线查询 /// /// /// /// /// public static int UpdateMaskCodeIsUseStator(string stationCode, string productSfcCode, bool isUseStator) { try { return DBHelper.sqlSugarDb.Updateable() .SetColumns(m => m.IsUseStator == isUseStator) .SetColumns(m => m.UpdatedTime == DateTime.Now) .SetColumns(m => m.UpdatedBy == CurrentUser.UserName) .Where(m => m.StationCode == stationCode && m.ProductSfcCode == productSfcCode).ExecuteCommand(); } catch (Exception ex) { var msg = ex == null ? "执行UpdateMaskCodeIsUseStator方法时异常" : ex.Message; LogHelper.WriteLog($"执行UpdateMaskCodeIsUseStator方法时异常:{msg}"); return 0; } } /// /// 新增打印信息 /// /// /// public static int AddPrintInfo(T_PrintInfo t_PrintInfo) { try { return DBHelper.sqlSugarDb.Insertable(t_PrintInfo).ExecuteCommand(); } catch (Exception ex) { var msg = ex == null ? "执行AddPrintInfo方法时异常" : ex.Message; LogHelper.WriteLog($"执行AddPrintInfo方法时异常:{msg}"); return 0; } } public static T_PrintInfo QueryPrintInfo(string barCode) { try { return DBHelper.sqlSugarDb.Queryable().First(m => m.BarCode == barCode); } catch (Exception ex) { var msg = ex == null ? "执行QueryPrintInfo方法时异常" : ex.Message; LogHelper.WriteLog($"执行QueryPrintInfo方法时异常:{msg}"); return null; } } /// /// 删除昨天的记录 /// /// public static int delPrintInfo() { try { var beginDate = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy/MM/dd") + " 00:00:00"); var endDate = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy/MM/dd") + " 23:59:59"); return DBHelper.sqlSugarDb.Deleteable().Where(m => m.CreatedTime >= beginDate && m.CreatedTime <= endDate).ExecuteCommand(); } catch (Exception ex) { var msg = ex == null ? "执行delPrintInfo方法时异常" : ex.Message; LogHelper.WriteLog($"执行delPrintInfo方法时异常:{msg}"); return 0; } } /// /// 初始化机型 /// /// /// public static int InitMachineType() { try { //判断掩码表是否有启用中的机型 var maskcodes = DBHelper.sqlSugarDb.Queryable().Where(m => m.IsUsed).ToList(); //如果没有则根据PLC中选中的机型值进行修改 if (maskcodes.Count <= 0) { return UpdateMskCodeIsUsedBySortIndex(1); } return 1; } catch (Exception ex) { var msg = ex == null ? "执行InitMachineType方法时异常" : ex.Message; LogHelper.WriteLog($"执行InitMachineType方法时异常:{msg}"); return 0; } } } }