Пример #1
0
        protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_L6MRMS_SEtR_1606 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Base();


            List <ORM_MRS_RUN_MeasurementRun_AccountDownloadCode> downloadCodes = ORM_MRS_RUN_MeasurementRun_AccountDownloadCode.Query.Search(Connection, Transaction, new ORM_MRS_RUN_MeasurementRun_AccountDownloadCode.Query()
            {
                Tenant_RefID         = securityTicket.TenantID,
                IsDeleted            = false,
                MeasurementRun_RefID = Parameter.ReadingSessionId
            });

            foreach (var downloadCode in downloadCodes)
            {
                ORM_CMN_BPT_BusinessParticipant businessParticipant = new ORM_CMN_BPT_BusinessParticipant();
                var bpLoad = businessParticipant.Load(Connection, Transaction, downloadCode.Account_RefID);
                if (bpLoad.Status != FR_Status.Success)
                {
                    continue;
                }

                ORM_CMN_BPT_BusinessParticipant_AccessCode bpAccountCode = ORM_CMN_BPT_BusinessParticipant_AccessCode.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_AccessCode.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    BusinessParticipant_RefID = businessParticipant.CMN_BPT_BusinessParticipantID
                }).SingleOrDefault();
                if (bpAccountCode == null)
                {
                    continue;
                }

                ORM_CMN_PER_PersonInfo personInfo = new ORM_CMN_PER_PersonInfo();
                var piLoad = personInfo.Load(Connection, Transaction, businessParticipant.IfNaturalPerson_CMN_PER_PersonInfo_RefID);
                if (piLoad.Status != FR_Status.Success)
                {
                    continue;
                }

                //sending email

                string subject = Parameter.EmailSubjectTemplate;
                string body    = String.Format
                                 (
                    Parameter.EmailBodyTemplate,
                    personInfo.FirstName,
                    personInfo.LastName,
                    downloadCode.DownloadCode,
                    bpAccountCode.Code
                                 );
                EmailUtils.SendMail(personInfo.PrimaryEmail, subject, body);
            }

            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L6MB_CUMBE_2302 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            var statusUploaded = ORM_MRS_RUN_MeasurementRun_Status.Query.Search(Connection, Transaction, new ORM_MRS_RUN_MeasurementRun_Status.Query()
            {
                GlobalPropertyMatchingID = EnumUtils.GetEnumDescription(MeasurementRunStatus.Uploaded),
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted = false
            }).Single();

            var run = new ORM_MRS_RUN_MeasurementRun()
            {
                Tenant_RefID             = securityTicket.TenantID,
                MRS_RUN_MeasurementRunID = Guid.NewGuid(),
                DateFrom            = DateTime.Now,
                DateThrough         = DateTime.MaxValue,
                CurrentStatus_RefID = statusUploaded.MRS_RUN_MeasurementRun_StatusID
            };

            returnValue.Result = run.MRS_RUN_MeasurementRunID;

            var account = ORM_USR_Account.Query.Search(Connection, Transaction, new ORM_USR_Account.Query()
            {
                Tenant_RefID  = securityTicket.TenantID,
                USR_AccountID = securityTicket.AccountID
            }).Single();


            var runHistory = new ORM_MRS_RUN_MeasurementRun_StatusHistory()
            {
                Tenant_RefID = securityTicket.TenantID,
                MRS_RUN_MeasurementRun_StatusHistoryID = Guid.NewGuid(),
                MeasurementRun_Status_RefID            = statusUploaded.MRS_RUN_MeasurementRun_StatusID,
                TriggeredBy_BusinessParticipant_RefID  = account.BusinessParticipant_RefID,
                MeasurementRun_RefID = run.MRS_RUN_MeasurementRunID,
                Comment = string.Empty
            };

            run.Save(Connection, Transaction);
            runHistory.Save(Connection, Transaction);

            foreach (var row in Parameter.Positions)
            {
                #region reader
                var readerPersonInfo = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, new ORM_CMN_PER_PersonInfo.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    PrimaryEmail = row.ReaderEmail
                }).SingleOrDefault();

                if (readerPersonInfo == null)
                {
                    readerPersonInfo = new ORM_CMN_PER_PersonInfo()
                    {
                        Tenant_RefID         = securityTicket.TenantID,
                        PrimaryEmail         = row.ReaderEmail,
                        CMN_PER_PersonInfoID = Guid.NewGuid()
                    };
                }

                readerPersonInfo.FirstName = row.ReaderFirstName;
                readerPersonInfo.LastName  = row.ReaderLastName;

                readerPersonInfo.Save(Connection, Transaction);

                var readerBP = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
                {
                    Tenant_RefID    = securityTicket.TenantID,
                    IsDeleted       = false,
                    IsNaturalPerson = true,
                    IfNaturalPerson_CMN_PER_PersonInfo_RefID = readerPersonInfo.CMN_PER_PersonInfoID
                }).SingleOrDefault();

                if (readerBP == null)
                {
                    readerBP = new ORM_CMN_BPT_BusinessParticipant()
                    {
                        CMN_BPT_BusinessParticipantID = Guid.NewGuid(),
                        Tenant_RefID    = securityTicket.TenantID,
                        IsNaturalPerson = true,
                        IfNaturalPerson_CMN_PER_PersonInfo_RefID = readerPersonInfo.CMN_PER_PersonInfoID,
                    };

                    readerBP.Save(Connection, Transaction);
                }

                var bpCode = ORM_CMN_BPT_BusinessParticipant_AccessCode.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant_AccessCode.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    BusinessParticipant_RefID = readerBP.CMN_BPT_BusinessParticipantID,
                    IsValid            = true,
                    IsDeviceAccessCode = true
                }).SingleOrDefault();

                if (bpCode == null)
                {
                    bpCode = new ORM_CMN_BPT_BusinessParticipant_AccessCode()
                    {
                        CMN_BPT_BusinessParticipant_AccessCodeID = Guid.NewGuid(),
                        Tenant_RefID = securityTicket.TenantID,
                        BusinessParticipant_RefID = readerBP.CMN_BPT_BusinessParticipantID,
                        IsValid            = true,
                        IsDeviceAccessCode = true,
                        ValidFrom          = DateTime.Now,
                        ValidThrough       = DateTime.MaxValue,
                        Code = StringUtils.CodeGen(8).ToLower()
                    };
                    bpCode.Save(Connection, Transaction);
                }

                #endregion

                #region meter
                var meter = ORM_MRS_MPT_Meter.Query.Search(Connection, Transaction, new ORM_MRS_MPT_Meter.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    SerialNumber = row.MeterSerialNumber
                }).SingleOrDefault();

                if (meter == null)
                {
                    meter = new ORM_MRS_MPT_Meter()
                    {
                        MRS_MPT_MeterID = Guid.NewGuid(),
                        SerialNumber    = row.MeterSerialNumber,
                        Tenant_RefID    = securityTicket.TenantID,
                    };
                    meter.Save(Connection, Transaction);
                }

                var meterBinding = ORM_MRS_MPT_MeasuringPoint_MeterBinding.Query.Search(Connection, Transaction, new ORM_MRS_MPT_MeasuringPoint_MeterBinding.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    Meter_RefID  = meter.MRS_MPT_MeterID
                }).SingleOrDefault();

                if (meterBinding == null)
                {
                    meterBinding = new ORM_MRS_MPT_MeasuringPoint_MeterBinding()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        ActiveFrom   = DateTime.Now,
                        Meter_RefID  = meter.MRS_MPT_MeterID,
                        MRS_MPT_MeasuringPoint_MeterBindingID = Guid.NewGuid()
                    };
                    meterBinding.Save(Connection, Transaction);
                }

                var measurentPoint = ORM_MRS_MPT_MeasuringPoint.Query.Search(Connection, Transaction, new ORM_MRS_MPT_MeasuringPoint.Query()
                {
                    Tenant_RefID             = securityTicket.TenantID,
                    IsDeleted                = false,
                    MRS_MPT_MeasuringPointID = meterBinding.MeasuringPoint_RefID
                }).SingleOrDefault();

                if (measurentPoint == null)
                {
                    measurentPoint = new ORM_MRS_MPT_MeasuringPoint()
                    {
                        Tenant_RefID             = securityTicket.TenantID,
                        MRS_MPT_MeasuringPointID = Guid.NewGuid()
                    };

                    meterBinding.MeasuringPoint_RefID = measurentPoint.MRS_MPT_MeasuringPointID;
                    meterBinding.Save(Connection, Transaction);

                    measurentPoint.Save(Connection, Transaction);
                }

                #endregion

                #region customer
                var customerOwnership = ORM_MRS_MPT_CustomerOwnership.Query.Search(Connection, Transaction, new ORM_MRS_MPT_CustomerOwnership.Query()
                {
                    Tenant_RefID   = securityTicket.TenantID,
                    IsDeleted      = false,
                    ContractNumber = row.ContractNumber
                }).SingleOrDefault();

                if (customerOwnership == null)
                {
                    var customerPersonInfo = new ORM_CMN_PER_PersonInfo()
                    {
                        Tenant_RefID         = securityTicket.TenantID,
                        FirstName            = row.ContractOwnerFirstName,
                        LastName             = row.ContractOwnerLastName,
                        CMN_PER_PersonInfoID = Guid.NewGuid()
                    };
                    customerPersonInfo.Save(Connection, Transaction);

                    var customerBP = new ORM_CMN_BPT_BusinessParticipant()
                    {
                        CMN_BPT_BusinessParticipantID = Guid.NewGuid(),
                        Tenant_RefID    = securityTicket.TenantID,
                        IsNaturalPerson = true,
                        IfNaturalPerson_CMN_PER_PersonInfo_RefID = customerPersonInfo.CMN_PER_PersonInfoID,
                    };
                    customerBP.Save(Connection, Transaction);

                    var customer = new ORM_CMN_BPT_CTM_Customer()
                    {
                        Tenant_RefID                  = securityTicket.TenantID,
                        CMN_BPT_CTM_CustomerID        = Guid.NewGuid(),
                        Ext_BusinessParticipant_RefID = customerBP.CMN_BPT_BusinessParticipantID
                    };
                    customer.Save(Connection, Transaction);

                    customerOwnership = new ORM_MRS_MPT_CustomerOwnership()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        MRS_MPT_CustomerOwnershipID = Guid.NewGuid(),
                        ContractNumber       = row.ContractNumber,
                        ValidFrom            = DateTime.Now,
                        Customer_RefID       = customer.CMN_BPT_CTM_CustomerID,
                        MeasuringPoint_RefID = measurentPoint.MRS_MPT_MeasuringPointID
                    };
                    customerOwnership.Save(Connection, Transaction);
                }
                else
                {
                    var customer = ORM_CMN_BPT_CTM_Customer.Query.Search(Connection, Transaction, new ORM_CMN_BPT_CTM_Customer.Query()
                    {
                        Tenant_RefID           = securityTicket.TenantID,
                        IsDeleted              = false,
                        CMN_BPT_CTM_CustomerID = customerOwnership.Customer_RefID
                    }).Single();

                    var customerBP = ORM_CMN_BPT_BusinessParticipant.Query.Search(Connection, Transaction, new ORM_CMN_BPT_BusinessParticipant.Query()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false,
                        CMN_BPT_BusinessParticipantID = customer.Ext_BusinessParticipant_RefID
                    }).Single();

                    var customerPI = ORM_CMN_PER_PersonInfo.Query.Search(Connection, Transaction, new ORM_CMN_PER_PersonInfo.Query()
                    {
                        Tenant_RefID         = securityTicket.TenantID,
                        IsDeleted            = false,
                        CMN_PER_PersonInfoID = customerBP.IfNaturalPerson_CMN_PER_PersonInfo_RefID
                    }).Single();

                    customerPI.FirstName = row.ContractOwnerFirstName;
                    customerPI.LastName  = row.ContractOwnerLastName;
                    customerPI.Save(Connection, Transaction);
                }

                customerOwnership.MeasuringPoint_RefID = measurentPoint.MRS_MPT_MeasuringPointID;
                customerOwnership.Save(Connection, Transaction);

                #region address
                var address = ORM_CMN_Address.Query.Search(Connection, Transaction, new ORM_CMN_Address.Query()
                {
                    Tenant_RefID    = securityTicket.TenantID,
                    IsDeleted       = false,
                    City_Name       = row.City,
                    Street_Name     = row.AddressName,
                    Street_Number   = row.AddressNumber,
                    City_PostalCode = row.ZipCode
                }).SingleOrDefault();

                if (address == null)
                {
                    address = new ORM_CMN_Address()
                    {
                        Tenant_RefID    = securityTicket.TenantID,
                        City_Name       = row.City,
                        Street_Name     = row.AddressName,
                        Street_Number   = row.AddressNumber,
                        City_PostalCode = row.ZipCode,
                        CMN_AddressID   = Guid.NewGuid()
                    };
                    address.Save(Connection, Transaction);
                }

                measurentPoint.CurrentAddress_RefID = address.CMN_AddressID;
                measurentPoint.Save(Connection, Transaction);

                #endregion

                #endregion

                #region route

                var route = ORM_MRS_RUT_Route.Query.Search(Connection, Transaction, new ORM_MRS_RUT_Route.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    DisplayName  = row.RouteName
                }).SingleOrDefault();

                if (route == null)
                {
                    route = new ORM_MRS_RUT_Route()
                    {
                        Tenant_RefID    = securityTicket.TenantID,
                        MRS_RUT_RouteID = Guid.NewGuid(),
                        DisplayName     = row.RouteName,
                        //Default_RouteReaderAccount_RefID = readerBP.CMN_BPT_BusinessParticipantID
                    };
                    route.Save(Connection, Transaction);
                }

                var routeMeasuringPoint = ORM_MRS_RUT_Route_MeasuringPoint.Query.Search(Connection, Transaction, new ORM_MRS_RUT_Route_MeasuringPoint.Query()
                {
                    Tenant_RefID         = securityTicket.TenantID,
                    IsDeleted            = false,
                    Route_RefID          = route.MRS_RUT_RouteID,
                    MeasuringPoint_RefID = measurentPoint.MRS_MPT_MeasuringPointID
                }).SingleOrDefault();

                if (routeMeasuringPoint == null)
                {
                    routeMeasuringPoint = new ORM_MRS_RUT_Route_MeasuringPoint()
                    {
                        Tenant_RefID = securityTicket.TenantID,
                        MRS_RUT_Route_MeasuringPointID = Guid.NewGuid(),
                        Route_RefID          = route.MRS_RUT_RouteID,
                        MeasuringPoint_RefID = measurentPoint.MRS_MPT_MeasuringPointID
                    };
                }

                routeMeasuringPoint.OrderSequence = row.SequenceInRoute;
                routeMeasuringPoint.Save(Connection, Transaction);


                #endregion

                #region measurement

                var measurement = new ORM_MRS_RUN_Measurement()
                {
                    Tenant_RefID          = securityTicket.TenantID,
                    MRS_RUN_MeasurementID = Guid.NewGuid(),
                    MeasurementRun_RefID  = run.MRS_RUN_MeasurementRunID,
                    MeasuringPoint_RefID  = routeMeasuringPoint.MRS_RUT_Route_MeasuringPointID
                };
                measurement.Save(Connection, Transaction);


                var run2route = ORM_MRS_RUN_MeasurementRun_Route.Query.Search(Connection, Transaction, new ORM_MRS_RUN_MeasurementRun_Route.Query()
                {
                    Tenant_RefID         = securityTicket.TenantID,
                    IsDeleted            = false,
                    Route_RefID          = route.MRS_RUT_RouteID,
                    MeasurementRun_RefID = run.MRS_RUN_MeasurementRunID
                }).SingleOrDefault();

                if (run2route == null)
                {
                    run2route = new ORM_MRS_RUN_MeasurementRun_Route()
                    {
                        MRS_RUN_MeasurementRun_RouteID = Guid.NewGuid(),
                        Tenant_RefID         = securityTicket.TenantID,
                        Route_RefID          = route.MRS_RUT_RouteID,
                        MeasurementRun_RefID = run.MRS_RUN_MeasurementRunID
                    }
                }
                ;

                run2route.BoundTo_Account_RefID = readerBP.CMN_BPT_BusinessParticipantID;
                run2route.Save(Connection, Transaction);


                #endregion
            }
            return(returnValue);

            #endregion UserCode
        }