Browse Source

项目结算

zmq 1 year ago
parent
commit
4726e2898b

+ 10 - 7
src/platform/ZhonTai.Admin/Core/Db/DbHelper.cs

@@ -199,13 +199,16 @@ public class DbHelper
                     {                        
                         e.Value = user.TenantId;
                     }                  
-                    if (e.Value != null && (long)e.Value == -1) {
-                        e.Value = "";
-                    }
-                    if (e.Value != null && (long)e.Value!=-1)
-                    {
-                        e.Value = e.Value;
-                    }
+                    if (e.Value != null) {
+                        if ((long)e.Value == -1)
+                        {
+                            e.Value = "";
+                        }
+                        else {
+                            e.Value = e.Value;
+                        }
+                        
+                    }                    
                     break;
 
             }

+ 12 - 4
src/platform/ZhonTai.Admin/Domain/Project/ProjectStatEntity.cs

@@ -61,15 +61,23 @@ namespace ZhonTai.Admin.Domain.Project
         /// <summary>
         /// 公司佣金
         /// </summary>
-        public decimal CompanyCommission { get; set; }
-        public decimal CompanyRatio { get; set; }
+        public decimal? CompanyCommission { get; set; }
+        public decimal? CompanyRatio { get; set; }
         /// <summary>
         /// 平台佣金
         /// </summary>
-        public decimal TenantCommission { get; set; }
+        public decimal? TenantCommission { get; set; }
         /// <summary>
         /// 平台比率
         /// </summary>
-        public decimal TenantyRatio { get; set; }
+        public decimal? TenantyRatio { get; set; }
+        /// <summary>
+        /// 用户佣金
+        /// </summary>
+        public decimal? UserCommission { get; set; }
+        /// <summary>
+        /// 用户角色
+        /// </summary>
+        public string UserRole { get; set; }
     }
 }

+ 1 - 0
src/platform/ZhonTai.Admin/Services/ProjectStat/Dto/GetCommissionOutput.cs

@@ -12,5 +12,6 @@ namespace ZhonTai.Admin.Services.ProjectStat.Dto
         public decimal TenantCommission { get; set; }        
         public decimal CompanyCommission { get; set; }
         public decimal CompanyRatio { get; set; }
+        public decimal UserCommission { get; set; }
     }
 }

+ 229 - 70
src/platform/ZhonTai.Admin/Services/ProjectStat/ProjectStatService.cs

@@ -27,6 +27,10 @@ using ZhonTai.Common.Extensions;
 using ZhonTai.Admin.Repositories.Project;
 using ZhonTai.Admin.Services.Project.Dto;
 using ZhonTai.Admin.Repositories.ProjectLink;
+using ZhonTai.Admin.Domain.KuaKe;
+using ZhonTai.Admin.Domain.Platform;
+using ZhonTai.Admin.Repositories.Platform;
+using StackExchange.Profiling.Data;
 
 namespace ZhonTai.Admin.Services.ProjectStat
 {
@@ -45,6 +49,8 @@ namespace ZhonTai.Admin.Services.ProjectStat
         private IHttpContextAccessor _httpContextAccessor => LazyGetRequiredService<IHttpContextAccessor>();
         private IProjectLinkRepository _projectLinkRepository => LazyGetRequiredService<IProjectLinkRepository>();
         private IProjectConfigRepository _projectConfigRepository => LazyGetRequiredService<IProjectConfigRepository>();
+        private IKuaKeRepository _kuaKeRepository => LazyGetRequiredService<IKuaKeRepository>();
+        private IPlatformUserRepository _platformUserRepository => LazyGetRequiredService<IPlatformUserRepository>();
 
         public ProjectStatService()
         {
@@ -185,7 +191,12 @@ namespace ZhonTai.Admin.Services.ProjectStat
                 {
                     throw new Exception($"文件不能为空");
                 }
-                var listPrice = await _projectPriceRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId).ToListAsync();
+                var project = await _projectRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.Id == ProjectId).ToOneAsync();
+                if (project == null)
+                {
+                    throw new Exception($"项目不存在");
+                }
+                var listPrice = await _projectPriceRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == project.Id).ToListAsync();
                 if (listPrice.Count() <= 0)
                 {
                     throw new Exception($"项目不存在");
@@ -219,80 +230,220 @@ namespace ZhonTai.Admin.Services.ProjectStat
 
                     await _projectStatRepository.Orm.Delete<ProjectStatEntity>().DisableGlobalFilter(FilterNames.Tenant).Where(m => listFuGai.Contains(m.EffectDateNum)).ExecuteAffrowsAsync();
                 }
