コード例 #1
0
        public IReadOnlyList <AggregationResult> AggregateInstances(ChartTime time,
                                                                    FilterDefinition <ClothKind> filter, FilterDefinition <Order> orderDateFilter)
        {
            var filters = Builders <ClothKind> .Filter.And(
                Builders <ClothKind> .Filter.Exists(nameof(RepositoryElement.DeletionDate), false),
                filter ?? Builders <ClothKind> .Filter.Empty);

            var daysPart = time == ChartTime.Day
        ? "'day': {  '$dayOfMonth': '$ClothInstances.ExecutionDate'  },"
        : string.Empty;
            var monthPart = time == ChartTime.Day || time == ChartTime.Mounth
        ? "'month' : {'$month': '$ClothInstances.ExecutionDate'},"
        : string.Empty;
            var projectDef   = $@"
{{
      SumPrice: {{$multiply:['$Price', '$ClothInstances.Amount']}}, 
      MeasureKind: '$MeasureKind',
      Price : '$Price',
      Count: '$Count',
      WashPrice: '$WashPrice',
      Name: '$Name',
      Amount: '$ClothInstances.Amount',
      DateTime: {{
        '$dateFromParts': {{
          {daysPart}
          {monthPart}
          'year': {{
            '$year': '$ClothInstances.ExecutionDate'
          }}        
        }}
      }}
}}";
            var readOnlyList = this.GetAggregationFluentForAggregation(false, filter, orderDateFilter)
                               .Match(filters)
                               .AppendStage <BsonDocument>(AggrStagesForChart[0].AsBsonDocument)
                               .AppendStage <BsonDocument>(AggrStagesForChart[1].AsBsonDocument)
                               .Project(projectDef)
                               .AppendStage <BsonDocument>(AggrStagesForChart[3].AsBsonDocument)
                               .AppendStage <BsonDocument>(AggrStagesForChart[4].AsBsonDocument)
                               .As <AggregationResult>()
                               .SortBy(x => x.DateTime).ToList();

            return(readOnlyList);
        }
コード例 #2
0
        public IReadOnlyList <AggregationResult> AggregateOrders(ChartTime time, FilterDefinition <Order> filter = null)
        {
            var filters = Builders <Order> .Filter.And(
                Builders <Order> .Filter.Exists(nameof(RepositoryElement.DeletionDate), false),
                filter ?? Builders <Order> .Filter.Empty);

            var groupingDefiniton = @"
{
  _id: '$DateTime',
  Price: {
    $sum: '$Price'
  },
  UnCountableCount : { 
    $sum : { 
      $cond:[
          {$eq: ['$MeasureKind', 1]},
          '$Amount', 
          0]
      }
  },
  Count : { 
    $sum : { 
      $cond:[
          {$in: ['$MeasureKind', [0,2]]},
          '$Amount', 
          0]
      }
  }
}";
            var daysPart          = time == ChartTime.Day
        ? "'day': {  '$dayOfMonth': '$ExecutionDate'  },"
        : string.Empty;
            var monthPart = time == ChartTime.Day || time == ChartTime.Mounth
        ? "'month' : {'$month': '$ExecutionDate'},"
        : string.Empty;

            var projectionDefinition = $@"
{{
  Price: '$Price',
  Amount: '$Instances.Amount',
  Count: '$Count',
  MeasureKind : '$ClothKind.MeasureKind',
  UnCountableCount:'$UnCountableCount',
  DateTime:{{$dateFromParts:{{
    {daysPart}
    {monthPart}
    'year': {{$year: ""$ExecutionDate""}}
    }}
  }}
}}";


            var readOnlyList = this.Collection.Aggregate()
                               .Match(filters)
                               .Unwind("Instances", new AggregateUnwindOptions <Order>()
            {
                PreserveNullAndEmptyArrays = true
            })
                               .Lookup("clothkinds", "Instances.ClothKind", "_id", "ClothKind")
                               .Unwind("ClothKind")
                               .Project(projectionDefinition)
                               .Group(groupingDefiniton)
                               .As <AggregationResult>()
                               .Sort(@"{_id: 1}")
                               .ToList();

            return(readOnlyList);
        }
