步步为盈
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.

375 lines
23 KiB

using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Db.Mds;
using BBWY.Server.Model.Dto;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Yitter.IdGenerator;
namespace BBWY.Server.Business
{
public class VenderBusiness : BasePlatformRelayBusiness, IDenpendency
{
private FreeSqlMultiDBManager freeSqlMultiDBManager;
private IIdGenerator idGenerator;
public VenderBusiness(FreeSqlMultiDBManager freeSqlMultiDBManager,
RestApiService restApiService,
IOptions<GlobalConfig> options,
IIdGenerator idGenerator, YunDingBusiness yunDingBusiness) : base(restApiService, options, yunDingBusiness)
{
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
this.idGenerator = idGenerator;
}
public VenderResponse GetVenderInfo(PlatformRequest platformRequest)
{
var relayAPIHost = GetPlatformRelayAPIHost(platformRequest.Platform);
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetVenderInfo", platformRequest, GetYunDingRequestHeader(), HttpMethod.Post);
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
var response = JsonConvert.DeserializeObject<ApiResponse<VenderResponse>>(sendResult.Content);
if (!response.Success)
throw new BusinessException(response.Msg) { Code = response.Code };
return response.Data;
}
public IList<LogisticsResponse> GetLogisticsList(PlatformRequest platformRequest)
{
var relayAPIHost = GetPlatformRelayAPIHost(platformRequest.Platform);
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetLogisticsList", platformRequest, GetYunDingRequestHeader(), System.Net.Http.HttpMethod.Post);
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
var response = JsonConvert.DeserializeObject<ApiResponse<IList<LogisticsResponse>>>(sendResult.Content);
if (!response.Success)
throw new BusinessException(response.Msg) { Code = response.Code };
return response.Data;
}
public string AcceptJDShopToken(JDShopToken jDShopToken)
{
string appKey, appSecret;
Enums.AppKeyType appKeyType;
if (jDShopToken.State == "merp_1034059314253266944")
{
//订单Key
appKey = "120EA9EC65AB017567D78CC1139EEEA5";
appSecret = "8a42bc2301e8439b896e99f5475e0a9b";
appKeyType = Enums.AppKeyType.;
}
else if (jDShopToken.State == "merp_1058051655896924160_2")
{
//商品Key
appKey = "E1AA9247D5583A6D87449CE6AB290185";
appSecret = "e479558ddd9e40f8929cfc00c6cbbc9c";
appKeyType = Enums.AppKeyType.;
}
else
{
throw new BusinessException($"不识别的state {jDShopToken.State}");
}
var venderResponse = GetVenderInfo(new PlatformRequest()
{
AppKey = appKey,
AppSecret = appSecret,
AppToken = jDShopToken.AccessToken,
Platform = Enums.Platform.
});
var shop = freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => s.ShopName == venderResponse.ShopName).ToOne();
if (shop == null)
throw new BusinessException($"未找到店铺 {venderResponse.ShopName}");
freeSqlMultiDBManager.MDSfsql.Update<Shops>(shop.Id)
.SetIf(string.IsNullOrEmpty(shop.ShopId), s => s.ShopId, venderResponse.ShopId)
.SetIf(string.IsNullOrEmpty(shop.ShopType), s => s.ShopType, venderResponse.ColType)
.SetIf(string.IsNullOrEmpty(shop.VenderId), s => s.VenderId, venderResponse.VenderId)
.SetIf(appKeyType == Enums.AppKeyType., s => s.AppToken, jDShopToken.AccessToken)
.SetIf(appKeyType == Enums.AppKeyType., s => s.AppToken2, jDShopToken.AccessToken)
.ExecuteAffrows();
return JsonConvert.SerializeObject(jDShopToken);
}
public long SaveShopSetting(ShopSettingRequest shopSettingRequest)
{
//根据shopId查询mds shop的主键Id
var shopId = shopSettingRequest.ShopId.ToString();
var mdsShop = freeSqlMultiDBManager.MDSfsql.Select<Model.Db.Mds.Shops>().Where(s => s.ShopId == shopId).ToOne();
if (mdsShop == null)
throw new BusinessException($"mds未找到shopId {shopSettingRequest.ShopId}");
if (shopSettingRequest.PurchaseAccountId == 0)
{
shopSettingRequest.PurchaseAccountId = idGenerator.NewLong();
var pa = new PurchaseAccount()
{
Id = shopSettingRequest.PurchaseAccountId,
AccountName = shopSettingRequest.AccountName,
AppKey = shopSettingRequest.AppKey,
AppSecret = shopSettingRequest.AppSecret,
AppToken = shopSettingRequest.AppToken,
CreateTime = DateTime.Now,
CreatorId = "",
Deleted = 0,
PurchasePlatformId = shopSettingRequest.PurchasePlatformId,
ShopId = shopSettingRequest.ShopId
};
var mdspa = new Purchaseaccount()
{
Id = shopSettingRequest.PurchaseAccountId.ToString(),
AccountName = shopSettingRequest.AccountName,
AppKey = shopSettingRequest.AppKey,
AppSecret = shopSettingRequest.AppSecret,
AppToken = shopSettingRequest.AppToken,
CreateTime = DateTime.Now,
CreatorId = "",
Deleted = 0,
PurchasePlatformId = ((int)shopSettingRequest.PurchasePlatformId).ToString(),
ShopId = mdsShop.Id
};
freeSqlMultiDBManager.BBWYfsql.Insert(pa).ExecuteAffrows();
freeSqlMultiDBManager.MDSfsql.Transaction(() =>
{
freeSqlMultiDBManager.MDSfsql.Insert(mdspa).ExecuteAffrows();
//修改扣点和管理密码
freeSqlMultiDBManager.MDSfsql.Update<Shops>(mdsShop.Id).Set(s => s.ManagePwd, shopSettingRequest.ManagerPwd)
.Set(s => s.PlatformCommissionRatio, shopSettingRequest.PlatformCommissionRatio)
.Set(s => s.DingDingKey, shopSettingRequest.DingDingKey)
.Set(s => s.DingDingWebHook, shopSettingRequest.DingDingWebHook)
.Set(s => s.SkuSafeTurnoverDays, shopSettingRequest.SkuSafeTurnoverDays)
.Set(s => s.SiNanDingDingKey, shopSettingRequest.SiNanDingDingKey)
.Set(s => s.SiNanDingDingWebHook, shopSettingRequest.SiNanDingDingWebHook)
.Set(s => s.SiNanPolicyLevel, shopSettingRequest.SiNanPolicyLevel)
.Set(s => s.QiKuDingDingKey, shopSettingRequest.QiKuDingDingKey)
.Set(s => s.QiKuDingDingWebHook, shopSettingRequest.QiKuDingDingWebHook)
.ExecuteAffrows();
});
}
else
{
freeSqlMultiDBManager.BBWYfsql.Update<PurchaseAccount>(shopSettingRequest.PurchaseAccountId)
.Set(pa => pa.AppKey, shopSettingRequest.AppKey)
.Set(pa => pa.AppSecret, shopSettingRequest.AppSecret)
.Set(pa => pa.AppToken, shopSettingRequest.AppToken)
.Set(pa => pa.AccountName, shopSettingRequest.AccountName)
.Set(pa => pa.PurchasePlatformId, shopSettingRequest.PurchasePlatformId)
.ExecuteAffrows();
freeSqlMultiDBManager.MDSfsql.Transaction(() =>
{
freeSqlMultiDBManager.MDSfsql.Update<Purchaseaccount>(shopSettingRequest.PurchaseAccountId.ToString())
.Set(pa => pa.AppKey, shopSettingRequest.AppKey)
.Set(pa => pa.AppSecret, shopSettingRequest.AppSecret)
.Set(pa => pa.AppToken, shopSettingRequest.AppToken)
.Set(pa => pa.AccountName, shopSettingRequest.AccountName)
.Set(pa => pa.PurchasePlatformId, ((int)shopSettingRequest.PurchasePlatformId).ToString())
.ExecuteAffrows();
//修改扣点和管理密码
freeSqlMultiDBManager.MDSfsql.Update<Shops>(mdsShop.Id).Set(s => s.ManagePwd, shopSettingRequest.ManagerPwd)
.Set(s => s.PlatformCommissionRatio, shopSettingRequest.PlatformCommissionRatio)
.Set(s => s.DingDingKey, shopSettingRequest.DingDingKey)
.Set(s => s.DingDingWebHook, shopSettingRequest.DingDingWebHook)
.Set(s => s.SkuSafeTurnoverDays, shopSettingRequest.SkuSafeTurnoverDays)
.Set(s => s.SiNanDingDingKey, shopSettingRequest.SiNanDingDingKey)
.Set(s => s.SiNanDingDingWebHook, shopSettingRequest.SiNanDingDingWebHook)
.Set(s => s.SiNanPolicyLevel, shopSettingRequest.SiNanPolicyLevel)
.Set(s => s.QiKuDingDingKey, shopSettingRequest.QiKuDingDingKey)
.Set(s => s.QiKuDingDingWebHook, shopSettingRequest.QiKuDingDingWebHook)
.ExecuteAffrows();
});
}
return shopSettingRequest.PurchaseAccountId;
}
public IList<DepartmentResponse> GetDeparmentList()
{
var relationGroups = freeSqlMultiDBManager.MDSfsql.Select<Userdepartment, Shopdepartment, Shops>()
.InnerJoin((ud, sd, s) => ud.Id == sd.DepartmentId)
.InnerJoin((ud, sd, s) => s.Id == sd.ShopId)
.Where((ud, sd, s) => !string.IsNullOrEmpty(s.ShopId))
.ToList((ud, sd, s) => new
{
DepartmentId = ud.Id,
ud.DepartmentName,
ShopPKId = s.Id,
s.ShopId,
s.ShopName,
s.ShopType,
s.AppKey,
s.AppSecret,
s.AppToken,
s.AppKey2,
s.AppSecret2,
s.AppToken2,
s.ManagePwd,
s.PlatformCommissionRatio,
s.PlatformId,
s.DingDingKey,
s.DingDingWebHook,
s.SkuSafeTurnoverDays,
s.SiNanDingDingKey,
s.SiNanDingDingWebHook,
s.SiNanPolicyLevel,
s.PJZSDingDingWebHook,
s.PJZSDingDingKey,
s.QiKuDingDingKey,
s.QiKuDingDingWebHook
}).GroupBy(x => x.DepartmentId);
if (relationGroups.Count() == 0)
return null;
var departmentList = new List<DepartmentResponse>();
var shopIdList = new List<string>(); //主键Id集合
foreach (var relationGroup in relationGroups)
{
var department = new DepartmentResponse()
{
Id = relationGroup.Key,
Name = relationGroup.FirstOrDefault().DepartmentName,
ShopList = relationGroup.Select(x => new ShopResponse()
{
Id = x.ShopPKId,
AppKey = string.IsNullOrEmpty(x.AppKey) ? "120EA9EC65AB017567D78CC1139EEEA5" : x.AppKey,
AppSecret = string.IsNullOrEmpty(x.AppSecret) ? "8a42bc2301e8439b896e99f5475e0a9b" : x.AppSecret,
AppToken = x.AppToken,
AppKey2 = x.AppKey2,
AppSecret2 = x.AppSecret2,
AppToken2 = x.AppToken2,
ManagePwd = x.ManagePwd,
PlatformId = (Enums.Platform)x.PlatformId,
PlatformCommissionRatio = x.PlatformCommissionRatio,
ShopId = x.ShopId,
ShopName = x.ShopName,
ShopType = x.ShopType,
DingDingKey = x.DingDingKey,
DingDingWebHook = x.DingDingWebHook,
SkuSafeTurnoverDays = x.SkuSafeTurnoverDays,
SiNanDingDingKey = x.SiNanDingDingKey,
SiNanDingDingWebHook = x.SiNanDingDingWebHook,
SiNanPolicyLevel = x.SiNanPolicyLevel,
PJZSDingDingKey = x.PJZSDingDingKey,
PJZSDingDingWebHook = x.PJZSDingDingWebHook,
QiKuDingDingKey = x.QiKuDingDingKey,
QiKuDingDingWebHook = x.QiKuDingDingWebHook
}).ToList()
};
departmentList.Add(department);
shopIdList.AddRange(department.ShopList.Select(s => s.Id));
}
shopIdList = shopIdList.Distinct().ToList();
var purchaseAccountList = freeSqlMultiDBManager.MDSfsql.Select<Purchaseaccount>().Where(pa => shopIdList.Contains(pa.ShopId))
.ToList(pa => new PurchaseAccountResponse()
{
AccountName = pa.AccountName,
AppKey = pa.AppKey,
AppSecret = pa.AppSecret,
AppToken = pa.AppToken,
Id = pa.Id,
ShopId = pa.ShopId,
PurchasePlatformId = (Enums.Platform)int.Parse(pa.PurchasePlatformId)
});
foreach (var d in departmentList)
{
foreach (var s in d.ShopList)
{
s.PurchaseList = purchaseAccountList.Where(pa => pa.ShopId == s.Id).ToList();
}
}
return departmentList;
}
public IList<ShopResponse> GetShopList(long? shopId = null, Enums.Platform? platform = null, bool filterTurnoverDays = false, bool filterSiNan = false, bool? isEnabled = true)
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.ShopId))
.WhereIf(shopId != null, s => s.ShopId == shopId.ToString())
.WhereIf(platform != null, s => s.PlatformId == (int)platform)
.WhereIf(filterTurnoverDays, s => s.SkuSafeTurnoverDays != 0)
.WhereIf(filterSiNan, s => !string.IsNullOrEmpty(s.SiNanDingDingWebHook))
.WhereIf(isEnabled != null, s => s.IsEnabled == isEnabled)
.ToList<ShopResponse>();
}
public ShopResponse GetShopByVenderId(string venderId)
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => s.VenderId == venderId)
.ToOne<ShopResponse>();
}
public IList<ShopResponse> GetShopListByShopIds(QueryShopByIdsRequest request)
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.ShopId))
.Where(s => request.ShopIds.Contains(s.ShopId))
.ToList<ShopResponse>();
}
public ShopResponse GetShopByShopId(string shopId)
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => s.ShopId == shopId).ToOne<ShopResponse>();
}
public IList<WaiterResponse> GetServiceGroupList(PlatformRequest request)
{
var relayAPIHost = GetPlatformRelayAPIHost(request.Platform);
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetServiceGroupList", request, GetYunDingRequestHeader(), HttpMethod.Post);
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
var response = JsonConvert.DeserializeObject<ApiResponse<IList<WaiterResponse>>>(sendResult.Content);
if (!response.Success)
throw new BusinessException(response.Msg) { Code = response.Code };
return response.Data;
}
public IList<Storehouse> GetStoreHouseList(PlatformRequest request)
{
var restApiResult = restApiService.SendRequest(GetPlatformRelayAPIHost(request.Platform), "api/PlatformSDK/GetStoreHouseList", new PlatformRequest()
{
AppKey = request.AppKey,
AppSecret = request.AppSecret,
AppToken = request.AppToken,
Platform = request.Platform,
SaveResponseLog = false
}, GetYunDingRequestHeader(), HttpMethod.Post);
if (restApiResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(restApiResult.Content);
var response = JsonConvert.DeserializeObject<ApiResponse<JArray>>(restApiResult.Content);
if (response.Data == null || response.Data.Count() == 0)
return null;
var storeHouseList = new List<Storehouse>();
foreach (var storeHouseJToken in response.Data)
{
var seq_num = storeHouseJToken.Value<string>("seq_num");
var storeHouse = new Storehouse()
{
Id = seq_num,
Name = storeHouseJToken.Value<string>("name"),
Platform = request.Platform,
CreateTime = DateTime.Now,
Status = (Enums.StockStatus)storeHouseJToken.Value<int>("use_flag")
//Type = type
};
storeHouseList.Add(storeHouse);
var type = (Enums.StockType)storeHouseJToken.Value<int>("type");
if (storeHouse.Name.Contains("云仓"))
type = Enums.StockType.;
storeHouse.Type = type;
}
return storeHouseList;
}
}
}