using iWareCommon.Common.EnumType; using iWareCommon.Common.Service; using iWareCommon.Utils; using iWareExcel.EXCEL.Dao; using iWareExcel.EXCEL.Entity; using iWareExcel.ORM; using iWareExcel.Properties; using System; using System.Data.Entity.Validation; using System.Linq; namespace iWareExcel.EXCEL.Service { public class WorkSheetService : CommonService { private static object Lock = new object(); private WorkSheetService():base(WorkSheetDao.GetInstance()) { } private static WorkSheetService Instance = null; /// /// 获取单例的方法 /// /// 角色服务的单例实体 public static WorkSheetService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new WorkSheetService(); } } } return Instance; } public override int Save(WorkSheetEntity t, out string msg) { msg = ""; using (var dbModel = new DbModelExcel()) { try { var workBooks = dbModel.EXCELWorkBooks.Where(x => x.id == t.WorkBookId).Select(x => new {x.id,x.name}).ToList(); t.WorkBookName = workBooks.Count > 0 ? workBooks[0].name : ""; return WorkSheetDao.GetInstance().Save(t,dbModel); } catch (DbEntityValidationException ex) { var errs = ex.EntityValidationErrors.SelectMany(validationResult => validationResult.ValidationErrors).Select(m => m.ErrorMessage); msg = string.Join(", ", errs); LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Save", msg); return -1; } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Save", ex.Message); return -1; } catch (Exception ex) { msg = ex.HResult == (int)EDbError.记录已存在 ? EDbError.记录已存在.ToString() : ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Save", msg); return -1; } } } public override int Update(WorkSheetEntity t, out string msg) { msg = ""; using (var dbModel = new DbModelExcel()) { try { var workBooks = dbModel.EXCELWorkBooks.Where(x => x.id == t.WorkBookId).Select(x => new {x.id,x.name}).ToList(); t.WorkBookName = workBooks.Count > 0 ? workBooks[0].name : ""; return WorkSheetDao.GetInstance().Update(t,dbModel); } catch (DbEntityValidationException ex) { var errs = ex.EntityValidationErrors.SelectMany(validationResult => validationResult.ValidationErrors).Select(m => m.ErrorMessage); msg = string.Join(", ", errs); LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", msg); return -1; } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", ex.Message); return -1; } catch (Exception ex) { msg = ex.HResult == (int)EDbError.记录已存在 ? EDbError.记录已存在.ToString() : ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Update", msg); return -1; } } } } }