SVN-Revision: r485
This commit is contained in:
cyf
2025-04-14 08:42:17 +00:00
parent 5c53ca5957
commit eea88e0b2b
20 changed files with 840 additions and 224 deletions
+469 -2
View File
@@ -47,6 +47,8 @@ using DevExpress.XtraTab;
using NPOI.SS.Util;
using DevExpress.Data;
using DevExpress.Data.Filtering;
using System.Data.SqlClient;
using DevExpress.Utils;
namespace Lskj.Control.Model
{
@@ -2092,6 +2094,7 @@ namespace Lskj.Control.Model
model.FieldType == ControlType.LabAutoCompleteValue ||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
model.FieldType == ControlType.LabMultiSelectValue ||
model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam
|| model.FieldType == ControlType.LabTreeLookValue
|| model.FieldType == ControlType.LabCheckBox
@@ -2102,7 +2105,7 @@ namespace Lskj.Control.Model
{
bool isEmpty = model.CanNull;
string fieldValue = GetValueByImportText(dataRow, col.FieldName, cell + "", isBillImport);
if ((model.FieldType == ControlType.LabMultiSelectValue ||
if ((model.FieldType == ControlType.LabMultiSelectValue || model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam) && cell.ToString().Contains(","))
{
string[] cellText = cell.ToString().Split(',');
@@ -2428,6 +2431,7 @@ namespace Lskj.Control.Model
model.FieldType == ControlType.LabAutoCompleteValue ||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
model.FieldType == ControlType.LabMultiSelectValue ||
model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam
|| model.FieldType == ControlType.LabTreeLookValue
|| model.FieldType == ControlType.LabCheckBox
@@ -2438,7 +2442,7 @@ namespace Lskj.Control.Model
{
bool isEmpty = model.CanNull;
string fieldValue = GetValueByImportText(dataRow, col.FieldName, cell + "", isBillImport);
if ((model.FieldType == ControlType.LabMultiSelectValue ||
if ((model.FieldType == ControlType.LabMultiSelectValue || model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam) && cell.ToString().Contains(","))
{
string[] cellText = cell.ToString().Split(',');
@@ -2593,6 +2597,467 @@ namespace Lskj.Control.Model
public static bool QuickImport(this GridView gridView, ModuleModel SysModel, string parmaryKey, string leftField, string leftValue,List<string> ImportReturnName, List<string> ImportReturnValue, GridControlEx ParentGridEx=null )
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "选择导入文件";
// dialog.Filter = "Excel|*.xls|Excel|*.xlsx";
dialog.Filter = SystemInfo.Instance.IsXlsxFirst ? "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls" :
"Excel文件(*.xls)|*.xls|Excel文件(*.xlsx)|*.xlsx";
DialogResult ImportResult = dialog.ShowDialog();
DataTable ImportTable = new DataTable();
if (ImportResult == DialogResult.OK)
{
string _importFlag = "lskjimport_errorFlag";
DataTable tableColumns = BaseModuleImpl.GetBaseGridColumns(SysModel.ModeCode);
GridControlEx gridControlEx = new GridControlEx();
if (gridView is BandedGridView && tableColumns != null && tableColumns.Rows.Count > 0)
{
gridControlEx = new BandedGridControlEx();
(gridControlEx as BandedGridControlEx).moduleModel = SysModel;
gridControlEx.SetReadOnlyColumns(tableColumns, GridCustomColumnStruct.BaseMainGridView + SysModel.FormKey);
GridBand gridBand = new GridBand();
gridBand.Caption = "导入消息";
gridBand.AppearanceHeader.Options.UseFont = true;
gridBand.AppearanceHeader.Font = new Font("宋体", 9, FontStyle.Bold);
gridBand.AppearanceHeader.Options.UseTextOptions = true;
gridBand.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
(gridControlEx.GridView as BandedGridView).Bands.AddRange(new GridBand[] { gridBand });
//BandedGridColumn
BandedGridColumn colMsg = new BandedGridColumn();
colMsg.Tag = null;
colMsg.FieldName = "import_errormsg";
colMsg.Width = 200;
colMsg.Caption = "导入错误信息";
colMsg.Visible = true;
colMsg.OwnerBand = gridBand;
gridControlEx.GridView.Columns.Add(colMsg);
BandedGridColumn RegressionMsg = new BandedGridColumn();
RegressionMsg.Tag = null;
RegressionMsg.FieldName = _importFlag;
RegressionMsg.Width = 200;
RegressionMsg.Caption = "是否导入成功";
RegressionMsg.Visible = true;
RegressionMsg.OwnerBand = gridBand;
gridControlEx.GridView.Columns.Add(RegressionMsg);
}
else
{
gridControlEx.SetEditColumns(tableColumns, GridCustomColumnStruct.BaseMainGridView + SysModel.FormKey);
gridControlEx.GridView.OptionsBehavior.Editable = false;//禁止编辑
GridColumn colMsg = new GridColumn();
colMsg.Tag = null;
colMsg.FieldName = "import_errormsg";
colMsg.Width = 200;
colMsg.Caption = "导入错误信息";
colMsg.Visible = true;
gridControlEx.GridView.Columns.Add(colMsg);
GridColumn RegressionMsg = new GridColumn();
RegressionMsg.Tag = null;
RegressionMsg.FieldName = _importFlag;
RegressionMsg.Width = 200;
RegressionMsg.Caption = "是否导入成功";
RegressionMsg.Visible = true;
gridControlEx.GridView.Columns.Add(RegressionMsg);
}
if (!BaseImpl.HasExistsColumn(SysModel.MenuTable,_importFlag))
{
BaseImpl.ExecSqlValue(string.Format("alter table {0} add {1} varchar(20) default('0')", SysModel.MenuTable, _importFlag));
}
string fileName = dialog.FileName;
string colFields = gridControlEx.GridView.Columns.ToString(',');
ImportTable = gridControlEx.GridView.ToExcelDataTable(fileName, colFields, true);
if (ImportTable==null&&ImportTable.Rows.Count < 1)
{
MessageBox.Show("没有任何数据可以进行导入", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
//if (MessageUtil.Show("如果导入数据量太多将花费较长时间,是否确认导入", MessageBoxButtons.YesNo) != DialogResult.Yes)
// return false;
//设置默认值
if (SystemInfo.Instance.ImportDefaultValue)
{
foreach (GridColumn col in gridView.VisibleColumns)
{
if (!ImportTable.Columns.Contains(col.FieldName)) ImportTable.Columns.Add(col.FieldName);
}
foreach (DataRow rowItem in ImportTable.Rows)
{
foreach (GridColumn col in gridView.VisibleColumns)
{
// 未包含字段,则使用控件默认值.
GridColumnModel model = col.Tag as GridColumnModel;
if (model != null && !string.IsNullOrEmpty(model.DefaultValue))
{
if (!string.IsNullOrEmpty(rowItem[col.FieldName] + "")) continue;
string fieldValue = ReplaceHelper.ReplaceRowParam(rowItem, model.DefaultValue);
if (ParentGridEx != null)
{
DataRow SelectTheLine = ParentGridEx.GetViewFocusedDataRow();
fieldValue = ReplaceHelper.ReplaceRowParam(SelectTheLine, fieldValue);
}
fieldValue = BaseImpl.GetDefaultValue(fieldValue);
if (!string.IsNullOrEmpty(fieldValue))
{
rowItem[col.FieldName] = fieldValue;
if (!string.IsNullOrEmpty(model.UnionFields))
{
SetUnionValue(model, rowItem[col.FieldName] + "", rowItem);
}
}
}
}
}
}
//判断导入条件
if (!string.IsNullOrEmpty(SysModel.ImportConditions))
{
List<int> ErrorLine = new List<int>();
int currRow = 0;
foreach (DataRow dr in ImportTable.Rows)
{
currRow++;
string cond = ReplaceHelper.ReplaceRowParam(dr, SysModel.ImportConditions);
if (ParentGridEx != null)
{
DataRow SelectTheLine = ParentGridEx.GetViewFocusedDataRow();
cond = ReplaceHelper.ReplaceRowParam(SelectTheLine, cond);
}
bool result = false;
if (cond.StartsWith("@") || cond.StartsWith("!"))
{
result = "1".Equals(BaseImpl.GetDefaultValue(cond));
}
else
{
result = ReplaceHelper.ReplaceRowParamCond(dr, cond);
}
if (!result)
{
ErrorLine.Add(currRow);
}
}
if (ErrorLine.Count > 0)
{
XtraMessageBox.Show("导入失败,第" + String.Join(", ", ErrorLine) + "条不满足条件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
//开始导入
//提交到数据库
string rowNum = "0";
string colNum = "0";
string colName = "";
int success = 0;
int failed = 0;
List<string> _calcFields = new List<string>();
DataTable dtCalcFields = BaseImpl.GetDataTableResult(string.Format("select name from sys.columns where object_id=object_id('{0}') and is_computed=1", SysModel.MenuTable));
if ((dtCalcFields != null) && (dtCalcFields.Rows.Count > 0))
{
foreach (DataRow dr in dtCalcFields.Rows)
{
_calcFields.Add(dr[0] + "");
}
}
try
{
string sql = "select * from " + SysModel.MenuTable + " where 1<>1";
SqlDataAdapter dat = BaseImpl.GetAdapterResult(sql);
SqlCommandBuilder scb = new SqlCommandBuilder(dat);
DataTable datatb = new DataTable();
DataTable failedData = new DataTable();
dat.Fill(datatb);
if (ImportReturnName.Count > 0)
{
foreach (string item in ImportReturnName)
{
if (!ImportTable.Columns.Contains(item))
ImportTable.Columns.Add(item);
}
}
failedData = ImportTable.Clone();
if (!failedData.Columns.Contains("import_errormsg"))
failedData.Columns.Add("import_errormsg");
if (!failedData.Columns.Contains(_importFlag))
failedData.Columns.Add(_importFlag);
for (int i = 0; i < ImportTable.Rows.Count; i++)
{
ImportTable.Rows[i][_importFlag] = "1";
if (ImportReturnName.Count > 0)
{
for (int j = 0; j < ImportReturnName.Count; j++)
{
string name = ImportReturnName[j];
string Value = ImportReturnValue[j];
ImportTable.Rows[i][name] = Value;
}
}
}
//所有导入的主键集合
List<string> parmaryKeys = new List<string>();
//根据全表字段类型进行默认填充
int currRow = 0;
foreach (DataRow dr in ImportTable.Rows)
{
if (SysModel.ImportPrimarykeyVerify && !string.IsNullOrWhiteSpace(parmaryKey))
{
if (parmaryKeys.Contains(dr[parmaryKey]))
{
continue;
}
else
{
parmaryKeys.Add(dr[parmaryKey] + "");
}
}
rowNum = (currRow + 1) + "";
DataRow tmpRow = datatb.NewRow();
foreach (DataColumn dc in datatb.Columns)
{
if (dc.DataType.Name.ToLower() == "string")
tmpRow[dc] = "";
if (dc.DataType.Name.ToLower().IndexOf("char") >= 0)
tmpRow[dc] = "";
if (dc.DataType.Name.ToLower().IndexOf("int") >= 0)
tmpRow[dc] = 0;
if (dc.DataType.Name.ToLower() == "decimal")
tmpRow[dc] = 0;
}
foreach (GridColumn gc in gridControlEx.GridView.Columns)
{
if (_calcFields.IndexOf(gc.FieldName) != -1)
continue;
if (datatb.Columns.Contains(gc.FieldName) && ImportTable.Columns.Contains(gc.FieldName))
{
colName = gc.Caption;
colNum = (gc.VisibleIndex + 1) + "";
GridColumnModel model = gc.Tag as GridColumnModel;
bool isEmpty = false;
if (model != null)
{
isEmpty = model.CanNull;
}
if (model != null && (model.FieldType == ControlType.LabTreeType ||
model.FieldType == ControlType.LabComboxValue ||
model.FieldType == ControlType.LabComboxValueParam ||
model.FieldType == ControlType.LabAutoCompleteValue ||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValue ||
model.FieldType == ControlType.LabMultiSelectValueParam ||
model.FieldType == ControlType.LabAutoGridValue ||
model.FieldType == ControlType.LabSelectReturnId
))
{
string fieldValue = GetValueByImportText(dr, gc.FieldName, dr[gc.FieldName] + "");
if (!string.IsNullOrEmpty(fieldValue))
{
tmpRow[gc.FieldName] = fieldValue;
}
}
else if (model != null && (model.FieldType == ControlType.LabDate ||
model.FieldType == ControlType.LabDateTime ||
model.FieldType == ControlType.LabDateTimeShort ||
model.FieldType == ControlType.LabTime ||
model.FieldType == ControlType.LabShortTime))
{
string fieldValue = ToDateTimeValue(dr[gc.FieldName].ToString().Trim());
if (!string.IsNullOrEmpty(fieldValue))
{
tmpRow[gc.FieldName] = fieldValue;
}
}
else if (model != null && (model.FieldType == ControlType.LabRemark) && !dr[gc.FieldName].ToString().Contains("\r\n"))
{
string fieldValue = dr[gc.FieldName].ToString().Replace("\n", "\r\n");
if (!string.IsNullOrEmpty(fieldValue))
{
tmpRow[gc.FieldName] = fieldValue;
}
}
//排除数值型导入空值的可能
else if (dr[gc.FieldName].ToString().Trim() != "")
{
GridColumn column = gridControlEx.GridView.Columns[gc.FieldName];
if (dr[gc.FieldName].ToString().Trim() == "校验")
tmpRow[gc.FieldName] = "1";
else if (dr[gc.FieldName].ToString().Trim() == "非校验")
tmpRow[gc.FieldName] = "0";
string fieldValue = SystemInfo.Instance.ImportReservedSpaces ? dr[gc.FieldName].ToString() : dr[gc.FieldName].ToString().Trim();
if (fieldValue == "是" || fieldValue == "否")
{
if (column.ColumnEdit != null && column.ColumnEdit.GetType() == typeof(RepositoryItemCheckEdit))
{
tmpRow[gc.FieldName] = "1";
}
else if (column.ColumnType != null && column.ColumnType.Name.ToLower() == "boolean")
{
tmpRow[gc.FieldName] = true;
}
else
{
tmpRow[gc.FieldName] = fieldValue;
}
}
else
{
tmpRow[gc.FieldName] = fieldValue;
}
}
}
}
setDefaultValue(datatb, tmpRow, SysModel.PrefixKey);
//if (mainKeyField != "")
// tmpRow[mainKeyField] = mainKeyValue;
if (SysModel.ConcatenatedPrefix)
{
if (datatb.Columns.Contains(SysModel.PrefixKey + leftField) && !string.IsNullOrWhiteSpace(leftValue) && SysModel.AssociationLeft)
{
if (string.IsNullOrEmpty(tmpRow[SysModel.PrefixKey + leftField] + "") || (tmpRow[SysModel.PrefixKey + leftField] + "").Equals("0"))
{
tmpRow[SysModel.PrefixKey + leftField] = leftValue;
}
}
}
else
{
if (datatb.Columns.Contains(leftField) && !string.IsNullOrWhiteSpace(leftValue) && SysModel.AssociationLeft)
{
if (string.IsNullOrEmpty(tmpRow[SysModel.PrefixKey + leftField] + "") || (tmpRow[SysModel.PrefixKey + leftField] + "").Equals("0"))
{
tmpRow[SysModel.PrefixKey + leftField] = leftValue;
}
}
}
foreach (GridColumn gc in gridControlEx.GridView.VisibleColumns)
{
if (tmpRow.Table.Columns.Contains(gc.FieldName))
{
string fieldValue = tmpRow[gc.FieldName] + "";
GridColumnModel model = gc.Tag as GridColumnModel;
if (model != null)
{
if (model.CanNull && string.IsNullOrEmpty(fieldValue))//必填
{
XtraMessageBox.Show("数据导入错误,数据不能为空,请检查!\r\n第" + (currRow + 1).ToString() + "行,第" + (gc.VisibleIndex + 1).ToString() + "列-" + gc.Caption, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
}
}
datatb.Rows.Add(tmpRow);
try
{
dat.Update(datatb);
datatb.AcceptChanges();
success++;
}
catch (Exception ex)
{
dr.RowError = ex.Message;
dr["import_errormsg"] = ex.Message;
failedData.ImportRow(dr);
datatb.Rows.Remove(tmpRow);
failed++;
//string Message = ErrorMessage.PromptErrorMessage(ex);
//MessageUtil.Show(Message, ex.Message);
break;
}
currRow++;
}
ImportTable.AcceptChanges();
XtraMessageBox.Show("数据导入完成,成功" + success.ToString() + "条,失败" + failed.ToString() + "条", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (failedData.Rows.Count > 0)
{
MessageUtil.Show(failedData.Rows[0]["import_errormsg"].ToString());
string sqlValue2 = string.Format("delete from {0} where {1}='1'", SysModel.MenuTable, _importFlag);
SqlHelper.ExecuteNonQuery(sqlValue2);
}
string sqlValue = string.Format("update {0} set {1}='0' ", SysModel.MenuTable, _importFlag);
sqlValue = string.Format("update {0} set {1}='0' where {1}!='0' ", SysModel.MenuTable, _importFlag);
SqlHelper.ExecuteNonQuery(sqlValue);
DataRow dataRow = datatb.Rows[0];
if (!string.IsNullOrWhiteSpace(SysModel.afterimportSql2) && failed == 0)
{
SqlHelper.ExecuteNonQuery(ReplaceHelper.ReplaceRowParam(dataRow, ReplaceHelper.ReplaceUserInfo(SysModel.afterimportSql2)));
}
datatb.Clear();
LogUtil.WriteDebug(SysModel.ModeCode, "导入数据", SysModel.MenuText, "数据导入,成功" + success.ToString() + "条,失败" + failed.ToString() + "条");
}
catch (Exception ex)
{
string mesage = ex.Message;
string sqlValue = string.Format("delete from {0} where {1}='1'", SysModel.MenuTable, _importFlag);
SqlHelper.ExecuteNonQuery(sqlValue);
foreach (GridColumn gc in gridControlEx.GridView.VisibleColumns)
{
if (ex.Message.Contains(gc.FieldName))
{
mesage = gc.Caption + " 格式不正确,请检查!";
break;
}
}
string errorMsg = string.Format("{0}\r\n第{1}行,第{2}列\r\n列名:{3}", mesage, rowNum, colNum, colName);
XtraMessageBox.Show("数据提交错误:\n" + errorMsg + "\r\n", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return true;
}
return false;
}
/// <summary>
/// 查找当前是否存在指定值
/// </summary>
/// <param name="dtTable"></param>
/// <param name="currentRow"></param>
/// <param name="key"></param>
private static void setDefaultValue(DataTable dtTable, DataRow currentRow,string _qzKey)
{
if ((dtTable.Columns.Contains(_qzKey + "operatorid")) && ((string.IsNullOrEmpty(currentRow[_qzKey + "operatorid"] + "") || currentRow[_qzKey + "operatorid"] + "" == "0")))
currentRow[_qzKey + "operatorid"] = ERPInfo.Instance.UserId;
if ((dtTable.Columns.Contains(_qzKey + "operatorname")) && ((string.IsNullOrEmpty(currentRow[_qzKey + "operatorname"] + "") || currentRow[_qzKey + "operatorname"] + "" == "0")))
currentRow[_qzKey + "operatorname"] = ERPInfo.Instance.UserName;
if ((dtTable.Columns.Contains(_qzKey + "operatedate")) && ((string.IsNullOrEmpty(currentRow[_qzKey + "operatedate"] + "") || currentRow[_qzKey + "operatedate"] + "" == "0")))
currentRow[_qzKey + "operatedate"] = DateTime.Now;
}
/// <summary>
/// <para>说明:计算列关联字段</para>
/// <para>创建人:龚宇超</para>
@@ -2798,6 +3263,7 @@ namespace Lskj.Control.Model
model.FieldType == ControlType.LabAutoCompleteValue ||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
model.FieldType == ControlType.LabMultiSelectValue ||
model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam
|| model.FieldType == ControlType.LabTreeLookValue
|| model.FieldType == ControlType.LabCheckBox
@@ -2871,6 +3337,7 @@ namespace Lskj.Control.Model
model.FieldType == ControlType.LabAutoCompleteValue ||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
model.FieldType == ControlType.LabMultiSelectValue ||
model.FieldType == ControlType.LabMultiSelectValueNew ||
model.FieldType == ControlType.LabMultiSelectValueParam
|| model.FieldType == ControlType.LabTreeLookValue
|| model.FieldType == ControlType.LabCheckBox