public async Task <OldPostParam_Out> OldPostLockObject([FromBody] PostLockObject_param_in pIn) { OldPostParam_Out pOut = new OldPostParam_Out(); try { if (pIn.ObjectID < 0) { Logger.Error("ObjectID < 0 ,参数传递错误"); SobeyRecException.ThrowSelfNoParam(pIn.ObjectID.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECTID_WRONG, Logger, null); } if (pIn.ObjectTypeID < OTID.OTID_VTR || pIn.ObjectTypeID > OTID.OTID_OTHER) { SobeyRecException.ThrowSelfNoParam(pIn.ObjectTypeID.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECT_TPYEID_IS_NOT_EXIST, Logger, null); } if (string.IsNullOrEmpty(pIn.userName))// == "" || pIn.userName == string.Empty) { pIn.userName = "******"; } if (pIn.TimeOut < 0) { SobeyRecException.ThrowSelfNoParam(pIn.TimeOut.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECT_TIMEOUT_IS_WRONG, Logger, null); } pOut.bRet = await _GlobalManager.SetLockObject(pIn.ObjectID, pIn.ObjectTypeID, pIn.userName, pIn.TimeOut); } catch (Exception ex) { pOut.errStr = ex.Message; Logger.Error("PostlockObject 异常发生: " + ex.ToString()); pOut.bRet = false; } return(pOut); }
public async Task UpdateUsersettingsAsync(DbpUsersettings usersetting) { try { if (!Context.DbpUsersetting.AsNoTracking().Any(x => x.Usercode == usersetting.Usercode && x.Settingtype == usersetting.Settingtype)) { //add Context.DbpUsersetting.Add(usersetting); } else { //update Context.Attach(usersetting); Context.Entry(usersetting).Property(x => x.Settingtext).IsModified = true; Context.Entry(usersetting).Property(x => x.Settingtextlong).IsModified = true; } await Context.SaveChangesAsync(); } catch (System.Exception ex) { Logger.Error(ex.ToString()); //SobeyRecException.ThrowSelf(Locallanguage.LoadString("Fill SetUserSetting Exception "),ex,2,10013019); SobeyRecException.ThrowSelfNoParam("UpdateUsersettingsAsync", GlobalDictionary.GLOBALDICT_CODE_FILL_USER_SETUSERSETTING_EXCEPTION, Logger, ex); } }
public async Task <bool> UnLockRowsAsync(DbpObjectstateinfo objectstateinfo, int TimeOut) { bool result = false; try { int nTime = -1; DateTime dtLock = new DateTime(); try { System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.CultureInfo("en-US", false).DateTimeFormat; dtfi.ShortTimePattern = "t"; //DateTime dt = DateTime.Parse("02-28-12 03:07PM", dtfi); nTime = Convert.ToInt32(objectstateinfo.Timeout); dtLock = DateTime.Parse(objectstateinfo.Begintime.ToString(), dtfi); } catch (System.Exception ex) { //Logger.Error(ex.ToString()); Logger.Error("UnLockRowsAsync 时间格式转换不对: " + ex.Message); return(false); } DateTime AddMillSec = dtLock.AddMilliseconds(nTime); DateTime dtNow = DateTime.Now; var selectResult = await Context.DbpObjectstateinfo.SingleOrDefaultAsync(x => x.Objectid == objectstateinfo.Objectid && x.Objecttypeid == objectstateinfo.Objecttypeid && x.Username == objectstateinfo.Username); if (AddMillSec < dtNow) //锁超时 { result = true; //UpdateLock(userName, dtNow, Convert.ToInt32(TimeOut), objectID, objectTypeID); if (selectResult != null) { selectResult.Begintime = dtNow; selectResult.Timeout = Convert.ToInt32(TimeOut); } } //UnLock(objectID, objectTypeID, userName); if (selectResult != null) { selectResult.Locklock = null; await Context.SaveChangesAsync(); } return(result); } catch (Exception ex) { SobeyRecException.ThrowSelfNoParam(objectstateinfo.Objectid.ToString(), GlobalDictionary.GLOBALDICT_CODE_EXECUTE_COMMAND_ERROR, Logger, ex); result = false; throw ex; } }
// public async Task <bool> IsChannelLock(int nChannel) { bool ret = false; try { var objectsateinfo = await Store.GetObjectstateinfoAsync(a => a.Where(x => x.Objectid == nChannel && x.Objecttypeid == (int)OTID.OTID_CHANNEL && x.Begintime.AddMilliseconds(x.Timeout) > DateTime.Now)); ret = objectsateinfo == null ? true : false;//true 无锁 } catch (Exception ex) { SobeyRecException.ThrowSelfNoParam(nChannel.ToString(), GlobalDictionary.GLOBALDICT_CODE_IN_ISCHANNELLOCK_READ_DATA_FAILED, null, ex); } return(ret); }
public async Task <bool> UnLockObjectAsync(DbpObjectstateinfo arrObjects) { try { if (arrObjects == null) { Logger.Error("arrObjects == null || arrObjects.Count <= 0 成立,解锁函数return false,解锁失败"); return(false); } //加锁 //var deleteObj = await Context.DbpObjectstateinfo.FirstOrDefaultAsync(x => x.Username == userName && ((objectID >= 0 && x.Objectid == objectID) || objectID < 0) && ((objectTypeID >= 0 && x.Objecttypeid == (int)objectTypeID) || objectTypeID < 0)); Context.Remove(arrObjects); int LineNum = await Context.SaveChangesAsync(); bool ret = false; if (LineNum > 0) { ret = true;//删除了锁,才返回正确 } else { Logger.Info("UnLockObjectAsync UnLockObject return false,解锁失败"); ret = false; arrObjects.Locklock = null; await Context.SaveChangesAsync(); } //解锁 //UnLock(objectID, objectTypeID, userName); return(ret); } catch (MySqlException ex) { Logger.Error(ex.ToString()); string message = "UnLockObject"; SobeyRecException.ThrowSelfNoParam(message, GlobalDictionary.GLOBALDICT_CODE_IN_SETUNLOCKOBJECT_READ_DATA_FAILED, Logger, ex); return(false); } catch (System.Exception ex) { Logger.Error(ex.ToString()); string message = "UnLockObject"; SobeyRecException.ThrowSelfNoParam(message, GlobalDictionary.GLOBALDICT_CODE_IN_SETUNLOCKOBJECT_EXCEPTION, Logger, ex); return(false); } }
public async Task <ResponseMessage> PostLockObject([FromBody] PostLockObject_param_in pIn) { ResponseMessage Response = new ResponseMessage(); try { if (pIn.ObjectID < 0) { Logger.Error("ObjectID < 0 ,参数传递错误"); SobeyRecException.ThrowSelfNoParam(pIn.ObjectID.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECTID_WRONG, Logger, null); } if (pIn.ObjectTypeID < OTID.OTID_VTR || pIn.ObjectTypeID > OTID.OTID_OTHER) { SobeyRecException.ThrowSelfNoParam(pIn.ObjectTypeID.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECT_TPYEID_IS_NOT_EXIST, Logger, null); } if (string.IsNullOrEmpty(pIn.userName))// == "" || pIn.userName == string.Empty) { pIn.userName = "******"; } if (pIn.TimeOut < 0) { SobeyRecException.ThrowSelfNoParam(pIn.TimeOut.ToString(), GlobalDictionary.GLOBALDICT_CODE_LOCK_OBJECT_TIMEOUT_IS_WRONG, Logger, null); } bool ret = await _GlobalManager.SetLockObjectAsync(pIn.ObjectID, pIn.ObjectTypeID, pIn.userName, pIn.TimeOut); Response.Code = ret ? ResponseCodeDefines.SuccessCode : ResponseCodeDefines.PartialFailure; } catch (Exception e) { if (e.GetType() == typeof(SobeyRecException))//sobeyexcep会自动打印错误 { SobeyRecException se = e as SobeyRecException; Response.Code = se.ErrorCode.ToString(); Response.Msg = se.Message; } else { Response.Code = ResponseCodeDefines.ServiceError; Response.Msg = "error info:" + e.ToString(); Logger.Error(Response.Msg); } } return(Response); }
public async Task <ResponseMessage> PostUnlockObject([FromBody] PostLockObject_param_in pIn) { ResponseMessage Response = new ResponseMessage(); //pOut.errStr = no_err; //pOut.bRet = true; try { if (pIn.ObjectID < -1) { SobeyRecException.ThrowSelfNoParam(pIn.ObjectID.ToString(), GlobalDictionary.GLOBALDICT_CODE_UNLOCK_OBJECTID_IS_WRONG, Logger, null); } if (pIn.ObjectTypeID < OTID.OTID_ALL || pIn.ObjectTypeID > OTID.OTID_OTHER) { SobeyRecException.ThrowSelfNoParam(pIn.ObjectID.ToString(), GlobalDictionary.GLOBALDICT_CODE_UNLOCK_OBJECT_TYPEID_IS_NOT_EXIST, Logger, null); } if (pIn.userName == "" || pIn.userName == String.Empty) { pIn.userName = "******"; //ApplicationLog.WriteInfo("userName is null!"); } bool bRet = await _GlobalManager.SetUnlockObjectAsync(pIn.ObjectID, pIn.ObjectTypeID, pIn.userName); Response.Code = bRet ? ResponseCodeDefines.SuccessCode : ResponseCodeDefines.PartialFailure; } catch (Exception e) { if (e.GetType() == typeof(SobeyRecException))//sobeyexcep会自动打印错误 { SobeyRecException se = e as SobeyRecException; Response.Code = se.ErrorCode.ToString(); Response.Msg = se.Message; } else { Response.Code = ResponseCodeDefines.ServiceError; Response.Msg = "error info:" + e.ToString(); Logger.Error(Response.Msg); } } return(Response); }
public async Task <DbpObjectstateinfo> LockRowsByConditionAsync(int objectID, OTID objectTypeID, string userName, int TimeOut = 500) { DbpObjectstateinfo objectstateinfo = null; try { objectstateinfo = await Context.DbpObjectstateinfo.SingleOrDefaultAsync(x => ((objectID >= 0 && x.Objectid == objectID) || objectID < 0) && (((int)objectTypeID >= 0 && x.Objecttypeid == (int)objectTypeID) || (int)objectTypeID < 0) && ((!string.IsNullOrEmpty(userName) && x.Username == userName) || string.IsNullOrEmpty(userName)) && (x.Locklock == "" || x.Locklock == null || x.Begintime < DateTime.Now.AddMilliseconds(TimeOut * (-1)))); if (objectstateinfo != null) { objectstateinfo.Locklock = Guid.NewGuid().ToString(); await Context.SaveChangesAsync(); } } catch (Exception ex) { SobeyRecException.ThrowSelfNoParam(objectID.ToString(), GlobalDictionary.GLOBALDICT_CODE_EXECUTE_COMMAND_ERROR, Logger, ex); } return(objectstateinfo); }
public void UnLock(int objectID, OTID objectTypeID, string userName) { var selectResult = Context.DbpObjectstateinfo.FirstOrDefault(x => x.Objectid == objectID && x.Objecttypeid == (int)objectTypeID && x.Username == userName); //updateCmd.CommandText = "UPDATE DBP_OBJECTSTATEINFO SET LOCKLOCK=NULL" + SelectStatement(); try { if (selectResult != null) { selectResult.Locklock = null; Context.SaveChangesAsync(); } } catch (MySqlException ex) { Logger.Error("UnLock发生异常:" + ex.Message); string message = "UnLock发生异常"; SobeyRecException.ThrowSelfNoParam(message, GlobalDictionary.GLOBALDICT_CODE_EXECUTE_COMMAND_ERROR, Logger, ex); } }
public async void UpdateLock(string strUser, DateTime dtBeginTime, int nTime, int objectID, OTID objectTypeID) { var selectResult = Context.DbpObjectstateinfo.FirstOrDefault(x => x.Objectid == objectID && x.Objecttypeid == (int)objectTypeID && x.Username == strUser); try { if (selectResult != null) { selectResult.Username = strUser; selectResult.Begintime = dtBeginTime; selectResult.Timeout = nTime; await Context.SaveChangesAsync(); } } catch (Exception ex) { Logger.Error("UpdateLock发生异常:" + ex.Message); string message = strUser; SobeyRecException.ThrowSelfNoParam(message, GlobalDictionary.GLOBALDICT_CODE_EXECUTE_COMMAND_ERROR, Logger, ex); } }
public List <DbpObjectstateinfo> LockSelect(int nTryCount, int TimeOut, int objectID, OTID objectTypeID, string userName) { //ArrayList retArray = new ArrayList(); try { //尝试加锁三次,若都不成功,则返回 string strLock = string.Empty; int nSelect = 0; int nTryed = 0; while (nSelect < 1 && nTryed < nTryCount) { if (nTryed > 0) { System.Threading.Thread.Sleep(100); //ApplicationLog.WriteInfo("SelectObjectStateInfoFactory.cs //LockSelect()//nTryed = "+nTryed.ToString()+",nSelect = "+nSelect.ToString()); } nSelect = LockRows(ref strLock, TimeOut, objectID, objectTypeID, userName); nTryed++; } if (nSelect < 1) { return(null); } var retLst = Context.DbpObjectstateinfo.AsNoTracking().Where(x => x.Objectid == objectID && x.Objecttypeid == (int)objectTypeID && x.Username == userName && x.Locklock == strLock); return(retLst.ToList()); } catch (MySqlException ex) { Logger.Error("LockSelect 发生异常:" + ex.Message); string message = "LockSelect"; SobeyRecException.ThrowSelfNoParam(message, GlobalDictionary.GLOBALDICT_CODE_SELECT_COMMAND_FAILD, Logger, ex); } return(null); }
protected int LockRows(ref string strLock, int TimeOut, int objectID, OTID objectTypeID, string userName) { int nEffectRow = 0; string strTempLock = Guid.NewGuid().ToString(); var selectResult = Context.DbpObjectstateinfo.FirstOrDefault(x => x.Objectid == objectID && x.Objecttypeid == (int)objectTypeID && x.Username == userName && (x.Locklock == "" || x.Locklock == null || x.Begintime < DateTime.Now.AddMilliseconds(TimeOut * (-1)))); try { if (selectResult != null) { selectResult.Locklock = strTempLock; nEffectRow = Context.SaveChangesAsync().Result; } } catch (Exception ex) { string message = strTempLock; SobeyRecException.ThrowSelfNoParam(strTempLock, GlobalDictionary.GLOBALDICT_CODE_EXECUTE_COMMAND_ERROR, Logger, ex); } strLock = strTempLock; return(nEffectRow); }