实现目的:

在金蝶ERP采购申请列表界面,勾选某些行采购申请物料信息后,点击按钮推送到第三方系统。

实现思路:

通过WebService方式实现两个系统间的数据交互。

实现过程:

1、新建项目,然后通过Visual Studio IDE工具解析第三方WebService服务端地址,具体参照网上教程。

2、项目的app.config配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BatchInsertPendingPurchaseDataServiceSoap" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://192.168.23.250/BatchInsertPendingPurchaseDataService.asmx"
                binding="basicHttpBinding" bindingConfiguration="BatchInsertPendingPurchaseDataServiceSoap"
                contract="WebServiceClient.BatchInsertPendingPurchaseDataServiceSoap"
                name="BatchInsertPendingPurchaseDataServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

3、创建WebService客户端调用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using K3Cloud.Purchase.Requisition.WebServiceClient;

namespace K3Cloud.Purchase.Requisition
{
    public class BachInsertPendingPurchaseDataClient
    {
        public static string BachInsertData(List<DataItem> dataItems)
        {
            try
            {
                string Url = "http://192.168.23.250/BatchInsertPendingPurchaseDataService.asmx";

                // 动态创建绑定和终结点
                var binding = new BasicHttpBinding();
                var endpoint = new EndpointAddress(Url);

                // 使用动态配置创建客户端
                using (var client = new BatchInsertPendingPurchaseDataServiceSoapClient(binding, endpoint))
                {
                    return client.BatchInsertData(dataItems.ToArray());
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine($"调用WebService时发生错误: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"内部错误: {ex.InnerException.Message}");
                }
                return ex.Message;
            }

        }
    }
}

4、新建列表插件,获取选中的列表行数据,然后调用WebService传输数据:

using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;

using Kingdee.BOS.Core.List;

using Kingdee.BOS.Core.List.PlugIn;

using Kingdee.BOS.Util;
using System.Collections.Generic;
using System.ComponentModel;

using System.Linq;
using System.Data;
using K3Cloud.Purchase.Requisition.TRMWebServiceClient;
using System;



namespace K3Cloud.Purchase.Requisition
{
    public class PushPurchaseRequisition:AbstractListPlugIn
    {

        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);

            if (e.BarItemKey.EqualsIgnoreCase("tbGetListSelectedRows"))
            {
                var lv = this.View as IListView;
                if (lv == null)
                {
                    return;
                }

                ListSelectedRowCollection selectedRows = lv.SelectedRowsInfo;
                if (selectedRows == null || selectedRows.Count == 0)
                {
                    this.View.ShowMessage("当前没有行被选中!");
                    return;
                }

                // 1. 收集所有选中行的单据体主键(FEntryID)
                var entryIds = selectedRows.Select(item => item.EntryPrimaryKeyValue).ToList();

                // 2. 构建批量查询SQL
                var sql = "SELECT * FROM vw_PUR_Requisition_y WHERE LineNumber IN ({0})";
                var paramNames = new List<string>();
                var sqlParams = new List<SqlParam>();

                for (int i = 0; i < entryIds.Count; i++)
                {
                    var paramName = $"@id{i}";
                    paramNames.Add(paramName);
                    sqlParams.Add(new SqlParam(paramName, KDDbType.Int64, entryIds[i]));
                }

                sql = string.Format(sql, string.Join(",", paramNames));

                // 3. 执行查询
                var ds = DBUtils.ExecuteDataSet(this.Context, sql, sqlParams);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    this.View.ShowMessage("未查询到选中的数据!");
                    return;
                }

                // 4. 转换为DataItem列表
                List<DataItem> dataItems = new List<DataItem>();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    dataItems.Add(new DataItem
                    {
                        MaterialCode = row["MaterialCode"].ToString(),
                        MaterialName = row["MaterialName"].ToString(),
                        SpecModel = row["SpecModel"].ToString(),
                        Unit = row["Unit"].ToString(),
                        Level1 = row["Level1"].ToString(),
                        PurchaseQuantity = Convert.ToDecimal(row["PurchaseQuantity"]), // 避免直接强制转换失败
                        ArrivalDate = Convert.ToDateTime(row["ArriveDate"]),
                    });
                }

                // 5. 调用WebService批量插入
                string res = BachInsertPendingPurchaseDataClient.BachInsertData(dataItems);
                this.View.ShowMessage(res);
            }
        }

    }
}

5、在BOS平台新增列表操作按钮、新增绑定列表插件,重启后可在采购申请列表页查看新增的按钮,在列表选中行数据后,点击该按钮即可查看效果。

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