public void Create(POFECDTO POFECDTO)
        {
            POFEC obj = _mapper.Map <POFEC>(POFECDTO);

            obj.CreatedDate = DateTime.Now;
            _unitOfWork.POFEC.Create(obj);
            _unitOfWork.SaveChanges();
        }
        public void Update(POFECDTO POFECDTO)
        {
            int   ID       = POFECDTO.ID;
            POFEC pipeline = _unitOfWork.POFEC.FindById(ID);

            if (pipeline != null)
            {
                int yearInService = (from p in _unitOfWork.PipeMaster.FindAll().Where(p => p.EquipmentNo == pipeline.EquipmentNo)
                                     select p.YearInService
                                     ).FirstOrDefault();
                decimal operatingTemperature = (from p in _unitOfWork.PipeMaster.FindAll().Where(p => p.EquipmentNo == pipeline.EquipmentNo)
                                                select p.OperatingTemperature.Value
                                                ).FirstOrDefault();

                string lastMeasuredThick = (from p in _unitOfWork.PipeMaster.FindAll().Where(p => p.EquipmentNo == pipeline.EquipmentNo)
                                            select p.UseLastMeasuredThick_ULMT
                                            ).FirstOrDefault();

                int?lastMeasuredYear = (from p in _unitOfWork.PipeMaster.FindAll().Where(p => p.EquipmentNo == pipeline.EquipmentNo)
                                        select p.LastMeasuredYear_LMY
                                        ).FirstOrDefault();

                if (!pipeline.AnalysisYear.HasValue)
                {
                    pipeline.AnalysisYear = DateTime.Now.Year;
                }
                pipeline.TheoriticalCR               = POFECDTO.TheoriticalCR;
                pipeline.EffectiveCR                 = POFECDTO.EffectiveCR;
                pipeline.EffectiveAge                = POFECDTO.EffectiveAge;
                pipeline.MeasuredLCR                 = POFECDTO.MeasuredLCR;
                pipeline.MeasuredSCR                 = POFECDTO.MeasuredSCR;
                pipeline.UseMeasuredLCR              = POFECDTO.UseMeasuredLCR;
                pipeline.UseMeasuredSCR              = POFECDTO.UseMeasuredSCR;
                pipeline.VeryHigh                    = POFECDTO.VeryHigh;
                pipeline.High                        = POFECDTO.High;
                pipeline.Medium                      = POFECDTO.Medium;
                pipeline.Low                         = POFECDTO.Low;
                pipeline.Found                       = POFECDTO.Found;
                pipeline.DamageFactor                = POFECDTO.DamageFactor;
                pipeline.POF                         = POFECDTO.POF;
                pipeline.LastMeasuredYear            = POFECDTO.LastMeasuredYear;
                pipeline.SoilInterfaceCondensation   = POFECDTO.SoilInterfaceCondensation;
                pipeline.PipeDirectBeamComplexDesign = POFECDTO.PipeDirectBeamComplexDesign;
                pipeline.RepaintedYear               = POFECDTO.RepaintedYear;
                pipeline.Driver                      = POFECDTO.Driver;
                pipeline.CoatingQuality              = POFECDTO.CoatingQuality;
                pipeline.CoatingAge                  = POFECDTO.CoatingAge;
                pipeline.CoatAdjustment              = POFECDTO.CoatAdjustment;
                pipeline.CorrosionType               = POFECDTO.CorrosionType;
                pipeline.ExternalProcess             = POFECDTO.ExternalProcess;
                pipeline.LineAge                     = POFECDTO.LineAge;

                pipeline.ModifiedBy   = POFECDTO.ModifiedBy;
                pipeline.ModifiedDate = DateTime.Now;

                // Repainted Year
                // TODO: Add Repainted column
                // update from Pipe report if repainted=Y then Report Year else YIS from pipe master
                if (!pipeline.RepaintedYear.HasValue)
                {
                    pipeline.RepaintedYear = yearInService;
                }

                // Coating Age
                if (pipeline.RepaintedYear.HasValue && pipeline.RepaintedYear.Value > 0)
                {
                    pipeline.CoatingAge = pipeline.AnalysisYear - pipeline.RepaintedYear;
                }
                else
                {
                    pipeline.CoatingAge = pipeline.AnalysisYear - yearInService;
                }

                // Line External Age
                if (lastMeasuredThick == "Y" && lastMeasuredYear.HasValue)
                {
                    pipeline.LineAge = pipeline.AnalysisYear - lastMeasuredYear.Value;
                }
                else
                {
                    pipeline.LineAge = pipeline.AnalysisYear - yearInService;
                }

                if (pipeline.LineAge >= pipeline.CoatingAge && pipeline.CoatingQuality == "L")
                {
                    pipeline.CoatAdjustment = 0;
                }
                else if (pipeline.LineAge >= pipeline.CoatingAge && pipeline.CoatingQuality == "M")
                {
                    pipeline.CoatAdjustment = Math.Min(5, pipeline.CoatingAge.Value);
                }
                else if (pipeline.LineAge >= pipeline.CoatingAge && pipeline.CoatingQuality == "H")
                {
                    pipeline.CoatAdjustment = Math.Min(15, pipeline.CoatingAge.Value);
                }
                else if (pipeline.LineAge < pipeline.CoatingAge && pipeline.CoatingQuality == "L")
                {
                    pipeline.CoatAdjustment = 0;
                }
                else if (pipeline.LineAge < pipeline.CoatingAge && pipeline.CoatingQuality == "M")
                {
                    pipeline.CoatAdjustment = Math.Min(5, (pipeline.CoatingAge.Value - pipeline.LineAge.Value));
                }
                else if (pipeline.LineAge < pipeline.CoatingAge && pipeline.CoatingQuality == "H")
                {
                    pipeline.CoatAdjustment = Math.Min(15, (pipeline.CoatingAge.Value - pipeline.LineAge.Value));
                }

                pipeline.EffectiveAge = pipeline.LineAge - pipeline.CoatAdjustment;

                // Theoretical CR
                if (pipeline.CorrosionType == "EC")
                {
                    pipeline.TheoriticalCR = (from p in _unitOfWork.ExternalCorrosionRate.FindAll().Where(p => p.OperatingTemperatureInDegC >= operatingTemperature && p.Driver == pipeline.Driver)
                                              select p.CorrosionRate
                                              ).FirstOrDefault();
                }
                else if (pipeline.CorrosionType == "CUI")
                {
                    pipeline.TheoriticalCR = (from p in _unitOfWork.CUICorrosionRate.FindAll().Where(p => p.OperatingTemperatureInDegC >= operatingTemperature && p.Driver == pipeline.Driver)
                                              select p.CorrosionRate
                                              ).FirstOrDefault();
                }

                // EffectiveCR
                if (pipeline.UseMeasuredLCR == "Y")
                {
                    pipeline.EffectiveCR = pipeline.MeasuredLCR;
                }
                else if (pipeline.UseMeasuredSCR == "Y")
                {
                    pipeline.EffectiveCR = pipeline.MeasuredSCR;
                }
                else if (pipeline.SoilInterfaceCondensation == "Y")
                {
                    pipeline.EffectiveCR = 2 * pipeline.TheoriticalCR;
                }
                else if (pipeline.PipeDirectBeamComplexDesign == "Y")
                {
                    pipeline.EffectiveCR = 2 * pipeline.TheoriticalCR;
                }
                else
                {
                    pipeline.EffectiveCR = pipeline.TheoriticalCR;
                }

                // Suceptability
                if (pipeline.CorrosionType == "EC")
                {
                    pipeline.Suceptability = (from p in _unitOfWork.ExternalSuceptablity.FindAll().Where(p => p.OperatingTemperatureInDegC_From < operatingTemperature && p.OperatingTemperatureInDegC_To >= operatingTemperature && p.Driver == pipeline.Driver)
                                              select p.Suceptability
                                              ).FirstOrDefault();
                }
                else if (pipeline.CorrosionType == "CUI")
                {
                    pipeline.Suceptability = (from p in _unitOfWork.CUISuceptablity.FindAll().Where(p => p.OperatingTemperatureInDegC_From < operatingTemperature && p.OperatingTemperatureInDegC_To >= operatingTemperature && p.Driver == pipeline.Driver)
                                              select p.Suceptability
                                              ).FirstOrDefault();
                }


                // Adjusted Suceptability
                if (pipeline.CorrosionType == "EC")
                {
                    pipeline.AdjustedSuceptability = pipeline.Suceptability;
                }
                else if (pipeline.CorrosionType == "CUI")
                {
                    if (pipeline.InsulationCondition == "Y" || pipeline.PipeDirectBeamComplexDesign == "Y")
                    {
                        pipeline.AdjustedSuceptability = pipeline.Suceptability;
                    }
                    else
                    {
                        pipeline.AdjustedSuceptability = pipeline.Suceptability;
                    }
                }
            }
            _unitOfWork.POFEC.Update(pipeline);
            _unitOfWork.SaveChanges();

            Update(pipeline.EquipmentNo, pipeline.AnalysisYear.Value);
        }