Esempio n. 1
0
        /// <summary>
        /// 根据id获取商品佣金实体
        /// </summary>
        /// <param name="search">查询类</param>
        /// <returns>结果</returns>
        public Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO GetCommodityCommissionExt(Guid id)
        {
            Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO model = new Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO();
            var commodityCommission = CommodityCommission.ObjectSet().FirstOrDefault(p => p.Id == id);

            if (commodityCommission != null)
            {
                var mallapplyid = MallApply.ObjectSet().FirstOrDefault(p => p.Id == commodityCommission.MallApplyId);
                if (mallapplyid != null)
                {
                    model.AppId = mallapplyid.AppId;
                }
                model.Id            = commodityCommission.Id;
                model.MallApplyId   = commodityCommission.MallApplyId;
                model.ModifiedOn    = commodityCommission.ModifiedOn;
                model.SubTime       = commodityCommission.SubTime;
                model.EffectiveTime = commodityCommission.EffectiveTime;
                model.Commission    = commodityCommission.Commission;
                model.UserId        = commodityCommission.UserId;
                model.IsDel         = commodityCommission.IsDel;
                model.CategoryId    = commodityCommission.CategoryId;
                model.CommodityId   = commodityCommission.CommodityId;
                model.CommodityName = commodityCommission.CommodityName;
            }
            return(model);
        }
