public static AttendanceSpanVM GetVM(AttendanceSpan span)
        {
            var vm = new AttendanceSpanVM();

            vm.AttendanceSpanId = span.AttendanceSpanId;
            vm.TimeType         = span.TimeType;
            vm.TimeTypeName     = EnumName.GetWorkTimeType(span.TimeType);

            perDtStart   = new PersianDateTime(span.StartAttendanceTime.DateTime);
            vm.StartTime = perDtStart.ToString("hh:mm:ss tt");

            if (span.EndAttendanceTime != null)
            {
                perDtEnd   = new PersianDateTime(span.EndAttendanceTime.DateTime);
                vm.EndTime = perDtEnd.ToString("hh:mm:ss tt");

                TimeSpan sp = span.EndAttendanceTime.DateTime.Subtract(span.StartAttendanceTime.DateTime);
                vm.DurationSecends = System.Convert.ToInt64(sp.TotalSeconds);
                vm.Duration        = Environment.Convert.TimeSpanToReadableString(sp);
            }

            return(vm);
        }
        public async Task <RepResult <bool> > GenerateSpans(int personnelId, DateTime dayDate)
        {
            if (dbContext != null)
            {
                await RemoveSpans(personnelId, dayDate);

                var qTimes = from a in dbContext.AttendanceTimes
                             where a.PersonnelId == personnelId && a.DateTime.Date == dayDate.Date.Date
                             orderby a.DateTime
                             select a;


                var day = await GetDayModel(personnelId, dayDate);

                var times = await qTimes.ToListAsync();

                if (times.Count > 0)
                {
                    AttendanceSpan        atdSpan  = new AttendanceSpan();
                    List <AttendanceSpan> spanlist = new List <AttendanceSpan>();
                    foreach (var item in times.Select((value, index) => (value, index)))
                    {
                        // first span item
                        if (item.index % 2 == 0)
                        {
                            atdSpan          = new AttendanceSpan();
                            atdSpan.TimeType = item.value.TimeType;

                            atdSpan.StartAttendanceTime = item.value;
                            atdSpan.Day = day;

                            if (item.index == times.Count - 1)
                            {
                                spanlist.Add(atdSpan);
                            }
                        }

                        // last span item
                        if (item.index % 2 != 0)
                        {
                            atdSpan.EndAttendanceTime = item.value;
                            spanlist.Add(atdSpan);
                        }
                    }

                    await dbContext.AttendanceSpans.AddRangeAsync(spanlist);

                    await dbContext.SaveChangesAsync();

                    var res = await GetSpans(day);

                    if (res.Successed)
                    {
                        return new RepResult <bool> {
                                   Successed = true, ResultObject = true
                        }
                    }
                    ;
                }
            }

            return(new RepResult <bool> ());
        }