using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using System.Data.Entity.Migrations; using System.Linq; using System.Text; using System.Threading.Tasks; using CK.SCP.Models; using CK.SCP.Models.ScpEntity; using CK.SCP.Utils; using System.Data.Entity.Core; using CK.SCP.Models.Enums; using CK.SCP.Models.AppBoxEntity; namespace CK.SCP.Controller { public class SCP_LOGINNUMBER_CONTROLLER { public static TA_LOGINNUMBER GetlistUserNumber(string username) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_LOGINNUMBER.Where(p => p.Name == username).FirstOrDefault(); } } public static ResultObject Save_TA_LOGINNUMBER(string username, LoginNumer p_state,string date) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { using (AppBoxContext _appdb = EntitiesFactory.CreateAppBoxInstance()) { var user = _appdb.Users.FirstOrDefault(t => t.Name == username); if (user == null) { _ret.MessageList.Add("用户名"+ username+"不存在,请输入正确用户名"); } else { var login = db.TA_LOGINNUMBER.FirstOrDefault(p => p.Name == username); if (p_state == LoginNumer.Sussess && login != null) { db.TA_LOGINNUMBER.Remove(login); } if (login != null && p_state == LoginNumer.Faile) { if (login.Number == 4&&login.Time==date) { login.Number = 5; db.TA_LOGINNUMBER.AddOrUpdate(login); user.Enabled = false; _appdb.Users.AddOrUpdate(user); _appdb.SaveChanges(); _ret.MessageList.Add("因您连续五次输入密码错误,该账号已经被设为未启用状态,请联系系统管理员"); } else if(login.Number <4 && login.Time == date) { login.Number = login.Number + 1; db.TA_LOGINNUMBER.AddOrUpdate(login); }else if(login.Time != date) { login.Time = date; login.Number = 1; db.TA_LOGINNUMBER.AddOrUpdate(login); } } else if (login == null && p_state == LoginNumer.Faile) { TA_LOGINNUMBER log = new TA_LOGINNUMBER(); log.Name = username; log.Number = 1; log.Time = DateTime.Now.ToShortDateString(); db.TA_LOGINNUMBER.AddOrUpdate(log); } } } if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_LOGINNUMBER_CONTROLLER), "Save_TA_LOGINNUMBER", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_LOGINNUMBER_CONTROLLER), "Save_TA_LOGINNUMBER", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_LOGINNUMBER_CONTROLLER), "Save_TA_LOGINNUMBER", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_LOGINNUMBER_CONTROLLER), "Save_TA_LOGINNUMBER", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } } }