コード例 #1
0
ファイル: EquipmentHelper.cs プロジェクト: ychung-mot/hets
        /// <summary>
        /// Recalculates seniority with the new sorting rule (sorting by equipment code) for the district equipment types that have the same seniority and received date
        /// </summary>
        /// <param name="context"></param>
        /// <param name="seniorityScoringRules"></param>
        /// <param name="connectionString"></param>
        public static void RecalculateSeniorityList(PerformContext context, string seniorityScoringRules, string connectionString)
        {
            // open a connection to the database
            DbAppContext dbContext = new DbAppContext(connectionString);

            // get equipment status
            int?equipmentStatusId = StatusHelper.GetStatusId(HetEquipment.StatusApproved, "equipmentStatus", dbContext);

            if (equipmentStatusId == null)
            {
                throw new ArgumentException("Status Code not found");
            }

            context.WriteLine("Recalculation Started");

            var progress = context.WriteProgressBar();

            progress.SetValue(0);

            var equipments = dbContext.HetEquipment.AsNoTracking()
                             .Where(x => x.EquipmentStatusTypeId == equipmentStatusId)
                             .GroupBy(x => new { x.LocalAreaId, x.DistrictEquipmentTypeId, x.Seniority, x.ReceivedDate })
                             .Where(x => x.Count() > 1)
                             .Select(x => new { x.Key.LocalAreaId, x.Key.DistrictEquipmentTypeId })
                             .Distinct()
                             .ToList();

            var count = 0;

            foreach (var equipment in equipments)
            {
                EquipmentHelper.RecalculateSeniority(equipment.LocalAreaId, equipment.DistrictEquipmentTypeId, dbContext, seniorityScoringRules);
                progress.SetValue(Convert.ToInt32(++count / equipments.Count * 100));
                context.WriteLine($"Processed {count} / {equipments.Count}");
            }

            progress.SetValue(100);

            context.WriteLine("Recalculation Finished");
        }
