2
schangxiang@126.com
2024-08-16 b47c50a2a514def7374b32d7194b2c599cba5625
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.BASE.Dao;
using iWareDataCore.BASE.Entity;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using iWareExcel.EXCEL.Entity;
using iWareExcel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.BASE.Service
{
    public class MaterialViewService : CommonService<MaterialViewEntity, BASEMaterialView, DbModelCore>
    {
 
        private static object Lock = new object();
 
        private MaterialViewService() : base(MaterialViewDao.GetInstance()) { }
 
        private static MaterialViewService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static MaterialViewService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new MaterialViewService();
                    }
                }
            }
            return Instance;
        }
 
        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <param name="path">文件在服务器上的路径</param>
        /// <param name="workBook">导入使用的模板</param>
        /// <param name="msg">异常错误消息</param>
        /// <returns>是否导入成功</returns>
        public bool ImportExcel(string path, WorkBookEntity workBook, out string msg)
        {
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    msg = "";
                    var ds = ExcelHelper.FromExcelByEPPlus(path, workBook, out msg);
                    if (!string.IsNullOrEmpty(msg))
                    {
                        throw new Exception(msg);
                    }
                    var materials = ds[0];
                    if (materials.Count < 1)
                    {
                        msg = "物料记录数为0,请修改后重新导入";
                        return false;
                    }
                    var types = dbModel.BASEMaterialTypes.Where(x => 1 == 1).ToList();
                    var i = 0;
                    var codes = new List<string>();
                    List<MaterialEntity> materlst = new List<MaterialEntity>();
                    var materialSaves = new List<MaterialEntity>();
                    var existMaterialcodes = dbModel.BASEMaterials.Select(x =>x.code).ToList();
                    bool b1 = true;
                    foreach (MaterialViewEntity m in materials)
                    {
                        i += 1;
                        if (string.IsNullOrEmpty(m.Code))
                        {
                            msg = string.Format("在第{0}行,物料号不能为空,请修改后重新导入", i + 1);
                            b1= false;
                            break;
                        }
 
                        if (string.IsNullOrEmpty(m.Wide))
                        {
                            msg = string.Format("在第{0}行,宽不能为空,请修改后重新导入", i + 1);
                            b1= false;
                            break;
                        }
                        var type = types.FirstOrDefault(x => x.name == m.TypeName);
                        if (type == null)
                        {
                            m.MaterialType = 5;//其他
                        }
                        else 
                        {
                            m.MaterialType = type.id;
                        }
                        if (existMaterialcodes.Contains(m.Code))
                        {
                            continue;
                        }
                        codes.Add(m.Code);
                        if (string.IsNullOrEmpty(m.Name)) { m.Name = m.Description; }
                        if (string.IsNullOrEmpty(m.Version)) { m.Version = "00"; }
                   
                        MaterialEntity mt=new MaterialEntity()
                        {
                            ClassificationSociety = m.ClassificationSociety,
                            Code = m.Code,
                            MaterialNo = m.MaterialNo,
                            Cost = m.Cost,
                            CuttingType = m.CuttingType,
                            Description = m.Description,
                            GuaranteePeriod = m.GuaranteePeriod,
                            IssueProjectNo = m.IssueProjectNo,
                            Length = m.Length,
                            SerialNo = m.SerialNo,
                            Name = m.Name,
                            LaneSeparation = m.Laneseparation,
                            MarkingPen = m.MarkingPen,
                            PageNo = m.PageNo,
                            Status = 1,
                            Thick = m.Thick,
                            Unit = m.Unit,
                            Remark = m.Remark,
                            ProcurementProjectNo = m.ProcurementProjectNo,
                            TypeId = m.MaterialType,
                            Version = m.Version,
                            Weight = m.Weight,
                            VerticalPosition = m.VerticalPosition,
                            Wide = m.Wide
                        };
                         materialSaves.Add(mt); 
 
                    }
                    if (!b1) 
                    {
                        return b1;
                    }
                    var sql = string.Format("INSERT INTO BASEMaterial(status, name, code, materialno, version, typeid, guaranteeperiod, cost, classificationsociety, unit, serialno, description, remark, verticalposition, weight, issueprojectno, procurementprojectno, cuttingtype, pageno, thick, wide, length, markingpen, laneseparation) VALUES");
                    for (var j = 0; j < materialSaves.Count ; j++)
                    {
                               sql += string.Format("({0}, '{1}', '{2}', '{3}', {4}, '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}', '{13}', '{14}', '{15}','{16}' ,'{17}', '{18}', '{19}', '{20}', '{21}','{22}','{23}' ),",
                                materialSaves[j].Status,
                                StringHelper.RelpaceQuot(materialSaves[j].Name),
                                StringHelper.RelpaceQuot(materialSaves[j].Code),
                                StringHelper.RelpaceQuot(materialSaves[j].MaterialNo),
                                StringHelper.RelpaceQuot(materialSaves[j].Version),
                                materialSaves[j].TypeId,
                                StringHelper.RelpaceQuot(materialSaves[j].GuaranteePeriod),
                                materialSaves[j].Cost,
                                StringHelper.RelpaceQuot(materialSaves[j].ClassificationSociety),
                                StringHelper.RelpaceQuot(materialSaves[j].Unit),
                                StringHelper.RelpaceQuot(materialSaves[j].SerialNo),
                                StringHelper.RelpaceQuot(materialSaves[j].Description),
                                StringHelper.RelpaceQuot(materialSaves[j].Remark),
                                StringHelper.RelpaceQuot(materialSaves[j].VerticalPosition),
                                StringHelper.RelpaceQuot(materialSaves[j].Weight),
                                StringHelper.RelpaceQuot(materialSaves[j].IssueProjectNo),
                                StringHelper.RelpaceQuot(materialSaves[j].ProcurementProjectNo),
                                StringHelper.RelpaceQuot(materialSaves[j].CuttingType),
                                StringHelper.RelpaceQuot(materialSaves[j].PageNo),
                                StringHelper.RelpaceQuot(materialSaves[j].Thick),
                                StringHelper.RelpaceQuot(materialSaves[j].Wide),
                                StringHelper.RelpaceQuot(materialSaves[j].Length),
                                StringHelper.RelpaceQuot(materialSaves[j].MarkingPen),
                                StringHelper.RelpaceQuot(materialSaves[j].LaneSeparation));
                    }
                    if (sql.EndsWith(","))
                    {
                        sql = sql.Substring(0, sql.Length - 1);
                    }
                    var num = dbModel.Database.ExecuteSqlCommand(sql);
 
                    return string.IsNullOrEmpty(msg);
 
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ImportExcel", ex.Message);
                    return false;
                }
            }
        }
 
 
 
 
 
    }
}