public JsonObject Import(DataTable dt, DataTable dtTemplate) { JsonObject json = new JsonObject(); if (dt == null || dt.Rows.Count == 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据为空"; return(json); } if (dtTemplate == null) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据导入失败,模板错误,请联系管理员"; return(json); } string checkTemplate = CommonHelper.Matching(dt, dtTemplate); if (checkTemplate != "") { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据导入失败," + checkTemplate + ",请下载最新模板"; return(json); } StringBuilder sbErro = new StringBuilder(); List <Model.Company> list = new List <Model.Company>(); int parmInt = 0; for (int i = 0; i < dt.Rows.Count; i++) { if (i < dtTemplate.Rows.Count) { //模板前几列不执行 continue; } bool checkNul = true;; for (int j = 0; j < dt.Columns.Count; j++) { if (dt.Rows[i][j] != null && dt.Rows[i][j].ToString() != "") { checkNul = false; break; } } if (checkNul) {//如果行数据为空则忽略 continue; } StringBuilder sbErroI = new StringBuilder(); //如果第一列为~开头,则为示例数据,不导入 if (dt.Rows[i][0] != null && dt.Rows[i][0].ToString() != "" && dt.Rows[i][0].ToString().Substring(0, 1) == "~") { continue; } Model.Company model = new Model.Company(); if (dt.Rows[i]["合作公司名称"] != null && dt.Rows[i]["合作公司名称"].ToString().Trim() != "") { model.CompanyName = dt.Rows[i]["合作公司名称"].ToString().Trim(); } else { sbErroI.Append(",合作公司名称不能为空"); } if (dt.Rows[i]["联系人"] != null && dt.Rows[i]["联系人"].ToString().Trim() != "") { model.Linkman = dt.Rows[i]["联系人"].ToString().Trim(); } if (dt.Rows[i]["联系人电话"] != null && dt.Rows[i]["联系人电话"].ToString().Trim() != "") { model.Telephone = dt.Rows[i]["联系人电话"].ToString().Trim(); } if (dt.Rows[i]["排序"] != null && dt.Rows[i]["排序"].ToString().Trim() != "") { if (int.TryParse(dt.Rows[i]["排序"].ToString().Trim(), out parmInt)) { model.OrderBy = parmInt; } else { sbErroI.Append(",排序[" + dt.Rows[i]["排序"].ToString().Trim() + "]必须为数字"); } } if (dt.Rows[i]["备注"] != null && dt.Rows[i]["备注"].ToString().Trim() != "") { model.Remark = dt.Rows[i]["备注"].ToString().Trim(); } if (sbErroI.Length > 0) { sbErro.Append(",第" + (i + 2) + "行数据:" + sbErroI.ToString().Substring(1)); } list.Add(model); } //如果有错误信息 if (sbErro.Length > 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = sbErro.ToString().Substring(1); return(json); } if (list.Count == 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据为空"; return(json); } try { int addCount = 0; int updateCount = 0; string userId = CurrentUser.User.Userinfo.UserID; DateTime timeNow = DateTime.Now; for (int i = 0; i < list.Count; i++) { List <Model.Company> modelExistsList = BLL.GetModelList("CompanyName='" + list[i].CompanyName + "' "); if (modelExistsList != null && modelExistsList.Count > 0) { list[i].id = modelExistsList[0].id; list[i].CompanyName = modelExistsList[0].CompanyName; list[i].CreateBy = modelExistsList[0].CreateBy; list[i].CreateTime = modelExistsList[0].CreateTime; list[i].UpdateBy = userId; list[i].UpdateTime = timeNow; BLL.Update(list[i]); updateCount++; } else { list[i].CreateBy = userId; list[i].CreateTime = timeNow; list[i].id = Guid.NewGuid().ToString(); BLL.Add(list[i]); addCount++; } } json.Status = JsonObject.STATUS_SUCCESS; json.Message = "导入成功"; if (addCount > 0) { json.Message += ",添加" + addCount + "条数据"; } if (updateCount > 0) { json.Message += ",修改" + updateCount + "条数据"; } return(json); } catch (Exception ex) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "导入失败,失败原因:" + ex.Message; return(json); } finally { } }
/// <summary> /// 保存数据字典数据 /// </summary> /// <returns></returns> public ActionResult Save(Model.Company model) { JsonObject json = new JsonObject(); StringBuilder sbErro = new StringBuilder(); if (string.IsNullOrEmpty(model.CompanyName)) { sbErro.Append(",客户名称不能为空"); } if (sbErro.Length > 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = sbErro.ToString().Substring(1); return(Json(json)); } //如果ID为空,则是添加 if (string.IsNullOrEmpty(model.id)) { //DepartmentName 唯一 List <Model.Company> modelExistsList = BLL.GetModelList("CompanyName='" + model.CompanyName + "' and IsDeleted!=1 "); if (modelExistsList != null && modelExistsList.Count > 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "添加失败,此数据已存在"; return(Json(json)); } model.IsDeleted = 0; model.CreateTime = DateTime.Now; model.CreateBy = CurrentUser.User.Userinfo.UserID; model.id = Guid.NewGuid().ToString(); bool res = BLL.Add(model); if (res) { json.Status = JsonObject.STATUS_SUCCESS; json.Message = "添加成功"; return(Json(json)); } else { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "添加失败"; return(Json(json)); } } else { //value 和 type 唯一确定一个text值 List <Model.Company> modelExistsList = BLL.GetModelList("CompanyName='" + model.CompanyName + "' and id!='" + model.id + "' and IsDeleted!=1 "); if (modelExistsList != null && modelExistsList.Count > 0) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "添加失败,此数据已存在"; return(Json(json)); } Model.Company modelOld = BLL.GetModel(model.id); if (modelOld == null) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据不存在"; return(Json(json)); } if (modelOld.IsDeleted == 1) { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "数据已删除"; return(Json(json)); } modelOld.CompanyName = model.CompanyName; modelOld.Linkman = model.Linkman; modelOld.Telephone = model.Telephone; modelOld.OrderBy = model.OrderBy; modelOld.Remark = model.Remark; modelOld.UpdateTime = DateTime.Now; modelOld.UpdateBy = CurrentUser.User.Userinfo.UserID; bool res = BLL.Update(modelOld); if (res) { json.Status = JsonObject.STATUS_SUCCESS; json.Message = "修改成功"; return(Json(json)); } else { json.Status = JsonObject.STATUS_FAIL; json.ErroMessage = "修改失败"; return(Json(json)); } } }