1、先创建发起请求的类
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace GraspFx.Web.Core.SureWin.HeiHu
{
public class WebUtils
{
private int _timeout = 100000;
public int Timeout
{
get
{
return this._timeout;
}
set
{
this._timeout = value;
}
}
public string DoPost(string url, string data)
{
HttpWebRequest webRequest = this.GetWebRequest(url, "POST");
webRequest.ContentType = "application/json;charset=utf-8";
byte[] bytes = Encoding.UTF8.GetBytes(data);
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
try
{
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
Encoding encoding = Encoding.GetEncoding("UTF-8");
if (httpWebResponse.CharacterSet != null)
{
encoding = Encoding.GetEncoding(httpWebResponse.CharacterSet);
}
return this.GetResponseAsString(httpWebResponse, encoding);
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream sr = response.GetResponseStream())
using (var reader = new StreamReader(sr))
{
string text = reader.ReadToEnd();
return text;
}
}
}
}
public string DoPost(string url, string data, Dictionary<string, string> headerParam)
{
try
{
HttpWebRequest webRequest = this.GetWebRequest(url, "POST");
webRequest.ContentType = "application/json;charset=utf-8";
foreach (var item in headerParam)
{
webRequest.Headers.Add(item.Key, item.Value);
}
byte[] bytes = Encoding.UTF8.GetBytes(data);
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
Encoding encoding = Encoding.GetEncoding("UTF-8");
if (httpWebResponse.CharacterSet != null)
{
encoding = Encoding.GetEncoding(httpWebResponse.CharacterSet);
}
return this.GetResponseAsString(httpWebResponse, encoding);
}
catch (WebException ex)
{
return ex.Message;
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream sr = response.GetResponseStream())
using (var reader = new StreamReader(sr))
{
string text = reader.ReadToEnd();
return text;
}
}
}
}
public string DoPost(string authorization, string url, string data)
{
HttpWebRequest webRequest = this.GetWebRequest(url, "POST");
webRequest.ContentType = "application/json;charset=utf-8";
webRequest.Headers.Add(HttpRequestHeader.Authorization, authorization);
byte[] bytes = Encoding.UTF8.GetBytes(data);
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
try
{
HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
Encoding encoding = Encoding.GetEncoding("UTF-8");
if (httpWebResponse.CharacterSet != null)
{
encoding = Encoding.GetEncoding(httpWebResponse.CharacterSet);
}
return this.GetResponseAsString(httpWebResponse, encoding);
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream sr = response.GetResponseStream())
using (var reader = new StreamReader(sr))
{
string text = reader.ReadToEnd();
return text;
}
}
}
}
public string DoGet(string url)
{
HttpWebRequest webRequest = this.GetWebRequest(url, "GET");
webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
HttpWebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
Encoding uTF = Encoding.UTF8;
return this.GetResponseAsString(rsp, uTF);
}
public string DoGet(string url, Dictionary<string, string> headerParam)
{
HttpWebRequest webRequest = this.GetWebRequest(url, "GET");
webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
foreach (var item in headerParam)
{
webRequest.Headers.Add(item.Key, item.Value);
}
HttpWebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
Encoding uTF = Encoding.UTF8;
return this.GetResponseAsString(rsp, uTF);
}
public HttpWebRequest GetWebRequest(string url, string method)
{
HttpWebRequest httpWebRequest;
if (url.Contains("https"))
{
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
//ServicePointManager.ser
//httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
}
else
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
}
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.Method = method;
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = this._timeout;
return httpWebRequest;
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
//if (sslPolicyErrors == SslPolicyErrors.None) {
// return true;
//}
//Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
Do not allow this client to communicate with unauthenticated servers.
//return false;
return true;
}
public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
Stream stream = null;
StreamReader streamReader = null;
string result;
try
{
stream = rsp.GetResponseStream();
streamReader = new StreamReader(stream, encoding);
result = streamReader.ReadToEnd();
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
if (stream != null)
{
stream.Close();
}
if (rsp != null)
{
rsp.Close();
}
}
return result;
}
}
}
2、再创建一个调用请求方法的类
using Carpa.Web.Script;
using GraspFx.Web.Core.SureWin.HeiHu;
using GraspFx.Web.Core.SureWin.Helper;
using GraspFx.Web.Core.SureWin.Model;
using GraspFx.Web.Core.SureWin.Model.RequestModel;
using GraspFx.Web.Core.SureWin.Model.ResultModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
namespace GraspFx.Web.Core.SureWin
{
public class RequestClient
{
private readonly string tokenCacheKey = "HH-Token";
private readonly string xclient = "lite-web";
private int _time = 1800; //过期时间
private int _minutes = 30; //过期时间
private HeiHuConfig _heihuConfig;
private GjpClient _gjpConfig;
public RequestClient(HeiHuConfig heihuConfig)
{
_heihuConfig = heihuConfig;
_gjpConfig = new GjpClient();
}
/// <summary>
/// 发起请求
/// </summary>
/// <param name="uri"></param>
/// <param name="data"></param>
/// <returns></returns>
private string DoRequest(string uri, object data, HttpMethod method)
{
string strResult = string.Empty;
try
{
//拼接请求地址
string url = (_heihuConfig.ApiUrl.StartsWith("http") ? "" : "http://") + _heihuConfig.ApiUrl + uri;
//返回报文
string result = GetRequesResult(url, data, method);
strResult = result;
return strResult;
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 发起请求
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <returns></returns>
private string GetRequesResult(string url, object data, HttpMethod method)
{
//返回数据
string result = string.Empty;
//json字符串数据
string jsonData = string.Empty;
if (data != null)
{
jsonData = JsonConvert.SerializeObject(data, Formatting.Indented);
}
//请求头数据
Dictionary<string, string> dicHeader = new Dictionary<string, string>();
//token
string token = string.Empty;
TokenResult tokenResult = new TokenResult();
try
{
tokenResult = GetToken();
if (tokenResult.statusCode == "200")
{
token = tokenResult.data;
}
else
{
return "token获取失败:" + tokenResult.message;
}
}
catch (Exception ex)
{
return "token获取异常:" + ex.Message;
}
dicHeader.Add("X-AUTH", token);
if (method == HttpMethod.Get)
{
result = new WebUtils().DoGet(url, dicHeader);
}
else
{
result = new WebUtils().DoPost(url, jsonData, dicHeader);
}
return result;
}
/// <summary>
/// 获取Token
/// </summary>
/// <returns></returns>
private TokenResult GetToken()
{
TokenResult tokenResult = new TokenResult();
try
{
//存在token信息直接返回
string token = string.Empty;
//缓存中取
object obj = HttpRuntime.Cache.Get(tokenCacheKey);
if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
{
tokenResult.statusCode = "200";
tokenResult.data = obj.ToString();
return tokenResult;
}
else
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "获取Token";
//请求地址
string url = (_heihuConfig.ApiUrl.StartsWith("http") ? "" : "http://") + _heihuConfig.ApiUrl + "/api/user/v1/users/_login";
log.Url = url;
//添加请求头参数
Dictionary<string, string> headerParam = new Dictionary<string, string>();
headerParam.Add("x-client", xclient);
//请求数据
HashObject data = new HashObject();
if (_heihuConfig.Type == 0)
{
data["phone"] = _heihuConfig.Phone;
data["password"] = _heihuConfig.PassWord.SHA3_224();
}
else
{
data["code"] = _heihuConfig.Code;
data["username"] = _heihuConfig.Username;
data["type"] = _heihuConfig.Type;
data["password"] = _heihuConfig.PassWord.SHA3_224();
}
//请求报文
string requestData = JsonConvert.SerializeObject(data, Formatting.Indented);
log.RequestSrting = requestData;
//请求接口
string result = new WebUtils().DoPost(url, requestData, headerParam);
log.ResponseString = result;
//json转实体类
try
{
tokenResult = JsonConvert.DeserializeObject<TokenResult>(result);
}
catch (Exception)
{
tokenResult.statusCode = "-1";
tokenResult.message = result;
}
log.Code = tokenResult.statusCode;
log.Message = tokenResult.message;
if (tokenResult.statusCode == "200")
{
//获取到添加到缓存
token = tokenResult.data;
TimeSpan ts = new TimeSpan(0, 0, _time);
HttpRuntime.Cache.Insert(tokenCacheKey, token, null, DateTime.Now.AddMinutes(_minutes), ts);
//添加日志
log.Success = true;
}
else
{
//添加日志
log.Success = false;
tokenResult.message = string.Format("获取Token失败:{0},{1}", tokenResult.statusCode, tokenResult.message);
//抛出异常
//throw new Exception(string.Format("获取Token失败:{0},{1}", tokenResult.statusCode, tokenResult.message));
}
_gjpConfig.AddHeiHuLog(log);
}
}
catch (Exception ex)
{
tokenResult.statusCode = "-1";
tokenResult.message = ex.Message;
}
return tokenResult;
}
/// <summary>
/// 上传新增商品信息
/// </summary>
public BaseResult UploadAddPtype(UploadPtypeRequest data)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传新增商品";
log.RequestSrting = JsonConvert.SerializeObject(data, Formatting.Indented);
//接口地址
string url = string.Format("/api/dytin/external/product/create");
log.Url = url;
//返回报文
string strresult = DoRequest(url, data, HttpMethod.Post);
log.ResponseString = strresult;
//返回数据
BaseResult result = new BaseResult();
try
{
//返回信息转实体类
result = JsonConvert.DeserializeObject<BaseResult>(strresult);
log.Code = result.code;
log.Message = result.msg;
//成功
if (result.code == "01000000")
{
log.Success = true;
}
else
{
log.Success = false;
}
_gjpConfig.AddHeiHuLog(log);
}
catch (Exception e)
{
string message = string.Format("上传新增商品信息失败:{0} 返回信息:{1}", e.Message, strresult);
//自定义错误返回参数 防止直接跳出
result.code = "-1";
result.msg = message;
//添加日志
log.Success = false;
log.Code = result.code;
log.Message = message;
_gjpConfig.AddHeiHuLog(log);
//抛出异常
//throw new Exception(message);
}
return result;
}
/// <summary>
/// 上传修改商品信息
/// </summary>
public BaseResult UploadUpdatePtype(UploadPtypeRequest data)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传修改商品";
log.RequestSrting = JsonConvert.SerializeObject(data, Formatting.Indented);
//接口地址
string url = string.Format("/api/dytin/external/product/update");
log.Url = url;
//返回报文
string strresult = DoRequest(url, data, HttpMethod.Post);
log.ResponseString = strresult;
//返回数据
BaseResult result = new BaseResult();
try
{
//返回信息转实体类
result = JsonConvert.DeserializeObject<BaseResult>(strresult);
log.Code = result.code;
log.Message = result.msg;
//成功
if (result.code == "01000000")
{
log.Success = true;
}
else
{
log.Success = false;
}
_gjpConfig.AddHeiHuLog(log);
}
catch (Exception e)
{
string message = string.Format("上传修改商品信息失败:{0} 返回信息:{1}", e.Message, strresult);
//自定义错误返回参数 防止直接跳出
result.code = "-1";
result.msg = message;
//添加日志
log.Success = false;
log.Code = result.code;
log.Message = message;
_gjpConfig.AddHeiHuLog(log);
//抛出异常
//throw new Exception(message);
}
return result;
}
/// <summary>
/// 上传出入库单信息
/// </summary>
public BaseResult UploadOutInBill(UploadOutInBillRequest data)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传出入库单";
log.RequestSrting = JsonConvert.SerializeObject(data, Formatting.Indented);
//接口地址
string url = string.Format("/api/dytin/external/stock/order/add");
log.Url = url;
//返回报文
string strresult = DoRequest(url, data, HttpMethod.Post);
log.ResponseString = strresult;
//返回数据
BaseResult result = new BaseResult();
try
{
//返回信息转实体类
result = JsonConvert.DeserializeObject<BaseResult>(strresult);
log.Code = result.code;
log.Message = result.msg;
//成功
if (result.code == "01000000")
{
log.Success = true;
}
else
{
log.Success = false;
}
_gjpConfig.AddHeiHuLog(log);
}
catch (Exception e)
{
string message = string.Format("上传出入库单信息失败:{0} 返回信息:{1}", e.Message, strresult);
//自定义错误返回参数 防止直接跳出
result.code = "-1";
result.msg = message;
//添加日志
log.Success = false;
log.Code = result.code;
log.Message = message;
_gjpConfig.AddHeiHuLog(log);
//抛出异常
//throw new Exception(message);
}
return result;
}
/// <summary>
/// 上传新增自定义表单信息
/// </summary>
public BaseResult UploadAddCustomOrder(UploadAddCustomOrderRequest data)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传新增自定义表单";
log.RequestSrting = JsonConvert.SerializeObject(data, Formatting.Indented);
//接口地址
string url = string.Format("/api/dytin/external/custom_form/create");
log.Url = url;
//返回报文
string strresult = DoRequest(url, data, HttpMethod.Post);
log.ResponseString = strresult;
//返回数据
BaseResult result = new BaseResult();
try
{
//返回信息转实体类
result = JsonConvert.DeserializeObject<BaseResult>(strresult);
log.Code = result.code;
log.Message = result.msg;
//成功
if (result.code == "01000000")
{
log.Success = true;
}
else
{
log.Success = false;
}
log.Message = result.msg;
}
catch (Exception e)
{
string message = string.Format("上传新增自定义表单信息失败:{0} 返回信息:{1}", e.Message, strresult);
//自定义错误返回参数 防止直接跳出
result.code = "-1";
result.msg = message;
//添加日志
log.Success = false;
log.Code = result.code;
log.Message = message;
_gjpConfig.AddHeiHuLog(log);
//抛出异常
//throw new Exception(message);
}
return result;
}
/// <summary>
/// 上传修改自定义表单信息
/// </summary>
public BaseResult UploadUpdateCustomOrder(UploadUpdateCustomOrderRequest data)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传修改自定义表单";
log.RequestSrting = JsonConvert.SerializeObject(data, Formatting.Indented);
//接口地址
string url = string.Format("/api/dytin/external/custom_form/item/update");
log.Url = url;
//返回报文
string strresult = DoRequest(url, data, HttpMethod.Post);
log.ResponseString = strresult;
//返回数据
BaseResult result = new BaseResult();
try
{
//返回信息转实体类
result = JsonConvert.DeserializeObject<BaseResult>(strresult);
log.Code = result.code;
log.Message = result.msg;
//成功
if (result.code == "01000000")
{
log.Success = true;
}
else
{
log.Success = false;
}
log.Message = result.msg;
}
catch (Exception e)
{
string message = string.Format("上传修改自定义表单信息失败:{0} 返回信息:{1}", e.Message, strresult);
//自定义错误返回参数 防止直接跳出
result.code = "-1";
result.msg = message;
//添加日志
log.Success = false;
log.Code = result.code;
log.Message = message;
_gjpConfig.AddHeiHuLog(log);
//抛出异常
//throw new Exception(message);
}
return result;
}
}
}
3、创建调用方法的类
using GraspFx.Web.Core.SureWin.Model;
using GraspFx.Web.Core.SureWin.Model.RequestModel;
using GraspFx.Web.Core.SureWin.Model.ResultModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GraspFx.Web.Core.SureWin
{
/// <summary>
/// 黑湖请求API
/// </summary>
public class HeiHuClient
{
private RequestClient _requestClient;
public HeiHuClient(HeiHuConfig config)
{
_requestClient = new RequestClient(config);
}
/// <summary>
/// 上传新增商品
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BaseResult UploadAddPtype(UploadPtypeRequest data)
{
return _requestClient.UploadAddPtype(data);
}
/// <summary>
/// 上传修改商品
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BaseResult UploadUpdatePtype(UploadPtypeRequest data)
{
return _requestClient.UploadUpdatePtype(data);
}
/// <summary>
/// 上传出入库单信息
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BaseResult UploadOutInBill(UploadOutInBillRequest data)
{
return _requestClient.UploadOutInBill(data);
}
/// <summary>
/// 上传新增自定义表单信息
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BaseResult UploadAddCustomOrder(UploadAddCustomOrderRequest data)
{
return _requestClient.UploadAddCustomOrder(data);
}
/// <summary>
/// 上传修改自定义表单信息
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public BaseResult UploadUpdateCustomOrder(UploadUpdateCustomOrderRequest data)
{
return _requestClient.UploadUpdateCustomOrder(data);
}
}
}
4、创建操作数据库的类
using Carpa.Web.Script;
using GraspFx.DbConnection;
using GraspFx.Web.Core.SureWin.Model;
using GraspFx.Web.Core.SureWin.Model.BaseTypeInfo;
using GraspFx.Web.FrameWorks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
namespace GraspFx.Web.Core.SureWin
{
/// <summary>
/// 管家婆方法
/// </summary>
public class GjpClient
{
#region 新增、修改操作
/// <summary>
/// 写入黑湖日志
/// </summary>
/// <param name="operation">操作</param>
/// <param name="code">特定编号,比如单据编号</param>
/// <param name="msg">日志消息</param>
public void AddHeiHuLog(HeiHuLog log)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"INSERT INTO HeiHuLog (RequestTime,Operation,Message,Url,RequestSrting,ResponseString,Success,Code) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}')", log.RequestTime, log.Operation,log.Message,log.Url,log.RequestSrting,log.ResponseString,Convert.ToInt32(log.Success),log.Code);
db.Execute(sql);
}
}
#endregion
#region 查询操作
/// <summary>
/// 查询商品信息
/// </summary>
/// <param name="usercode">编号</param>
/// <returns></returns>
public PtypeInfo QueryPtypeInfo(string usercode)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT P.UserCode,P.FullName,P.Standard,PU.FullName AS UnitName,PP.PreSalePrice1,ISNULL(GS.CostPrice,0) AS CostPrice FROM dbo.Bas_PType P
INNER JOIN dbo.Bas_PType_Price PP ON P.PID = PP.PID
INNER JOIN dbo.Bas_PType_Unit PU ON P.BaseUnitId = PU.UId
LEFT JOIN Main_GoodsStocks GS ON GS.PID = P.PID
WHERE P.UserCode = '{0}'", usercode);
return db.QueryFirstOrDefault<PtypeInfo>(sql);
}
}
/// <summary>
/// 查询采购入库单信息
/// </summary>
/// <param name="billid">id</param>
/// <returns></returns>
public List<BuyBillInfo> QueryBuyBillInfo(string billid)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT BI.BillCode,BI.Posttime,BI.Explain,BB.ShowOrder,P.UserCode AS PUserCode,BB.Qty FROM dbo.Bill_BuyIndex BI
INNER JOIN dbo.Bill_BuyBill BB ON BB.BillNumberId = BI.BillNumberId
INNER JOIN dbo.Bas_PType P ON BB.PID = P.PID
WHERE BI.BillType = 34 AND BI.Draft = 0 AND BI.RedWord = 0 AND BI.BillNumberId = '{0}'", billid);
DataTable dt = db.QueryDataTable(sql);
return ToList<BuyBillInfo>(dt);
}
}
/// <summary>
/// 查询采购退货单信息
/// </summary>
/// <param name="billid">id</param>
/// <returns></returns>
public List<BuyBackBillInfo> QueryBuyBackBillInfo(string billid)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT BI.BillCode,BI.Posttime,BI.Explain,BB.ShowOrder,P.UserCode AS PUserCode,BB.Qty FROM dbo.Bill_BuyIndex BI
INNER JOIN dbo.Bill_BuyBackBill BB ON BB.BillNumberId = BI.BillNumberId
INNER JOIN dbo.Bas_PType P ON BB.PID = P.PID
WHERE BI.BillType = 6 AND BI.Draft = 0 AND BI.RedWord = 0 AND BI.BillNumberId = '{0}'", billid);
DataTable dt = db.QueryDataTable(sql);
return ToList<BuyBackBillInfo>(dt);
}
}
/// <summary>
/// 查询销售出库单信息
/// </summary>
/// <param name="billid">id</param>
/// <returns></returns>
public List<SaleBillInfo> QuerySaleBillInfo(string billid)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT BI.BillCode,BI.Posttime,BI.Explain,BB.ShowOrder,P.UserCode AS PUserCode,BB.Qty FROM dbo.Bill_SaleIndex BI
INNER JOIN dbo.Bill_SaleBill BB ON BB.BillNumberId = BI.BillNumberId
INNER JOIN dbo.Bas_PType P ON BB.PID = P.PID
WHERE BI.BillType = 11 AND BI.Draft = 0 AND BI.RedWord = 0 AND BI.BillNumberId = '{0}'", billid);
DataTable dt = db.QueryDataTable(sql);
return ToList<SaleBillInfo>(dt);
}
}
/// <summary>
/// 查询销售退货单信息
/// </summary>
/// <param name="billid">id</param>
/// <returns></returns>
public List<SaleBackBillInfo> QuerySaleBackBillInfo(string billid)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT BI.BillCode,BI.Posttime,BI.Explain,BB.ShowOrder,P.UserCode AS PUserCode,BB.Qty FROM dbo.Bill_SaleIndex BI
INNER JOIN dbo.Bill_SaleBackBill BB ON BB.BillNumberId = BI.BillNumberId
INNER JOIN dbo.Bas_PType P ON BB.PID = P.PID
WHERE BI.BillType = 45 AND BI.Draft = 0 AND BI.RedWord = 0 AND BI.BillNumberId = '{0}'", billid);
DataTable dt = db.QueryDataTable(sql);
return ToList<SaleBackBillInfo>(dt);
}
}
/// <summary>
/// 查询销售订单信息
/// </summary>
/// <param name="billid">id</param>
/// <returns></returns>
public List<SaleOrderInfo> QuerySaleOrderInfo(string billid)
{
using (DbContext db = IocInstance.Resolve<IDbContextFactory>().Create())
{
string sql = string.Format(@"SELECT BI.BillCode,P.UserCode AS PUserCode,P.typeId AS PtypeId FROM dbo.Bill_OrderIndex BI
INNER JOIN dbo.Bill_OrderBill BB ON BB.BillNumberId = BI.BillNumberId
INNER JOIN dbo.Bas_PType P ON BB.PID = P.PID
WHERE BI.BillType = 300 AND BI.BillNumberId = '{0}'", billid);
DataTable dt = db.QueryDataTable(sql);
return ToList<SaleOrderInfo>(dt);
}
}
#endregion
#region 工具
/// <summary>
/// DataTable转List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public List<T> ToList<T>(DataTable dt) where T : class, new()
{
List<T> list = new List<T>();
//确认参数有效,若无效则返回Null
if (dt == null)
return list;
else if (dt.Rows.Count == 0)
return list;
Dictionary<string, string> dicField = new Dictionary<string, string>();
Dictionary<string, string> dicProperty = new Dictionary<string, string>();
Type type = typeof(T);
//创建字段字典,方便查找字段名
foreach (var item in type.GetFields())
{
dicField.Add(item.Name.ToLower(), item.Name);
}
//创建属性字典,方便查找属性名
foreach (var item in type.GetProperties())
{
dicProperty.Add(item.Name.ToLower(), item.Name);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
//创建泛型对象
T _t = Activator.CreateInstance<T>();
for (int j = 0; j < dt.Columns.Count; j++)
{
string memberKey = dt.Columns[j].ColumnName.ToLower();
//字段赋值
if (dicField.ContainsKey(memberKey))
{
FieldInfo theField = type.GetField(dicField[memberKey]);
var dbValue = dt.Rows[i][j];
if (dbValue.GetType() == typeof(DBNull))
dbValue = null;
if (dbValue != null)
{
Type memberType = theField.FieldType;
//dbValue = dbValue.ChangeType(memberType);
dbValue = Convert.ChangeType(dbValue, memberType);
}
theField.SetValue(_t, dbValue);
}
//属性赋值
if (dicProperty.ContainsKey(memberKey))
{
PropertyInfo theProperty = type.GetProperty(dicProperty[memberKey]);
if (!theProperty.CanWrite) continue;
var dbValue = dt.Rows[i][j];
if (dbValue.GetType() == typeof(DBNull))
dbValue = null;
if (dbValue != null)
{
Type memberType = theProperty.PropertyType;
if (memberType.IsGenericType && memberType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
NullableConverter newNullableConverter = new NullableConverter(memberType);
dbValue = newNullableConverter.ConvertFrom(dbValue);
}
else
{
dbValue = Convert.ChangeType(dbValue, memberType);
}
}
theProperty.SetValue(_t, dbValue, null);
}
}
list.Add(_t);
}
return list;
}
#endregion
}
}
5、写一个获取配置的方法
using Carpa.Web.Script;
using GraspFx.DbConnection;
using GraspFx.Web.Core.SureWin.Model;
using GraspFx.Web.FrameWorks;
namespace GraspFx.Web.Core.SureWin.Helper
{
public class HeiHuConfigHelper
{
private string sql = @"select top 1 * from HeiHuConfig";
private HeiHuConfig ConvertToConfig(IHashObject obj)
{
HeiHuConfig config = new HeiHuConfig();
if (obj != null)
{
config.ApiUrl = obj.GetValue<string>("apiurl");
config.Phone = obj.GetValue<string>("phone");
config.Code = obj.GetValue<string>("code");
config.Username = obj.GetValue<string>("username");
config.Type = obj.GetValue<int>("type");
config.PassWord = obj.GetValue<string>("password");
config.UploadPtype = obj.GetValue<bool>("uploadptype");
config.UploadKtype = obj.GetValue<bool>("uploadktype");
config.UploadBuyBill = obj.GetValue<bool>("uploadbuybill");
config.UploadBuyBackBill = obj.GetValue<bool>("uploadbuybackbill");
config.UploadSaleOrder = obj.GetValue<bool>("uploadsaleorder");
config.UploadSaleBill = obj.GetValue<bool>("uploadsalebill");
config.UploadSaleBackBill = obj.GetValue<bool>("uploadsalebackbill");
}
return config;
}
public HeiHuConfig config
{
get
{
HeiHuConfig _config = GetHeiHuConfigFromDb();
return _config;
}
}
public HeiHuConfig GetHeiHuConfigFromDb()
{
IHashObject obj = GetHeiHuConfigFromHash();
return ConvertToConfig(obj);
}
public IHashObject GetHeiHuConfigFromHash()
{
IHashObject config;
using (DbContext _db = IocInstance.Resolve<IDbContextFactory>().Create())
{
config = _db.SelectFirstRow(sql);
}
return IHashObjectKeyToLower(config);
}
/// <summary>
/// 哈希函数key转小写
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private IHashObject IHashObjectKeyToLower(IHashObject data)
{
IHashObject newData = new HashObject();
foreach (var item in data.Keys)
{
if (!newData.Keys.Contains(item.ToLower()))
{
newData[item.ToLower()] = data[item];
}
}
return newData;
}
}
}
6、核心方法
using Carpa.Web.Script;
using GraspFx.Web.Core.SureWin.Helper;
using GraspFx.Web.Core.SureWin.Model;
using GraspFx.Web.Core.SureWin.Model.BaseTypeInfo;
using GraspFx.Web.Core.SureWin.Model.RequestModel;
using GraspFx.Web.Core.SureWin.Model.ResultModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GraspFx.Web.Core.SureWin
{
public class HeiHuCore
{
/// <summary>
/// 黑湖配置帮助
/// </summary>
private HeiHuConfigHelper _configHelper;
/// <summary>
/// 管家婆数据库调用
/// </summary>
private GjpClient _gjpClient;
/// <summary>
/// 黑湖配置
/// </summary>
private HeiHuConfig _heihuConfig;
/// <summary>
/// 黑湖请求API
/// </summary>
private HeiHuClient _client
{
get
{
return new HeiHuClient(_heihuConfig);
}
}
/// <summary>
/// 黑湖业务核心
/// </summary>
public HeiHuCore()
{
_gjpClient = new GjpClient();
_configHelper = new HeiHuConfigHelper();
_heihuConfig = _configHelper.config;
}
/// <summary>
/// 上传商品
/// </summary>
public List<BaseResult> UploadPtype(List<string> usercodes)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadPtype)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传商品";
log.Message = "上传商品配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传商品配置已关闭" });
return retList;
}
//循环上传商品信息
foreach (var usercode in usercodes)
{
//查询商品信息
PtypeInfo ptypeInfo = _gjpClient.QueryPtypeInfo(usercode);
if (ptypeInfo == null)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传商品";
log.Message = "未查询到商品编号为" + usercode + "的商品!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到商品编号为" + usercode + "的商品!" });
continue;
}
//商品信息转上传商品信息
UploadPtypeRequest uploadData = ptypeInfo.ToUploadPtypeRequest();
//返回参数
BaseResult result = _client.UploadAddPtype(uploadData);
//商品存在则修改
if (result.code == "1020")
{
result = _client.UploadUpdatePtype(uploadData);
}
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传采购入库单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadBuyBill(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadBuyBill)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传采购入库单";
log.Message = "上传采购入库单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传采购入库单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<BuyBillInfo> billList = _gjpClient.QueryBuyBillInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传采购入库单";
log.Message = "未查询到单据id为" + billid + "的采购入库单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的采购入库单!" });
continue;
}
//单据信息转上传单据信息
UploadOutInBillRequest uploadData = billList.ToUploadBuyBillRequest();
//返回参数
BaseResult result = _client.UploadOutInBill(uploadData);
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传采购退货单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadBuyBackBill(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadBuyBackBill)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传采购退货单";
log.Message = "上传采购退货单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传采购退货单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<BuyBackBillInfo> billList = _gjpClient.QueryBuyBackBillInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传采购退货单";
log.Message = "未查询到单据id为" + billid + "的采购退货单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的采购退货单!" });
continue;
}
//单据信息转上传单据信息
UploadOutInBillRequest uploadData = billList.ToUploadBuyBackBillRequest();
//返回参数
BaseResult result = _client.UploadOutInBill(uploadData);
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传销售出库单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadSaleBill(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadSaleBill)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售出库单";
log.Message = "上传销售出库单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传销售出库单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<SaleBillInfo> billList = _gjpClient.QuerySaleBillInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售出库单";
log.Message = "未查询到单据id为" + billid + "的销售出库单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的销售出库单!" });
continue;
}
//单据信息转上传单据信息
UploadOutInBillRequest uploadData = billList.ToUploadSaleBillRequest();
//返回参数
BaseResult result = _client.UploadOutInBill(uploadData);
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传销售退货单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadSaleBackBill(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadSaleBackBill)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售退货单";
log.Message = "上传销售退货单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传销售退货单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<SaleBackBillInfo> billList = _gjpClient.QuerySaleBackBillInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售退货单";
log.Message = "未查询到单据id为" + billid + "的销售退货单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的销售退货单!" });
continue;
}
//单据信息转上传单据信息
UploadOutInBillRequest uploadData = billList.ToUploadSaleBackBillRequest();
//返回参数
BaseResult result = _client.UploadOutInBill(uploadData);
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传新增销售订单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadAddSaleOrder(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadSaleOrder)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售订单";
log.Message = "上传销售订单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传销售订单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<SaleOrderInfo> billList = _gjpClient.QuerySaleOrderInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售订单";
log.Message = "未查询到单据id为" + billid + "的销售订单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的销售订单!" });
continue;
}
//单据信息转上传单据信息
UploadAddCustomOrderRequest uploadData = billList.ToUploadAddSaleOrderRequest();
//返回参数
BaseResult result = _client.UploadAddCustomOrder(uploadData);
retList.Add(result);
}
return retList;
}
/// <summary>
/// 上传修改销售订单
/// </summary>
/// <returns></returns>
public List<BaseResult> UploadUpdateSaleOrder(List<string> billids)
{
List<BaseResult> retList = new List<BaseResult>();
//判断有没有开启配置
if (!_heihuConfig.UploadSaleOrder)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售订单";
log.Message = "上传销售订单配置已关闭!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "上传销售订单配置已关闭" });
return retList;
}
//循环上传单据信息
foreach (var billid in billids)
{
//查询单据信息
List<SaleOrderInfo> billList = _gjpClient.QuerySaleOrderInfo(billid);
if (billList == null || billList.Count == 0)
{
//添加日志
HeiHuLog log = new HeiHuLog();
log.RequestTime = DateTime.Now;
log.Operation = "上传销售订单";
log.Message = "未查询到单据id为" + billid + "的销售订单!";
log.Success = false;
log.Code = "-1";
_gjpClient.AddHeiHuLog(log);
retList.Add(new BaseResult() { msg = "未查询到单据id为" + billid + "的销售订单!" });
continue;
}
//单据信息转上传单据信息
UploadUpdateCustomOrderRequest uploadData = billList.ToUploadUpdateSaleOrderRequest();
//返回参数
BaseResult result = _client.UploadUpdateCustomOrder(uploadData);
retList.Add(result);
}
return retList;
}
}
}
7、如何调取方法
#region 黑湖小工单定制
//上传单据
HeiHuCore core = new HeiHuCore();
List<BaseResult> retList = core.UploadBuyBill(new List<string>() { BuyBillEntity.BillNumberId.ToString() });
#endregion
8、下面是要用到的扩展方法
using GraspFx.Web.Core.SureWin.Model.BaseTypeInfo;
using GraspFx.Web.Core.SureWin.Model.RequestModel;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace GraspFx.Web.Core.SureWin.Helper
{
/// <summary>
/// 扩展工具帮助
/// </summary>
public static class ExtendUtil
{
/// <summary>
/// SHA3_224算法
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string SHA3_224(this string input)
{
// 使用SHA384计算hash值
byte[] hashBytes = SHA384.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
// 截取前28字节(SHA3-224的长度)
byte[] sha3_224Bytes = new byte[28];
Array.Copy(hashBytes, sha3_224Bytes, 28);
// 将字节转换为十六进制字符串
return BitConverter.ToString(sha3_224Bytes).Replace("-", "").ToLowerInvariant();
}
/// <summary>
/// 转商品信息
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static UploadPtypeRequest ToUploadPtypeRequest(this PtypeInfo info)
{
UploadPtypeRequest upload = new UploadPtypeRequest();
upload.productCode = info.UserCode;
upload.productName = info.FullName;
upload.productSpecification = info.Standard;
upload.unit = info.UnitName;
upload.originType = 0;
upload.costPrice = info.CostPrice;
upload.salesPrice = info.PreSalePrice1;
return upload;
}
/// <summary>
/// 转采购入库单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadOutInBillRequest ToUploadBuyBillRequest(this List<BuyBillInfo> list)
{
UploadOutInBillRequest upload = new UploadOutInBillRequest();
if (list != null && list.Count > 0)
{
upload.orderNo = list[0].BillCode;
upload.orderType = 10;
upload.bizAt = list[0].Posttime.ToString("yyyy-MM-dd HH:mm:ss");
upload.bizType = 12;
upload.remark = list[0].Explain;
//明细
foreach (var item in list)
{
OutInBillDetail detail = new OutInBillDetail();
detail.seq = item.ShowOrder;
detail.productCode = item.PUserCode;
detail.qty = item.Qty;
upload.stockOrderDetails.Add(detail);
}
}
return upload;
}
/// <summary>
/// 转采购退货单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadOutInBillRequest ToUploadBuyBackBillRequest(this List<BuyBackBillInfo> list)
{
UploadOutInBillRequest upload = new UploadOutInBillRequest();
if (list != null && list.Count > 0)
{
upload.orderNo = list[0].BillCode;
upload.orderType = 20;
upload.bizAt = list[0].Posttime.ToString("yyyy-MM-dd HH:mm:ss");
upload.bizType = 25;
upload.remark = list[0].Explain;
//明细
foreach (var item in list)
{
OutInBillDetail detail = new OutInBillDetail();
detail.seq = item.ShowOrder;
detail.productCode = item.PUserCode;
detail.qty = item.Qty;
upload.stockOrderDetails.Add(detail);
}
}
return upload;
}
/// <summary>
/// 转销售出库单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadOutInBillRequest ToUploadSaleBillRequest(this List<SaleBillInfo> list)
{
UploadOutInBillRequest upload = new UploadOutInBillRequest();
if (list != null && list.Count > 0)
{
upload.orderNo = list[0].BillCode;
upload.orderType = 20;
upload.bizAt = list[0].Posttime.ToString("yyyy-MM-dd HH:mm:ss");
upload.bizType = 22;
upload.remark = list[0].Explain;
//明细
foreach (var item in list)
{
OutInBillDetail detail = new OutInBillDetail();
detail.seq = item.ShowOrder;
detail.productCode = item.PUserCode;
detail.qty = item.Qty;
upload.stockOrderDetails.Add(detail);
}
}
return upload;
}
/// <summary>
/// 转销售退货单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadOutInBillRequest ToUploadSaleBackBillRequest(this List<SaleBackBillInfo> list)
{
UploadOutInBillRequest upload = new UploadOutInBillRequest();
if (list != null && list.Count > 0)
{
upload.orderNo = list[0].BillCode;
upload.orderType = 10;
upload.bizAt = list[0].Posttime.ToString("yyyy-MM-dd HH:mm:ss");
upload.bizType = 15;
upload.remark = list[0].Explain;
//明细
foreach (var item in list)
{
OutInBillDetail detail = new OutInBillDetail();
detail.seq = item.ShowOrder;
detail.productCode = item.PUserCode;
detail.qty = item.Qty;
upload.stockOrderDetails.Add(detail);
}
}
return upload;
}
/// <summary>
/// 转新增销售订单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadAddCustomOrderRequest ToUploadAddSaleOrderRequest(this List<SaleOrderInfo> list)
{
UploadAddCustomOrderRequest upload = new UploadAddCustomOrderRequest();
if (list != null && list.Count > 0)
{
upload.formCode = list[0].BillCode;
upload.formType = 101;
//明细
foreach (var item in list)
{
CustomOrderDetail detail = new CustomOrderDetail();
detail.projectCode = item.PUserCode;
detail.projectId = item.PtypeId;
upload.projectCOList.Add(detail);
}
}
return upload;
}
/// <summary>
/// 转修改销售订单信息
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static UploadUpdateCustomOrderRequest ToUploadUpdateSaleOrderRequest(this List<SaleOrderInfo> list)
{
UploadUpdateCustomOrderRequest upload = new UploadUpdateCustomOrderRequest();
if (list != null && list.Count > 0)
{
upload.formCode = list[0].BillCode;
//明细
foreach (var item in list)
{
CustomOrderDetail detail = new CustomOrderDetail();
detail.projectCode = item.PUserCode;
detail.projectId = item.PtypeId;
upload.projectCOList.Add(detail);
}
}
return upload;
}
}
}
8、所有用到的的类都在下面了
namespace GraspFx.Web.Core.SureWin.Model
{
/// <summary>
/// 黑湖配置
/// </summary>
public class HeiHuConfig
{
/// <summary>
/// 接口地址
/// </summary>
public string ApiUrl { get; set; }
/// <summary>
/// 登录手机号
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 工厂代码
/// </summary>
public string Code { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string Username { get; set; }
/// <summary>
/// 登录类型 type=0 (默认)代表手机号+密码方式 type=1 代表 工厂+账号+密码方式,此时type必传
/// </summary>
public int Type { get; set; }
/// <summary>
/// 密码
/// </summary>
public string PassWord { get; set; }
/// <summary>
/// 上传商品
/// </summary>
public bool UploadPtype { get; set; }
/// <summary>
/// 上传仓库
/// </summary>
public bool UploadKtype { get; set; }
/// <summary>
/// 上传采购入库单
/// </summary>
public bool UploadBuyBill { get; set; }
/// <summary>
/// 上传采购退货单
/// </summary>
public bool UploadBuyBackBill { get; set; }
/// <summary>
/// 上传销售订单
/// </summary>
public bool UploadSaleOrder { get; set; }
/// <summary>
/// 上传销售出库单
/// </summary>
public bool UploadSaleBill { get; set; }
/// <summary>
/// 上传销售出库单
/// </summary>
public bool UploadSaleBackBill { get; set; }
}
}
using System;
namespace GraspFx.Web.Core.SureWin.Model
{
/// <summary>
/// 黑湖日志
/// </summary>
public class HeiHuLog
{
/// <summary>
/// 请求时间
/// </summary>
public DateTime RequestTime { get; set; }
/// <summary>
/// 请求方法
/// </summary>
public string Operation { get; set; }
/// <summary>
/// 返回信息
/// </summary>
public string Message { get; set; }
/// <summary>
/// 请求地址
/// </summary>
public string Url { get; set; }
/// <summary>
/// 请求报文
/// </summary>
public string RequestSrting { get; set; }
/// <summary>
/// 返回报文
/// </summary>
public string ResponseString { get; set; }
/// <summary>
/// 成功
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 编码
/// </summary>
public string Code { get; set; }
}
}
namespace GraspFx.Web.Core.SureWin.Model.ResultModel
{
/// <summary>
/// 返回参数
/// </summary>
public class BaseResult
{
/// <summary>
/// 成功与否 成功:01000000 ,其他为失败
/// </summary>
public string code { get; set; }
/// <summary>
/// 返回数据。当失败时该字段返回值为null
/// </summary>
public object data { get; set; }
/// <summary>
/// 返回消息
/// </summary>
public string msg { get; set; }
}
}
namespace GraspFx.Web.Core.SureWin.Model.ResultModel
{
/// <summary>
/// 登录返回结果
/// </summary>
public class TokenResult
{
/// <summary>
/// 状态编号
/// </summary>
public string statusCode { get; set; }
/// <summary>
/// 信息
/// </summary>
public string message { get; set; }
/// <summary>
/// 数据
/// </summary>
public string data { get; set; }
/// <summary>
/// 额外的数据
/// </summary>
public string extraData { get; set; }
}
}
using System.Collections.Generic;
namespace GraspFx.Web.Core.SureWin.Model.RequestModel
{
/// <summary>
/// 上传自定义表单
/// </summary>
public class UploadAddCustomOrderRequest
{
/// <summary>
/// 表单编号
/// </summary>
public string formCode { get; set; }
/// <summary>
/// 自定义表单类型:101 销售订单 102 生产计划 103 装配工单
/// </summary>
public int formType { get; set; }
/// <summary>
/// 自定义表单表头自定义字段
/// </summary>
public List<object> customFieldValueList { get; set; }
/// <summary>
/// 明细行,明细行及相关上限同桌面端一致
/// </summary>
public List<CustomOrderDetail> projectCOList { get; set; } = new List<CustomOrderDetail>();
}
/// <summary>
/// 自定义表单明细
/// </summary>
public class CustomOrderDetail
{
/// <summary>
/// 编号
/// </summary>
public string projectCode { get; set; }
/// <summary>
/// Id
/// </summary>
public int projectId { get; set; }
}
}
using System.Collections.Generic;
namespace GraspFx.Web.Core.SureWin.Model.RequestModel
{
/// <summary>
/// 上传出入库单信息
/// </summary>
public class UploadOutInBillRequest
{
/// <summary>
/// 单据编号 如果没有,则按系统内部规则自动生成单据编号
/// </summary>
public string orderNo { get; set; }
/// <summary>
/// 10 入库单,20 出库单
/// </summary>
public int orderType { get; set; }
/// <summary>
/// 出入库时间 格式为:yyyy-MM-dd HH:mm:ss,如不传,则默认请求时间
/// </summary>
public string bizAt { get; set; }
/// <summary>
/// 11 普通入库 12 采购入库 13 成品入库 14 调拨入库 15 销售退货入库 16 其他入库 21 普通出库 22 销售出库 23 生产出库 24 调拨出库 25 采购退货出库 26 其他出库
/// </summary>
public int bizType { get; set; }
/// <summary>
/// 备注
/// </summary>
public string remark { get; set; }
/// <summary>
/// 出入库自定义字段
/// </summary>
public object[] stockOrderCustomFieldsValue { get; set; }
/// <summary>
/// 出入库明细
/// </summary>
public List<OutInBillDetail> stockOrderDetails { get; set; } = new List<OutInBillDetail>();
}
/// <summary>
/// 出入库明细
/// </summary>
public class OutInBillDetail
{
/// <summary>
/// 产品明细序号
/// </summary>
public int seq { get; set; }
/// <summary>
/// 产品编号
/// </summary>
public string productCode { get; set; }
/// <summary>
/// 出入库数量
/// </summary>
public decimal qty { get; set; }
/// <summary>
/// 出入库明细自定义字段
/// </summary>
public object[] stockOrderDetailCustomFieldsValue { get; set; }
}
}
namespace GraspFx.Web.Core.SureWin.Model.RequestModel
{
/// <summary>
/// 上传商品信息
/// </summary>
public class UploadPtypeRequest
{
/// <summary>
/// 产品编号,校验所传产品编号在系统是否存在,若已存在,则接口返回“1020”。若不传,由系统自动编号生成。
/// </summary>
public string productCode { get; set; }
/// <summary>
/// 产品名称,校验是否传输该字段,若没有,则接口返回“1021”
/// </summary>
public string productName { get; set; }
/// <summary>
/// 产品规格,校验所传产品编号在系统是否存在,若不存在,则接口返回“1022”
/// </summary>
public string productSpecification { get; set; }
/// <summary>
/// 工艺路线型号(可以理解为code),校验所传工艺路线编号在系统是否存在,若不存在,则接口返回“1023”
/// </summary>
public string model { get; set; }
/// <summary>
/// 单位名称,校验是否传输该字段,若没有,则接口返回“1024”,校验所传单位在系统是否存在,若不存在,则接口返回“1025”
/// </summary>
public string unit { get; set; }
/// <summary>
/// 产品属性,默认0 自制, 0: 自制 1: 外购 2: 委外
/// </summary>
public int originType { get; set; }
/// <summary>
/// 成本单价
/// </summary>
public decimal costPrice { get; set; }
/// <summary>
/// 销售单价
/// </summary>
public decimal salesPrice { get; set; }
/// <summary>
/// 安全库存
/// </summary>
public decimal safetyQty { get; set; }
/// <summary>
/// 最大库存
/// </summary>
public decimal maxQty { get; set; }
/// <summary>
/// 最小库存
/// </summary>
public decimal minQty { get; set; }
/// <summary>
/// 产品自定义字段值(不支持附件、关联属性)
/// </summary>
public object[] customFieldValues { get; set; }
}
}
using System.Collections.Generic;
namespace GraspFx.Web.Core.SureWin.Model.RequestModel
{
/// <summary>
/// 上传自定义表单
/// </summary>
public class UploadUpdateCustomOrderRequest
{
/// <summary>
/// customFormFieldId(自定义表单主键id)或fromCode任意一个必填,用于定位待更新自定义表单
/// </summary>
public int customFormFieldId { get; set; }
/// <summary>
/// customFormFieldId(自定义表单主键id)或fromCode任意一个必填,用于定位待更新自定义表单
/// </summary>
public string formCode { get; set; }
/// <summary>
/// 明细行,明细行及相关上限同桌面端一致
/// </summary>
public List<CustomOrderDetail> projectCOList { get; set; } = new List<CustomOrderDetail>();
}
}
using System;
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 单据基类
/// </summary>
public class BaseBillInfo
{
/// <summary>
/// 单据编号
/// </summary>
public string BillCode { get; set; }
/// <summary>
/// 过账时间
/// </summary>
public DateTime Posttime { get; set; }
/// <summary>
/// 备注
/// </summary>
public string Explain { get; set; }
/// <summary>
/// 排序
/// </summary>
public int ShowOrder { get; set; }
/// <summary>
/// 商品编号
/// </summary>
public string PUserCode { get; set; }
/// <summary>
/// 数量
/// </summary>
public decimal Qty { get; set; }
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 采购退货单信息
/// </summary>
public class BuyBackBillInfo : BaseBillInfo
{
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 采购入库单信息
/// </summary>
public class BuyBillInfo : BaseBillInfo
{
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 商品信息
/// </summary>
public class PtypeInfo
{
/// <summary>
/// 编号
/// </summary>
public string UserCode { get; set; }
/// <summary>
/// 名称
/// </summary>
public string FullName { get; set; }
/// <summary>
/// 规格
/// </summary>
public string Standard { get; set; }
/// <summary>
/// 单位名称
/// </summary>
public string UnitName { get; set; }
/// <summary>
/// 销售单价1
/// </summary>
public decimal PreSalePrice1 { get; set; }
/// <summary>
/// 成本单价
/// </summary>
public decimal CostPrice { get; set; }
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 销售退货单信息
/// </summary>
public class SaleBackBillInfo : BaseBillInfo
{
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 销售出库单信息
/// </summary>
public class SaleBillInfo : BaseBillInfo
{
}
}
namespace GraspFx.Web.Core.SureWin.Model.BaseTypeInfo
{
/// <summary>
/// 销售订单信息
/// </summary>
public class SaleOrderInfo
{
/// <summary>
/// 订单编号
/// </summary>
public string BillCode { get; set; }
/// <summary>
/// 商品编号
/// </summary>
public string PUserCode { get; set; }
/// <summary>
/// 商品Id
/// </summary>
public int PtypeId { get; set; }
}
}