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

229 lines
13 KiB

3 years ago

using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
3 years ago
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Db.Mds;
3 years ago
using BBWY.Server.Model.Dto;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
3 years ago
using System;
3 years ago
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
3 years ago
using Yitter.IdGenerator;
3 years ago
namespace BBWY.Server.Business
{
public class VenderBusiness : BasePlatformRelayBusiness, IDenpendency
{
3 years ago
private FreeSqlMultiDBManager freeSqlMultiDBManager;
private IIdGenerator idGenerator;
public VenderBusiness(FreeSqlMultiDBManager freeSqlMultiDBManager,
RestApiService restApiService,
IOptions<GlobalConfig> options,
IIdGenerator idGenerator) : base(restApiService, options)
3 years ago
{
3 years ago
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
this.idGenerator = idGenerator;
3 years ago
}
public VenderResponse GetVenderInfo(PlatformRequest platformRequest)
{
var relayAPIHost = GetPlatformRelayAPIHost(platformRequest.Platform);
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetVenderInfo", platformRequest, null, HttpMethod.Post);
3 years ago
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)
3 years ago
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, null, 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 };
3 years ago
return response.Data;
}
public void AcceptJDShopToken(JDShopToken jDShopToken)
{
var venderResponse = GetVenderInfo(new PlatformRequest()
{
AppKey = "120EA9EC65AB017567D78CC1139EEEA5",
AppSecret = "866a9877f5f24b03b537483b4defe75d",
AppToken = jDShopToken.Access_Token,
Platform = Enums.Platform.
});
_ = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/UpdateShop", new
{
venderResponse.ShopName,
venderResponse.ShopId,
ShopType = venderResponse.ColType,
AppToken = jDShopToken.Access_Token
}, new Dictionary<string, string>() { { "qy", "qy" } }, HttpMethod.Post);
}
3 years ago
public long SaveShopSetting(ShopSettingRequest shopSettingRequest)
{
3 years ago
//根据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}");
3 years ago
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(),
3 years ago
ShopId = mdsShop.Id
3 years ago
};
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)
.ExecuteAffrows();
3 years ago
});
}
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)
3 years ago
.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())
3 years ago
.ExecuteAffrows();
//修改扣点和管理密码
freeSqlMultiDBManager.MDSfsql.Update<Shops>(mdsShop.Id).Set(s => s.ManagePwd, shopSettingRequest.ManagerPwd)
.Set(s => s.PlatformCommissionRatio, shopSettingRequest.PlatformCommissionRatio)
.ExecuteAffrows();
3 years ago
});
}
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.ManagePwd,
s.PlatformCommissionRatio,
s.PlatformId
}).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) ? "866a9877f5f24b03b537483b4defe75d" : x.AppSecret,
AppToken = x.AppToken,
ManagePwd = x.ManagePwd,
3 years ago
PlatformId = (Enums.Platform)x.PlatformId,
PlatformCommissionRatio = x.PlatformCommissionRatio,
ShopId = x.ShopId,
ShopName = x.ShopName,
3 years ago
ShopType = x.ShopType
}).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()
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.ShopId)).ToList<ShopResponse>();
}
3 years ago
}
}