Esempio n. 2
0
        /// <summary>
        /// 修改佣金信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ResultDTO UpdateCommodityCommissionExt(Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO model)
        {
            ResultDTO dto = null;

            try
            {
                ContextSession contextSession      = ContextFactory.CurrentThreadContext;
                var            commodityCommission = CommodityCommission.ObjectSet().FirstOrDefault(p => p.Id == model.Id);
                if (commodityCommission != null)
                {
                    if (!string.IsNullOrWhiteSpace(model.CategoryId.ToString()) && (!model.CategoryId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
                    {
                        commodityCommission.CategoryId = model.CategoryId;
                    }
                    commodityCommission.ModifiedOn = DateTime.Now;
                    if (!string.IsNullOrWhiteSpace(model.MallApplyId.ToString()) && (!model.MallApplyId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
                    {
                        commodityCommission.MallApplyId = model.MallApplyId;
                    }
                    if (!string.IsNullOrWhiteSpace(model.UserId.ToString()) && (!model.UserId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
                    {
                        commodityCommission.UserId = model.UserId;
                    }
                    if (!string.IsNullOrWhiteSpace(model.CommodityId.ToString()) && (!model.CommodityId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
                    {
                        commodityCommission.CommodityId = model.CommodityId;
                    }
                    if (!string.IsNullOrWhiteSpace(commodityCommission.CommodityName))
                    {
                        commodityCommission.CommodityName = model.CommodityName;
                    }
                    commodityCommission.Commission    = model.Commission;
                    commodityCommission.EffectiveTime = DateTime.Now;
                    commodityCommission.IsDel         = model.IsDel;
                    commodityCommission.EntityState   = EntityState.Modified;
                    contextSession.SaveChanges();
                    dto = new ResultDTO()
                    {
                        ResultCode = 0, Message = "修改成功", isSuccess = true
                    };
                }
                else
                {
                    dto = new ResultDTO()
                    {
                        ResultCode = 1, Message = "商品佣金信息不存在", isSuccess = false
                    };
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("商品佣金信息修改异常。MallApply:{0}", ex.Message), ex);
                dto = new ResultDTO()
                {
                    ResultCode = 1, Message = ex.Message, isSuccess = false
                };
            }
            return(dto);
        }
Esempio n. 3
0
        /// <summary>
        /// 查询佣金信息
        /// </summary>
        /// <param name="search">查询类</param>
        /// <returns>结果</returns>
        public List <Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO> GetCommodityCommissionListExt(Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO search)
        {
            var CommodityCommissionlist = CommodityCommission.ObjectSet().Where(p => p.IsDel == true).OrderByDescending(p => p.SubTime).ToList();

            if (!string.IsNullOrWhiteSpace(search.MallApplyId.ToString()) && (!search.MallApplyId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
            {
                CommodityCommissionlist = CommodityCommissionlist.Where(p => p.MallApplyId == search.MallApplyId).ToList();
            }
            if (!string.IsNullOrWhiteSpace(search.CommodityId.ToString()) && (!search.CommodityId.ToString().Contains("00000000-0000-0000-0000-000000000000")))
            {
                CommodityCommissionlist = CommodityCommissionlist.Where(p => p.CommodityId == search.CommodityId).ToList();
            }
            List <Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO> searchlist = new List <Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO>();

            if (CommodityCommissionlist.Count() > 0)
            {
                foreach (var item in CommodityCommissionlist)
                {
                    CBC.Deploy.CustomDTO.UserBasicInfoDTO commodityuser = CBCSV.Instance.GetUserBasicInfoNew(item.UserId);
                    Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO model = new Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO();
                    model.Id            = item.Id;
                    model.SubTime       = item.SubTime;
                    model.ModifiedOn    = item.ModifiedOn;
                    model.Commission    = item.Commission;
                    model.EffectiveTime = item.EffectiveTime;
                    model.UserId        = item.UserId;
                    if (commodityuser != null)
                    {
                        model.UserName = commodityuser.Name;
                    }
                    model.CategoryId    = item.CategoryId;
                    model.MallApplyId   = item.MallApplyId;
                    model.CommodityId   = item.CommodityId;
                    model.CommodityName = item.CommodityName;
                    model.IsDel         = item.IsDel;
                    searchlist.Add(model);
                }
            }
            return(searchlist);
        }
Esempio n. 4
0
        /// <summary>
        /// 删除佣金信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ResultDTO DelCommodityCommissionExt(Jinher.AMP.BTP.Deploy.CustomDTO.MallApply.CommodityCommissionDTO model)
        {
            ResultDTO dto = null;

            try
            {
                ContextSession contextSession      = ContextFactory.CurrentThreadContext;
                var            commodityCommission = CommodityCommission.ObjectSet().FirstOrDefault(p => p.Id == model.Id);
                if (commodityCommission != null)
                {
                    commodityCommission.EntityState = EntityState.Deleted;
                    contextSession.Delete(commodityCommission);
                    contextSession.SaveChanges();
                    dto = new ResultDTO()
                    {
                        ResultCode = 0, Message = "删除商品佣金成功", isSuccess = true
                    };
                }
                else
                {
                    dto = new ResultDTO()
                    {
                        ResultCode = 1, Message = "商品佣金信息不存在", isSuccess = false
                    };
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("商品信息删除异常。MallApply:{0}", ex.Message));
                dto = new ResultDTO()
                {
                    ResultCode = 1, Message = ex.Message, isSuccess = false
                };
            }
            return(dto);
        }
Esempio n. 5
0
        /// <summary>
        /// 根据订单生成待结算项
        /// </summary>
        public static SettleAccountsDetails CreateSettleAccountDetails(ContextSession contextSession, CommodityOrder commodityOrder, MallApply mall, int?orderServiceState = null)
        {
            //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id);

            var currentDate = DateTime.Now;

            // 1:商家,2:金和众销(给金和分的钱),3:商贸众销(给分享者分的钱),4:商贸众筹,5:推广主分成,6:应用主分成,7金和分润,8买家,9一级分销,10二级分销,11三级分销,12渠道推广,20:一级代理,21:二级代理
            //var payeeType = new List<int> { 3, 5, 9, 10, 11, 20, 21 };
            //var orderitemlist = OrderItem.ObjectSet().Where(n => n.CommodityOrderId == commodityOrder.Id).
            //    GroupJoin(OrderItemShare.ObjectSet().Where(s => payeeType.Contains(s.PayeeType)), o => o.Id, s => s.OrderItemId, (o, s) =>
            //        new { o.Id, o.Name, o.Number, o.RealPrice, o.CommodityId, o.ComCategoryId, Commission = s.Sum(_ => (decimal?)_.Commission) }).ToList();

            // 暂不计算推广佣金
            var orderitemlist = OrderItem.ObjectSet().Where(n => n.CommodityOrderId == commodityOrder.Id).Select(o =>
                                                                                                                 new { o.Id, o.Name, o.Number, o.RealPrice, o.CommodityId, o.ComCategoryId, o.CommodityAttributes }).ToList();
            bool isSetSettleAmount = true;
            List <SettleAccountsOrderItem> items = new List <SettleAccountsOrderItem>();
            var payTime = commodityOrder.PaymentTime.Value;

            foreach (var orderItem in orderitemlist)
            {
                // 结算单订单项
                SettleAccountsOrderItem saItem = new SettleAccountsOrderItem();
                saItem.Id                    = saItem.OrderItemId = orderItem.Id;
                saItem.ModifiedOn            = saItem.SubTime = currentDate;
                saItem.OrderId               = commodityOrder.Id;
                saItem.OrderItemRefundAmount = 0;               // 暂无单品退货
                saItem.OrderItemPromotionCommissionAmount = 0M; // orderItem.Commission ?? 0M;
                saItem.OrderItemName   = orderItem.Name;
                saItem.OrderItemNumber = orderItem.Number;
                saItem.OrderItemPrice  = orderItem.RealPrice.Value;
                // 自营他配
                if (mall.Type == 0)
                {
                    var commoditySettleAmount = CommoditySettleAmount.ObjectSet()
                                                .Where(c => c.CommodityId == orderItem.CommodityId && c.EffectiveTime < payTime)
                                                .OrderByDescending(c => c.SubTime)
                                                .ThenByDescending(c => c.EffectiveTime)
                                                .FirstOrDefault();
                    if (commoditySettleAmount == null)
                    {
                        // 未设置结算价,暂不结算
                        //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id + ",自营商家,未设置结算价,商品ID:" + orderItem.CommodityId);
                        return(null);
                    }
                    //LogHelper.Info("开始生成结算项,订单ID:" + commodityOrder.Id + ",自营商家,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价:" + commoditySettleAmount.CommodityAttrJson);

                    var saAttrs = JsonHelper.JsonDeserialize <List <Jinher.AMP.BTP.Deploy.CustomDTO.CommodityAttributePrice> >(commoditySettleAmount.CommodityAttrJson);
                    var attrs   = (orderItem.CommodityAttributes ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (attrs.Length == 0)
                    {
                        // 无属性
                        saItem.SettleAmount = saAttrs[0].SettlePrice.Value;
                    }
                    else if (attrs.Length == 1)
                    {
                        // 单属性
                        var settlePrice = saAttrs.Where(a => a.AttributeValue == attrs[0]).FirstOrDefault();
                        if (settlePrice == null)
                        {
                            LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,结算价设置异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价ID:" + commoditySettleAmount.Id + ",结算价属性:" + commoditySettleAmount.CommodityAttrJson);
                            return(null);
                        }
                        saItem.SettleAmount = settlePrice.SettlePrice.Value;
                    }
                    else if (attrs.Length == 2)
                    {
                        // 双属性
                        var settlePrice = saAttrs.Where(a =>
                                                        (a.AttributeValue == attrs[0] && a.SecAttributeValue == attrs[1]) ||
                                                        (a.AttributeValue == attrs[1] && a.SecAttributeValue == attrs[0]))
                                          .FirstOrDefault();
                        if (settlePrice == null)
                        {
                            LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,结算价设置异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes + ",结算价ID:" + commoditySettleAmount.Id + ",结算价属性:" + commoditySettleAmount.CommodityAttrJson);
                            return(null);
                        }
                        saItem.SettleAmount = settlePrice.SettlePrice.Value;
                    }
                    else
                    {
                        LogHelper.Error("SettleAccountHelper.CreateSettleAccountDetails 生成结算单失败,商品属性异常,商品ID:" + orderItem.CommodityId + ",商品属性:" + orderItem.CommodityAttributes);
                        return(null);
                    }
                    //orderItem.com
                }
                // 第三方
                else if (mall.Type == 1)
                {
                    // 查询商品易捷币抵用数量
                    var yjbInfo = YJBSV.GetOrderItemYJBInfo(commodityOrder.EsAppId.Value, commodityOrder.Id);
                    if (!yjbInfo.IsSuccess)
                    {
                        saItem.OrderItemYJBAmount = 0;
                    }
                    else
                    {
                        var currentCommodityYJBInfo = yjbInfo.Data.Items.Where(c => c.CommodityId == orderItem.CommodityId).FirstOrDefault();
                        if (currentCommodityYJBInfo != null && currentCommodityYJBInfo.IsMallYJB)
                        {
                            saItem.OrderItemYJBAmount = currentCommodityYJBInfo.InsteadCashAmount;
                        }
                    }

                    // 计算实际成交价



                    #region 计算商品佣金比例
                    // 获取佣金比例:按结算日期获取有效的(启用日期小于等于结算日期的最近一次设置的佣金比例)佣金比例【基础、类目、商品】;
                    saItem.BaseCommission = BaseCommission.ObjectSet().Join(MallApply.ObjectSet(),
                                                                            b => b.MallApplyId, m => m.Id, (b, m) => new { b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                            Where(t => t.EsAppId == commodityOrder.EsAppId && t.AppId == commodityOrder.AppId && t.EffectiveTime < payTime).
                                            OrderByDescending(t => t.SubTime).Select(t => (decimal?)t.Commission).FirstOrDefault();

                    // 查询当前商品的类目
                    List <Guid> categoryIds    = new List <Guid>();
                    var         tempCategories = CommodityCategory.ObjectSet().Where(c => c.AppId == commodityOrder.EsAppId && c.CommodityId == orderItem.CommodityId).Join(Category.ObjectSet(), cc => cc.CategoryId, c => c.Id, (cc, c) => new { c.Id, c.CurrentLevel, c.ParentId, cc.IsDel }).ToList();
                    foreach (var item in tempCategories)
                    {
                        if (item.IsDel.HasValue && item.IsDel.Value)
                        {
                            continue;
                        }
                        if (item.CurrentLevel == 3 || item.CurrentLevel == 4 || item.CurrentLevel == 5)
                        {
                            var lv2 = Category.FindByID(item.ParentId.Value);
                            if (item.CurrentLevel == 3)
                            {
                                categoryIds.Add(lv2.ParentId.Value);
                                continue;
                            }
                            var lv3 = Category.FindByID(lv2.ParentId.Value);
                            if (item.CurrentLevel == 4)
                            {
                                categoryIds.Add(lv3.ParentId.Value);
                                continue;
                            }
                            var lv4 = Category.FindByID(lv3.ParentId.Value);
                            categoryIds.Add(lv4.ParentId.Value);
                        }
                        else if (item.CurrentLevel == 2)
                        {
                            categoryIds.Add(item.ParentId.Value);
                        }
                        else if (item.CurrentLevel == 1)
                        {
                            categoryIds.Add(item.Id);
                        }
                    }

                    var commissions = CategoryCommission.ObjectSet().
                                      Join(MallApply.ObjectSet(), b => b.MallApplyId, m => m.Id,
                                           (b, m) => new { b.CategoryId, b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                      Where(t =>
                                            t.EsAppId == commodityOrder.EsAppId &&
                                            t.AppId == commodityOrder.AppId &&
                                            categoryIds.Contains(t.CategoryId) &&
                                            t.EffectiveTime < payTime).
                                      GroupBy(t => t.CategoryId).
                                      Select(group => group.OrderByDescending(g => g.SubTime).Select(g => g.Commission).FirstOrDefault()).
                                      ToList();
                    if (commissions.Count > 0)
                    {
                        saItem.CategoryCommission = commissions.Min();
                    }
                    saItem.CommodityCommission = CommodityCommission.ObjectSet().Join(MallApply.ObjectSet(),
                                                                                      b => b.MallApplyId, m => m.Id, (b, m) => new { b.CommodityId, b.Commission, b.EffectiveTime, b.SubTime, m.AppId, m.EsAppId }).
                                                 Where(t => t.EsAppId == commodityOrder.EsAppId && t.AppId == commodityOrder.AppId &&
                                                       t.CommodityId == orderItem.CommodityId && t.EffectiveTime < payTime).
                                                 OrderByDescending(t => t.SubTime).Select(t => (decimal?)t.Commission).FirstOrDefault();
                    #endregion

                    // 商城佣金计算公式:商品销售价*佣金比例【商品佣金优先,类目佣金其次、基础佣金最后,三选一计算】* 购买数量;
                    saItem.PromotionAmount = Math.Truncate(
                        (saItem.CommodityCommission.HasValue ? saItem.CommodityCommission.Value :
                         (saItem.CategoryCommission.HasValue ? saItem.CategoryCommission.Value :
                          (saItem.BaseCommission.HasValue ? saItem.BaseCommission.Value : 0)
                         )) * saItem.OrderItemPrice * saItem.OrderItemNumber) / 100;
                }

                saItem.EntityState = EntityState.Added;
                items.Add(saItem);
            }

            // 结算单订单详情
            SettleAccountsDetails sad = new SettleAccountsDetails();
            sad.Id                = sad.OrderId = commodityOrder.Id;
            sad.EntityState       = EntityState.Added;
            sad.ModifiedOn        = sad.SubTime = currentDate;
            sad.IsSettled         = false;
            sad.AppId             = commodityOrder.AppId;
            sad.EsAppId           = commodityOrder.EsAppId.Value;
            sad.OrderCode         = commodityOrder.Code;
            sad.OrderSubTime      = payTime;
            sad.OrderAmount       = commodityOrder.Price;
            sad.OrderRealAmount   = commodityOrder.RealPrice.Value;
            sad.OrderFreight      = commodityOrder.Freight;
            sad.IsSetSettleAmount = isSetSettleAmount;

            // 导入订单时,记录退款金额
            if (commodityOrder.State == 7 || orderServiceState == 7)
            {
                var refund = OrderRefundAfterSales.ObjectSet().Where(o => o.OrderId == commodityOrder.Id).FirstOrDefault();
                // 全额退款 不计入结算单
                if (refund.IsFullRefund == 1)
                {
                    sad.IsSettled = true;
                    if (contextSession != null)
                    {
                        foreach (var item in items)
                        {
                            contextSession.SaveObject(item);
                        }
                        contextSession.SaveObject(sad);
                    }
                    return(sad);
                }
                else
                {
                    sad.OrderRefundAmount = refund.RefundMoney;
                }
            }
            else
            {
                sad.OrderRefundAmount = 0; // 暂无单品退货
            }

            //// 计算商城优惠券金额
            //var orderP ayDetailId = OrderPayDetail.ObjectSet().Where(c => c.OrderId == commodityOrder.Id && c.ObjectType == 1).Select(c => c.ObjectId).FirstOrDefault();
            //if (orderPayDetailId != Guid.Empty)
            //{
            //    var request = new Coupon.Deploy.CustomDTO.ListCouponNewRequestDTO
            //    {
            //        CouponIds = new List<Guid> { orderPayDetailId },
            //        UserId = commodityOrder.UserId
            //    };
            //    var coupons = CouponSV.Instance.GetUserCouponsByIds(request);
            //    if (coupons.IsSuccess)
            //    {
            //        var firstCoupon = coupons.Data[0];

            //        sad.OrderCouponAmount = firstCoupon.Cash;
            //        // 判断是否为电商馆
            //        sad.IsMallCoupon = ZPHSV.Instance.IsAppPavilion(firstCoupon.ShopId);
            //    }
            //    else
            //    {
            //        LogHelper.Error(string.Format("CouponSV.GetUserCouponsByIds返回结果不成功,入参 CouponIds: {0} UserId: {1},出参 Code: {2} Info: {3}。", orderPayDetailId, commodityOrder.UserId, coupons.Code, coupons.Info));
            //    }
            //}

            // 计算商家结算金额

            decimal spreadMoney = 0;
            if (commodityOrder.SpreadGold > 0)
            {
                spreadMoney = commodityOrder.SpreadGold.ToMoney();
            }
            sad.OrderSpreadAmount = spreadMoney;

            // 1.第三方
            if (mall.Type == 1)
            {
                sad.OrderYJBAmount = items.Sum(i => i.OrderItemYJBAmount);

                // 商城佣金
                sad.PromotionAmount = items.Sum(i => i.PromotionAmount);

                // 优先级:1推广主、2三级分销、 3众销
                //sad.OrderPromotionCommissionAmount = commodityOrder.SpreadGold > 0 ? commodityOrder.SpreadGold.ToMoney() :
                //    commodityOrder.DistributeMoney > 0 ? commodityOrder.DistributeMoney : commodityOrder.Commission;
                // 老版本,已弃 商家结算金额 = 订单总额 + 商城优惠券总金额 - 退款总金额 - 推广佣金总额 - 商城佣金总额
                // 版本1:商家结算金额 =  实收款 +  商城易捷币抵用金额 - 退款金额  - 商城佣金
                // 版本2:商家结算金额 =  实收款 +  商城易捷币抵用金额 - 退款金额  - 商城佣金 - 推广佣金总额
                // 商家结算金额
                sad.SellerAmount = Math.Truncate((sad.OrderRealAmount + sad.OrderYJBAmount - sad.OrderRefundAmount - sad.PromotionAmount - sad.OrderSpreadAmount.Value) * 100) / 100;
            }
            // 0.自营他配
            else if (mall.Type == 0)
            {
                // 商品结算价
                decimal totalsSettlePrice = 0;
                foreach (var item in items)
                {
                    totalsSettlePrice += item.SettleAmount * item.OrderItemNumber;
                }
                sad.SettleAmount = totalsSettlePrice;
                // 商家结算金额 = 商品结算价*数量 + 运费 - 退款金额
                sad.SellerAmount = Math.Truncate((sad.SettleAmount + sad.OrderFreight - sad.OrderRefundAmount) * 100) / 100;
                // 商城佣金
                sad.PromotionAmount = Math.Truncate((sad.OrderRealAmount - sad.SellerAmount - sad.OrderSpreadAmount.Value) * 100) / 100;
            }
            // 2.自营自配自采
            // 3.自营自配统采
            if (contextSession != null)
            {
                foreach (var item in items)
                {
                    contextSession.SaveObject(item);
                }
                contextSession.SaveObject(sad);
            }
            return(sad);
        }