You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.6 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Forks.JsonRpc.Client;
using Forks.JsonRpc.Client.Data;
namespace RpcHelper {
public static class RpcHelperUtil {
private static bool _isInit;
public static string InsertGathering(string data) {
//测试用的
if (data == "ceshi")
{
using (var sr = new StreamReader("D:\\测试报文.txt"))
{
data = sr.ReadToEnd();
}
}
var logName = DateTime.Today.Date.ToString("yyyyMMdd") + "Gatheringlog.txt";
var results = new Results();
// var path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
var path = "D:\\zhifutonglog\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string resultStr;
using (var textWriter = new StreamWriter(path + logName, true, Encoding.UTF8)) {
textWriter.WriteLine("{0} 收款单接口开始接收数据:------------------------------------", DateTime.Now);
textWriter.WriteLine(data);
Console.WriteLine("{0}:开始接受数据:", DateTime.Now);
Console.WriteLine(data);
try {
var dmo = ParseXmlToMsg<Gatherings>(data);
if (!_isInit) {
var url = System.Configuration.ConfigurationManager.AppSettings["url"];
RpcFacade.Init(url, "QingDaoWanFuRpc");
_isInit = true;
}
var username = System.Configuration.ConfigurationManager.AppSettings["username"];
var strPwd = System.Configuration.ConfigurationManager.AppSettings["pwd"];
RpcFacade.Login(username, strPwd);
foreach (var bill in dmo.Bills) {
var r = new Result();
r.RequestId = bill.Head.RequestId;
textWriter.WriteLine("开始处理单据:" + r.RequestId);
Console.WriteLine("开始处理单据:" + r.RequestId);
results.SendResult.Add(r);
var gathering = new RpcObject("/MainSystem/B3Sale/BO/Gathering");
SetString(gathering, "AccountCustomer_OuterCode", bill.Head.CustomerCode.Trim());// 客户的外部编码
SetDateTime(gathering, "GatheringTime", bill.Head.Time);
SetDecimal(gathering, "GatheringMoney", bill.Head.Money);
try {
var id = RpcFacade.Call<long>("/MainSystem/B3QingDaoWanFu/Rpc/GatheringRpc/Insert", gathering);
textWriter.WriteLine("成功创建收款单No." + id);
r.Content = "成功创建收款单No." + id;
r.Resultcode = "0";
r.Bwpid = id.ToString();
} catch (Exception e1) {
r.Resultcode = "1";
r.Content = "调用错误,原因:" + e1.Message;
textWriter.WriteLine("内部错误:" + e1.Message);
Console.WriteLine("内部错误:" + e1.Message);
}
}
} catch (Exception e) {
results.Resultcode = "1";
results.Content = "调用错误,原因:" + e.Message;
textWriter.WriteLine("内部错误:" + e.Message);
Console.WriteLine("内部错误:" + e.Message);
} finally {
RpcFacade.Logout();
}
if (results.SendResult.Count > 0 && results.SendResult.All(x => x.Resultcode == "0")) {
results.Resultcode = "0";
} else {
results.Resultcode = "1";
}
resultStr = ObjToXml(results, Formatting.Indented, Encoding.GetEncoding("gb2312"));
textWriter.WriteLine("返回报文:" + resultStr);
textWriter.WriteLine("{0} 收款单接口处理完毕 ", DateTime.Now);
}
return resultStr;
}
public static string ObjToXml(object obj, Formatting formatting, Encoding encoding) {
using (var stream = new MemoryStream()) {
using (var writer = new XmlTextWriter(stream, encoding)) {
writer.Formatting = formatting;
writer.IndentChar = '\t';
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
new XmlSerializer(obj.GetType()).Serialize(writer, obj, ns);
}
return encoding.GetString(stream.ToArray());
}
}
public static T ParseXmlToMsg<T>(string xml) {
using (TextReader reader = new StringReader(xml)) {
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
}
}
private static void SetLong(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, long.Parse(value));
}
}
private static void SetDateTime(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, Convert.ToDateTime(value));
}
}
private static void SetDecimal(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, decimal.Parse(value));
}
}
private static void SetInt(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, int.Parse(value));
}
}
private static void SetShort(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, short.Parse(value));
}
}
private static void SetString(RpcObject rpcObject, string name, string value) {
if (!string.IsNullOrEmpty(value)) {
rpcObject.Set(name, value);
}
}
}
}