schangxiang@126.com
2025-09-17 ab9d9126ced7d6dac0e14c3ede5a49fdb7fc94df
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
104
105
106
107
108
109
110
111
112
113
114
115
116
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 WorkCellService : CommonService<WorkCellEntity, EXCELWorkCell, DbModelExcel>
    {
        private static object Lock = new object();
 
        private WorkCellService():base(WorkCellDao.GetInstance()) { }
 
        private static WorkCellService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static WorkCellService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new WorkCellService();
                    }
                }
            }
            return Instance;
        }
 
        public override int Save(WorkCellEntity t, out string msg)
        {
            msg = "";
            using (var dbModel = new DbModelExcel())
            {
                try
                {
                    var workSheets = dbModel.EXCELWorkSheets.Where(x => x.id == t.WorkSheetId).Select(x => new { x.id, x.name,x.workbookid,x.workbookname }).ToList();
                    t.WorkSheetName = workSheets.Count > 0 ? workSheets[0].name : "";
                    t.WorkBookName = workSheets.Count > 0 ? workSheets[0].workbookname : "";
                    t.WorkBookId = workSheets.Count > 0 ? workSheets[0].workbookid : 0;
                    return WorkCellDao.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(WorkCellEntity t, out string msg)
        {
            msg = "";
            using (var dbModel = new DbModelExcel())
            {
                try
                {
                    var workSheets = dbModel.EXCELWorkSheets.Where(x => x.id == t.WorkSheetId).Select(x => new { x.id, x.name, x.workbookid, x.workbookname }).ToList();
                    t.WorkSheetName = workSheets.Count > 0 ? workSheets[0].name : "";
                    t.WorkBookName = workSheets.Count > 0 ? workSheets[0].workbookname : "";
                    t.WorkBookId = workSheets.Count > 0 ? workSheets[0].workbookid : 0;
                    return WorkCellDao.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;
                }
            }
        }
 
 
    }
}