schangxiang@126.com
2025-09-17 a32e5a5b296cab5ccc20953ca4e801ca4f27bd85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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<WorkBookEntity, EXCELWorkBook, DbModelExcel>
    {
        private static object Lock = new object();
 
        private WorkBookService() : base(WorkBookDao.GetInstance()) { }
 
        private static WorkBookService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static WorkBookService GetInstance()
        {
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new WorkBookService();
                    }
                }
            }
            return Instance;
        }
 
 
        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="param">查询条件</param>
        /// <param name="msg">异常错误信息</param>
        /// <returns>工作簿列表</returns>
        public override List<WorkBookEntity> QueryByParam(QueryParam param, out string msg)
        {
            msg = "";
            using(var dbModel = new DbModelExcel())
            {
                try 
                {
                    var workBooks = WorkBookDao.GetInstance().QueryByParam(param, dbModel);
                    var workBookIds = new List<int>();
                    var workBookDict = new Dictionary<int, WorkBookEntity>();
                    
                    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<int>();
                    var workSheetDict = new Dictionary<int, WorkSheetEntity>();
 
                    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<WorkBookEntity>();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "QueryByParam", ex.Message);
                    return new List<WorkBookEntity>();
                }
            }
            
        }
 
 
 
 
    }
 
        
}