自己写了一个自定义的webapi接口,调用的时候代码走到调用系统标准的保存服务时报错,提示“给定关键字不在字典中。”
[code]using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Const;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.Operation;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.WebApi.ServicesStub;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
namespace JrDev.Bos.WebApi.Jdscrkd.ServicePlugIn
{
[Description("后处理订单下推简单生产入库单")]
public class Cls_Push : AbstractWebApiBusinessService
{
public Cls_Push(KDServiceContext context)
: base(context)
{
}
public string ExecuteService(string json)
{
string resultMsg = "";
try
{
string finid = "";
if (json != null)
{
Dictionary
finid = dic["fid"];
}
else
{
finid = this.KDContext.RequestExtractor.GetFormValue("fid");
}
//-------获取生产入库中间表信息begin---------//
string sql1 = "/*dialect*/select nvl(cp.FYDID,0) fsourceBillId,t1.fbizdate,nvl(t1.fstockerUserId,0) fstockerUserId,nvl(t1.FAUDITERUSERID,0) FAUDITERUSERID,nvl(t1.forgid,0) forgid, ";
sql1 += " nvl(hcl.fdept,0) fdept from JR_PDA_MANUFACTURERECBILL t1 left join jr_cpbzdj cp on t1.fsourceBillnumber = cp.fbillno left join Jr_Hcldd hcl on cp.FYDID = hcl.fid";
sql1 += " where t1.id = " + finid;
DataTable billHead = QueryResult(sql1);
//-------获取生产入库中间表信息end---------//
//-------获取生产入库分录中间表信息begin---------//
string sql2 = "/*dialect*/select nvl(t1.fwarehouseId,0) fwarehouseId,nvl(t1.flocationId,0) flocationId,nvl(t1.fqty,0) fqty,nvl(t1.fmaterialId,0) fmaterialId,nvl(t1.flot,' ') flot,";
sql2 += " nvl(t1.fbarcode,' ') fbarcode,nvl(t1.fbaseqty,0) fbaseqty,nvl(t1.fassistqty,0) fassistqty from JR_PDA_MANUFACTURERECBILLENTRY t1 where t1.parentid = " + finid;
DataTable billEntry = QueryResult(sql2);
//-------获取生产入库分录中间表信息end---------//
//-------获取生产入库分录中间表汇总信息begin---------//
string sql3 = "/*dialect*/select nvl(t1.fwarehouseId,0) fwarehouseId,nvl(t1.flocationId,0) flocationId,sum(nvl(t1.fqty,0)) fqty from JR_PDA_MANUFACTURERECBILLENTRY t1 ";
sql3 += " where t1.parentid = " + finid;
sql3 += " group by t1.fwarehouseId,t1.flocationId";
DataTable billEntry_sum = QueryResult(sql3);
//-------获取生产入库分录中间表汇总信息end---------//
if (billHead.Rows.Count > 0)
{
//--------调用服务插件实现下推功能begin---------------------------//
ClassLibrary.App.ServicePlugIn.CustPushTestPlugIn pushTest = new ClassLibrary.App.ServicePlugIn.CustPushTestPlugIn();
string source = "XA_HCLDD";//后处理订单单据标识
string target = "SP_InStock";//简单生产入库单标识
if(billHead.Rows[0]["fsourceBillId"].ToString() == "0")
{
Dictionary
error.Add("IsSuccess", "false");
error.Add("fid", "0");
error.Add("fnumber", "");
error.Add("msg", "未找到相应的源单信息!");
resultMsg = JsonConvert.SerializeObject(error);
}
else
{
string getSourceSql = "select FID from Jr_Hcldd where FID = " + billHead.Rows[0]["fsourceBillId"];//sql语句,通过系统方法获取下推数据
string targetBillTypeId = "JDSCRK01_SYS";//简单生产入库单上单据类型id
IOperationResult result = pushTest.AutoPush(this.KDContext.Session.AppContext, source, target, getSourceSql, targetBillTypeId, billHead, billEntry, billEntry_sum);
Dictionary
if (result.IsSuccess)
{
OperateResultCollection opc = result.OperateResult;//获取操作结果中的信息
OperateResult op = opc[0];//获取操作结果对象
string msg = op.Message;//操作结果对象中的信息
string fid = op.PKValue.ToString();//操作成功是返回的单据主键和单据编号
string fnumber = op.Number;
reDic.Add("IsSuccess", "true");
reDic.Add("fid", fid);
reDic.Add("fnumber", fnumber);
reDic.Add("msg", msg);
}
else
{
string vm = "";
List
for (int i = 0; i < rv.Count; i++)
{
ValidationErrorInfo v = rv[i];
vm += v.Message;
}
reDic.Add("IsSuccess", "false");
reDic.Add("fid", "0");
reDic.Add("fnumber", "");
reDic.Add("msg", vm);
}
resultMsg = JsonConvert.SerializeObject(reDic);
//--------调用服务插件实现下推功能end---------------------------//
}
}
else
{
//--------自定义错误信息用于返回begin---------------------------//
Dictionary
error.Add("IsSuccess", "false");
error.Add("fid", "0");
error.Add("fnumber", "");
error.Add("msg", "未找到相应的条码信息");
resultMsg = JsonConvert.SerializeObject(error);
//--------自定义错误信息用于返回begin---------------------------//
}
}
catch (Exception e)
{
Dictionary
error.Add("IsSuccess", "false");
error.Add("fid", "0");
error.Add("fnumber", "");
error.Add("msg", e.Message);
resultMsg = JsonConvert.SerializeObject(error);
}
this.KDContext.WebContext.Write(resultMsg);
return resultMsg;
}
public DataTable QueryResult(string sql)
{
DataTable dt = DBServiceHelper.ExecuteDataSet(this.KDContext.Session.AppContext, sql).Tables[0];
return dt;
}
}
}
//服务端插件代码
//销售退货单 下推 应收单 服务端代码(Cloud5.0)
//新建立.net 工程
//引用组件 Kingdee.BOS.dll、Kingdee.BOS.Core.dll、Kingdee.BOS.DataEntity.dll、Kingdee.BOS.App.dll、Kingdee.BOS.Contracts.dll
//wanghl 2015-03-24
namespace ClassLibrary.App.ServicePlugIn
{
///
/// 下推 单据转换 测试
///
[Description("服务端插件")]
public class CustPushTestPlugIn : AbstractOperationServicePlugIn
{
///
/// 下推 单据转换
///
/// 源单 业务对象标识
/// 目标单 业务对象标识
/// 源单 查询语句
/// 目标单 转换目标类型
///
public IOperationResult AutoPush(Context context, string source, string target, string getSourceSql, string sargetBillTypeId, DataTable billHead, DataTable billEntry, DataTable billEntry_sum)
{
IOperationResult result = new OperationResult();
//获取单据转换规则
List
ConvertRuleElement ruleElement = null;
foreach (ConvertRuleElement rule in ruleList)
{
if (rule.OriginKey == "8607e756-7f53-4219-aefb-95d1536e3dda")//单据转换规则唯一标识
{
ruleElement = rule;
}
}
//如下代码 直接通过查询数据库获取单据转换源单数据
ListSelectedRowCollection rows = new ListSelectedRowCollection();
int i = 0;
using (IDataReader reader = DBUtils.ExecuteReader(context, getSourceSql))
{
while (reader.Read())
{
ListSelectedRow row = new ListSelectedRow(reader["FID"].ToString(), string.Empty, i++, source);
rows.Add(row);
}
}
PushArgs pushArgs = new PushArgs(ruleElement, rows.ToArray());
pushArgs.TargetBillTypeId = sargetBillTypeId;
//转换生成目标单
ConvertOperationResult convertResult = ServiceHelper.GetService
DynamicObject[] destObjs = convertResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();
//获取数据元对象,修改其中数据
for (int k = 0; k < destObjs.Count(); k++)
{
IViewService viewService = ServiceHelper.GetService
//获取元数据服务
IMetaDataService metadataService = ServiceHelper.GetService
DynamicObject doj = destObjs[k];
doj["OwnerId_Id"] = billHead.Rows[0]["forgid"];
doj["StockOrgId_Id"] = billHead.Rows[0]["forgid"];
//注释时不会报错,一起用就会报错
DynamicObject st = doj["StockOrgId"] as DynamicObject;
DynamicObject pod = st.Clone(false) as DynamicObject;
pod["Id"] = billHead.Rows[0]["forgid"].ToString();
doj["PrdOrgId"] = st;
doj["PrdOrgId_Id"] = billHead.Rows[0]["forgid"].ToString();
//DynamicObjectCollection po = doj["PrdOrgId"] as DynamicObjectCollection;
//po[0]["Id"] = billHead.Rows[0]["forgid"].ToString();
//if (billHead.Rows[0]["forgid"].ToString() != "0")
//{
// //获取org元数据
// FormMetadata org = metadataService.Load(context, "ORG_Organizations") as FormMetadata;
// //构建过滤条件
// string forgid = billHead.Rows[0]["forgid"].ToString();
// QueryBuilderParemeter queryParameter = new QueryBuilderParemeter();
// queryParameter.BusinessInfo = org.BusinessInfo;
// queryParameter.FilterClauseWihtKey = "forgid = " + forgid;
// //赋值:
// DynamicObject[] orgObj = viewService.Load(
// context,
// org.BusinessInfo.GetDynamicObjectType(),
// queryParameter);
// if (orgObj.Count() > 0)
// {
// doj["PrdOrgId"] = orgObj[0];
// }
// doj["PrdOrgId_Id"] = billHead.Rows[0]["forgid"].ToString();
//}
//doj["PrdOrgId_Id"] = "1";
doj["BillType_Id"] = "JDSCRK01_SYS";
DynamicObjectCollection entry0 = (DynamicObjectCollection)doj["Entity"];
for (int l = 0; l < entry0.Count; l++)
{
DynamicObject doje = entry0[l];
for (int m = 0; m < billEntry_sum.Rows.Count; m++)
{
if (billHead.Rows[0]["fdept"].ToString() != "0")
{
//获取sksh元数据
FormMetadata stock = metadataService.Load(context, "BD_Department") as FormMetadata;
//构建过滤条件
string fdeptid = billHead.Rows[0]["fdept"].ToString();
QueryBuilderParemeter queryParameter = new QueryBuilderParemeter();
queryParameter.BusinessInfo = stock.BusinessInfo;
queryParameter.FilterClauseWihtKey = "FDEPTID = " + fdeptid;
//赋值:
DynamicObject[] sdeptObj = viewService.Load(
context,
stock.BusinessInfo.GetDynamicObjectType(),
queryParameter);
if (sdeptObj.Count() > 0)
{
doje["WorkShopId"] = sdeptObj[0];
}
doje["WorkShopId_Id"] = billHead.Rows[0]["fdept"].ToString();
}
if (billEntry_sum.Rows[m]["fwarehouseId"].ToString() != "0")
{
FormMetadata stock = metadataService.Load(context, "BD_STOCK") as FormMetadata;
//构建过滤条件
string fstid = Convert.ToString(billEntry_sum.Rows[m]["fwarehouseId"].ToString());
QueryBuilderParemeter queryParameter = new QueryBuilderParemeter();
queryParameter.BusinessInfo = stock.BusinessInfo;
queryParameter.FilterClauseWihtKey = "FSTOCKID = " + fstid;
//赋值:
DynamicObject[] stockObj = viewService.Load(
context,
stock.BusinessInfo.GetDynamicObjectType(),
queryParameter);
if (stockObj.Count() > 0)
{
doje["StockId"] = stockObj[0];
}
doje["StockId_Id"] = billEntry_sum.Rows[m]["fwarehouseId"].ToString();
}
if (billEntry_sum.Rows[m]["flocationId"].ToString() != "0")
{
FormMetadata stockLoc = metadataService.Load(context, "BD_FLEXVALUESDETAIL") as FormMetadata;
string fslid = Convert.ToString(billEntry_sum.Rows[m]["flocationId"].ToString());
QueryBuilderParemeter queryParameter = new QueryBuilderParemeter();
queryParameter = new QueryBuilderParemeter();
queryParameter.BusinessInfo = stockLoc.BusinessInfo;
queryParameter.FilterClauseWihtKey = "FID = " + fslid;
//赋值:
DynamicObject[] stockLocObj = viewService.Load(
context,
stockLoc.BusinessInfo.GetDynamicObjectType(),
queryParameter);
if (stockLocObj.Count() > 0)
{
doje["StockLocId"] = stockLocObj[0];
}
doje["StockLocId_Id"] = billEntry_sum.Rows[m]["flocationId"].ToString();
}
doje["RealQty"] = billEntry_sum.Rows[m]["fqty"].ToString();
doje["StockStatusId_Id"] = "10000";
}
}
DynamicObjectCollection entry1 = (DynamicObjectCollection)doj["Jr_Sp_RkxxEntry"];
DynamicObject rkEntry1 = entry1[0];
if (billEntry.Rows.Count > 0)
{
if (billEntry.Rows.Count > 1)
{
for (int num = 1; num < billEntry.Rows.Count; num++)
{
DynamicObject cp = rkEntry1.Clone(false, true) as DynamicObject;
cp["Seq"] = num + 1;
entry1.Add(cp);
}
}
for (int num = 0; num < billEntry.Rows.Count; num++)
{
string fstockid = "";
string fstocklocid = "";
string ftm = "";
string flot = "";
string fqty = "0";
string fbaseqty = "0";
string fassistqty = "0";
Dictionary
fstockid = billEntry.Rows[num]["fwarehouseId"].ToString();
fstocklocid = billEntry.Rows[num]["flocationId"].ToString();
ftm = billEntry.Rows[num]["fbarcode"].ToString();
flot = billEntry.Rows[num]["flot"].ToString();
fqty = billEntry.Rows[num]["fqty"].ToString();
fbaseqty = billEntry.Rows[num]["fbaseqty"].ToString();
fassistqty = billEntry.Rows[num]["fassistqty"].ToString();
dic.Add("fstockid", fstockid);
dic.Add("fstocklocid", fstocklocid);
dic.Add("ftm", ftm);
dic.Add("flot", flot);
dic.Add("fqty", fqty);
dic.Add("fbaseqty", fbaseqty);
dic.Add("fassistqty", fassistqty);
//-------获取成品包装登记单信息begin---------//
string sql_rk = "/*dialect*/select nvl(t1.fbillno, ' ') fbillno,nvl(t2.fentryid, 0) fentryid,nvl(t1.fmaterialid,0) fmaterialid,nvl(t1.fauxpropid,0) fauxpropid,";
sql_rk += " nvl(t1.fkhsh,' ') fkhsh,nvl(t1.fqrs,' ') fqrs,nvl(t1.fgh,' ') fgh,nvl(t1.flot,' ') flot,nvl(t2.fmz, 0) fmz,nvl(t2.ftzs, 0) ftzs,nvl(t2.ftzzl, 0) ftzzl,";
sql_rk += " nvl(t2.fpz, 0) fpz,nvl(t2.fjz, 0) fjz,nvl(t2.fgdhc, 0) fgdhc,nvl(t2.fschc, 0) fschc,nvl(t2.fbz, 0) fbz,nvl(t2.fnote, ' ') fnote";
sql_rk += " from jr_cpbzdj t1 left join jr_cpbzdj_entry t2 on t1.fid = t2.fid where t2.fxh = '" + billEntry.Rows[num]["fbarcode"].ToString() + "'";
DataTable rkDt = QueryResult(context, sql_rk);
//-------获取成品包装登记单信息end---------//
if (rkDt.Rows.Count > 0)
{
this.setRkEntry(entry1[num], rkDt, dic);
}
}
}
}
//合并转换操作结果
//result.MergeResult(convertResult);
//目标单元数据
FormMetadata destFormMetadata = ServiceHelper.GetService
//保存目标单据
IOperationResult saveResult = ServiceHelper.GetService
//合并保存操作结果
//result.MergeResult(saveResult);
//根据操作结果构造返回结果
//if ((result.ValidationErrors != null && result.ValidationErrors.Count > 0))
//{
// result.IsSuccess = false;
//}
return saveResult;
}
public DynamicObject setRkEntry(DynamicObject entry, DataTable dt, Dictionary
{
entry["FCpBzNo"] = dt.Rows[0]["fbillno"].ToString();
entry["FCpBzId"] = dt.Rows[0]["fentryid"].ToString();
entry["FBoxNumber"] = dic["ftm"];
entry["FStockIdX_Id"] = dic["fstockid"];
entry["FSpId_Id"] = dic["fstocklocid"];
entry["FBatchno"] = dt.Rows[0]["flot"].ToString();
entry["FMatId_Id"] = dt.Rows[0]["fmaterialid"].ToString();
entry["FAuxpropIdX_Id"] = dt.Rows[0]["fauxpropid"].ToString();
entry["FKhSh"] = dt.Rows[0]["fkhsh"].ToString();
entry["FQrs"] = dt.Rows[0]["fqrs"].ToString();
entry["FMz"] = dt.Rows[0]["fmz"].ToString();
entry["FTzs"] = dic["fassistqty"];
entry["FTzzl"] = dt.Rows[0]["ftzzl"].ToString();
entry["FPz"] = dt.Rows[0]["fpz"].ToString();
entry["FJzx"] = dic["fbaseqty"];
entry["FGdhc"] = dt.Rows[0]["fgdhc"].ToString();
entry["FSchc"] = dt.Rows[0]["fschc"].ToString();
entry["FBz"] = dic["fqty"];
entry["FNote"] = dt.Rows[0]["fnote"].ToString();
return entry;
}
public DataTable QueryResult(Context context, string sql)
{
DataTable dt = DBServiceHelper.ExecuteDataSet(context, sql).Tables[0];
return dt;
}
}
}[/code]
推荐阅读