public ActionResult Index(string orderDate, string keywords, string orderids, DateTime?startDateTime, DateTime?endDateTime, int?orderStatus, int pageNo = 1, int pageSize = 10)
        {
            ViewBag.Grant = null;

            if (!string.IsNullOrEmpty(orderids) && orderids.IndexOf(',') <= 0)
            {
                ViewBag.Grant = _iShopBonusService.GetByOrderId(long.Parse(orderids));
            }

            DateTime?startDate = startDateTime;
            DateTime?endDate   = endDateTime;

            if (!string.IsNullOrEmpty(orderDate) && orderDate.ToLower() != "all")
            {
                switch (orderDate.ToLower())
                {
                case "threemonth":
                    startDate = DateTime.Now.AddMonths(-3);
                    break;

                case "halfyear":
                    startDate = DateTime.Now.AddMonths(-6);
                    break;

                case "year":
                    startDate = DateTime.Now.AddYears(-1);
                    break;

                case "yearago":
                    endDate = DateTime.Now.AddYears(-1);
                    break;
                }
            }

            if (orderStatus.HasValue && orderStatus == 0)
            {
                orderStatus = null;
            }

            var queryModel = new OrderQuery()
            {
                StartDate      = startDate,
                EndDate        = endDate,
                Status         = (Model.OrderInfo.OrderOperateStatus?)orderStatus,
                UserId         = CurrentUser.Id,
                SearchKeyWords = keywords,
                PageSize       = pageSize,
                PageNo         = pageNo
            };

            var orders        = OrderApplication.GetOrders(queryModel);
            var orderComments = OrderApplication.GetOrderCommentCount(orders.Models.Select(p => p.Id));
            var orderItems    = OrderApplication.GetOrderItemsByOrderId(orders.Models.Select(p => p.Id));
            var orderRefunds  = OrderApplication.GetOrderRefunds(orderItems.Select(p => p.Id));

            PagingInfo info = new PagingInfo
            {
                CurrentPage  = pageNo,
                ItemsPerPage = pageSize,
                TotalItems   = orders.Total
            };

            ViewBag.pageInfo = info;
            ViewBag.UserId   = CurrentUser.Id;
            var siteSetting = _iSiteSettingService.GetSiteSettings();
            var shopBonus   = _iShopBonusService;

            ViewBag.SalesRefundTimeout = siteSetting.SalesReturnTimeout;

            var cashDepositsService = _iCashDepositsService;
            //IEnumerable<OrderListModel> orderList = new IEnumerable<OrderListModel>();
            IEnumerable <OrderListModel> orderList = orders.Models.Select(item => new OrderListModel
            {
                Id                 = item.Id,
                ActiveType         = item.ActiveType,
                OrderType          = item.OrderType,
                Address            = item.Address,
                CellPhone          = item.CellPhone,
                CloseReason        = item.CloseReason,
                CommisTotalAmount  = item.CommisAmount,
                DiscountAmount     = item.DiscountAmount,
                ExpressCompanyName = item.ExpressCompanyName,
                FinishDate         = item.FinishDate,
                Freight            = item.Freight,
                GatewayOrderId     = item.GatewayOrderId,
                IntegralDiscount   = item.IntegralDiscount,
                UserId             = item.UserId,
                ShopId             = item.ShopId,
                ShopName           = item.ShopName,
                ShipTo             = item.ShipTo,
                OrderTotalAmount   = item.OrderTotalAmount,
                PaymentTypeName    = item.PaymentTypeName,
                //满额减
                FullDiscount  = item.FullDiscount,
                OrderStatus   = item.OrderStatus,
                RefundStats   = item.RefundStats,
                CommentCount  = orderComments.ContainsKey(item.Id)?orderComments[item.Id]:0,
                OrderDate     = item.OrderDate,
                PaymentType   = item.PaymentType,
                PickupCode    = item.PickupCode,
                OrderItemList = orderItems.Where(oi => oi.OrderId == item.Id).Select(oItem =>
                {
                    var itemrefund = orderRefunds.Where(or => or.OrderItemId == oItem.Id).FirstOrDefault();
                    var orderItem  = new OrderItemListModel
                    {
                        Id                     = oItem.Id,
                        ProductId              = oItem.ProductId,
                        Color                  = oItem.Color,
                        Size                   = oItem.Size,
                        Version                = oItem.Version,
                        ProductName            = oItem.ProductName,
                        ThumbnailsUrl          = oItem.ThumbnailsUrl,
                        SalePrice              = oItem.SalePrice,
                        SkuId                  = oItem.SkuId,
                        Quantity               = oItem.Quantity,
                        CashDepositsObligation = cashDepositsService.GetCashDepositsObligation(oItem.ProductId),
                    };

                    if (itemrefund != null)
                    {
                        orderItem.RefundStats  = itemrefund.RefundStatusValue;
                        orderItem.ItemRefundId = itemrefund.Id;

                        string showRefundStats = "";
                        if (itemrefund.SellerAuditStatus == OrderRefundInfo.OrderRefundAuditStatus.Audited)
                        {
                            showRefundStats = itemrefund.ManagerConfirmStatus.ToDescription();
                        }
                        else if (item.DeliveryType == CommonModel.Enum.DeliveryType.SelfTake || (item.ShopBranchId.HasValue && item.ShopBranchId.Value > 0))//如果是自提预约单或分配门店预约单则转为门店审核状态
                        {
                            showRefundStats = ((CommonModel.Enum.OrderRefundShopAuditStatus)itemrefund.SellerAuditStatus).ToDescription();
                        }
                        else
                        {
                            showRefundStats = itemrefund.SellerAuditStatus.ToDescription();
                        }

                        orderItem.ShowRefundStats = showRefundStats;
                    }
                    orderItem.EnabledRefundAmount = oItem.EnabledRefundAmount;
                    ProductTypeInfo typeInfo      = _iTypeService.GetTypeByProductId(oItem.ProductId);
                    orderItem.ColorAlias          = (typeInfo == null || string.IsNullOrEmpty(typeInfo.ColorAlias)) ? SpecificationType.Color.ToDescription() : typeInfo.ColorAlias;
                    orderItem.SizeAlias           = (typeInfo == null || string.IsNullOrEmpty(typeInfo.SizeAlias)) ? SpecificationType.Size.ToDescription() : typeInfo.SizeAlias;
                    orderItem.VersionAlias        = (typeInfo == null || string.IsNullOrEmpty(typeInfo.VersionAlias)) ? SpecificationType.Version.ToDescription() : typeInfo.VersionAlias;

                    return(orderItem);
                }).ToList(),
                ReceiveBonus = shopBonus.GetGrantByUserOrder(item.Id, CurrentUser.Id),
            }).ToList();

            foreach (var o in orderList)
            {
                o.HasAppendComment = HasAppendComment(o);
            }

            #region 数据补偿
            List <long> ordidl = orderList.Select(d => d.Id).ToList();
            if (ordidl.Count > 0)
            {
                foreach (var item in orderList)
                {
                    var _ord = orders.Models.FirstOrDefault(o => o.Id == item.Id);

                    item.IsRefundTimeOut     = OrderApplication.IsRefundTimeOut(_ord);
                    item.EnabledRefundAmount = _ord.OrderEnabledRefundAmount;
                    //退款状态补偿
                    var _tmpobj = orderRefunds.FirstOrDefault(d => d.OrderId == item.Id && d.RefundMode == OrderRefundInfo.OrderRefundMode.OrderRefund);
                    if (_tmpobj != null)
                    {
                        item.RefundStats   = (int)_tmpobj.SellerAuditStatus;
                        item.OrderRefundId = _tmpobj.Id;
                    }

                    item.OrderCanRefund = false;

                    if (item.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.Finish)
                    {
                        if (item.FinishDate.Value.AddDays(siteSetting.SalesReturnTimeout) > DateTime.Now)
                        {
                            item.OrderCanRefund = true;
                        }
                    }
                    if (item.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitReceiving)
                    {
                        item.OrderCanRefund = true;
                    }
                    if (item.PaymentType == Himall.Model.OrderInfo.PaymentTypes.CashOnDelivery)
                    {
                        if (item.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.Finish)
                        {
                            item.OrderCanRefund = true;
                        }
                    }
                    else
                    {
                        item.OrderCanRefund = true;
                    }

                    item.FightGroupCanRefund = true;   //非拼团预约单默认可退

                    //拼团状态补偿
                    if (item.OrderType == OrderInfo.OrderTypes.FightGroup)
                    {
                        var fgord = _iFightGroupService.GetFightGroupOrderStatusByOrderId(item.Id);
                        if (fgord != null)
                        {
                            item.FightGroupJoinStatus = fgord.GetJoinStatus;
                            item.FightGroupCanRefund  = fgord.CanRefund;
                        }
                        else
                        {
                            item.FightGroupJoinStatus = CommonModel.FightGroupOrderJoinStatus.JoinFailed;
                            item.FightGroupCanRefund  = false;
                        }
                        item.OrderCanRefund = item.OrderCanRefund && item.FightGroupCanRefund;
                    }
                }
            }
            #endregion

            return(View(orderList.ToList()));
        }
 public static ShopBonusGrantInfo GetByOrderId(long orderid)
 {
     return(_iShopBonusService.GetByOrderId(orderid));
 }