示例#1
0
        private void GetMeasuresFromActivityConcept(ActivityConcept concept)
        {
            var measure = new Measure()
            {
                Name = concept.Name,
                Time = 0
            };

            foreach (var filterRule in concept.Rules)
            {
                try
                {
                    var activities = _attUow.Activities.GetAll();
                    var list       = _activityQueryExecuter.Query(activities, filterRule.Expression).ToList();

                    /*var sql = "SELECT * FROM Activities";
                     *
                     * if (!String.IsNullOrEmpty(filterRule.Expression))
                     * {
                     *  sql += " WHERE " + filterRule.Expression;
                     * }
                     *
                     * var list = _attUow.Activities.GetBySQL(sql).ToList();*/
                    var time = list.Sum(m => m.Life);
                    measure.Time += time;

                    var monthList = list.Where(m => m.Date.Year == DateTime.Now.Year &&
                                               m.Date.Month == DateTime.Now.Month).ToList();
                    var timeThisMonth = monthList.Sum(m => m.Life);
                    measure.TimeMonthAccumulation += timeThisMonth;

                    var todayList = monthList.Where(m => m.Date.Day == DateTime.Now.Day).ToList();
                    var timeToday = todayList.Sum(m => m.Life);
                    measure.TimeToday += timeToday;
                }
                catch (Exception e)
                {
                    Measures.Add(new Measure()
                    {
                        Name = e.Message, Time = 0.0
                    });
                }
            }
            measure.TimeMonthAccumulation -= measure.TimeToday;
            measure.TimeRestAccumulation   = Math.Round((measure.Time - measure.TimeMonthAccumulation - measure.TimeToday), 2);
            Measures.Add(measure);
        }
        // POST api/filterrule
        public HttpResponseMessage Post(FilterRule filterRule)
        {
            var item = Uow.FilterRules.GetById(filterRule.Id);

            _activityQueryExecuter.Query(new List <Activity>().AsQueryable(), filterRule.Expression);
            var compilerResults = _activityQueryExecuter.CompilerResults;

            if (compilerResults.Errors.Count > 0)
            {
                var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
                var reason   = "Invalid expression: ";

                foreach (var error in compilerResults.Errors)
                {
                    var currentError = error.ToString();
                    currentError = currentError.Substring(currentError.IndexOf("error", System.StringComparison.Ordinal));
                    currentError = currentError.Substring(currentError.IndexOf(":", System.StringComparison.Ordinal) + 1);

                    reason += "<br/>-" + currentError;
                }
                response.ReasonPhrase = reason;
                return(response);
            }

            //update
            if (null != item)
            {
                item.Name        = filterRule.Name;
                item.Description = filterRule.Description;
                item.Expression  = filterRule.Expression;

                Uow.FilterRules.Update(item);
                Uow.Commit();
                return(new HttpResponseMessage(HttpStatusCode.Accepted));
            }
            else //create
            {
                filterRule.ActivityConcepts.Clear();
                Uow.FilterRules.Add(filterRule);
                Uow.Commit();
                return(new HttpResponseMessage(HttpStatusCode.Created));
            }
        }