Exemple #1
0
        public ActionResult CreateOrUpdate(CompanyEmployeeViewModel model)
        {
            ValidateCompanyEmployeeViewModel(model);

            if (ModelState.IsValid)
            {
                if (model.Id.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                    {
                        //判断是否有baseinfo
                        var baseInfoEntity = _employeeBaseInfoService.GetByIdCard(model.IDCard);
                        if (baseInfoEntity == null)
                        {
                            baseInfoEntity             = new EmployeeBaseInfo();
                            baseInfoEntity.Id          = Guid.NewGuid();
                            baseInfoEntity.IDCard      = model.IDCard;
                            baseInfoEntity.Sex         = model.Sex;
                            baseInfoEntity.UserName    = model.UserName;
                            baseInfoEntity.CreatedBy   = _workContext.CurrentMembershipUser.Username;
                            baseInfoEntity.CreatetDate = DateTime.Now;
                            _employeeBaseInfoService.Add(baseInfoEntity);
                            unitOfWork.SaveChanges();
                        }
                        else//覆盖掉原来的数据,奇怪的设计
                        {
                            baseInfoEntity.Sex      = model.Sex;
                            baseInfoEntity.UserName = model.UserName;
                        }
                        var company = _companyService.GetById(model.CompanyId);
                        //职业史
                        var lastWork = baseInfoEntity.WorkHistories.OrderByDescending(x => x.EntryDate).FirstOrDefault();
                        if (lastWork == null || lastWork.EntryDate > model.StartPostDate)
                        {
                            var workHistory = new EmployeeWorkHistory
                            {
                                WorkType         = model.WorkType,
                                Department       = model.Department,
                                EntryDate        = model.StartPostDate,
                                LeaveDate        = model.EndPostDate,
                                AdverseFactor    = model.AdverseFactor,
                                CompanyName      = company.CompanyName,
                                EmployeeBaseInfo = baseInfoEntity,
                                CreatedBy        = WorkContext.CurrentMembershipUser.Username,
                                Deleted          = false
                            };
                            _employeeWorkHistoryService.Add(workHistory);
                        }
                        if (model.EndPostDate != null)
                        {
                            if (model.LeaveDate == null)
                            {
                                model.LeaveDate = model.EndPostDate;
                            }
                            if (lastWork != null && model.StartPostDate == lastWork.EntryDate && company.CompanyName == lastWork.CompanyName && model.Department == lastWork.Department && model.WorkType == lastWork.WorkType && model.AdverseFactor == lastWork.AdverseFactor)
                            {
                                lastWork.LeaveDate = model.EndPostDate;
                            }
                        }
                        if (model.EntryDate == null)
                        {
                            model.EntryDate = model.StartPostDate;
                        }

                        //体检状态根据上岗时间及离岗时间自动生成(上岗时间在三个月内自动默认为上岗前,与数据库关联,未做岗前体检的人员超过三个月自动默认为在岗
                        if (model.EndPostDate != null)
                        {
                            model.HealthStatusId = 12;
                        }
                        else
                        {
                            if ((DateTime.Now - Convert.ToDateTime(model.StartPostDate)).Days > 90)
                            {
                                model.HealthStatusId = 11;
                            }
                            else
                            {
                                model.HealthStatusId = 13;
                            }
                        }
                        var entity = new CompanyEmployee()
                        {
                            Id               = Guid.NewGuid(),
                            AdverseMonthes   = model.AdverseMonthes,
                            TotalWorkMonthes = model.TotalWorkMonthes,
                            EntryDate        = model.EntryDate,
                            LeaveDate        = model.LeaveDate,
                            StartPostDate    = model.StartPostDate,

                            EndPostDate      = model.EndPostDate,
                            AdverseFactor    = model.AdverseFactor,
                            Comment          = model.Comment,
                            ContactPhone     = model.ContactPhone,
                            Department       = model.Department,
                            Email            = model.Email,
                            ProtectType      = model.ProtectType,
                            WorkNumber       = model.WorkNumber,
                            WorkType         = model.WorkType,
                            Company          = company,
                            HealthStatus     = _categoryService.GetById(model.HealthStatusId),
                            Married          = _categoryService.GetById(model.MarriedId),
                            MigrantWorker    = _categoryService.GetById(model.MigrantWorkerId),
                            EmployeeBaseInfo = baseInfoEntity,

                            CreatedBy   = _workContext.CurrentMembershipUser.Username,
                            CreatedDate = DateTime.Now
                        };

                        //7、总工龄企业未维护空白项的根据职业史中的入岗时间自动生成,接害工龄企业未维护的,根据职业史中危害因素为非空白的记录推算时间,如企业维护时间的按维护时间为准。
                        if (model.TotalWorkMonthes == null)
                        {
                            var earliestWorkHisotory = baseInfoEntity.WorkHistories.OrderBy(x => x.EntryDate).FirstOrDefault();
                            if (earliestWorkHisotory != null)
                            {
                                entity.TotalWorkMonthes = Math.Abs(DateTime.Now.Month - entity.StartPostDate.Value.Month) + 12 * (DateTime.Now.Year - entity.StartPostDate.Value.Year);
                            }
                        }

                        if (model.AdverseMonthes == null)
                        {
                            var workHisotries = baseInfoEntity.WorkHistories.OrderBy(x => x.EntryDate);
                            int monthes       = 0;
                            foreach (var item in workHisotries)
                            {
                                if (!string.IsNullOrEmpty(item.AdverseFactor))
                                {
                                    monthes += (Math.Abs(item.LeaveDate.Value.Month - item.EntryDate.Value.Month) + 12 * (item.LeaveDate.Value.Year - item.EntryDate.Value.Year));
                                }
                            }
                            entity.AdverseMonthes = monthes;
                        }
                        _companyEmployeeService.Add(entity);
                        unitOfWork.Commit();

                        SuccessNotification("添加成功");
                        PrepareCompanyEmployeeViewModel(model, entity);
                        return(View(model));
                    }
                }
                else
                {
                    var entity = _companyEmployeeService.GetById(model.Id);
                    if (entity != null)
                    {
                        using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                        {
                            //判断是否有baseinfo,修改状态下应该是有的
                            var baseInfoEntity = _employeeBaseInfoService.GetByIdCard(model.IDCard);
                            if (baseInfoEntity == null)
                            {
                                baseInfoEntity             = new EmployeeBaseInfo();
                                baseInfoEntity.Id          = Guid.NewGuid();
                                baseInfoEntity.IDCard      = model.IDCard;
                                baseInfoEntity.Sex         = model.Sex;
                                baseInfoEntity.UserName    = model.UserName;
                                baseInfoEntity.CreatedBy   = _workContext.CurrentMembershipUser.Username;
                                baseInfoEntity.CreatetDate = DateTime.Now;
                                _employeeBaseInfoService.Add(baseInfoEntity);
                            }
                            else//覆盖掉原来的数据,奇怪的设计
                            {
                                baseInfoEntity.Sex      = model.Sex;
                                baseInfoEntity.UserName = model.UserName;
                            }

                            entity.AdverseMonthes   = model.AdverseMonthes;
                            entity.TotalWorkMonthes = model.TotalWorkMonthes;
                            entity.EntryDate        = model.EntryDate;
                            entity.LeaveDate        = model.LeaveDate;
                            if (model.EntryDate == null)
                            {
                                entity.EntryDate = model.StartPostDate;
                            }

                            entity.StartPostDate = model.StartPostDate;
                            var company = _companyService.GetById(model.CompanyId);

                            var lastWork = baseInfoEntity.WorkHistories.OrderByDescending(x => x.EntryDate).FirstOrDefault();
                            if (lastWork == null || lastWork.EntryDate > model.StartPostDate)
                            {
                                var workHistory = new EmployeeWorkHistory
                                {
                                    WorkType         = model.WorkType,
                                    Department       = model.Department,
                                    EntryDate        = model.StartPostDate,
                                    LeaveDate        = model.EndPostDate,
                                    AdverseFactor    = model.AdverseFactor,
                                    CompanyName      = company.CompanyName,
                                    EmployeeBaseInfo = baseInfoEntity,
                                    CreatedBy        = WorkContext.CurrentMembershipUser.Username,
                                    Deleted          = false
                                };
                                _employeeWorkHistoryService.Add(workHistory);
                            }
                            if (model.EndPostDate != null)
                            {
                                if (model.LeaveDate == null)
                                {
                                    entity.LeaveDate = model.EndPostDate;
                                }


                                if (lastWork != null && model.StartPostDate == lastWork.EntryDate && entity.Company.CompanyName == lastWork.CompanyName)
                                {
                                    lastWork.LeaveDate = model.EndPostDate;
                                }
                            }
                            entity.EndPostDate = model.EndPostDate;

                            entity.AdverseFactor = model.AdverseFactor;
                            entity.Comment       = model.Comment;
                            entity.ContactPhone  = model.ContactPhone;
                            entity.Department    = model.Department;
                            entity.Email         = model.Email;
                            entity.ProtectType   = model.ProtectType;
                            entity.WorkNumber    = model.WorkNumber;
                            entity.WorkType      = model.WorkType;
                            entity.Company       = company;

                            //体检状态根据上岗时间及离岗时间自动生成(上岗时间在三个月内自动默认为上岗前,与数据库关联,未做岗前体检的人员超过三个月自动默认为在岗
                            if (entity.EndPostDate != null)
                            {
                                model.HealthStatusId = 12;
                            }
                            else
                            {
                                if ((DateTime.Now - Convert.ToDateTime(model.StartPostDate)).Days > 90)
                                {
                                    model.HealthStatusId = 11;
                                }
                                else
                                {
                                    model.HealthStatusId = 13;
                                }
                            }

                            entity.HealthStatus     = _categoryService.GetById(model.HealthStatusId);
                            entity.Married          = _categoryService.GetById(model.MarriedId);
                            entity.MigrantWorker    = _categoryService.GetById(model.MigrantWorkerId);
                            entity.EmployeeBaseInfo = baseInfoEntity;

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

                            unitOfWork.Commit();

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