public async Task <MainResponse> CreateUpdateClass(AddClassRequest addClassRequest, string actionBy)
        {
            if (addClassRequest.ClassId == 0)
            {
                var classNumber = _classRepository.GetSingle(x => x.ClassNumber == addClassRequest.ClassNumber && x.IsActive == true && x.IsDeleted == false);
                if (classNumber != null && classNumber.ClassId > 0)
                {
                    _mainResponse.Message = Constants.CLASS_NUMBER_EXIST;
                    _mainResponse.Success = false;
                    return(_mainResponse);
                }
                var classExist = _classRepository.GetSingle(x => x.Name == addClassRequest.Name && x.AgeGroup == addClassRequest.AgeGroup &&
                                                            x.IsActive == true && x.IsDeleted == false);
                if (classExist != null && classExist.ClassId > 0)
                {
                    _mainResponse.Message = Constants.CLASS_EXIST;
                    _mainResponse.Success = false;
                    return(_mainResponse);
                }
                var classes = new Classes
                {
                    ClassNumber   = addClassRequest.ClassNumber,
                    ClassHeaderId = addClassRequest.ClassHeaderId,
                    Name          = addClassRequest.Name,
                    Location      = addClassRequest.Location,
                    AgeGroup      = addClassRequest.AgeGroup,
                    IsActive      = true,
                    CreatedBy     = actionBy,
                    CreatedDate   = DateTime.Now
                };
                var _class = await _classRepository.AddAsync(classes);


                var schedule = new ScheduleDates
                {
                    ClassId     = _class.ClassId,
                    Date        = addClassRequest.ScheduleDate,
                    Time        = addClassRequest.ScheduleTime,
                    IsActive    = true,
                    CreatedBy   = actionBy,
                    CreatedDate = DateTime.Now,
                };
                await _scheduleDateRepository.AddAsync(schedule);

                if (addClassRequest.getClassSplit != null)
                {
                    foreach (var split in addClassRequest.getClassSplit)
                    {
                        var classSplit = new ClassSplits
                        {
                            ClassId               = _class.ClassId,
                            SplitNumber           = addClassRequest.SplitNumber,
                            ChampionShipIndicator = addClassRequest.ChampionShipIndicator,
                            Entries               = split.Entries,
                            IsActive              = true,
                            CreatedBy             = actionBy,
                            CreatedDate           = DateTime.Now,
                        };
                        await _splitClassRepository.AddAsync(classSplit);
                    }
                }

                _mainResponse.Message = Constants.CLASS_CREATED;
                _mainResponse.Success = true;
                _mainResponse.NewId   = _class.ClassId;
                return(_mainResponse);
            }
            else
            {
                var checkNumber = _classRepository.GetSingle(x => x.ClassId == addClassRequest.ClassId && x.ClassNumber == addClassRequest.ClassNumber &&
                                                             x.IsActive == true && x.IsDeleted == false);
                if (checkNumber == null)
                {
                    var classNumber = _classRepository.GetSingle(x => x.ClassNumber == addClassRequest.ClassNumber && x.IsActive == true && x.IsDeleted == false);
                    if (classNumber != null && classNumber.ClassId > 0)
                    {
                        _mainResponse.Message = Constants.CLASS_NUMBER_EXIST;
                        _mainResponse.Success = false;
                        return(_mainResponse);
                    }
                }
                var checkName = _classRepository.GetSingle(x => x.Name == addClassRequest.Name && x.AgeGroup == addClassRequest.AgeGroup && x.ClassId == addClassRequest.ClassId &&
                                                           x.IsActive == true && x.IsDeleted == false);
                if (checkName == null)
                {
                    var classExist = _classRepository.GetSingle(x => x.Name == addClassRequest.Name && x.AgeGroup == addClassRequest.AgeGroup &&
                                                                x.IsActive == true && x.IsDeleted == false);
                    if (classExist != null && classExist.ClassId > 0)
                    {
                        _mainResponse.Message = Constants.CLASS_EXIST;
                        _mainResponse.Success = false;
                        return(_mainResponse);
                    }
                }

                var updateClass = _classRepository.GetSingle(x => x.ClassId == addClassRequest.ClassId && x.IsActive == true && x.IsDeleted == false);
                if (updateClass != null)
                {
                    updateClass.ClassNumber   = addClassRequest.ClassNumber;
                    updateClass.ClassHeaderId = addClassRequest.ClassHeaderId;
                    updateClass.Name          = addClassRequest.Name;
                    updateClass.Location      = addClassRequest.Location;
                    updateClass.AgeGroup      = addClassRequest.AgeGroup;
                    updateClass.ModifiedBy    = actionBy;
                    updateClass.ModifiedDate  = DateTime.Now;
                    await _classRepository.UpdateAsync(updateClass);
                }

                var updateClassSchedule = _scheduleDateRepository.GetSingle(x => x.ClassId == addClassRequest.ClassId && x.IsActive == true && x.IsDeleted == false);
                if (updateClassSchedule != null)
                {
                    updateClassSchedule.Date         = addClassRequest.ScheduleDate;
                    updateClassSchedule.Time         = addClassRequest.ScheduleTime;
                    updateClassSchedule.ModifiedBy   = actionBy;
                    updateClassSchedule.ModifiedDate = DateTime.Now;
                    await _scheduleDateRepository.UpdateAsync(updateClassSchedule);
                }

                if (addClassRequest.getClassSplit != null)
                {
                    _splitClassRepository.DeleteSplitsByClassId(addClassRequest);

                    foreach (var split in addClassRequest.getClassSplit)
                    {
                        var classSplit = new ClassSplits
                        {
                            ClassId               = addClassRequest.ClassId,
                            SplitNumber           = addClassRequest.SplitNumber,
                            ChampionShipIndicator = addClassRequest.ChampionShipIndicator,
                            Entries               = split.Entries,
                            IsActive              = true,
                            CreatedBy             = actionBy,
                            CreatedDate           = DateTime.Now
                        };
                        await _splitClassRepository.AddAsync(classSplit);
                    }
                    ;
                }
                _mainResponse.Message = Constants.CLASS_UPDATED;
                _mainResponse.Success = true;
                _mainResponse.NewId   = addClassRequest.ClassId;
                return(_mainResponse);
            }
        }
        public List <PrdYearMonthCrustScheduleItem> GetScheduleList(string _ScheduleYear, string _ScheduleMonth, string _ProductionFloor)
        {
            using (_context)
            {
                var YearMonthID = (from y in _context.PRD_YearMonth.AsEnumerable()
                                   where y.ScheduleYear == _ScheduleYear & y.ScheduleMonth == _ScheduleMonth & (y.ProductionFloor).ToString() == _ProductionFloor &
                                   y.ScheduleFor == "CRP"
                                   select y.YearMonID).FirstOrDefault();

                var YearMonthSchedule = (from s in _context.PRD_YearMonthSchedule.AsEnumerable()
                                         where s.YearMonID == YearMonthID
                                         select new
                {
                    ScheduleID = s.ScheduleID
                }).ToList();

                var YearMonthScheduleDate = (from s in YearMonthSchedule.AsEnumerable()
                                             join d in _context.PRD_YearMonthScheduleDate on s.ScheduleID equals d.ScheduleID into ScheduleDates
                                             from d in ScheduleDates.DefaultIfEmpty()
                                             select new
                {
                    ScheduleDateID = (d == null ? 0 : d.ScheduleDateID)
                }).ToList();

                var ScheduleItem = (from i in _context.PRD_YearMonthFinishScheduleItem.AsEnumerable()
                                    join d in YearMonthScheduleDate on i.ScheduleDateID equals d.ScheduleDateID into Items
                                    from d in Items.DefaultIfEmpty()

                                    join b in _context.Sys_Buyer on i.BuyerID equals b.BuyerID into Buyers
                                    from b in Buyers.DefaultIfEmpty()

                                    join o in _context.SLS_BuyerOrder on i.BuyerOrderID equals o.BuyerOrderID into Orders
                                    from o in Orders.DefaultIfEmpty()

                                    join u in _context.Sys_Unit on i.AvgSizeUnit equals u.UnitID into AVGUnits
                                    from u in AVGUnits.DefaultIfEmpty()

                                    join tu in _context.Sys_Unit on i.ThicknessUnit equals tu.UnitID into ThickUnits
                                    from tu in ThickUnits.DefaultIfEmpty()

                                    join a in _context.Sys_Article on i.ArticleID equals a.ArticleID into Articles
                                    from a in Articles.DefaultIfEmpty()

                                    join it in _context.Sys_ItemType on i.ItemTypeID equals it.ItemTypeID into ItemTypes
                                    from it in ItemTypes.DefaultIfEmpty()

                                    join ls in _context.Sys_LeatherStatus on i.LeatherStatusID equals ls.LeatherStatusID into LeatherStatus
                                    from ls in LeatherStatus.DefaultIfEmpty()

                                    select new PrdYearMonthCrustScheduleItem
                {
                    ScheduleItemID = i.ScheduleItemID,
                    ScheduleProductionNo = i.ScheduleProductionNo,
                    BuyerID = i.BuyerID,
                    BuyerName = (b == null ? null : b.BuyerName),
                    BuyerOrderID = i.BuyerOrderID,
                    BuyerOrderNo = (o == null ? null : o.BuyerOrderNo),

                    ItemTypeID = i.ItemTypeID,
                    ItemTypeName = (it == null ? null : it.ItemTypeName),

                    LeatherStatusID = i.LeatherStatusID,
                    LeatherStatusName = (ls == null ? null : ls.LeatherStatusName),
                    ArticleID = i.ArticleID,
                    ArticleNo = i.ArticleNo,
                    ArticleName = (a == null ? null : a.ArticleName),
                    ArticleChallanID = i.ArticleChallanID,
                    ArticleChallanNo = i.ArticleChallanNo,
                    AvgSize = i.AvgSize,
                    AvgSizeUnit = i.AvgSizeUnit,
                    AvgSizeUnitName = (u == null ? null : u.UnitName),
                    SideDescription = i.SideDescription,
                    SelectionRange = i.SelectionRange,
                    Thickness = i.Thickness,
                    ThicknessUnit = i.ThicknessUnit,
                    ThicknessUnitName = (tu == null ? null : tu.UnitName),
                    ThicknessAt = i.ThicknessAt == "AFSV" ? "After Shaving" : "After Finishing",
                    Remarks = i.Remarks
                }).ToList();

                return(ScheduleItem);
            }
        }