using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using Kingdee.BOS.App.Data; using Kingdee.BOS.BusinessEntity; using Kingdee.BOS.Core; using Kingdee.BOS.Core.Bill; using Kingdee.BOS.Core.DynamicForm; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel; using Kingdee.BOS.Core.Interaction; using Kingdee.BOS.Core.Metadata; using Kingdee.BOS.Core.Metadata.FormElement; using Kingdee.BOS.JSON; using Kingdee.BOS.KDThread; using Kingdee.BOS.Log; using Kingdee.BOS.Orm; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.ServiceHelper; using Kingdee.BOS.ServiceHelper.Excel; using Kingdee.BOS.Util; namespace HNJNSH910.GDHNJN.BusinessForm.BusinessPlugIn { [HotUpdate] [Description("加油接口--上传文件")] public class ImportCommon : AbstractDynamicFormPlugIn { private DataTable dtexcel; private string _filePath; private List _dataObjs; private IBillView _billView; private string importObjectId = ""; private string billTypeId = ""; private string btnStr = ""; private List coverBaseNumers; public override void ButtonClick(ButtonClickEventArgs e) { base.ButtonClick(e); importObjectId = View.Model.GetValue("F_PCMT_importObjectId").ToString(); billTypeId = View.Model.GetValue("F_PCMT_billTypeId").ToString(); if ((btnStr = e.Key.ToUpperInvariant()) == null) { return; } string text = btnStr; if (!(text == "F_PCMT_IMPORTDATA")) { if (text == "F_PCMT_COVERIMPORTDATA" && IsNotNull()) { StartAsyncTaskAction(); } } else if (IsNotNull()) { StartAsyncTaskAction(); } } private bool IsNotNull() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown ExcelOperation val = new ExcelOperation(); DataSet val2 = null; val2 = val.ReadFromFile(_filePath, 0, 0); dtexcel = val2.Tables[0]; if (dtexcel == null) { View.ShowMessage("没有找到模板请重新选择!"); return false; } return true; } private void Import() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown ExcelOperation val = new ExcelOperation(); DataSet val2 = null; val2 = val.ReadFromFile(_filePath, 2, 1); dtexcel = val2.Tables[0]; string importIndexStr = GetImportIndexStr(); List list = new List(); for (int i = 0; i < ((InternalDataCollectionBase)dtexcel.Rows).Count; i++) { if (!list.Contains(dtexcel.Rows[i][importIndexStr])) { list.Add(dtexcel.Rows[i][importIndexStr].ToString()); } } _dataObjs = new List(); foreach (string item in list) { DataRow[] array = dtexcel.Select(importIndexStr + "='" + item + "'"); DataTable dt = DataTableExtensions.CopyToDataTable((IEnumerable)array); ImportData(dt, importObjectId, billTypeId); } OperateOption operateOption = OperateOption.Create(); int num = _dataObjs.Count / 1000 + ((_dataObjs.Count % 1000 != 0) ? 1 : 0); IOperationResult operationResult = null; DynamicObject[] arrObjs = _dataObjs.ToArray(); for (int j = 0; j < num; j++) { DynamicObject[] currentObjs = GetCurrentObjs(arrObjs, j, 1000); IOperationResult operationResult2 = BusinessDataServiceHelper.Save(base.Context, _billView.BillBusinessInfo, currentObjs, operateOption, "Save"); Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is true."); if (operationResult == null) { operationResult = operationResult2; Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is success."); } else { operationResult.MergeResult(operationResult2); Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is success."); } } if (operationResult == null) { View.ShowErrMessage("未知原因导致单据保存失败!"); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult == null."); return; } if (operationResult.IsSuccess) { View.ShowOperateResult(operationResult.OperateResult); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsSuccess == true."); return; } if (operationResult.InteractionContext != null && operationResult.InteractionContext.Option.GetInteractionFlag().Count > 0) { Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.InteractionContext != null."); } if (operationResult.IsShowMessage) { Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsShowMessage."); operationResult.MergeValidateErrors(); View.ShowOperateResult(operationResult.OperateResult); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsShowMessage执行完成."); } else { View.ShowMessage("上传完成"); } } private void CoverImport() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown ExcelOperation val = new ExcelOperation(); DataSet val2 = null; val2 = val.ReadFromFile(_filePath, 2, 1); dtexcel = val2.Tables[0]; string importIndexStr = GetImportIndexStr(); List list = new List(); for (int i = 0; i < ((InternalDataCollectionBase)dtexcel.Rows).Count; i++) { if (!list.Contains(dtexcel.Rows[i][importIndexStr])) { list.Add(dtexcel.Rows[i][importIndexStr].ToString()); } } _dataObjs = new List(); coverBaseNumers = new List(); foreach (string item in list) { DataRow[] array = dtexcel.Select(importIndexStr + "='" + item + "'"); DynamicObjectCollection dynamicObjectCollection = CoverImportData(item, array); if (dynamicObjectCollection == null || dynamicObjectCollection.Count == 0) { DataTable dt = DataTableExtensions.CopyToDataTable((IEnumerable)array); ImportData(dt, importObjectId, billTypeId); } } OperateOption operateOption = OperateOption.Create(); IOperationResult operationResult = null; int num = _dataObjs.Count / 1000 + ((_dataObjs.Count % 1000 != 0) ? 1 : 0); DynamicObject[] arrObjs = _dataObjs.ToArray(); for (int j = 0; j < num; j++) { DynamicObject[] currentObjs = GetCurrentObjs(arrObjs, j, 1000); IOperationResult operationResult2 = BusinessDataServiceHelper.Save(base.Context, _billView.BillBusinessInfo, currentObjs, operateOption, "Save"); Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is true."); if (operationResult == null) { operationResult = operationResult2; Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is success."); } else { operationResult.MergeResult(operationResult2); Logger.Info(importObjectId + " " + billTypeId + " 第" + (j + 1) + "批导入执行Save操作", "is success."); } } IOperationResult operationResult3 = null; bool flag = false; if (coverBaseNumers.Count != 0) { string text = ""; foreach (string coverBaseNumer in coverBaseNumers) { if (operationResult3 == null) { text = text + "编码" + coverBaseNumer + ":修改覆盖完成!" + Environment.NewLine; } else { operationResult3.OperateResult.Add(new OperateResult { Name = coverBaseNumer, Message = "覆盖完成", MessageType = MessageType.Normal, SuccessStatus = true }); operationResult.MergeResult(operationResult3); } } flag = true; if (operationResult3 == null) { View.ShowMessage(text); } } if (operationResult == null) { if (!flag) { View.ShowErrMessage("未知原因导致单据保存失败!"); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult == null."); return; } } else { if (operationResult.IsSuccess) { View.ShowOperateResult(operationResult.OperateResult); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsSuccess == true."); return; } if (operationResult.InteractionContext != null && operationResult.InteractionContext.Option.GetInteractionFlag().Count > 0) { Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.InteractionContext != null."); } if (operationResult.IsShowMessage) { Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsShowMessage."); operationResult.MergeValidateErrors(); View.ShowOperateResult(operationResult.OperateResult); Logger.Info(importObjectId + " " + billTypeId + "导入执行Save操作", "saveResult.IsShowMessage执行完成."); return; } } View.ShowMessage("上传完成"); } private DynamicObjectCollection CoverImportData(string billNo, DataRow[] dr) { DynamicObjectCollection dynamicObjectCollection = null; switch (importObjectId) { case "BD_CUSTOMER": dynamicObjectCollection = GetCoverBillObjs(billNo, "T_BD_CUSTOMER"); if (dynamicObjectCollection != null && dynamicObjectCollection.Count != 0) { updateCust(dr, billNo); if (!coverBaseNumers.Contains(billNo)) { coverBaseNumers.Add(billNo); } } break; case "BD_MATERIAL": dynamicObjectCollection = GetCoverBillObjs(billNo, "T_BD_MATERIAL"); if (dynamicObjectCollection != null && dynamicObjectCollection.Count != 0) { updateMaterial(dr, billNo); if (!coverBaseNumers.Contains(billNo)) { coverBaseNumers.Add(billNo); } } break; case "WISE_ItemClass_3003": dynamicObjectCollection = GetCoverBillObjs(billNo, "Wise_t_Cust100018"); if (dynamicObjectCollection != null && dynamicObjectCollection.Count != 0) { updateOilCard(dr, billNo); if (!coverBaseNumers.Contains(billNo)) { coverBaseNumers.Add(billNo); } } break; } return dynamicObjectCollection; } private DynamicObjectCollection GetCoverBillObjs(string billNo, string tableName) { List list = new List(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("/*dialect*/select * from {0} where FNUMBER='{1}'", tableName, billNo); return DBUtils.ExecuteDynamicObject(base.Context, stringBuilder.ToString(), null, null, (CommandType)1); } private void updateCust(DataRow[] dr, string billNo) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("/*dialect*/update T_BD_CUSTOMER_L set FNAME='{1}'\r\n from T_BD_CUSTOMER_L a\r\n inner join T_BD_CUSTOMER b on a.FCUSTID=b.FCUSTID\r\n where FNUMBER='{0}';\r\n\r\n update T_BD_CUSTOMER set FADDRESS ='{2}' ,FMODIFYDATE =GETDATE()\r\n from T_BD_CUSTOMER a\r\n where FNUMBER='{0}'", billNo, dr[0]["客户名称"].ToString(), dr[0]["地址"].ToString()); DBUtils.Execute(base.Context, stringBuilder.ToString()); } private void updateMaterial(DataRow[] dr, string billNo) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("/*dialect*/update T_BD_MATERIAL_L set FNAME='{1}' ,FSPECIFICATION='{2}' \r\n from T_BD_MATERIAL_L a \r\n inner join T_BD_MATERIAL b on a.FMATERIALID=b.FMATERIALID \r\n where FNUMBER='{0}';\r\n --update t_BD_MaterialBase set FBASEUNITID=(select FUNITID from T_BD_UNIT_L where FNAME='xx' )\r\n --from t_BD_MaterialBase a\r\n --inner join T_BD_MATERIAL b on a.FMATERIALID=b.FMATERIALID \r\n --where b.FNUMBER='xx' ;", billNo, dr[0]["名称"].ToString(), dr[0]["规格型号"].ToString()); DBUtils.Execute(base.Context, stringBuilder.ToString()); } private void updateOilCard(DataRow[] dr, string billNo) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("/*dialect*/update Wise_t_Cust100018 set F_103='{1}' ,F_104='{2}' \r\n from Wise_t_Cust100018 a \r\n where FNUMBER='{0}'", billNo, dr[0]["车牌号码"].ToString(), dr[0]["客户代码"].ToString()); DBUtils.Execute(base.Context, stringBuilder.ToString()); } private DynamicObject[] GetCurrentObjs(DynamicObject[] arrObjs, int startIndexBatch, int count) { if (arrObjs.Length < count) { return arrObjs; } int num = startIndexBatch * count; int num2 = num + (count - 1); if (arrObjs.Length < num2) { num2 = arrObjs.Length - 1; } Logger.Info("分批导入序号:", num + " -" + num2); DynamicObject[] array = new DynamicObject[num2 - num + 1]; for (int i = 0; i < num2 - num + 1; i++) { array[i] = arrObjs[num + i]; } return array; } private string GetImportIndexStr() { string result = ""; switch (importObjectId) { case "BD_MATERIAL": result = "代码"; break; case "BD_CUSTOMER": result = "客户代码"; break; case "WISE_ItemClass_3003": result = "卡片代码"; break; case "BD_Supplier": result = "代码"; break; case "SAL_OUTSTOCK": if (billTypeId == "SAL_OUTSTOCK_OIL") { result = "油单号"; } if (billTypeId == "SAL_OUTSTOCK_SHOP") { result = "单据编号"; } break; case "STK_INSTOCK": result = "单据编号"; break; } return result; } public override void CustomEvents(CustomEventsArgs e) { if (e.Key.EqualsIgnoreCase("F_PCMT_FileUpdate")) { if (e.EventName.EqualsIgnoreCase("FILECHANGED")) { JSONObject jSONObject = KDObjectConverter.DeserializeObject(e.EventArgs); if (jSONObject != null) { JSONArray jSONArray = new JSONArray(jSONObject["NewValue"].ToString()); if (jSONArray.Count > 0) { string text = (jSONArray[0] as Dictionary)["ServerFileName"].ToString(); if (CheckFile(text)) { _filePath = GetFilePath(text); EnableButton("F_PCMT_ImportData", bEnable: true); EnableButton("F_PCMT_CoverImportData", bEnable: true); } } else { EnableButton("F_PCMT_ImportData", bEnable: false); EnableButton("F_PCMT_CoverImportData", bEnable: false); } } } else if (e.EventName.EqualsIgnoreCase("STATECHANGED")) { JSONObject jSONObject2 = KDObjectConverter.DeserializeObject(e.EventArgs); if (jSONObject2["State"].ToString() != "2") { EnableButton("F_PCMT_ImportData", bEnable: false); EnableButton("F_PCMT_CoverImportData", bEnable: false); } } } base.CustomEvents(e); } private bool CheckFile(string fileName) { bool flag = false; string[] array = fileName.Split('.'); if (array.Length == 2 && (array[1].EqualsIgnoreCase("xls") || array[1].EqualsIgnoreCase("xlsx"))) { flag = true; } if (!flag) { View.ShowWarnningMessage("请选择正确的文件进行引入。"); } return flag; } private void EnableButton(string key, bool bEnable) { View.GetControl