コード例 #1
0
        public async Task <RepResult <PersonnelVM> > Add(PersonnelVM vm)
        {
            if (dbContext != null)
            {
                if (!await dbContext.Personnels.AnyAsync(a => a.NationalCode.Contains(vm.NationalCode)))
                {
                    var obj = PersonnelVM.GetAnyObject(vm, true);
                    dbContext.Entry(obj.Item1).State = EntityState.Added;
                    dbContext.Entry(obj.Item2).State = EntityState.Added;
                    //await dbContext.Personnels.AddAsync(obj.Item1);
                    //await dbContext.Users.AddAsync(obj.Item2);
                    await dbContext.SaveChangesAsync();

                    return(new RepResult <PersonnelVM> {
                        Successed = true, ResultObject = (await Get(obj.Item1.PersonnelId)).ResultObject
                    });
                }
                else
                {
                    return(new RepResult <PersonnelVM> {
                        Successed = false, Message = "پرسنل با این کد ملی در سیستم ثبت شده است. شما قادر به ثبت مجدد نمی باشید", ResultObject = null
                    });
                }
            }

            return(new RepResult <PersonnelVM>());
        }
コード例 #2
0
        public async Task <RepResult <bool> > ReNewDatabse()
        {
            if (dbContext != null)
            {
                dbContext.AttendanceSpans.RemoveRange(dbContext.AttendanceSpans.ToList());
                dbContext.AttendanceTimes.RemoveRange(dbContext.AttendanceTimes.ToList());
                dbContext.Days.RemoveRange(dbContext.Days.ToList());
                dbContext.ActionRequests.RemoveRange(dbContext.ActionRequests.ToList());
                dbContext.Personnels.RemoveRange(dbContext.Personnels.ToList());
                dbContext.Users.RemoveRange(dbContext.Users.ToList());

                await dbContext.SaveChangesAsync();

                DbInitializer.Seed(dbContext);

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

            return(new RepResult <bool>());
        }
        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> ());
        }
コード例 #4
0
        public async Task <RepResult <bool> > InsertAttendanceTime(int personnelId, WorkTimeType timeType, DateTime dateTime)
        {
            if (dbContext != null)
            {
                var query = from a in dbContext.AttendanceTimes
                            where a.PersonnelId == personnelId && a.DateTime.Date == dateTime.Date
                            orderby a.DateTime descending
                            select a;

                var enumer = query.ToAsyncEnumerable();
                var any    = await enumer.Any();

                var count = await enumer.Count();

                if (any)
                {
                    var lastObj = await query.FirstOrDefaultAsync();

                    if (lastObj.DateTime > dateTime)
                    {
                        return(new RepResult <bool> {
                            Successed = false, Message = "زمان که برای ثبت تعیین شده از آخرین زمان که در سیستم ثبت شده کوچکتر است ", ResultObject = false
                        });
                    }
                }

                if (any && (count % 2) == 1)
                {
                    var lastObj = await query.FirstOrDefaultAsync();

                    if (lastObj.TimeType != timeType)
                    {
                        if (timeType == WorkTimeType.LeaveWork ||
                            timeType == WorkTimeType.Mission ||
                            timeType == WorkTimeType.OverTime)
                        {
                            await dbContext.AttendanceTimes.AddAsync(new AttendanceTime()
                            {
                                PersonnelId = personnelId,
                                DateTime    = dateTime,
                                TimeType    = lastObj.TimeType
                            });
                        }
                    }
                }

                await dbContext.AttendanceTimes.AddAsync(new AttendanceTime()
                {
                    PersonnelId = personnelId,
                    DateTime    = dateTime.AddSeconds(1),
                    TimeType    = timeType
                });

                await dbContext.SaveChangesAsync();

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

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