-                //二维码编号列表
-                List<string> lstID = (from d in datatable.AsEnumerable() select d.Field<string>("二维码编号")).ToList();
-                var listLink = await _projectLinkRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId && lstID.Contains(m.Num)).ToListAsync();
-                var listTenant = listLink.Select(m => m.TenantId).Distinct().ToList();
-                //抽成
-                var listDraw = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).ToListAsync();
+                if (project.ProjectType == 2)
+                {
+                    #region 夸克项目
+                    //二维码编号列表
+                    List<string> lstID = (from d in datatable.AsEnumerable() select d.Field<string>("二维码编号")).ToList();
+                    var listLink = await _kuaKeRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId && lstID.Contains(m.KuaKeID)).ToListAsync();
 
+                    var listTenant = listLink.Select(m => m.TenantId).Distinct().ToList();
 
-                List<ProjectStatEntity> list = new List<ProjectStatEntity>();
-                foreach (DataRow row in datatable.Rows)
-                {
-                    string num = row["二维码编号"]?.ToString();
-                    DateTime EffectDate = row["作业日期"].ToDateTime();
-                    int EffectDateNum = EffectDate.ToString("yyyyMMdd").ToInt();
+                    //公司抽成信息
+                    var listCompanyDraw = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).ToListAsync();
+                    //平台抽成信息
+                    var listTenantDraw = await _platformUserRepository.Select
+                        .DisableGlobalFilter(FilterNames.Tenant)
+                        .Where(m => listTenant.Contains(m.TenantId) && m.Role == "2").ToListAsync(m => new
+                        {
+                            m.Id,
+                            m.CommissionRatio,
+                            m.TenantId,
+                            m.Role
+                        });
 
