コード例 #1
0
        public ActionResult CreateFuneral(List <KeyValue> deceased, List <KeyValue> informant, List <KeyValue> nextOfKin,
                                          List <KeyValue> doctor, List <KeyValue> homeAffairsOfficer, List <KeyValue> funeral)
        {
            if (funeral == null)
            {
                funeral = new List <KeyValue>();
            }

            if (deceased != null && deceased.Count > 0)
            {
                var deceasedAddress = KeyValueService.GetAttributeValue(deceased, "FullAddress");
                if (deceasedAddress != null)
                {
                    var address = new List <KeyValue>();
                    KeyValueService.AddAttribute(address, "Id", Guid.NewGuid().ToString());
                    KeyValueService.AddAttribute(address, "FullAddress", deceasedAddress);
                    var addressReturnObject = AddressService.InsertAddress(address);
                    if (addressReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(deceased, "AddressId", addressReturnObject.Id);
                    }
                }

                var deceasedPersonId = Guid.NewGuid().ToString();
                KeyValueService.AddAttribute(deceased, "Id", deceasedPersonId);
                GenericModelService.AddAuditAttributeForCreateEvent(deceased, GetCurrentUserId());
                var deceasedPersonReturnObject = PersonService.InsertPerson(deceased);
                if (deceasedPersonReturnObject.State == "success")
                {
                    KeyValueService.AddAttribute(deceased, "PersonId", deceasedPersonId);
                    var deceasedReturnObject = DeceasedService.InsertDeceased(deceased);
                    if (deceasedReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(funeral, "DeceasedId", deceasedReturnObject.Id);
                    }
                }
            }

            if (informant != null && informant.Count > 0)
            {
                var informantPersonId = Guid.NewGuid().ToString();
                KeyValueService.AddAttribute(informant, "Id", informantPersonId);
                GenericModelService.AddAuditAttributeForCreateEvent(informant, GetCurrentUserId());
                var informantPersonReturnObject = PersonService.InsertPerson(informant);
                if (informantPersonReturnObject.State == "success")
                {
                    KeyValueService.AddAttribute(informant, "PersonId", informantPersonId);
                    var informantReturnObject = InformantService.InsertInformant(informant);
                    if (informantReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(funeral, "InformantId", informantReturnObject.Id);
                    }
                }
            }

            if (nextOfKin != null && nextOfKin.Count > 0)
            {
                var nextOfKinPersonId = Guid.NewGuid().ToString();
                KeyValueService.AddAttribute(nextOfKin, "Id", nextOfKinPersonId);
                GenericModelService.AddAuditAttributeForCreateEvent(nextOfKin, GetCurrentUserId());
                var nextOfKinPersonReturnObject = PersonService.InsertPerson(nextOfKin);
                if (nextOfKinPersonReturnObject.State == "success")
                {
                    KeyValueService.AddAttribute(nextOfKin, "PersonId", nextOfKinPersonId);
                    var nextOfKinReturnObject = NextOfKinService.InsertNextOfKin(nextOfKin);
                    if (nextOfKinReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(funeral, "NextOfKinId", nextOfKinReturnObject.Id);
                    }
                }
            }

            if (doctor != null && doctor.Count > 0)
            {
                if (KeyValueService.AttributeContainsValue(doctor, "Id"))
                {
                    var doctorPersonId = KeyValueService.GetAttributeValue(doctor, "Id");
                    KeyValueService.AddAttribute(funeral, "DoctorId", doctorPersonId);
                }
                else
                {
                    var doctorPersonId = Guid.NewGuid().ToString();
                    KeyValueService.AddAttribute(doctor, "Id", doctorPersonId);
                    GenericModelService.AddAuditAttributeForCreateEvent(doctor, GetCurrentUserId());
                    var nextOfKinPersonReturnObject = PersonService.InsertPerson(doctor);
                    if (nextOfKinPersonReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(doctor, "PersonId", doctorPersonId);
                        var doctorReturnObject = DoctorService.InsertDoctor(doctor);
                        if (doctorReturnObject.State == "success")
                        {
                            KeyValueService.AddAttribute(funeral, "DoctorId", doctorReturnObject.Id);
                        }
                    }
                }
            }

            if (homeAffairsOfficer != null && homeAffairsOfficer.Count > 0)
            {
                if (KeyValueService.AttributeContainsValue(homeAffairsOfficer, "Id"))
                {
                    var homeAffairsOfficerPersonId = KeyValueService.GetAttributeValue(homeAffairsOfficer, "Id");
                    KeyValueService.AddAttribute(funeral, "HomeAffairsOfficerId", homeAffairsOfficerPersonId);
                }
                else
                {
                    var homeAffairsOfficerPersonId = Guid.NewGuid().ToString();
                    KeyValueService.AddAttribute(homeAffairsOfficer, "Id", homeAffairsOfficerPersonId);
                    GenericModelService.AddAuditAttributeForCreateEvent(homeAffairsOfficer, GetCurrentUserId());
                    var homeAffairsOfficerPersonReturnObject = PersonService.InsertPerson(homeAffairsOfficer);
                    if (homeAffairsOfficerPersonReturnObject.State == "success")
                    {
                        KeyValueService.AddAttribute(homeAffairsOfficer, "PersonId",
                                                     homeAffairsOfficerPersonReturnObject.Id);
                        var homeAffairsOfficerReturnObject =
                            HomeAffairsOfficerService.InsertHomeAffairesOfficer(homeAffairsOfficer);
                        if (homeAffairsOfficerReturnObject.State == "success")
                        {
                            KeyValueService.AddAttribute(funeral, "HomeAffairsOfficerId",
                                                         homeAffairsOfficerReturnObject.Id);
                        }
                    }
                }
            }

            if (funeral.Count <= 0)
            {
                return(Json(new { state = "error", message = "" }, JsonRequestBehavior.AllowGet));
            }
            var funeralId = Guid.NewGuid().ToString();

            KeyValueService.AddAttribute(funeral, "Id", funeralId);

            #region Get Vehicle Number

            var counterConfiguration   = NumberConfigurationService.GetNextNumber("funeral");
            var counterConfigurationId = (Guid)counterConfiguration.FirstOrDefault(x => x.Key == "Id").Value;
            var entityName             = (string)counterConfiguration.FirstOrDefault(x => x.Key == "EntityName").Value;
            var nextNumber             = (int)counterConfiguration.FirstOrDefault(x => x.Key == "NextNumber").Value;
            var prefix        = (string)counterConfiguration.FirstOrDefault(x => x.Key == "Prefix").Value;
            var incrementBy   = (int)counterConfiguration.FirstOrDefault(x => x.Key == "IncrementBy").Value;
            var length        = (int)counterConfiguration.FirstOrDefault(x => x.Key == "Length").Value;
            var number        = length == 0 ? "" : nextNumber.ToString("D" + length);
            var funeralNumber = prefix + number;

            #endregion

            KeyValueService.AddAttribute(funeral, "FuneralNumber", funeralNumber);

            GenericModelService.AddAuditAttributeForCreateEvent(funeral, GetCurrentUserId());
            var funeralReturnObject = FuneralService.InsertFuneral(funeral);

            if (funeralReturnObject.State != "success")
            {
                return(Json(new { state = "error", message = "" }, JsonRequestBehavior.AllowGet));
            }
            nextNumber = nextNumber + incrementBy;
            NumberConfigurationService.SetNextNumber(counterConfigurationId.ToString(), entityName, nextNumber);
            return(Json(new { state = "success", funeralId = funeralReturnObject.Id }, JsonRequestBehavior.AllowGet));
        }