| | |
| | | /*********************************************************************** |
| | | * Project: CoreCms |
| | | * Project: baifenBinfa |
| | | * ProjectName: 百分兵法管理系统 |
| | | * Web: hhtp://chuanyin.com |
| | | * Web: http://chuanyin.com |
| | | * Author: |
| | | * Email: |
| | | * CreateTime: 202403/02 |
| | |
| | | using CoreCms.Net.IServices; |
| | | using CoreCms.Net.Loging; |
| | | using CoreCms.Net.Model.Entities; |
| | | 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 |
| | |
| | | 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; |
| | | |
| | | public CoreCmsDistributionServices(IUnitOfWork unitOfWork, ICoreCmsDistributionRepository dal, ICoreCmsDistributionOrderRepository distributionOrderRepository, ICoreCmsUserRepository userRepository, ICoreCmsGoodsRepository goodsRepository, ICoreCmsUserGradeRepository userGradeRepository, ICoreCmsSettingServices settingServices, ICoreCmsOrderRepository orderRepository, ICoreCmsDistributionGradeRepository distributionGradeRepository, ICoreCmsDistributionResultRepository distributionResultRepository, ICoreCmsDistributionConditionServices coreCmsDistributionConditionServices, ICoreCmsUserBalanceServices userBalanceServices) |
| | | { |
| | | this._dal = dal; |
| | | _distributionOrderRepository = distributionOrderRepository; |
| | |
| | | _coreCmsDistributionConditionServices = coreCmsDistributionConditionServices; |
| | | base.BaseDal = dal; |
| | | _unitOfWork = unitOfWork; |
| | | _userBalanceServices = userBalanceServices; |
| | | |
| | | |
| | | } |
| | | |
| | | #region 获取经销商信息 |
| | |
| | | { |
| | | 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> 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; |
| | | var 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; |
| | | |
| | | } |
| | | } |
| | | } |