-                    var link = await _projectLinkRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId && m.Num == num).ToOneAsync();
-                    foreach (var price in listPrice)
+                    List<ProjectStatEntity> list = new List<ProjectStatEntity>();
+                    foreach (DataRow row in datatable.Rows)
                     {
-                        decimal ratio = 0m;
-                        int drawway = 0;
-                        //平台单独抽成
-                        var drawtenant = listDraw.Where(m => m.DrawType == 3 && m.TenantId == link.TenantId && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
-                        if (drawtenant != null)
-                        {
-                            drawway = 2;
-                            ratio = drawtenant.DrawAmount.Value;
-                        }
-                        else
+                        string num = row["二维码编号"]?.ToString();
+                        DateTime EffectDate = row["作业日期"].ToDateTime();
+                        int EffectDateNum = EffectDate.ToString("yyyyMMdd").ToInt();
+
+                        var link = listLink.Where(m => m.ProjectId == ProjectId && m.KuaKeID == num).FirstOrDefault();
+                        foreach (var price in listPrice)
                         {
-                            //项目单独设置
-                            var drawproject = listDraw.Where(m => m.DrawType == 2 && m.ProjectId == price.ProjectId && m.ProjectPriceId == price.Id && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
-                            if (drawproject != null)
+                            GetCommissionOutput drawcommission = null;
+                            string UserRole = ""; //用户是否为平台管理员
+                            if (link != null)
                             {
-                                drawway = 2;
-                                ratio = drawproject.DrawAmount.Value;
+                                decimal companyRatio = 0m;//公司抽成比率,
+                                int companydrawway = 0; //公司抽成方式
+                                #region 公司抽成平台信息
+                                //单独设置抽成平台
+                                var drawtenant = listCompanyDraw.Where(m => m.DrawType == 3 && m.TenantId == link.TenantId && m.EffectDate <= EffectDate).OrderByDescending(m => m.EffectDate).FirstOrDefault();
+                                if (drawtenant != null)
+                                {
+                                    companydrawway = 2;
+                                    companyRatio = drawtenant.DrawAmount.Value;
+                                }
+                                else
+                                {
+                                    //项目单独设置
+                                    var drawproject = listCompanyDraw.Where(m => m.DrawType == 2 && m.ProjectId == price.ProjectId && m.ProjectPriceId == price.Id && m.EffectDate <= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
+                                    if (drawproject != null)
+                                    {
+                                        companydrawway = 2;
+                                        companyRatio = drawproject.DrawAmount.Value;
+                                    }
+                                    else
+                                    {
+                                        //公司单独设价
+                                        var drawcompany = listCompanyDraw.Where(m => m.DrawType == 1 && m.EffectDate <= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
+                                        if (drawcompany != null)
+                                        {
+                                            companydrawway = 1;
+                                            companyRatio = drawcompany.DrawRatio.Value;
+                                        }
+                                    }
+                                }
+                                #endregion
+
+                                decimal tenantRatio = 0m;//平台抽成比率
+                                #region 平台抽成用户信息
+                                var userDraw = listTenantDraw.Where(m => m.TenantId == link.TenantId).FirstOrDefault();
+                                if (userDraw != null)
+                                {
+                                    tenantRatio = userDraw.CommissionRatio;
+                                    UserRole = userDraw.Id == link.UserId ? userDraw.Role : "";
+                                }
+                                #endregion 
+                                if (userDraw != null)
+                                    drawcommission = GetCommission(price.Price, companyRatio, companydrawway, tenantRatio);
                             }
-                            else
+                            var model = new ProjectStatEntity()
                             {
-                                //公司单独设价
-                                var drawcompany = listDraw.Where(m => m.DrawType == 1 && m.ProjectId == 0 && m.ProjectPriceId == 0 && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
-                                if (drawcompany != null)
+                                Num = num,
+                                EffectDate = EffectDate,
+                                EffectDateNum = EffectDateNum,
+                                ProjectPriceId = price.Id,
+                                ProjectId = price.ProjectId,
+                                ValidCount = row[price.Name].ToInt(),
+                                Name = price.Name,
+                                CommissionPrice = 0,
+                                IsSettle = 0,
+                                TenantId = link?.TenantId ?? 0,
+                                UsedUserId = link?.UserId ?? 0,
+                                CompanyCommission = drawcommission?.CompanyCommission,
+                                CompanyRatio = drawcommission?.CompanyRatio,
+                                TenantCommission = drawcommission.TenantCommission,
+                                TenantyRatio = drawcommission.TenantRatio,
+                                UserRole =UserRole,
+                            };
+                            list.Add(model);
+                        }
+                    }
+                    await _projectStatRepository.InsertAsync(list);
+                    #endregion
+                }
+                else
+                {
+                    #region 普通拉新项目
+                    //二维码编号列表
+                    List<string> lstID = (from d in datatable.AsEnumerable() select d.Field<string>("二维码编号")).ToList();
+                    var listLink = await _projectLinkRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId && lstID.Contains(m.Num)).ToListAsync();
+                    var listTenant = listLink.Select(m => m.TenantId).Distinct().ToList();
+                    //公司抽成
+                    var listDraw = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).ToListAsync();
+
+                    //平台抽成信息
+                    var listTenantDraw = await _platformUserRepository.Select
+                        .DisableGlobalFilter(FilterNames.Tenant)
+                        .Where(m => listTenant.Contains(m.TenantId) && m.Role == "2").ToListAsync(m => new
+                        {
+                            m.Id,
+                            m.CommissionRatio,
+                            m.TenantId,
+                            m.Role
+                        });
+
+
+                    List<ProjectStatEntity> list = new List<ProjectStatEntity>();
+                    foreach (DataRow row in datatable.Rows)
+                    {
+                        string num = row["二维码编号"]?.ToString();
+                        DateTime EffectDate = row["作业日期"].ToDateTime();
+                        int EffectDateNum = EffectDate.ToString("yyyyMMdd").ToInt();
+
+                        var link = listLink.Where(m => m.ProjectId == ProjectId && m.Num == num).FirstOrDefault();
+                        foreach (var price in listPrice)
+                        {
+                            GetCommissionOutput drawcommission = null;
+                            string UserRole = "";
+                            if (link != null)
+                            {
+                                decimal companyRatio = 0m;//公司抽成
+                                int companydrawway = 0;//公司抽成方式
+                                #region 公司抽成
+                                //平台单独抽成
+                                var drawtenant = listDraw.Where(m => m.DrawType == 3 && m.TenantId == link.TenantId && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
+                                if (drawtenant != null)
+                                {
+                                    companydrawway = 2;
+                                    companyRatio = drawtenant.DrawAmount.Value;
+                                }
+                                else
                                 {
-                                    drawway = 1;
-                                    ratio = drawcompany.DrawRatio.Value;
+                                    //项目单独设置
+                                    var drawproject = listDraw.Where(m => m.DrawType == 2 && m.ProjectId == price.ProjectId && m.ProjectPriceId == price.Id && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
+                                    if (drawproject != null)
+                                    {
+                                        companydrawway = 2;
+                                        companyRatio = drawproject.DrawAmount.Value;
+                                    }
+                                    else
+                                    {
+                                        //公司单独设价
+                                        var drawcompany = listDraw.Where(m => m.DrawType == 1 && m.ProjectId == 0 && m.ProjectPriceId == 0 && m.EffectDate >= EffectDate).OrderBy(m => m.EffectDate).FirstOrDefault();
+                                        if (drawcompany != null)
+                                        {
+                                            companydrawway = 1;
+                                            companyRatio = drawcompany.DrawRatio.Value;
+                                        }
+                                    }
                                 }
+                                #endregion
+
+                                decimal tenantRatio = 0m;//平台抽成比率
+                                #region 平台抽成用户信息
+                                var userDraw = listTenantDraw.Where(m => m.TenantId == link.TenantId).FirstOrDefault();
+                                if (userDraw != null)
+                                {
+                                    tenantRatio = userDraw.CommissionRatio;
+                                    UserRole = userDraw.Id == link.UsedUserId ? userDraw.Role : "";
+                                }
+                                #endregion
+                                if (userDraw != null)
+                                    drawcommission = GetCommission(price.Price, companyRatio, companydrawway, tenantRatio);
                             }
+                            var model = new ProjectStatEntity()
+                            {
+                                Num = num,
+                                EffectDate = EffectDate,
+                                EffectDateNum = EffectDateNum,
+                                ProjectPriceId = price.Id,
+                                ProjectId = price.ProjectId,
+                                ValidCount = row[price.Name].ToInt(),
+                                Name = price.Name,
+                                CommissionPrice = 0,
+                                IsSettle = 0,
+                                TenantId = link?.TenantId,
+                                UsedUserId = link?.UsedUserId,
+                                CompanyCommission = drawcommission?.CompanyCommission,
+                                CompanyRatio = drawcommission?.CompanyRatio,
+                                TenantCommission = drawcommission?.TenantCommission,
+                                TenantyRatio = drawcommission?.TenantRatio,
+                            };
+                            list.Add(model);
                         }
-                        var commission = GetCommission(price.Price, ratio, drawway);
-                        var model = new ProjectStatEntity()
-                        {
-                            Num = num,
-                            EffectDate = EffectDate,
-                            EffectDateNum = EffectDateNum,
-                            ProjectPriceId = price.Id,
-                            ProjectId = price.ProjectId,
-                            ValidCount = row[price.Name].ToInt(),
-                            Name = price.Name,
-                            CommissionPrice = 0,
-                            IsSettle = 0,
-                            TenantId = link?.TenantId,
-                            UsedUserId = link?.UsedUserId,
-                            CompanyCommission = commission.CompanyCommission,
-                            CompanyRatio = commission.CompanyRatio,
-                            TenantCommission = commission.TenantCommission,
-                            TenantyRatio = commission.TenantRatio,
-                        };
-                        list.Add(model);
                     }
+                    await _projectStatRepository.InsertAsync(list);
+                    #endregion
                 }
-                await _projectStatRepository.InsertAsync(list);
+
             }
             catch (Exception ex)
             {
-                throw new Exception($"导入项目统计异常");
+                throw new Exception($"导入项目统计异常:" + ex.Message);
             }
             finally
             {
@@ -336,7 +487,8 @@ namespace ZhonTai.Admin.Services.ProjectStat
             }
             //更新项目            
             await _projectStatRepository.UpdateDiy.SetDto(
-            new {
+            new
+            {
                 IsSettle = 1,
                 SettleTime = DateTime.Now
             })
@@ -348,26 +500,33 @@ namespace ZhonTai.Admin.Services.ProjectStat
         /// 获取佣金
         /// </summary>
         /// <param name="price">项目价格</param>
-        /// <param name="ratio">公司抽成比率,抽成方式是2,则抽成金额</param>
-        /// <param name="drawType">抽成方式1比率 2佣金</param>
-        private GetCommissionOutput GetCommission(decimal price, decimal ratio, int drawType)
+        /// <param name="compnayRatio">公司抽成比率,抽成方式是2,则抽成金额</param>
+        /// <param name="companyDrawWay">抽成方式1比率 2佣金</param>
+        /// <param name="tenantRatio">平台抽成比率</param>
+        private GetCommissionOutput GetCommission(decimal price, decimal compnayRatio, int companyDrawWay, decimal tenantRatio)
         {
             GetCommissionOutput output = new GetCommissionOutput();
             decimal compnayCommssion = 0m;
-            if (drawType == 1)
+            if (companyDrawWay == 1)
             {
-                compnayCommssion = UtilConvertExtension.ToDecimalCutWithN(price * ratio * 0.01m, 0m, 1);
+                compnayCommssion = UtilConvertExtension.ToDecimalCutWithN(price * compnayRatio * 0.01m, 0m, 1);
             }
-            else if (drawType == 2)
+            else if (companyDrawWay == 2)
             {
-                compnayCommssion = price - ratio;
+                compnayCommssion = price - compnayRatio;
             }
+            //公司抽成
+            output.CompanyCommission = compnayCommssion;
+            output.CompanyRatio = compnayRatio;
 
-            var tenatCommssion = price - compnayCommssion;
+            //平台抽成
+            var tenatPrice = price - compnayCommssion;
+            var tenatCommssion = UtilConvertExtension.ToDecimalCutWithN(tenatPrice * tenantRatio * 0.01m, 0m, 1);
             output.TenantCommission = tenatCommssion;
-            output.TenantRatio = 0m;
-            output.CompanyCommission = compnayCommssion;
-            output.CompanyRatio = ratio;
+            output.TenantRatio = tenantRatio;
+            //用户佣金
+            var userCommission = tenatPrice - tenatCommssion;
+            output.UserCommission = userCommission;
             return output;
         }
         #endregion

+ 1 - 1
src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs

@@ -313,7 +313,7 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
                 Name = user.Name,
                 Phone = user.Mobile,
                 Password = user.Password,
-                Role = "1",
+                Role = "2",
                 ParentId = "0_",
                 InviteCode = invite
             };