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 System; using System.Collections.Generic; 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 options, IIdGenerator idGenerator) : base(restApiService, options) { 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, null, HttpMethod.Post); if (sendResult.StatusCode != System.Net.HttpStatusCode.OK) throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode }; var response = JsonConvert.DeserializeObject>(sendResult.Content); if (!response.Success) throw new BusinessException(response.Msg) { Code = response.Code }; return response.Data; } public IList 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>>(sendResult.Content); if (!response.Success) throw new BusinessException(response.Msg) { Code = response.Code }; 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() { { "qy", "qy" } }, HttpMethod.Post); } public long SaveShopSetting(ShopSettingRequest shopSettingRequest) { //根据shopId查询mds shop的主键Id var shopId = shopSettingRequest.ShopId.ToString(); var mdsShop = freeSqlMultiDBManager.MDSfsql.Select().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(mdsShop.Id).Set(s => s.ManagePwd, shopSettingRequest.ManagerPwd) .Set(s => s.PlatformCommissionRatio, shopSettingRequest.PlatformCommissionRatio) .ExecuteAffrows(); }); } else { freeSqlMultiDBManager.BBWYfsql.Update(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) .ExecuteAffrows(); freeSqlMultiDBManager.MDSfsql.Transaction(() => { freeSqlMultiDBManager.MDSfsql.Update(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) .ExecuteAffrows(); //修改扣点和管理密码 freeSqlMultiDBManager.MDSfsql.Update(mdsShop.Id).Set(s => s.ManagePwd, shopSettingRequest.ManagerPwd) .Set(s => s.PlatformCommissionRatio, shopSettingRequest.PlatformCommissionRatio) .ExecuteAffrows(); }); } return shopSettingRequest.PurchaseAccountId; } } }