private void ValidateHealthResultViewModelForSearch(HealthResultViewModel model)
 {
     if (string.IsNullOrEmpty(model.IDCard) || model.IDCard.Length != 18)
     {
         ModelState.AddModelError("IDCard", "无效的身份证号");
     }
 }
        private void PrepareHealthResultViewModel(HealthResultViewModel model, HealthResult entity)
        {
            var companies = _companyService.GetAll();

            foreach (var item in companies)
            {
                model.AvailableCompanies.Add(new SelectListItem
                {
                    Text     = item.CompanyName,
                    Value    = item.Id.ToString(),
                    Selected = entity != null &&
                               entity.CompanyEmployee != null &&
                               entity.CompanyEmployee.Company != null &&
                               item.Id == entity.CompanyEmployee.Company.Id
                });
            }
        }
        public ActionResult CreateOrUpdate(Guid?id = null)
        {
            if (id != null)
            {
                var entity = _healthResultService.GetById(id.Value);
                if (entity != null)
                {
                    var model = new HealthResultViewModel()
                    {
                        Id                = entity.Id,
                        UserName          = entity.CompanyEmployee.EmployeeBaseInfo.UserName,
                        IDCard            = entity.CompanyEmployee.EmployeeBaseInfo.IDCard,
                        Sex               = entity.CompanyEmployee.EmployeeBaseInfo.Sex,
                        AdverseMonthes    = entity.CompanyEmployee.AdverseMonthes,
                        AdverseFactor     = entity.CompanyEmployee.AdverseFactor,
                        CompanyId         = entity.CompanyEmployee.Company.Id,
                        CompanyEmployeeId = entity.CompanyEmployee.Id,

                        HealthDate      = entity.HealthDate,
                        ReportDate      = DateTime.Parse(entity.ReportDate),
                        HealthPerson    = entity.HealthPerson,
                        HealthByCompany = entity.HealthByCompany,

                        MainPositiveResult = entity.MainPositiveResult,
                        Result             = entity.Result,
                        HealthCode         = entity.HealthCode,
                        ImageCode          = entity.ImageCode,
                        ReportCode         = entity.ReportCode,
                    };
                    PrepareHealthResultViewModel(model, entity);
                    return(View(model));
                }
                else
                {
                    ErrorNotification(new Exception("编辑失败,未找到Id为" + id.ToString() + "的体检信息"));
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                var model = new HealthResultViewModel();
                PrepareHealthResultViewModel(model, null);
                return(View(model));
            }
        }
 private void ValidateHealthResultViewModelForCreateOrUpdate(HealthResultViewModel model)
 {
 }
        public ActionResult CreateOrUpdate(HealthResultViewModel model, string command)
        {
            if (command == "searchEmployee")
            {
                ValidateHealthResultViewModelForSearch(model);

                if (ModelState.IsValid)
                {
                    var entityEmplyee = _companyEmployeeService.GetEmployee(model.IDCard, model.CompanyId);
                    if (entityEmplyee != null)
                    {
                        model.UserName          = entityEmplyee.EmployeeBaseInfo.UserName;
                        model.IDCard            = entityEmplyee.EmployeeBaseInfo.IDCard;
                        model.Sex               = entityEmplyee.EmployeeBaseInfo.Sex;
                        model.AdverseMonthes    = entityEmplyee.AdverseMonthes;
                        model.AdverseFactor     = entityEmplyee.AdverseFactor;
                        model.CompanyId         = entityEmplyee.Company.Id;
                        model.CompanyEmployeeId = entityEmplyee.Id;
                    }
                    else
                    {
                        ErrorNotification(new Exception("未找到员工"));
                        model = new HealthResultViewModel();
                    }
                }
                else
                {
                    ErrorNotification(new Exception("输入信息有误"));
                }
            }
            else if (command == "createOrUpdate")
            {
                ValidateHealthResultViewModelForCreateOrUpdate(model);

                if (ModelState.IsValid)
                {
                    if (model.Id.ToString() == "00000000-0000-0000-0000-000000000000")
                    {
                        using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                        {
                            var entity = new HealthResult()
                            {
                                Id = Guid.NewGuid(),
                                MainPositiveResult = model.MainPositiveResult,
                                Result             = model.Result,
                                HealthCode         = model.HealthCode,
                                ImageCode          = model.ImageCode,
                                ReportCode         = model.ReportCode,
                                HealthDate         = model.HealthDate,
                                ReportDate         = model.ReportDate.HasValue ? model.ReportDate.Value.ToString("yyyy-MM-dd") : "",
                                HealthPerson       = model.HealthPerson,
                                CompanyEmployee    = _companyEmployeeService.GetById(model.CompanyEmployeeId),
                                HealthByCompany    = model.HealthByCompany,

                                CreatedBy   = _workContext.CurrentMembershipUser.Username,
                                CreatedDate = DateTime.Now
                            };
                            _healthResultService.Add(entity);
                            unitOfWork.Commit();

                            SuccessNotification("添加成功");
                            model = new HealthResultViewModel();
                            PrepareHealthResultViewModel(model, entity);
                            return(View(model));
                        }
                    }
                    else
                    {
                        var entity = _healthResultService.GetById(model.Id);
                        if (entity != null)
                        {
                            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                            {
                                entity.MainPositiveResult = model.MainPositiveResult;
                                entity.Result             = model.Result;
                                entity.HealthCode         = model.HealthCode;
                                entity.ImageCode          = model.ImageCode;
                                entity.ReportCode         = model.ReportCode;
                                entity.HealthDate         = model.HealthDate;
                                entity.ReportDate         = model.ReportDate.HasValue ? model.ReportDate.Value.ToString("yyyy-MM-dd") : "";
                                entity.HealthPerson       = model.HealthPerson;
                                entity.CompanyEmployee    = _companyEmployeeService.GetById(model.CompanyEmployeeId);
                                entity.HealthByCompany    = model.HealthByCompany;

                                entity.UpdatedBy   = _workContext.CurrentMembershipUser.Username;
                                entity.UpdatedDate = DateTime.Now;

                                unitOfWork.Commit();

                                SuccessNotification("编辑成功");
                                return(RedirectToAction("Index"));
                            }
                        }
                        else
                        {
                            ErrorNotification(new Exception("编辑失败,未找到Id为" + model.Id.ToString() + "的体检信息"));
                            return(RedirectToAction("Index"));
                        }
                    }
                }
                else
                {
                    ErrorNotification(new Exception("编辑失败,输入信息有误"));
                    PrepareHealthResultViewModel(model, null);
                    return(View(model));
                }
            }

            PrepareHealthResultViewModel(model, null);
            return(View(model));
        }