コード例 #2
0
        public static void MergeDistrictEquipmentTypes(PerformContext context, string seniorityScoringRules,
                                                       string connectionString)
        {
            // open a connection to the database
            DbAppContext dbContext = new DbAppContext(connectionString);

            // get equipment status
            int?equipmentStatusId = StatusHelper.GetStatusId(HetEquipment.StatusApproved, "equipmentStatus", dbContext);

            if (equipmentStatusId == null)
            {
                throw new ArgumentException("Status Code not found");
            }

            // **************************************************
            // Phase 1: Identify Master District Equipment Types
            // **************************************************
            context.WriteLine("Phase 1: Identify Master District Equipment Types");
            IProgressBar progress = context.WriteProgressBar();

            progress.SetValue(0);

            // get records
            List <MergeRecord> masterList = dbContext.HetDistrictEquipmentType.AsNoTracking()
                                            .Where(x => x.ServiceAreaId != null &&
                                                   x.Deleted == false)
                                            .Select(x => new MergeRecord
            {
                DistrictEquipmentTypeId = x.DistrictEquipmentTypeId,
                DistrictEquipmentName   = x.DistrictEquipmentName,
                EquipmentPrefix         = GetPrefix(x.DistrictEquipmentName),
                DistrictId      = x.DistrictId,
                EquipmentTypeId = x.EquipmentTypeId
            })
                                            .Distinct()
                                            .ToList();

            // sort the list accordingly
            masterList = masterList
                         .OrderBy(x => x.DistrictId)
                         .ThenBy(x => x.EquipmentTypeId)
                         .ThenBy(x => x.EquipmentPrefix).ToList();

            int    increment       = 0;
            int?   currentDistrict = -1;
            int?   masterDistrictEquipmentTypeId = -1;
            int?   currentEquipmentType          = -1;
            string currentPrefix = "";

            foreach (MergeRecord detRecord in masterList)
            {
                bool newMerge;

                if (detRecord.DistrictId != currentDistrict ||
                    detRecord.EquipmentTypeId != currentEquipmentType ||
                    detRecord.EquipmentPrefix != currentPrefix)
                {
                    newMerge             = true;
                    currentDistrict      = detRecord.DistrictId;
                    currentEquipmentType = detRecord.EquipmentTypeId;
                    currentPrefix        = detRecord.EquipmentPrefix;

                    masterDistrictEquipmentTypeId = detRecord.DistrictEquipmentTypeId;
                    detRecord.Master = true;
                    detRecord.MasterDistrictEquipmentTypeId = masterDistrictEquipmentTypeId;
                }
                else
                {
                    newMerge         = false;
                    detRecord.Master = false;
                    detRecord.MasterDistrictEquipmentTypeId = masterDistrictEquipmentTypeId;
                }

                // kickoff the merge for this district, equipment type and prefix
                if (newMerge)
                {
                    int    district   = currentDistrict ?? -1;
                    int    type       = currentEquipmentType ?? -1;
                    string prefix     = currentPrefix;
                    string masterName = "";

                    List <MergeRecord> types = masterList
                                               .Where(x => x.DistrictId == district &&
                                                      x.EquipmentTypeId == type &&
                                                      x.EquipmentPrefix == prefix).ToList();

                    // create master name and update master record
                    foreach (MergeRecord equipmentType in types)
                    {
                        string temp  = equipmentType.DistrictEquipmentName.Replace(currentPrefix, "").Trim();
                        int    start = temp.IndexOf("-", StringComparison.Ordinal);
                        if (start > -1)
                        {
                            start++;
                        }
                        int length = temp.Length - start < 0 ? 0 : temp.Length - start;
                        temp = temp.Substring(start, length).Trim();

                        masterName = masterName.Length > 0 ?
                                     $"{masterName} | {temp}" :
                                     temp;
                    }

                    masterName = $"{currentPrefix} - {masterName}";
                    types.ElementAt(0).DistrictEquipmentName = masterName;
                }

                // update status bar
                increment++;
                progress.SetValue(Convert.ToInt32((decimal)(masterList.Count - (masterList.Count - increment)) / masterList.Count * 100));
            }

            // done!
            progress.SetValue(100);

            // **************************************************
            // Phase 2: Update Master District Equipment Types
            // **************************************************
            context.WriteLine("Phase 2: Update Master District Equipment Types");
            progress = context.WriteProgressBar();
            progress.SetValue(0);

            List <MergeRecord> masterRecords = masterList.Where(x => x.Master).ToList();

            increment = 0;

            foreach (MergeRecord detRecord in masterRecords)
            {
                // get det record & update name
                HetDistrictEquipmentType det = dbContext.HetDistrictEquipmentType
                                               .First(x => x.DistrictEquipmentTypeId == detRecord.DistrictEquipmentTypeId);

                det.DistrictEquipmentName = detRecord.DistrictEquipmentName;
                det.ServiceAreaId         = null;

                // save changes to district equipment types and associated equipment records
                dbContext.SaveChangesForImport();

                // update status bar
                increment++;
                progress.SetValue(Convert.ToInt32((decimal)(masterRecords.Count - (masterRecords.Count - increment)) / masterRecords.Count * 100));
            }

            // done!
            progress.SetValue(100);

            // **************************************************
            // Phase 3: Update Non-Master District Equipment Types
            // **************************************************
            context.WriteLine("Phase 3: Update Non-Master District Equipment Types");
            progress = context.WriteProgressBar();
            progress.SetValue(0);

            List <MergeRecord> mergeRecords = masterList.Where(x => !x.Master).ToList();

            increment = 0;

            foreach (MergeRecord detRecord in mergeRecords)
            {
                int originalDistrictEquipmentTypeId = detRecord.DistrictEquipmentTypeId;
                int?newDistrictEquipmentTypeId      = detRecord.MasterDistrictEquipmentTypeId;

                // get equipment & update
                IEnumerable <HetEquipment> equipmentRecords = dbContext.HetEquipment
                                                              .Where(x => x.DistrictEquipmentTypeId == originalDistrictEquipmentTypeId);

                foreach (HetEquipment equipment in equipmentRecords)
                {
                    equipment.DistrictEquipmentTypeId = newDistrictEquipmentTypeId;
                }

                // save changes to associated equipment records
                dbContext.SaveChangesForImport();

                // get det record
                HetDistrictEquipmentType det = dbContext.HetDistrictEquipmentType
                                               .First(x => x.DistrictEquipmentTypeId == originalDistrictEquipmentTypeId);

                // delete old det record
                HetRentalRequest request = dbContext.HetRentalRequest.AsNoTracking()
                                           .FirstOrDefault(x => x.DistrictEquipmentTypeId == originalDistrictEquipmentTypeId);

                HetLocalAreaRotationList rotationList = dbContext.HetLocalAreaRotationList.AsNoTracking()
                                                        .FirstOrDefault(x => x.DistrictEquipmentTypeId == originalDistrictEquipmentTypeId);

                if (request != null || rotationList != null)
                {
                    det.Deleted = true;
                }
                else
                {
                    dbContext.HetDistrictEquipmentType.Remove(det);
                }

                // save changes to district equipment types and associated equipment records
                dbContext.SaveChangesForImport();

                // update status bar
                increment++;
                progress.SetValue(Convert.ToInt32((decimal)(mergeRecords.Count - (mergeRecords.Count - increment)) / mergeRecords.Count * 100));
            }

            // done!
            progress.SetValue(100);

            // **************************************************
            // Phase 4: Update seniority and block assignments
            // **************************************************
            context.WriteLine("Phase 4: Update seniority and block assignments");
            progress = context.WriteProgressBar();
            progress.SetValue(0);

            increment = 0;

            foreach (MergeRecord detRecord in masterRecords)
            {
                // update the seniority and block assignments for the master record
                List <HetLocalArea> localAreas = dbContext.HetEquipment.AsNoTracking()
                                                 .Include(x => x.LocalArea)
                                                 .Where(x => x.EquipmentStatusTypeId == equipmentStatusId &&
                                                        x.DistrictEquipmentTypeId == detRecord.DistrictEquipmentTypeId)
                                                 .Select(x => x.LocalArea)
                                                 .Distinct()
                                                 .ToList();

                foreach (HetLocalArea localArea in localAreas)
                {
                    EquipmentHelper.RecalculateSeniority(localArea.LocalAreaId,
                                                         detRecord.DistrictEquipmentTypeId, dbContext, seniorityScoringRules);
                }

                // save changes to equipment records
                dbContext.SaveChangesForImport();

                // update status bar
                increment++;
                progress.SetValue(Convert.ToInt32((decimal)(masterRecords.Count - (masterRecords.Count - increment)) / masterRecords.Count * 100));
            }

            // done!
            progress.SetValue(100);

            // **************************************************
            // Phase 5: Cleanup "empty" District Equipment Types
            // **************************************************
            context.WriteLine("Phase 5: Cleanup empty District Equipment Types");
            progress = context.WriteProgressBar();
            progress.SetValue(0);

            // get records
            List <HetDistrictEquipmentType> districtEquipmentTypes = dbContext.HetDistrictEquipmentType.AsNoTracking()
                                                                     .Include(x => x.HetEquipment)
                                                                     .Where(x => x.Deleted == false)
                                                                     .Distinct()
                                                                     .ToList();

            increment = 0;

            foreach (HetDistrictEquipmentType districtEquipmentType in districtEquipmentTypes)
            {
                int districtEquipmentTypeId = districtEquipmentType.DistrictEquipmentTypeId;

                // does this det have any equipment records?
                if (districtEquipmentType.HetEquipment.Count < 1)
                {
                    // get det record
                    HetDistrictEquipmentType det = dbContext.HetDistrictEquipmentType
                                                   .First(x => x.DistrictEquipmentTypeId == districtEquipmentTypeId);

                    // delete old det record
                    HetRentalRequest request = dbContext.HetRentalRequest.AsNoTracking()
                                               .FirstOrDefault(x => x.DistrictEquipmentTypeId == districtEquipmentTypeId);

                    HetLocalAreaRotationList rotationList = dbContext.HetLocalAreaRotationList.AsNoTracking()
                                                            .FirstOrDefault(x => x.DistrictEquipmentTypeId == districtEquipmentTypeId);

                    if (request != null || rotationList != null)
                    {
                        det.Deleted = true;
                    }
                    else
                    {
                        dbContext.HetDistrictEquipmentType.Remove(det);
                    }

                    // save changes to district equipment types and associated equipment records
                    dbContext.SaveChangesForImport();
                }

                // update status bar
                increment++;
                progress.SetValue(Convert.ToInt32((decimal)(districtEquipmentTypes.Count - (districtEquipmentTypes.Count - increment)) / districtEquipmentTypes.Count * 100));
            }

            // done!
            progress.SetValue(100);
        }
