using iWareCommon.Common.Entity; 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.Collections.Generic; using System.Data.Entity.Validation; using System.Linq; namespace iWareExcel.EXCEL.Service { public class WorkBookService : CommonService { private static object Lock = new object(); private WorkBookService() : base(WorkBookDao.GetInstance()) { } private static WorkBookService Instance = null; /// /// 获取单例的方法 /// /// 角色服务的单例实体 public static WorkBookService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new WorkBookService(); } } } return Instance; } /// /// 根据条件查询 /// /// 查询条件 /// 异常错误信息 /// 工作簿列表 public override List QueryByParam(QueryParam param, out string msg) { msg = ""; using(var dbModel = new DbModelExcel()) { try { var workBooks = WorkBookDao.GetInstance().QueryByParam(param, dbModel); var workBookIds = new List(); var workBookDict = new Dictionary(); workBooks.ForEach(x => { workBookIds.Add(x.Id); workBookDict.Add(x.Id, x); }); var workSheets = dbModel.EXCELWorkSheets.Where(x => workBookIds.Contains(x.workbookid)).OrderBy(x => x.sheetindex).ToList(); var workSheetIds = new List(); var workSheetDict = new Dictionary(); workSheets.ForEach(x => { workSheetIds.Add(x.id); var workSheet = new WorkSheetEntity(x); workSheetDict.Add(x.id, workSheet); workBookDict[x.workbookid].WorkSheets.Add(workSheet); }); var workCells = dbModel.EXCELWorkCells.Where(x => workSheetIds.Contains(x.worksheetid)).OrderBy(x => x.cellindex).ToList(); workCells.ForEach(x => workSheetDict[x.worksheetid].WorkCells.Add(new WorkCellEntity(x))); return workBooks; } 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(), "QueryByParam", msg); return new List(); } catch (Exception ex) { msg = ex.Message; LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "QueryByParam", ex.Message); return new List(); } } } } }