Exemplo n.º 1
0
 public ISubtotalResult SelectVouchersGrouped(IVoucherGroupedQuery query)
 => m_Db.SelectVouchersGrouped(query);
Exemplo n.º 2
0
        /// <inheritdoc />
        public IEnumerable <Balance> SelectVouchersGrouped(IVoucherGroupedQuery query)
        {
            if (query.Subtotal.GatherType != GatheringType.VoucherCount)
            {
                throw new InvalidOperationException("记账凭证分类汇总只能计数");
            }
            if (query.Subtotal.EquivalentDate.HasValue)
            {
                throw new InvalidOperationException("记账凭证分类汇总不能等值");
            }

            var level = query.Subtotal.Levels.Aggregate(SubtotalLevel.None, (total, l) => total | l);

            if (query.Subtotal.AggrType != AggregationType.None)
            {
                level |= query.Subtotal.AggrInterval;
            }

            if (level.HasFlag(SubtotalLevel.User))
            {
                throw new InvalidOperationException("记账凭证不能按用户分类汇总");
            }
            if (level.HasFlag(SubtotalLevel.Currency))
            {
                throw new InvalidOperationException("记账凭证不能按币种分类汇总");
            }
            if (level.HasFlag(SubtotalLevel.Title))
            {
                throw new InvalidOperationException("记账凭证不能按一级科目分类汇总");
            }
            if (level.HasFlag(SubtotalLevel.SubTitle))
            {
                throw new InvalidOperationException("记账凭证不能按二级科目分类汇总");
            }
            if (level.HasFlag(SubtotalLevel.Content))
            {
                throw new InvalidOperationException("记账凭证不能按内容分类汇总");
            }
            if (level.HasFlag(SubtotalLevel.Remark))
            {
                throw new InvalidOperationException("记账凭证不能按备注分类汇总");
            }

            var preF = query.VoucherQuery.Accept(new MongoDbNativeVoucher());

            BsonDocument pprj;

            if (!level.HasFlag(SubtotalLevel.Day))
            {
                pprj = ProjectNothing;
            }
            else if (!level.HasFlag(SubtotalLevel.Week))
            {
                pprj = ProjectNothingButDate;
            }
            else if (level.HasFlag(SubtotalLevel.Year))
            {
                pprj = ProjectNothingButYear;
            }
            else if (level.HasFlag(SubtotalLevel.Month))
            {
                pprj = ProjectNothingButMonth;
            }
            else // if (level.HasFlag(SubtotalLevel.Week))
            {
                pprj = ProjectNothingButWeek;
            }

            var prj = new BsonDocument();

            if (level.HasFlag(SubtotalLevel.Day))
            {
                prj["date"] = "$date";
            }

            var grp = new BsonDocument
            {
                ["_id"]   = prj,
                ["count"] = new BsonDocument {
                    ["$sum"] = 1
                }
            };

            var fluent = m_Vouchers.Aggregate().Match(preF).Project(pprj).Group(grp);

            return(fluent.ToEnumerable().Select(b => BsonSerializer.Deserialize <Balance>(b)));
        }