コード例 #3
0
        private void calculateAndReloadChart()
        {
            var rp     = ReportManager.getInstance().ReportModel;
            var orders = ReportManager.getInstance().ReportModel.payOrders.ToList();
            Dictionary <string, int> valuesByTimes = new Dictionary <string, int>();
            ChartTime chartTime = ChartTime.ChartByHourse;

            if (rp.startDate == rp.endDate)   //gio
            {
                chartTime = ChartTime.ChartByHourse;
                for (int i = 0; i < 24; i++)
                {
                    valuesByTimes[i.ToString()] = 0;
                }
            }
            else if ((rp.endDate - rp.startDate).TotalDays >= 735)     // nam
            {
                chartTime = ChartTime.ChartByYear;
                for (int i = rp.startDate.Year; i <= rp.endDate.Year; i++)
                {
                    valuesByTimes[i.ToString()] = 0;
                }
            }
            else if ((rp.endDate - rp.startDate).TotalDays >= 24)     // thang
            {
                chartTime = ChartTime.ChartByMonth;
                for (int i = rp.startDate.Year; i <= rp.endDate.Year; i++)
                {
                    for (int j = 1; j <= 12; j++)
                    {
                        var month   = new DateTime(i, j, 1);
                        var monthTs = UtilFuction.GetTime(month);
                        var startTs = UtilFuction.GetTime(rp.startDate) - TimeSpan.TicksPerDay * 31 * 10000;
                        var endTs   = UtilFuction.GetTime(rp.endDate);
                        if (startTs <= monthTs && endTs >= monthTs)
                        {
                            valuesByTimes[j.ToString("D2") + '/' + i.ToString()] = 0;
                        }
                    }
                }
            }
            else     //ngay
            {
                chartTime = ChartTime.ChartByDate;
                for (int i = rp.startDate.Year; i <= rp.endDate.Year; i++)
                {
                    for (int j = 1; j <= 12; j++)
                    {
                        var totalDay = DateTime.DaysInMonth(i, j);
                        for (int z = 1; z <= totalDay; z++)
                        {
                            var day     = new DateTime(i, j, z);
                            var dayTs   = UtilFuction.GetTime(day);
                            var startTs = UtilFuction.GetTime(rp.startDate);
                            var endTs   = UtilFuction.GetTime(rp.endDate);
                            if (startTs <= dayTs && endTs >= dayTs)
                            {
                                valuesByTimes[z.ToString("D2") + '/' + j.ToString("D2")] = 0;
                            }
                        }
                    }
                }
            }
            while (orders.Count > 0)
            {
                var frontOrder = orders[0];
                switch (chartTime)
                {
                case ChartTime.ChartByHourse: {
                    valuesByTimes[frontOrder.CreatedDate.TimeOfDay.Hours.ToString()] += 1;
                    break;
                }

                case ChartTime.ChartByDate: {
                    valuesByTimes[frontOrder.CreatedDate.ToString("dd/MM")] += 1;
                    break;
                }

                case ChartTime.ChartByMonth: {
                    valuesByTimes[frontOrder.CreatedDate.ToString("MM/yyyy")] += 1;
                    break;
                }

                case ChartTime.ChartByYear: {
                    valuesByTimes[frontOrder.CreatedDate.ToString("yyyy")] += 1;
                    break;
                }
                }

                orders.RemoveAt(0);
            }
            List <KeyValuePair <string, int> > MyValue = new List <KeyValuePair <string, int> >();

            foreach (KeyValuePair <string, int> entry in valuesByTimes)
            {
                MyValue.Add(new KeyValuePair <string, int>(entry.Key, entry.Value));
            }

            //List<KeyValuePair<string, int>> MyValue = new List<KeyValuePair<string, int>>();
            //MyValue.Add(new KeyValuePair<string, int>("Administration", 20));
            //MyValue.Add(new KeyValuePair<string, int>("Management", 36));
            //MyValue.Add(new KeyValuePair<string, int>("Development", 89));
            //MyValue.Add(new KeyValuePair<string, int>("Support", 270));
            //MyValue.Add(new KeyValuePair<string, int>("Sales", 140));
            LineChart1.DataContext = MyValue;
        }