| | |
| | | /*********************************************************************** |
| | | * Project: CoreCms |
| | | * Project: baifenBinfa |
| | | * ProjectName: 百分兵法管理系统 |
| | | * Web: hhtp://chuanyin.com |
| | | * Web: http://chuanyin.com |
| | | * Author: |
| | | * Email: |
| | | * CreateTime: 202403/02 |
| | |
| | | using CoreCms.Net.IRepository; |
| | | using CoreCms.Net.IRepository.UnitOfWork; |
| | | using CoreCms.Net.IServices; |
| | | using CoreCms.Net.IServices.baifenbingfa; |
| | | using CoreCms.Net.Loging; |
| | | using CoreCms.Net.Model.Entities; |
| | | using CoreCms.Net.Model.Entities.baifenbingfa.apply; |
| | | using CoreCms.Net.Model.Entities.Distribution; |
| | | using CoreCms.Net.Model.ViewModels.Basics; |
| | | using CoreCms.Net.Model.ViewModels.DTO.Distribution; |
| | | using CoreCms.Net.Model.ViewModels.UI; |
| | |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | using NPOI.SS.Formula.Functions; |
| | | using Org.BouncyCastle.Asn1.Cmp; |
| | | using SqlSugar; |
| | | using static CoreCms.Net.Configuration.GlobalEnumVars; |
| | | |
| | | |
| | | namespace CoreCms.Net.Services |
| | | { |
| | | /// <summary> |
| | | /// 分销商表 接口实现 |
| | | /// 经销商表 接口实现 |
| | | /// </summary> |
| | | public class CoreCmsDistributionServices : BaseServices<CoreCmsDistribution>, ICoreCmsDistributionServices |
| | | { |
| | |
| | | private readonly ICoreCmsOrderRepository _orderRepository; |
| | | private readonly ICoreCmsSettingServices _settingServices; |
| | | private readonly IUnitOfWork _unitOfWork; |
| | | public CoreCmsDistributionServices(IUnitOfWork unitOfWork, ICoreCmsDistributionRepository dal, ICoreCmsDistributionOrderRepository distributionOrderRepository, ICoreCmsUserRepository userRepository, ICoreCmsGoodsRepository goodsRepository, ICoreCmsUserGradeRepository userGradeRepository, ICoreCmsSettingServices settingServices, ICoreCmsOrderRepository orderRepository, ICoreCmsDistributionGradeRepository distributionGradeRepository, ICoreCmsDistributionResultRepository distributionResultRepository, ICoreCmsDistributionConditionServices coreCmsDistributionConditionServices) |
| | | private readonly ICoreCmsUserBalanceServices _userBalanceServices; |
| | | private readonly ICoreCmsUserServices _coreCmsUserServices; |
| | | private readonly IBfbfComAPIService _bfbfComAPIService; |
| | | |
| | | |
| | | |
| | | public CoreCmsDistributionServices(IBfbfComAPIService bfbfComAPIService, IUnitOfWork unitOfWork, ICoreCmsDistributionRepository dal, ICoreCmsDistributionOrderRepository distributionOrderRepository, ICoreCmsUserRepository userRepository, ICoreCmsGoodsRepository goodsRepository, ICoreCmsUserGradeRepository userGradeRepository, ICoreCmsSettingServices settingServices, ICoreCmsOrderRepository orderRepository, ICoreCmsDistributionGradeRepository distributionGradeRepository, ICoreCmsDistributionResultRepository distributionResultRepository, ICoreCmsDistributionConditionServices coreCmsDistributionConditionServices, ICoreCmsUserBalanceServices userBalanceServices |
| | | , ICoreCmsUserServices coreCmsUserServices) |
| | | { |
| | | this._dal = dal; |
| | | _distributionOrderRepository = distributionOrderRepository; |
| | |
| | | _coreCmsDistributionConditionServices = coreCmsDistributionConditionServices; |
| | | base.BaseDal = dal; |
| | | _unitOfWork = unitOfWork; |
| | | _userBalanceServices = userBalanceServices; |
| | | _coreCmsUserServices = coreCmsUserServices; |
| | | |
| | | |
| | | _bfbfComAPIService = bfbfComAPIService; |
| | | |
| | | |
| | | } |
| | | |
| | | #region 获取分销商信息 |
| | | #region 获取经销商信息 |
| | | /// <summary> |
| | | /// 获取分销商信息 |
| | | /// 获取经销商信息 |
| | | /// </summary> |
| | | /// <param name="userId">用户id</param> |
| | | /// <param name="checkStatus">是否检查满足条件</param> |
| | |
| | | info.TotalSettlementAmount = await _distributionOrderRepository.GetSumAsync( |
| | | p => p.isSettlement != (int)GlobalEnumVars.DistributionOrderSettlementStatus.SettlementCancel && p.userId == userId, |
| | | p => p.amount); |
| | | var lisdata = await _unitOfWork.GetDbClient().Queryable<CoreCmsDistributionOrder>().Where(x=>x.userId == userId&&x.isSettlement== (int)GlobalEnumVars.DistributionOrderSettlementStatus.SettlementNo) |
| | | .Select(x=>new CoreCmsDistributionOrder |
| | | { |
| | | amount = x.amount, |
| | | OderState= SqlFunc.Subqueryable<CoreCmsOrder>().Where(c=>c.orderId==x.orderId).Select(c=>c.confirmStatus), |
| | | |
| | | }).WithCache(10).ToListAsync(); |
| | | //可领金额 |
| | | info.DistributionKeLingTotals = lisdata.Where(x=>x.OderState== (int)GlobalEnumVars.OrderConfirmStatus.ConfirmReceipt |
| | | || x.OderState == (int)GlobalEnumVars.OrderShipStatus.Yes).Sum(x=>x.amount); |
| | | |
| | | //应领金额 |
| | | info.DistributionYingLingTotals = lisdata.Where(x => x.OderState == (int)GlobalEnumVars.OrderConfirmStatus.ReceiptNotConfirmed |
| | | || x.OderState == (int)GlobalEnumVars.OrderShipStatus.Yes).Sum(x => x.amount); ; |
| | | //已结算金额 |
| | | info.SettlementAmount = await _distributionOrderRepository.GetSumAsync( |
| | | p => p.isSettlement == (int)GlobalEnumVars.DistributionOrderSettlementStatus.SettlementYes && p.userId == userId, |
| | |
| | | } |
| | | #endregion |
| | | |
| | | #region 检查是否可以成为分销商 |
| | | //检查是否可以成为分销商 |
| | | #region 检查是否可以成为经销商 |
| | | //检查是否可以成为经销商 |
| | | public async Task CheckCondition(Dictionary<string, DictionaryKeyValues> allConfigs, CoreCmsDistribution info, int userId = 0) |
| | | { |
| | | //判断消费 |
| | | info.ConditionStatus = false;//条件状态 |
| | | info.ConditionProgress = 0; |
| | | //获取成为分销商条件 |
| | | //获取成为经销商条件 |
| | | //var DistributionType = CommonHelper.GetConfigDictionary(allConfigs, GlobalSettingConstVars.DistributionType).ObjectToInt(0); |
| | | var distributionMoney = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DistributionMoney).ObjectToInt(0); |
| | | //支付金额 |
| | |
| | | } |
| | | else |
| | | { |
| | | info.ConditionMsg = "您的条件未满足,请任意购买一件商品即可成为分销商。"; |
| | | info.ConditionMsg = "您的条件未满足,请任意购买一件商品即可成为经销商。"; |
| | | } |
| | | } |
| | | else if (distributionGoods is > 0 and 3) //购买指定商品 |
| | |
| | | var goodsInfo = await _goodsRepository.QueryByIdAsync(distributionGoodsId); |
| | | if (goodsInfo != null) |
| | | { |
| | | info.ConditionMsg = "您的条件未满足,请购买指定的【" + goodsInfo.name + "】商品即可成为分销商。"; |
| | | info.ConditionMsg = "您的条件未满足,请购买指定的【" + goodsInfo.name + "】商品即可成为经销商。"; |
| | | } |
| | | else |
| | | { |
| | | info.ConditionMsg = "您的条件未满足,请购买指定的商品即可成为分销商。"; |
| | | info.ConditionMsg = "您的条件未满足,请购买指定的商品即可成为经销商。"; |
| | | |
| | | } |
| | | } |
| | |
| | | public async Task<WebApiCallBack> GetGradeAndCommission(int userId) |
| | | { |
| | | var jm = new WebApiCallBack(); |
| | | //百分兵法判断是否是经销商 |
| | | if(!await _bfbfComAPIService.IsDictionary(userId)) |
| | | { |
| | | jm.msg = "不是经销商的,不返利"; |
| | | return jm; |
| | | } |
| | | |
| | | var info = await _dal.QueryByClauseAsync(p => p.userId == userId); |
| | | if (info == null) |
| | | { |
| | | jm.msg = "不是分销商的,不返利"; |
| | | jm.msg = "不是经销商的,不返利"; |
| | | return jm; |
| | | } |
| | | var allConfigs = await _settingServices.GetConfigDictionaries(); |
| | |
| | | var info = await _dal.QueryByClauseAsync(p => p.userId == userId); |
| | | if (info is { gradeId: > 0 }) |
| | | { |
| | | //找下有没有可以升级的分销商等级(一次只升一级) |
| | | //找下有没有可以升级的经销商等级(一次只升一级) |
| | | var grade = await _distributionGradeRepository.QueryByClauseAsync(p => p.sortId > info.gradeId && p.isAutoUpGrade == true, p => p.sortId, OrderByType.Asc, true); |
| | | if (grade == null) return jm; |
| | | { |
| | |
| | | { |
| | | return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock); |
| | | } |
| | | |
| | | |
| | | #endregion |
| | | |
| | | public async Task<(decimal,DateTime?, DateTime?)> AchievementTotal(int UserId) |
| | | { |
| | | var StartTime= await _unitOfWork.GetDbClient() |
| | | .Queryable<DistributionAchievement>() |
| | | .Where(x=>x.userID == UserId&&x.isdelete==false) |
| | | .Select(x=>x.endTime) |
| | | .OrderByDescending(x=>x) |
| | | .FirstAsync(); |
| | | if (StartTime == null) |
| | | ///最多统计两年内的业绩 |
| | | StartTime = DateTime.Now.AddYears(-2); |
| | | else |
| | | StartTime= StartTime.Value.AddSeconds(1); |
| | | var endTime = DateTime.Now; |
| | | //payStatus = (int)GlobalEnumVars.OrderPayStatus.Refunded, |
| | | // status = (int)GlobalEnumVars.OrderStatus.Complete |
| | | //var result = await _balanceServices.Change(item.userId, (int)GlobalEnumVars.UserBalanceSourceTypes.Distribution, |
| | | // item.amount, item.orderId); |
| | | var total= await _unitOfWork.GetDbClient().Queryable<CoreCmsOrder>().Where(x => x.status == (int)GlobalEnumVars.OrderStatus.Complete |
| | | && (x.payStatus != (int)GlobalEnumVars.OrderPayStatus.No || x.payStatus != (int)GlobalEnumVars.OrderPayStatus.Refunded) |
| | | && x.isdel == false |
| | | && x.updateTime > StartTime |
| | | && x.updateTime < endTime |
| | | ).SumAsync(x => x.orderAmount); |
| | | |
| | | var orderToler= await _unitOfWork.GetDbClient().Queryable<DistributionAchievementOrder>().Where(x => x.userID == UserId |
| | | && x.createTime > StartTime |
| | | && x.createTime < endTime).SumAsync(x => x.achievementTotal); |
| | | total = total + orderToler; |
| | | if (total==0) |
| | | return (total,null, null); |
| | | else |
| | | return (total, StartTime, endTime); |
| | | } |
| | | |
| | | public async Task<decimal> AchievementBonus(decimal AttributeTotal) |
| | | { |
| | | |
| | | var allConfigs = await _settingServices.GetConfigDictionaries(); |
| | | var key = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryAchievementSet).ObjectToInt(0); |
| | | return AttributeTotal *(decimal)key / 100; |
| | | } |
| | | |
| | | public async Task<bool> SettledAchievement(int UserId) |
| | | { |
| | | //非供应商,停止 |
| | | var da = await _dal.GetCountAsync(x => x.userId == UserId && x.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes, isDataCache: true, cacheTimes: 1); |
| | | if (da == 0) |
| | | return true; |
| | | var att = await AchievementTotal(UserId); |
| | | if(att.Item1>0) |
| | | { |
| | | DistributionAchievement oder = new DistributionAchievement |
| | | { |
| | | achievementTotals = att.Item1, |
| | | achievementBonus = await AchievementBonus(att.Item1), |
| | | createBy="系统结算", |
| | | createTime=DateTime.Now, |
| | | startTime=att.Item2, |
| | | endTime = att.Item3, |
| | | userID = UserId |
| | | |
| | | }; |
| | | //建立结算单 |
| | | var id= await _unitOfWork.GetDbClient().Insertable(oder).ExecuteCommandIdentityIntoEntityAsync(); |
| | | var a= await _userBalanceServices.Change(UserId, (int )UserBalanceSourceTypes.DistributionAchievement, |
| | | oder.achievementBonus,id.ToString()); |
| | | if(a.code!=0) |
| | | { |
| | | NLogUtil.WriteAll(NLog.LogLevel.Fatal, LogType.DataBase, "经销商业绩提成写入余额失败", a.msg); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | |
| | | public async Task<bool> SettledAllAchievement() |
| | | { |
| | | |
| | | |
| | | //查询用户 |
| | | var coreCmsUsers = await _userRepository.QueryListByClauseAsync(x => x.isDelete == false && x.status == 1); |
| | | |
| | | foreach(var coreCmsUser in coreCmsUsers) |
| | | { //非供应商,停止 |
| | | var da = await _dal.GetCountAsync(x => x.userId == coreCmsUser.id && x.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes, isDataCache: true, cacheTimes: 1); |
| | | if (da == 0) |
| | | continue; |
| | | |
| | | //查询是否已经计算过了 |
| | | DateTime StartTime = new DateTime(DateTime.Now.AddYears(-1).Year, 1, 1); |
| | | DateTime endTime = new DateTime(DateTime.Now.Year, 1, 1).AddDays(-1); |
| | | var total = _unitOfWork.GetDbClient().Queryable<DistributionAchievement>().Where(x => |
| | | x.isdelete == false |
| | | && x.userID == coreCmsUser.id |
| | | && x.startTime == StartTime |
| | | && x.endTime == endTime |
| | | ).ToList(); |
| | | if (total != null && total.Count > 0) |
| | | continue; |
| | | |
| | | var att = await AchievementTotalAll(coreCmsUser.id); |
| | | if (att.Item1 > 0) |
| | | { |
| | | DistributionAchievement oder = new DistributionAchievement |
| | | { |
| | | achievementTotals = att.Item1, |
| | | achievementBonus = await AchievementBonuscount(att.Item1), |
| | | createBy = "系统结算", |
| | | createTime = DateTime.Now, |
| | | startTime = att.Item2, |
| | | endTime = att.Item3, |
| | | userID = coreCmsUser.id |
| | | |
| | | }; |
| | | //建立结算单 |
| | | var id = await _unitOfWork.GetDbClient().Insertable(oder).ExecuteCommandIdentityIntoEntityAsync(); |
| | | if (oder.achievementBonus > 0) |
| | | { |
| | | var jm = await _coreCmsUserServices.UpdateBalance(coreCmsUser.id, oder.achievementBonus, "经销商推广费用写入余额"); |
| | | if (jm.code != 0) |
| | | { |
| | | NLogUtil.WriteAll(NLog.LogLevel.Fatal, LogType.DataBase, "经销商推广费用写入余额失败", jm.msg); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public async Task<(decimal, DateTime?, DateTime?)> AchievementTotalAll(int UserId) |
| | | { |
| | | |
| | | |
| | | DateTime StartTime = new DateTime(DateTime.Now.AddYears(-1).Year, 1, 1); |
| | | DateTime endTime = new DateTime(DateTime.Now.Year, 1, 1); |
| | | |
| | | |
| | | var total = await _unitOfWork.GetDbClient().Queryable<CoreCmsOrder>().Where(x => x.status == (int)GlobalEnumVars.OrderStatus.Complete |
| | | && (x.payStatus != (int)GlobalEnumVars.OrderPayStatus.No || x.payStatus != (int)GlobalEnumVars.OrderPayStatus.Refunded) |
| | | && x.isdel == false |
| | | && x.userId == UserId |
| | | && x.updateTime > StartTime |
| | | && x.updateTime < endTime |
| | | ).SumAsync(x => x.orderAmount); |
| | | |
| | | return (total, StartTime, endTime.AddDays(-1)); |
| | | } |
| | | |
| | | |
| | | public async Task<decimal> AchievementBonuscount(decimal AttributeTotal) |
| | | { |
| | | |
| | | var allConfigs = await _settingServices.GetConfigDictionaries(); |
| | | var tuiGuangFeiYong = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryTuiGuangFeiYong).ObjectToInt(0); |
| | | var yeJiMenKan = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryYeJiMenKan).ObjectToInt(0); |
| | | if(AttributeTotal <= yeJiMenKan) |
| | | { |
| | | return 0; |
| | | } |
| | | return (AttributeTotal - (decimal)yeJiMenKan) * (decimal)tuiGuangFeiYong / 100; |
| | | } |
| | | |
| | | |
| | | public async Task<bool> CreateDistributionAchievementOrder(CreateDAOrderInParam Parma) |
| | | { |
| | | //非供应商,停止 |
| | | var da= await _dal.GetCountAsync(x=>x.userId== Parma.UserID && x.verifyStatus== (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes,isDataCache:true,cacheTimes:1); |
| | | if (da == 0) |
| | | return true; |
| | | DistributionAchievementOrder oder = new DistributionAchievementOrder |
| | | { |
| | | achievementTotal = Parma.Money, |
| | | createBy = "经销商业绩写入", |
| | | createTime = DateTime.Now, |
| | | orderId = Parma.OderId, |
| | | userID = Parma.UserID, |
| | | orderType=(int ) Parma.SourceTypes |
| | | |
| | | |
| | | |
| | | |
| | | }; |
| | | |
| | | return await _unitOfWork.GetDbClient().Updateable(oder).ExecuteCommandAsync()>0; |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取经销商推广费 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public List<DistributionRankingDTO> QueryAchievement(int UserId) |
| | | { |
| | | var totals = _unitOfWork.GetDbClient().Queryable<DistributionAchievement>().Where(x => |
| | | x.isdelete == false |
| | | && x.userID == UserId |
| | | ).OrderByDescending(x=>x.startTime).ToList(); |
| | | |
| | | var distributionRankingDTOs = new List<DistributionRankingDTO>(); |
| | | foreach(var distributionAchievement in totals) |
| | | { |
| | | var distributionRankingDTO = new DistributionRankingDTO(); |
| | | distributionRankingDTO.startTime = distributionAchievement.startTime.HasValue ? distributionAchievement.startTime.Value.ToString("yyyy-MM-dd") : ""; |
| | | distributionRankingDTO.endTime = distributionAchievement.endTime.HasValue ? distributionAchievement.endTime.Value.ToString("yyyy-MM-dd") : ""; |
| | | distributionRankingDTO.achievementTotals = distributionAchievement.achievementTotals.ToString("F2"); |
| | | distributionRankingDTO.achievementBonus = distributionAchievement.achievementBonus.ToString("F2"); |
| | | distributionRankingDTOs.Add(distributionRankingDTO); |
| | | } |
| | | |
| | | return distributionRankingDTOs; |
| | | } |
| | | } |
| | | } |