コード例 #3
0
        public virtual IActionResult DistrictEquipmentTypesIdPost([FromRoute] int id, [FromBody] HetDistrictEquipmentType item)
        {
            if (id != item.DistrictEquipmentTypeId)
            {
                // not found
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // add or update equipment type
            if (item.DistrictEquipmentTypeId > 0)
            {
                bool exists = _context.HetDistrictEquipmentType.Any(a => a.DistrictEquipmentTypeId == id);

                // not found
                if (!exists)
                {
                    return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
                }

                // get equipment status
                int?equipmentStatusId = StatusHelper.GetStatusId(HetEquipment.StatusApproved, "equipmentStatus", _context);

                if (equipmentStatusId == null)
                {
                    return(new NotFoundObjectResult(new HetsResponse("HETS-23", ErrorViewModel.GetDescription("HETS-23", _configuration))));
                }

                // get record
                HetDistrictEquipmentType equipment = _context.HetDistrictEquipmentType
                                                     .Include(x => x.EquipmentType)
                                                     .First(x => x.DistrictEquipmentTypeId == id);

                // HETS-1163 - Recalculate seniority and Blk assignment
                // for change in Blue book section number to and from
                bool currentIsDumpTruck = equipment.EquipmentType.IsDumpTruck;

                HetEquipmentType newEquipmentType = _context.HetEquipmentType.AsNoTracking()
                                                    .FirstOrDefault(x => x.EquipmentTypeId == item.EquipmentType.EquipmentTypeId);

                if (newEquipmentType == null)
                {
                    return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
                }

                bool updateSeniority = currentIsDumpTruck != newEquipmentType.IsDumpTruck;

                // modify record
                equipment.DistrictEquipmentName    = item.DistrictEquipmentName;
                equipment.ConcurrencyControlNumber = item.ConcurrencyControlNumber;
                equipment.DistrictId      = item.District.DistrictId;
                equipment.EquipmentTypeId = item.EquipmentType.EquipmentTypeId;

                // update seniority and assignments for this District Equipment Type (HETS-1163)
                if (updateSeniority)
                {
                    IConfigurationSection scoringRules = _configuration.GetSection("SeniorityScoringRules");
                    string seniorityScoringRules       = GetConfigJson(scoringRules);

                    // update the seniority and block assignments for the master record
                    List <HetLocalArea> localAreas = _context.HetEquipment.AsNoTracking()
                                                     .Include(x => x.LocalArea)
                                                     .Where(x => x.EquipmentStatusTypeId == equipmentStatusId &&
                                                            x.DistrictEquipmentTypeId == equipment.DistrictEquipmentTypeId)
                                                     .Select(x => x.LocalArea)
                                                     .Distinct()
                                                     .ToList();

                    foreach (HetLocalArea localArea in localAreas)
                    {
                        EquipmentHelper.RecalculateSeniority(localArea.LocalAreaId, equipment.DistrictEquipmentTypeId, _context, seniorityScoringRules);
                    }
                }
            }
            else
            {
                HetDistrictEquipmentType equipment = new HetDistrictEquipmentType
                {
                    DistrictEquipmentName = item.DistrictEquipmentName,
                    DistrictId            = item.District.DistrictId,
                    EquipmentTypeId       = item.EquipmentType.EquipmentTypeId
                };

                _context.HetDistrictEquipmentType.Add(equipment);
            }

            // save the changes
            _context.SaveChanges();

            // get the id (in the case of new records)
            id = item.DistrictEquipmentTypeId;

            // return the updated equipment type record
            HetDistrictEquipmentType equipmentType = _context.HetDistrictEquipmentType.AsNoTracking()
                                                     .Include(x => x.District)
                                                     .ThenInclude(y => y.Region)
                                                     .Include(x => x.EquipmentType)
                                                     .FirstOrDefault(a => a.DistrictEquipmentTypeId == id);

            return(new ObjectResult(new HetsResponse(equipmentType)));
        }