/// <summary> /// 重写查询事件 /// </summary> public override void GetList() { if (ObjName.Substring(0, 3).ToLower() == "vs_") { string spName = ObjName.Substring(3); string conn = CrossDb.GetConn(ObjName); jsonResult = Select_SP(spName, conn).ToJson(true, false, true); } else if (Query <string>("isCompany") == "1") { string spName = "sp_SelectCompany"; string conn = CrossDb.GetConn(ObjName); jsonResult = SelectList_SP(spName, conn).ToJson(true, false, true); } else { base.GetList(); } }
public void companyAuth(byte type, bool result, string errMsg) { string userName = Query <string>("uid"); using (MAction action = new MAction("data0073", CrossDb.GetConn("data0073"))) { MDataRow row = null; string where = string.Format("USER_LOGIN_NAME='{0}' AND ACTIVE_FLAG=0", userName); if (action.Fill(where)) { row = action.Data; } if (row == null || row.Count == 0) { result = false; errMsg = LangConst.NoCompanyAuth; } } if (type == 1)//页面直接调用时重写Token { using (MAction action = new MAction(TableNames.Sys_User)) { string where = string.Format("Status=1 and UserName='******'", userName); if (action.Fill(where)) { string userID = action.Get <string>(Sys_User.UserID); userName = action.Get <string>(Sys_User.UserName); string fullName = action.Get <string>(Sys_User.FullName, userName); string roleIDs = action.Get <string>(Sys_User.RoleIDs); string token = EncrpytHelper.Encrypt(DateTime.Now.Day + "," + userID + "," + userName + "," + fullName + "," + roleIDs); UserAuth.SetToken(token, userName); UserAuth.WriteCookie(token, userName, ""); } else { result = false; errMsg = LangConst.UserNotExists; } } } jsonResult = JsonHelper.OutResult(result, errMsg); }
public override void Update() { switch (TableName) { case "Data0023": jsonResult = purchaseLogic.UpdateSupplier(CrossTableName, CrossDb.GetConn(ObjName)); break; case "Data0028": jsonResult = purchaseLogic.UpdateSuppPrice(CrossTableName, CrossDb.GetConn(ObjName)); break; case "Data0017": jsonResult = purchaseLogic.UpdateData0017(CrossTableName, CrossDb.GetConn(ObjName)); break; default: base.Update(); break; } }
/// <summary> /// 针对导出重写查询 /// </summary> /// <param name="st"></param> /// <returns></returns> protected override MDataTable Select(GridConfig.SelectType st) { MDataTable dt; if (st.ToString() == "Export" && ObjName.Substring(0, 3).ToLower() == "vs_") { string spName = ObjName.Substring(3); string conn = CrossDb.GetConn(ObjName); dt = Select_SP(spName, conn); } else if (Query <string>("isCompany") == "1") { string spName = "sp_SelectCompany"; string conn = CrossDb.GetConn(ObjName); dt = SelectList_SP(spName, conn); } else { dt = base.Select(st); } return(dt); }
/// <summary> /// 数据结构行刷新 /// </summary> /// <param name="objName"></param> /// <param name="objCode"></param> /// <param name="dt">原有数据</param> /// <returns></returns> public static bool Flesh(string objName, string objCode, MDataTable dt, out string msg) { bool result = false; msg = LangConst.NoNewColumn; MDataTable newDt = dt.GetSchema(false); //移除表结构缓存 string tableKey = CacheManage.GetKey(CacheKeyType.Schema, objName, CrossDb.GetConn(objName)); CacheManage.LocalInstance.Remove(tableKey); FillTable(objName, objCode, newDt);//重新获取。 MDataTable addTable = dt.GetSchema(false); // bool needUpdate = false; foreach (MDataRow row in newDt.Rows) { MDataRow mr = dt.FindRow(string.Format("Field='{0}'", row.Get <string>("Field"))); if (mr == null)//找不到,则添加行 { row.Set(Config_Grid.OrderNum, 255); row.Set(Config_Grid.Hidden, true); row.Set(Config_Grid.GridID, Guid.NewGuid()); addTable.Rows.Add(row); } } if (addTable.Rows.Count > 0) { result = addTable.AcceptChanges(AcceptOp.InsertWithID); if (!result) { msg = Convert.ToString((Exception)addTable.DynamicData); } } return(result); }
/// <summary> /// 批量更新或插入。 /// </summary> /// <param name="dt"></param> /// <param name="excelRow"></param> /// <returns></returns> public static bool AcceptChanges(MDataTable dt, MDataRow excelRow, string objName = null) { if (excelRow == null) { MDataTable dtImportUnique = GridConfig.GetList(objName, GridConfig.SelectType.ImportUnique); string[] names = null; if (dtImportUnique != null && dtImportUnique.Rows.Count > 0) { names = new String[dtImportUnique.Rows.Count]; for (int i = 0; i < dtImportUnique.Rows.Count; i++) { names[i] = dtImportUnique.Rows[i].Get <string>(Config_Grid.Field); } } return(dt.AcceptChanges(AcceptOp.Auto, CrossDb.GetConn(dt.TableName), names)); } bool result = true; //获取相关配置 string[] tables = excelRow.Get <string>(Config_Excel.TableNames).Split(','); MDataTable configTable = GetExcelInfo(excelRow.Get <string>(Config_Excel.ExcelID)); Dictionary <string, string> rowPrimaryValue = new Dictionary <string, string>(); //存档每个表每行的主键值。 Dictionary <string, string> wherePrimaryValue = new Dictionary <string, string>(); //存档where条件对应的主键值。 int acceptType = excelRow.Get <int>(Config_Excel.AcceptType); using (MAction action = new MAction(CrossDb.GetEnum(tables[0]))) { action.SetAopState(AopOp.CloseAll); action.BeginTransation(); AppConfig.Debug.OpenDebugInfo = false; IExcelConfig excelConfigExtend = ExcelConfigFactory.GetExcelConfigExtend(); foreach (var table in tables) { GC.Collect();//后面的Fill查询代码循环上万次会增长太多内存,提前调用,能降低内存。 action.ResetTable(table); for (int i = 0; i < dt.Rows.Count; i++) { action.Data.Clear(); var row = dt.Rows[i]; foreach (var cell in row) //遍历所有数据行 { if (cell.Struct.TableName != null && cell.Struct.TableName.ToLower() == table.ToLower()) //过滤出属于本表的字段。 { string[] items = cell.ColumnName.Split('.'); string columnName = items[items.Length - 1]; action.Set(columnName, cell.Value); } } #region 检测是否需要插入外键。 MDataTable foreignTable = configTable.FindAll("TableName='" + table + "' and IsForeignkey=1"); if (foreignTable != null) { foreach (var foreignRow in foreignTable.Rows) { string formatter = foreignRow.Get <string>("Formatter", "").Trim('.'); string fTableName = foreignRow.Get <string>("ForeignTable"); if (string.IsNullOrEmpty(formatter)) { //获取主键外值键 string key = fTableName + i; if (rowPrimaryValue.ContainsKey(key)) { string value = rowPrimaryValue[key]; action.Set(foreignRow.Get <string>("Field"), value); } } else // 从其它自定义列取值。 { MDataCell cell = row[formatter]; cell = cell ?? row[fTableName + "." + formatter]; if (cell != null) { action.Set(foreignRow.Get <string>("Field"), cell.Value); } } } foreignTable = null; } #endregion #region //获取唯一联合主键,检测是否重复 string where = string.Empty; MDataRowCollection rowList = configTable.FindAll("TableName='" + table + "' and IsUnique=1"); if (rowList != null && rowList.Count > 0) { bool isUniqueOr = excelRow.Get <bool>(Config_Excel.WhereType); List <MDataCell> cells = new List <MDataCell>(); StringBuilder errText = new StringBuilder(); int errorCount = 0; foreach (var item in rowList) { var cell = action.Data[item.Get <string>(Config_ExcelInfo.Field)]; if (cell != null) { if (cell.IsNullOrEmpty) // 唯一主键是必填写字段 { errorCount++; errText.Append("[" + LangConst.The + "" + (i + 1) + LangConst.Row + "]:" + cell.Struct.ColumnName + "[" + cell.Struct.Description + "]" + LangConst.CantBeEmpty + "!\r\n"); } else { cells.Add(cell); } } } if (errorCount > 0) { if (!isUniqueOr || errorCount == rowList.Count) { result = false; dt.DynamicData = new Exception(errText.ToString()); goto err; } } MDataCell[] item2s = cells.ToArray(); where = action.GetWhere(!isUniqueOr, item2s); item2s = null; rowList = null; } if (!string.IsNullOrEmpty(where)) { MDataRow data = action.Data.Clone(); action.SetSelectColumns(action.Data.PrimaryCell.ColumnName); if (action.Fill(where))//根据条件查出主键ID (数据被清空) { string key = table + where; if (wherePrimaryValue.ContainsKey(key)) { rowPrimaryValue.Add(table + i, wherePrimaryValue[key]);//记录上一个主键值。 } else { rowPrimaryValue.Add(table + i, action.Get <string>(action.Data.PrimaryCell.ColumnName)); //记录上一个主键值。 } action.Data.LoadFrom(data, RowOp.IgnoreNull, false); //还原数据。 if (action.Data.GetState() == 2 && acceptType != 1) //排除掉仅插入选项 { ExcelResult eResult = excelConfigExtend.BeforeUpdate(action.Data, row); if (eResult == ExcelResult.Ignore || (eResult == ExcelResult.Default && action.Update(where))) { continue;//已经存在了,更新,准备下一条。 } else { result = false; dt.DynamicData = new Exception("[" + LangConst.The + (i + 1) + LangConst.Row + "]:" + action.DebugInfo); goto err; } } else { continue;//已经存在了,同时没有可更新字段 } } else if (action.RecordsAffected == -2)//产生错误信息,发生异常 { result = false; dt.DynamicData = new Exception("[" + LangConst.The + (i + 1) + LangConst.Row + "]:" + action.DebugInfo); goto err; } } #endregion if (action.Data.GetState() == 0 || acceptType == 2) //仅更新则跳过插入 { continue; //没有可映射插入的列。 } //插入前,调用函数(插入特殊主键等值) string errMsg; ExcelResult excelResult = excelConfigExtend.BeforeInsert(action.Data, row, out errMsg); if (excelResult == ExcelResult.Ignore) { continue; } if (excelResult == ExcelResult.Error || !action.Insert(InsertOp.ID)) { result = false; action.RollBack(); if (string.IsNullOrEmpty(errMsg)) { errMsg = "[" + LangConst.The + (i + 1) + LangConst.Row + "]:" + action.DebugInfo; } dt.DynamicData = new Exception(errMsg); excelConfigExtend.OnInsertError(errMsg, dt); goto err; } //插入后事件(可以触发其它事件) excelConfigExtend.AfterInsert(action.Data, row, i == dt.Rows.Count - 1); string primaryKey = action.Get <string>(action.Data.PrimaryCell.ColumnName); rowPrimaryValue.Add(table + i, primaryKey);//记录上一个主键值。 if (!wherePrimaryValue.ContainsKey(table + where)) { wherePrimaryValue.Add(table + where, primaryKey); } } } err: action.EndTransation(); excelConfigExtend.Dispose(); } return(result); }