聚水潭接口对接
This commit is contained in:
16
src/WMS.Web.Domain/IService/Public/IJuShuiTanService.cs
Normal file
16
src/WMS.Web.Domain/IService/Public/IJuShuiTanService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto.JuShuiTan;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
|
||||
namespace WMS.Web.Domain.IService.Public
|
||||
{
|
||||
public interface IJuShuiTanService
|
||||
{
|
||||
Task GetTest();
|
||||
Task<Result<List<JuShuiTanStockResponse>>> GetStock();
|
||||
Task<Result<List<JuShuiTanInventoryResponse>>> GetInventory(List<int> StockId);
|
||||
}
|
||||
}
|
||||
14
src/WMS.Web.Domain/Options/JuShuiTanOptions.cs
Normal file
14
src/WMS.Web.Domain/Options/JuShuiTanOptions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Domain.Options
|
||||
{
|
||||
public class JuShuiTanOptions
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string AppKey { get; set; }
|
||||
public string AppSecret { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -27,13 +27,15 @@ namespace WMS.Web.Domain.Services
|
||||
private readonly ILingXingService _lingXingService;
|
||||
private readonly IBasicsRepositories _basicsRepositories;
|
||||
public readonly IBasicsRepositories _transactionRepositories;
|
||||
public readonly IJuShuiTanService _juShuiTanService;
|
||||
public ProductInventoryService(IErpService erpService,
|
||||
ISingleDataService singleDataService,
|
||||
IErpBasicDataExtendService erpBasicDataExtendService,
|
||||
IProductInventoryRepositories repositories,
|
||||
ILingXingService lingXingService,
|
||||
IBasicsRepositories basicsRepositories,
|
||||
IBasicsRepositories transactionRepositories)
|
||||
IBasicsRepositories transactionRepositories,
|
||||
IJuShuiTanService juShuiTanService)
|
||||
{
|
||||
_erpService = erpService;
|
||||
_singleDataService = singleDataService;
|
||||
@@ -42,9 +44,13 @@ namespace WMS.Web.Domain.Services
|
||||
_lingXingService = lingXingService;
|
||||
_basicsRepositories = basicsRepositories;
|
||||
_transactionRepositories = transactionRepositories;
|
||||
_juShuiTanService = juShuiTanService;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 金蝶
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Result> Erp()
|
||||
{
|
||||
//获取单点配置
|
||||
@@ -93,12 +99,85 @@ namespace WMS.Web.Domain.Services
|
||||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
|
||||
public Task<Result> JuShuiTan()
|
||||
/// <summary>
|
||||
/// 聚水潭
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<Result> JuShuiTan()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
//获取单点配置
|
||||
var r = await _basicsRepositories.GetUcStockByHeadOfficeAsync("", 1);
|
||||
var listNames = r.Where(w =>
|
||||
w.ManagementSystem == 2 && !string.IsNullOrEmpty(w.WarehouseCodeOfJushuitan))
|
||||
.Select(s => s.WarehouseCodeOfJushuitan)
|
||||
.ToList();
|
||||
//获取领星仓库
|
||||
var resStock = await _juShuiTanService.GetStock();
|
||||
if (!resStock.IsSuccess) return resStock;
|
||||
|
||||
var ids = resStock.Data.Where(w => listNames.Contains(w.Name)).Select(s => s.Id).ToList();
|
||||
//获取领星库存
|
||||
var resInventory = await _juShuiTanService.GetInventory(ids);
|
||||
if (!resInventory.IsSuccess) return resStock;
|
||||
|
||||
//物料
|
||||
var materials_result = await _erpService.BillQueryForMaterial();
|
||||
List<ErpMaterialDto> materials = new List<ErpMaterialDto>();
|
||||
if (materials_result.IsSuccess)
|
||||
materials = materials_result.Data.ToList();
|
||||
|
||||
List<ProductInventory> inventoryList = new List<ProductInventory>();
|
||||
foreach (var item in resInventory.Data)
|
||||
{
|
||||
//如果物料不匹配 过滤
|
||||
var m = materials.FirstOrDefault(f => f.Specifications.Equals(item.sku_id));
|
||||
if (m == null) continue;
|
||||
//找仓库
|
||||
var l_stock = resStock.Data.FirstOrDefault(f => f.Id == item.wms_co_id);
|
||||
if (l_stock == null) continue;
|
||||
var stock = r.FirstOrDefault(f => f.WarehouseCodeOfLingxing.Equals(l_stock.Name));
|
||||
if (stock == null) continue;
|
||||
|
||||
var entity = new ProductInventory()
|
||||
{
|
||||
Type = ProductInventoryType.JushuiTan,
|
||||
MaterialNumber = m.MaterialNumber,
|
||||
Customer = "",
|
||||
OrgCode = stock.Code,
|
||||
StockCode = stock.ErpOrgCode,
|
||||
Qty = item.ky_qty,
|
||||
BeforeQty = item.qty ?? 0
|
||||
};
|
||||
inventoryList.Add(entity);
|
||||
}
|
||||
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
|
||||
Result res_Rollback = Result.ReSuccess();
|
||||
bool isSuccess = true;
|
||||
//修改库存
|
||||
//先删除之前的再添加
|
||||
if (res_Rollback.IsSuccess)
|
||||
{
|
||||
isSuccess = await _repositories.Delete(ProductInventoryType.LingXing, false);
|
||||
if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError);
|
||||
}
|
||||
if (res_Rollback.IsSuccess)
|
||||
{
|
||||
isSuccess = await _repositories.AddRange(inventoryList, false);
|
||||
if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError);
|
||||
}
|
||||
//提交事务
|
||||
isSuccess = _transactionRepositories.CommitTransaction(res_Rollback.IsSuccess ? false : true, _transaction);
|
||||
if (!res_Rollback.IsSuccess) return res_Rollback;
|
||||
if (!isSuccess)
|
||||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||||
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
/// <summary>
|
||||
/// 领星
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Result> LingXing()
|
||||
{
|
||||
//获取单点配置
|
||||
|
||||
218
src/WMS.Web.Domain/Services/Public/JuShuiTanService.cs
Normal file
218
src/WMS.Web.Domain/Services/Public/JuShuiTanService.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto.JuShuiTan;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
using WMS.Web.Domain.Options;
|
||||
|
||||
namespace WMS.Web.Domain.Services.Public
|
||||
{
|
||||
public class JuShuiTanService : IJuShuiTanService
|
||||
{
|
||||
private readonly IHttpClientService _httpClientService;
|
||||
private readonly ILogger<JuShuiTanService> _logger;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private int hours = 10;//过期时间 默认10小时
|
||||
private readonly JuShuiTanOptions _option;
|
||||
|
||||
public JuShuiTanService(IHttpClientService httpClientService, ILogger<JuShuiTanService> logger,
|
||||
IMemoryCache memoryCache, IOptions<JuShuiTanOptions> option)
|
||||
{
|
||||
this._memoryCache = memoryCache;
|
||||
this._httpClientService = httpClientService;
|
||||
this._logger = logger;
|
||||
_option = option?.Value;
|
||||
}
|
||||
public async Task GetTest()
|
||||
{
|
||||
//await GetStock();
|
||||
var list = new List<int>();
|
||||
list.Add(10923342);
|
||||
await GetInventory(list);
|
||||
}
|
||||
|
||||
public async Task<Result<List<JuShuiTanStockResponse>>> GetStock()
|
||||
{
|
||||
List<JuShuiTanStockResponse> list = new List<JuShuiTanStockResponse>();
|
||||
|
||||
JuShuiTanStockRequst request = new JuShuiTanStockRequst();
|
||||
var resContent = PostUrl("/open/wms/partner/query", JsonConvert.SerializeObject(request));
|
||||
JObject resData = JObject.Parse(resContent);
|
||||
|
||||
if (Convert.ToInt32(resData["code"]) != 0)
|
||||
_logger.LogInformation($"聚水潭获取仓库失败:{resData["msg"].ToString()}");
|
||||
string json = resData["data"]["datas"].ToString();
|
||||
var datas = JsonConvert.DeserializeObject<List<JuShuiTanStockResponse>>(json);
|
||||
list.AddRange(datas);
|
||||
|
||||
int count = Convert.ToInt32(resData["data"]["page_count"]);
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
request.page_index++;
|
||||
resContent = PostUrl("/open/wms/partner/query", JsonConvert.SerializeObject(request));
|
||||
resData = JObject.Parse(resContent);
|
||||
|
||||
if (Convert.ToInt32(resData["code"]) != 0)
|
||||
_logger.LogInformation($"聚水潭获取仓库失败:{resData["msg"].ToString()}");
|
||||
json = resData["data"]["datas"].ToString();
|
||||
datas = JsonConvert.DeserializeObject<List<JuShuiTanStockResponse>>(json);
|
||||
list.AddRange(datas);
|
||||
}
|
||||
return Result<List<JuShuiTanStockResponse>>.ReSuccess(list);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Result<List<JuShuiTanInventoryResponse>>> GetInventory(List<int> StockId)
|
||||
{
|
||||
List<JuShuiTanInventoryResponse> list = new List<JuShuiTanInventoryResponse>();
|
||||
foreach (var id in StockId)
|
||||
{
|
||||
JuShuiTanInventoryRequst request = new JuShuiTanInventoryRequst();
|
||||
request.wms_co_id = id;//10923342;
|
||||
request.page_size = 100;
|
||||
request.modified_begin = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
request.modified_end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
var resContent = PostUrl("/open/inventory/query", JsonConvert.SerializeObject(request));
|
||||
JObject resData = JObject.Parse(resContent);
|
||||
|
||||
if (Convert.ToInt32(resData["code"]) != 0)
|
||||
_logger.LogInformation($"聚水潭获取即时库存失败:{resData["msg"].ToString()}");
|
||||
string json = resData["data"]["inventorys"].ToString();
|
||||
var datas = JsonConvert.DeserializeObject<List<JuShuiTanInventoryResponse>>(json);
|
||||
list.AddRange(datas);
|
||||
|
||||
int count = Convert.ToInt32(resData["data"]["page_count"]);
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
request.page_index++;
|
||||
resContent = PostUrl("/open/inventory/query", JsonConvert.SerializeObject(request));
|
||||
resData = JObject.Parse(resContent);
|
||||
|
||||
if (Convert.ToInt32(resData["code"]) != 0)
|
||||
_logger.LogInformation($"聚水潭获取即时库存失败:{resData["msg"].ToString()}");
|
||||
json = resData["data"]["inventorys"].ToString();
|
||||
datas = JsonConvert.DeserializeObject<List<JuShuiTanInventoryResponse>>(json);
|
||||
list.AddRange(datas);
|
||||
}
|
||||
}
|
||||
//主仓实际库存-订单占有数+虚拟库存+采购在途(业务设置)+进货仓(业务设置)+销退仓库存(业务设置)
|
||||
foreach (var item in list)
|
||||
{
|
||||
item.qty = item.qty == null ? 0 : item.qty;
|
||||
item.ky_qty = (item.qty ?? 0) - (item.order_lock ?? 0) + (item.virtual_qty ?? 0)
|
||||
+ (item.purchase_qty ?? 0) + (item.in_qty ?? 0) + (item.return_qty ?? 0);
|
||||
}
|
||||
var resList = list.Where(w => w.qty > 0 || w.ky_qty > 0).ToList();
|
||||
return Result<List<JuShuiTanInventoryResponse>>.ReSuccess(resList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用接口
|
||||
/// </summary>
|
||||
private string PostUrl(string urlStr, string biz)
|
||||
{
|
||||
Hashtable ht = new Hashtable();
|
||||
System.DateTime starttime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
||||
string time = Math.Floor((System.DateTime.Now - starttime).TotalSeconds).ToString();
|
||||
string app_key = _option.AppKey;// "17231841286c45f99eb9acf445404349";
|
||||
string app_secret = _option.AppSecret;// "5ce9641ae6db4abb8b96eea7df6a81e9";
|
||||
string access_token = _option.AccessToken;// "4605358c37374780a8a4395e6cdb1c3f";
|
||||
|
||||
|
||||
//string biz = "{\"modified_begin\":\"2022-06-10 00:00:00\",\"modified_end\":\"2022-06-12 00:00:00\",\"page_index\":1,\"page_size\":30}";
|
||||
|
||||
ht.Add("access_token", access_token);
|
||||
ht.Add("app_key", app_key);
|
||||
ht.Add("biz", biz);
|
||||
ht.Add("charset", "utf-8");
|
||||
ht.Add("timestamp", time);
|
||||
ht.Add("version", "2");
|
||||
string sign = getSign(app_secret, ht);
|
||||
ht.Add("sign", sign);
|
||||
string posturl = _option.Url + urlStr;
|
||||
Encoding datacode = Encoding.UTF8;
|
||||
string paramData = GetParamData(ht);
|
||||
return PostWebRequest(posturl, paramData, datacode);
|
||||
}
|
||||
|
||||
string getSign(string signKey, Hashtable param)
|
||||
{
|
||||
|
||||
ArrayList keys = new ArrayList(param.Keys);
|
||||
keys.Sort(); //按字母顺序进行排序
|
||||
string resultStr = "";
|
||||
foreach (string key in keys)
|
||||
{
|
||||
if (key != null && key != "" && key != "sign")
|
||||
{
|
||||
resultStr = resultStr + key + param[key];
|
||||
}
|
||||
}
|
||||
resultStr = signKey + resultStr;
|
||||
MD5 md5 = MD5.Create();
|
||||
byte[] rstRes = md5.ComputeHash(Encoding.UTF8.GetBytes(resultStr));
|
||||
string hex = BitConverter.ToString(rstRes, 0).Replace("-", string.Empty).ToLower();
|
||||
|
||||
return hex;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
|
||||
{
|
||||
string ret = string.Empty;
|
||||
try
|
||||
{
|
||||
|
||||
byte[] byteArray = dataEncode.GetBytes(paramData);
|
||||
//转化
|
||||
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
|
||||
webReq.Method = "POST";
|
||||
webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
|
||||
webReq.ContentLength = byteArray.Length;
|
||||
|
||||
Stream newStream = webReq.GetRequestStream();
|
||||
newStream.Write(byteArray, 0, byteArray.Length);
|
||||
//写入参数
|
||||
newStream.Close();
|
||||
|
||||
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
|
||||
StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
|
||||
ret = sr.ReadToEnd();
|
||||
sr.Close();
|
||||
response.Close();
|
||||
newStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private string GetParamData(Hashtable ht)
|
||||
{
|
||||
string str = "";
|
||||
foreach (DictionaryEntry items in ht)
|
||||
{
|
||||
|
||||
str += items.Key.ToString() + "=" + System.Web.HttpUtility.UrlEncode(items.Value.ToString(), System.Text.Encoding.UTF8) + "&";
|
||||
}
|
||||
|
||||
return str.Substring(0, str.Length - 1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user