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 { private static object Lock = new object(); private MaterialViewService() : base(MaterialViewDao.GetInstance()) { } private static MaterialViewService Instance = null; /// /// 获取单例的方法 /// /// 角色服务的单例实体 public static MaterialViewService GetInstance() { if (Instance == null) { lock (Lock) { if (Instance == null) { Instance = new MaterialViewService(); } } } return Instance; } /// /// 导入Excel /// /// 文件在服务器上的路径 /// 导入使用的模板 /// 异常错误消息 /// 是否导入成功 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(); List materlst = new List(); var materialSaves = new List(); 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; } } } /// /// 导入物料基础数据,来源MES 【Editby shaocx,2025-09-17】 /// /// /// /// public bool ImportExcelFromMes(List materials, out string msg) { using (var dbModel = new DbModelCore()) { try { msg = ""; if (materials.Count < 1) { msg = "物料记录数为0,请修改后重新导入"; return false; } var types = dbModel.BASEMaterialTypes.Where(x => 1 == 1).ToList(); var i = 0; var codes = new List(); List materlst = new List(); var materialSaves = new List(); 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; } } } } }