public static OpResult Import(ImportSet obj, System.Web.HttpFileCollectionBase httpFiles, string fieldName, string columnName) { var op = new OpResult(); var errLs = new List <string>(); int count = 0; try { Dictionary <string, char> fieldCols = null; DataTable dt = null; op = ImportSetService.ImportSet(obj, httpFiles, fieldName, columnName, ref fieldCols, ref dt); if (!op.Successed) { return(op); } var brandClass = SysDataDictService.FindList(o => o.DicPSN == (int)DicType.品牌分类 && o.CompanyId == CommonService.CompanyId); var otherClass = brandClass.FirstOrDefault(o => o.Title.StartsWith("其")); var brands = GetAllProductBrands(null).ToList(); var max = SysDataDictService.GetMaxSN; var clsIdx = Convert.ToInt32(fieldCols["ClassifyId"]) - 65; var titleIdx = Convert.ToInt32(fieldCols["Title"]) - 65; count = dt.Rows.Count; for (int i = dt.Rows.Count - 1; i >= 0; i--) { try { var dr = dt.Rows[i]; var text = dr[clsIdx].ToString(); if (text.IsNullOrEmpty()) { continue; } var cls = brandClass.FirstOrDefault(o => o.Title == text); if (cls != null) { dr[clsIdx] = cls.DicSN.ToString(); } else { if (obj.RefCreate) { var data = new Sys.Entity.SysDataDictionary() { DicPSN = (int)DicType.品牌分类, DicSN = max++, Status = true, Title = text, CompanyId = CommonService.CompanyId }; SysDataDictService.Add(data); brandClass.Add(data); dr[clsIdx] = data.DicSN.ToString(); } else if (otherClass != null) { dr[clsIdx] = otherClass.DicSN.ToString(); } else { errLs.Add("品牌分类[" + text + "]不存在!"); dt.Rows.RemoveAt(i);//去除不导入 continue; } } text = dr[titleIdx].ToString().Trim(); if (brands.Any(o => o.Title == text)) { errLs.Add("品牌名称[" + text + "]已存在!"); dt.Rows.RemoveAt(i);//去除不导入 } else { brands.Add(new ProductBrand() { Title = text }); } } catch (Exception e) { throw new Exception("品牌分类处理失败!", e); } } max = MaxSN; StringBuilder sb = new StringBuilder(); sb.Append("begin tran "); foreach (DataRow dr in dt.Rows) { sb.Append("insert into "); sb.Append(obj.TableName); sb.Append("(CompanyId,BrandSN,State,"); sb.Append(string.Join(",", fieldCols.Keys)); sb.Append(") values("); sb.AppendFormat("{0},", obj.CompanyId); sb.AppendFormat("{0},", max++); sb.AppendFormat("1,"); foreach (var de in fieldCols) { var index = Convert.ToInt32(de.Value) - 65; try { var text = dr[index].ToString(); sb.Append("'" + text + "',"); } catch (Exception e) { throw new Exception("列选择超过范围!", e); } } sb = sb.Remove(sb.Length - 1, 1); sb.Append(");"); } sb.Append(" commit tran"); op.Successed = new Pharos.Logic.DAL.CommonDAL()._db.ExecuteNonQueryText(sb.ToString(), null) > 0; Log.WriteInsert("品牌导入", Sys.LogModule.档案管理); } catch (Exception ex) { op.Message = ex.Message; op.Successed = false; Log.WriteError(ex); errLs.Add("导入出现异常!"); } return(CommonService.GenerateImportHtml(errLs, count)); }
public static OpResult Import(ImportSet obj, System.Web.HttpFileCollectionBase httpFiles, string fieldName, string columnName, short businessType) { var op = new OpResult(); var errLs = new List <string>(); int count = 0; try { Dictionary <string, char> fieldCols = null; DataTable dt = null; op = ImportSetService.ImportSet(obj, httpFiles, fieldName, columnName, ref fieldCols, ref dt); if (!op.Successed) { return(op); } var supplierClass = SysDataDictService.FindList(o => o.DicPSN == (int)DicType.供应商分类); var otherClass = supplierClass.FirstOrDefault(o => o.Title.StartsWith("其")); var suppliers = GetList(); var users = UserInfoService.GetList(); var max = SysDataDictService.GetMaxSN; var clsIdx = Convert.ToInt32(fieldCols["ClassifyId"]) - 65; var titleIdx = Convert.ToInt32(fieldCols["Title"]) - 65; var fulltitleIdx = Convert.ToInt32(fieldCols["FullTitle"]) - 65; var linkmanIdx = Convert.ToInt32(fieldCols["Linkman"]) - 65; var designeeIdx = Convert.ToInt32(fieldCols["Designee"]) - 65; count = dt.Rows.Count; for (int i = dt.Rows.Count - 1; i >= 0; i--) { try { var dr = dt.Rows[i]; var text = dr[clsIdx].ToString(); if (text.IsNullOrEmpty()) { errLs.Add("行号[" + i + "]供应商分类为空!"); dt.Rows.RemoveAt(i);//去除不导入 continue; } var cls = supplierClass.FirstOrDefault(o => o.Title == text); if (cls != null) { dr[clsIdx] = cls.DicSN.ToString(); } else { if (obj.RefCreate) { var data = new Sys.Entity.SysDataDictionary() { DicPSN = (int)DicType.供应商分类, DicSN = max++, Status = true, Title = text, CompanyId = CommonService.CompanyId }; SysDataDictService.Add(data); supplierClass.Add(data); dr[clsIdx] = data.DicSN.ToString(); } else if (otherClass != null) { dr[clsIdx] = otherClass.DicSN.ToString(); } else { errLs.Add("供应商分类[" + text + "]不存在!"); dt.Rows.RemoveAt(i);//去除不导入 continue; } } text = dr[designeeIdx].ToString(); var user = users.FirstOrDefault(o => o.FullName == text); if (user != null) { dr[designeeIdx] = user.UID; } else { errLs.Add("指派人[" + text + "]不存在!"); dt.Rows.RemoveAt(i); continue; } text = dr[titleIdx].ToString().Trim(); var fulltext = dr[fulltitleIdx].ToString().Trim(); var linkman = dr[linkmanIdx].ToString().Trim(); if (text.IsNullOrEmpty()) { errLs.Add("行号[" + text + "]简称为空!"); dt.Rows.RemoveAt(i); } else if (fulltext.IsNullOrEmpty()) { errLs.Add("简称[" + text + "]全称为空!"); dt.Rows.RemoveAt(i); } else if (linkman.IsNullOrEmpty()) { errLs.Add("简称[" + text + "]联系人为空!"); dt.Rows.RemoveAt(i); } else if (suppliers.Any(o => o.Title == text)) { errLs.Add("简称[" + text + "]已存在!"); dt.Rows.RemoveAt(i); } else if (suppliers.Any(o => o.FullTitle == fulltext)) { errLs.Add("全称[" + fulltext + "]已存在!"); dt.Rows.RemoveAt(i); } else { suppliers.Add(new Supplier() { Title = text, FullTitle = fulltext }); } } catch (Exception e) { throw new Exception("供应商分类处理失败!", e); } } StringBuilder sb = new StringBuilder(); sb.Append("begin tran "); foreach (DataRow dr in dt.Rows) { sb.Append("insert into Supplier"); sb.Append("(CompanyId,BusinessType,Id,"); sb.Append(string.Join(",", fieldCols.Keys)); sb.Append(") values("); sb.AppendFormat("{0},", obj.CompanyId); sb.AppendFormat(businessType + ","); sb.AppendFormat("'{0}',", CommonRules.GUID); foreach (var de in fieldCols) { var index = Convert.ToInt32(de.Value) - 65; try { var text = dr[index].ToString(); sb.Append("'" + text + "',"); } catch (Exception e) { throw new Exception("列选择超过范围!", e); } } sb = sb.Remove(sb.Length - 1, 1); sb.Append(");"); } sb.Append(" commit tran"); op.Successed = new Pharos.Logic.DAL.CommonDAL()._db.ExecuteNonQueryText(sb.ToString(), null) > 0; log.WriteInsert(obj.TableName == "Supplier" ? "供应商导入" : "批发商导入", Pharos.Sys.LogModule.档案管理); } catch (Exception ex) { op.Message = ex.Message; op.Successed = false; Log.WriteError(ex); errLs.Add("导入出现异常!"); } return(CommonService.GenerateImportHtml(errLs, count)); }