using BBWYB.Common.Log; using BBWYB.Common.Models; using BBWYB.Server.Model; using BBWYB.Server.Model.Db; using BBWYB.Server.Model.Db.Mds; using BBWYB.Server.Model.Db.MDS; using BBWYB.Server.Model.Dto; using Org.BouncyCastle.Ocsp; using SDKAdapter.OperationPlatform.Client; using SDKAdapter.OperationPlatform.Models; using Yitter.IdGenerator; namespace BBWYB.Server.Business { public class VenderBusiness : BaseBusiness, IDenpendency { private OP_PlatformClientFactory opPlatformClientFactory; private FreeSqlMultiDBManager fsqlManager; private KuaiDi100Manager kuaiDi100Manager; public VenderBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, FreeSqlMultiDBManager fsqlManager, OP_PlatformClientFactory opPlatformClientFactory, KuaiDi100Manager kuaiDi100Manager) : base(fsql, nLogManager, idGenerator) { this.fsqlManager = fsqlManager; this.opPlatformClientFactory = opPlatformClientFactory; this.kuaiDi100Manager = kuaiDi100Manager; } public IList GetShopList(long? shopId = null, Enums.Platform? platform = null, string shopName = "") { return fsqlManager.MDSfsql.Select().Where(s => !string.IsNullOrEmpty(s.ShopId)) .WhereIf(shopId != null, s => s.ShopId == shopId.ToString()) .WhereIf(platform != null, s => s.PlatformId == (int)platform) .WhereIf(!string.IsNullOrEmpty(shopName), s => s.ShopName == shopName) .ToList(); } public IList GetShopList(List shopIds) { return fsqlManager.MDSfsql.Select().Where(s => !string.IsNullOrEmpty(s.ShopId) && shopIds.Contains(s.ShopId)) .ToList(); } public IList GetYiJiaGroup() { return fsqlManager.MDSfsql.Select().Where(d => d.ParentDepartmentId == "1760971468360912896").ToList(); } public string SaveShopSetting(ShopSettingRequest shopSettingRequest) { //根据shopId查询mds shop的主键Id var shopId = shopSettingRequest.ShopId.ToString(); var mdsShop = fsqlManager.MDSfsql.Select().Where(s => s.ShopId == shopId).ToOne(); if (mdsShop == null) throw new BusinessException($"mds未找到shopId {shopSettingRequest.ShopId}"); if (string.IsNullOrEmpty(shopSettingRequest.PurchaseAccountId) || shopSettingRequest.PurchaseAccountId == "0") { var isRepeat = fsqlManager.MDSfsql.Select().Where(pa => pa.ShopId == mdsShop.Id && pa.AppKey == shopSettingRequest.AppKey && pa.AppToken == shopSettingRequest.AppToken).Any(); if (isRepeat) throw new BusinessException($"采购账号重复"); shopSettingRequest.PurchaseAccountId = idGenerator.NewLong().ToString(); var mdspa = new Purchaseaccount() { Id = shopSettingRequest.PurchaseAccountId, 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 }; fsqlManager.MDSfsql.Insert(mdspa).ExecuteAffrows(); } else { fsqlManager.MDSfsql.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) .Set(pa => pa.PurchasePlatformId, ((int)shopSettingRequest.PurchasePlatformId).ToString()) .ExecuteAffrows(); } return shopSettingRequest.PurchaseAccountId; } public IList GetPurchaserList(QueryPurchaseAccountRequest request) { var purchasePlatofrmId = request.PurchasePlatofrmId != null ? ((int)request.PurchasePlatofrmId).ToString() : string.Empty; var shopIdStr = request.ShopId.ToString(); var mdsShop = fsqlManager.MDSfsql.Select().Where(s => s.ShopId == shopIdStr).ToOne(); var plist = fsqlManager.MDSfsql.Select() .Where(pa => pa.ShopId == mdsShop.Id && pa.Deleted == 0) .WhereIf(!string.IsNullOrEmpty(purchasePlatofrmId), pa => pa.PurchasePlatformId == purchasePlatofrmId) .WhereIf(!string.IsNullOrEmpty(request.AccountName), pa => pa.AccountName == request.AccountName) .ToList(); foreach (var pa in plist) pa.ShopId = shopIdStr; return plist; } public void DeletePurchaseAccount(long purchaseAccountId) { var pk = purchaseAccountId.ToString(); fsqlManager.MDSfsql.Update(pk).Set(pa => pa.Deleted, 1).ExecuteAffrows(); } public IList GetExpressCompanyList(PlatformRequest request) { return opPlatformClientFactory.GetClient((SDKAdapter.AdapterEnums.PlatformType)request.Platform) .GetExpressCompanyList(new OP_QueryExpressCompanyRequest() { AppKey = request.AppKey, AppSecret = request.AppSecret, AppToken = request.AppToken, Platform = (SDKAdapter.AdapterEnums.PlatformType)request.Platform }); } public IList GetKuaiDi100ExpressCompanyList(KuaiDi100ExpressSearchRequest request) { return kuaiDi100Manager.GetKuaiDi100ExpressCompanyList(request); } } }