Ejemplo n.º 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));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析员工信息
        /// </summary>
        private void ParseToCompanyEmployee(List <string> values, ref CompanyEmployee entity, List <ImportErrorColModel> errors)
        {
            //检查数据项
            Category Married = _categoryRepository.GetByName(string.IsNullOrEmpty(values[3]) ? "未知" : values[3], "MarriedType");

            if (Married == null)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 3,
                    Message  = ERROR_MARRIED,
                });
            }
            //var Married = string.IsNullOrEmpty(values[3]) ? string.Empty : values[3];
            var EntryDate_Year = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[4]) ? "0" : values[4], out EntryDate_Year))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 4,
                    Message  = ERROR_YEAR,
                });
            }
            var EntryDate_Month = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[5]) ? "0" : values[5], out EntryDate_Month))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 5,
                    Message  = ERROR_MONTH,
                });
            }
            if (!string.IsNullOrEmpty(values[5]) && (EntryDate_Month < 1 || EntryDate_Month > 12))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 5,
                    Message  = ERROR_MONTH,
                });
            }
            var LeaveDate_Year = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[6]) ? "0" : values[6], out LeaveDate_Year))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 6,
                    Message  = ERROR_YEAR,
                });
            }
            var LeaveDate_Month = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[7]) ? "0" : values[7], out LeaveDate_Month))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 7,
                    Message  = ERROR_MONTH,
                });
            }
            if (!string.IsNullOrEmpty(values[7]) && (LeaveDate_Month < 1 || LeaveDate_Month > 12))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 7,
                    Message  = ERROR_MONTH,
                });
            }

            var StartPostDate_Year = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[8]) ? "0" : values[8], out StartPostDate_Year))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 8,
                    Message  = ERROR_YEAR,
                });
            }
            if (StartPostDate_Year == 0)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 8,
                    Message  = ERROR_STARTPOST_EMPTY,
                });
            }
            var StartPostDate_Month = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[9]) ? "0" : values[9], out StartPostDate_Month))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 9,
                    Message  = ERROR_MONTH,
                });
            }
            if (StartPostDate_Month == 0)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 9,
                    Message  = ERROR_STARTPOST_EMPTY,
                });
            }
            if (!string.IsNullOrEmpty(values[9]) && (StartPostDate_Month < 1 || StartPostDate_Month > 12))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 9,
                    Message  = ERROR_MONTH,
                });
            }
            var EndPostDate_Year = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[10]) ? "0" : values[10], out EndPostDate_Year))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 10,
                    Message  = ERROR_YEAR,
                });
            }
            var EndPostDate_Month = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[11]) ? "0" : values[11], out EndPostDate_Month))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 11,
                    Message  = ERROR_MONTH,
                });
            }
            if (!string.IsNullOrEmpty(values[11]) && (EndPostDate_Month < 1 || EndPostDate_Month > 12))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 11,
                    Message  = ERROR_MONTH,
                });
            }
            var AdverseFactor    = string.IsNullOrEmpty(values[12]) ? "" : values[12];
            var TotalWorkMonthes = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[13]) ? "0" : values[13], out TotalWorkMonthes))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 13,
                    Message  = ERROR_MONTH,
                });
            }
            var AdverseMonthes = 0;

            if (!int.TryParse(string.IsNullOrEmpty(values[14]) ? "0" : values[14], out AdverseMonthes))
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 14,
                    Message  = ERROR_MONTH,
                });
            }
            var WorkNumber   = string.IsNullOrEmpty(values[15]) ? "" : values[15];
            var ContactPhone = string.IsNullOrEmpty(values[16]) ? "" : values[16];
            var Department   = string.IsNullOrEmpty(values[17]) ? "" : values[17];
            var WorkType     = string.IsNullOrEmpty(values[18]) ? "" : values[18];
            //Category HealthStatus = _categoryRepository.GetByName(string.IsNullOrEmpty(values[19]) ? "未知" : values[19], "HealthStatus");
            //if (HealthStatus == null)
            //{
            //    errors.Add(new ImportErrorColModel()
            //    {
            //        ColIndex = 19,
            //        Message = ERROR_HEALTHSTATUS,
            //    });
            //}
            Category MigrantWorker = _categoryRepository.GetByName(string.IsNullOrEmpty(values[20]) ? "未知" : values[20], "YesNotType");

            if (MigrantWorker == null)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 20,
                    Message  = ERROR_MIGRANTWORKER,
                });
            }
            var Leader      = string.IsNullOrEmpty(values[21]) ? string.Empty : values[21];
            var ProtectType = string.IsNullOrEmpty(values[22]) ? "" : values[22];
            var Email       = string.IsNullOrEmpty(values[23]) ? "" : values[23];

            var      StartPostDate = new DateTime(StartPostDate_Year, StartPostDate_Month == 0 ? 1 : StartPostDate_Month, 1);
            DateTime?EndPostDate   = null;

            if (EndPostDate_Year != 0)
            {
                EndPostDate = new DateTime(EndPostDate_Year, EndPostDate_Month == 0 ? 1 : EndPostDate_Month, 1);
            }
            DateTime?EntryDate = null;

            if (EntryDate_Year != 0)
            {
                EntryDate = new DateTime(EntryDate_Year, EntryDate_Month == 0 ? 1 : EntryDate_Month, 1);
            }
            DateTime?LeaveDate = null;

            if (LeaveDate_Year != 0)
            {
                LeaveDate = new DateTime(LeaveDate_Year, LeaveDate_Month == 0 ? 1 : LeaveDate_Month, 1);
            }

            if ((EndPostDate != null || StartPostDate != null) && EndPostDate <= StartPostDate)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 11,
                    Message  = "离岗时间必须大于上岗时间",
                });
            }

            if (LeaveDate <= EntryDate)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 7,
                    Message  = "离职时间必须大于入职时间",
                });
            }

            if (StartPostDate < EntryDate)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 5,
                    Message  = "上岗时间不能小于入职时间",
                });
            }

            if ((LeaveDate != null || StartPostDate != null) && LeaveDate <= StartPostDate)
            {
                errors.Add(new ImportErrorColModel()
                {
                    ColIndex = 7,
                    Message  = "离职时间必须大于上岗时间",
                });
            }
            var healthStatusId = 21;

            //没有错误,开始解析
            if (errors.Count == 0)
            {
                entity.Company       = _currentUserCompany;
                entity.Married       = Married;
                entity.EntryDate     = GetDateTime(EntryDate_Year, EntryDate_Month);
                entity.LeaveDate     = GetDateTime(LeaveDate_Year, LeaveDate_Month);
                entity.StartPostDate = GetDateTime(StartPostDate_Year, StartPostDate_Month);

                entity.EndPostDate = GetDateTime(EndPostDate_Year, EndPostDate_Month);

                entity.AdverseFactor    = AdverseFactor;
                entity.TotalWorkMonthes = TotalWorkMonthes;

                entity.AdverseMonthes = AdverseMonthes;
                entity.WorkNumber     = WorkNumber;
                entity.ContactPhone   = ContactPhone;
                entity.Department     = Department;
                entity.WorkType       = WorkType;
                //entity.HealthStatus = HealthStatus;
                //体检状态根据上岗时间及离岗时间自动生成(上岗时间在三个月内自动默认为上岗前,与数据库关联,未做岗前体检的人员超过三个月自动默认为在岗
                if (EndPostDate != null)
                {
                    healthStatusId = 12;
                }
                else
                {
                    if ((DateTime.Now - Convert.ToDateTime(StartPostDate)).Days > 90)
                    {
                        healthStatusId = 11;
                    }
                    else
                    {
                        healthStatusId = 13;
                    }
                }
                entity.HealthStatus  = _categoryRepository.GetById(healthStatusId);
                entity.MigrantWorker = MigrantWorker;
                entity.ProtectType   = ProtectType;
                entity.Email         = Email;
                //职业史
                var lastWork = entity.EmployeeBaseInfo.WorkHistories.OrderByDescending(x => x.EntryDate).FirstOrDefault();
                if (lastWork == null || lastWork.EntryDate > entity.StartPostDate)
                {
                    var workHistory = new EmployeeWorkHistory
                    {
                        WorkType         = entity.WorkType,
                        Department       = entity.Department,
                        EntryDate        = entity.StartPostDate,
                        LeaveDate        = entity.EndPostDate,
                        AdverseFactor    = entity.AdverseFactor,
                        CompanyName      = _currentUserCompany.CompanyName,
                        EmployeeBaseInfo = entity.EmployeeBaseInfo,
                        CreatedBy        = _workContext.CurrentMembershipUser.Username,
                        Deleted          = false
                    };

                    _employeeWorkHistoryService.Add(workHistory);
                }

                if (entity.EndPostDate != null)
                {
                    if (lastWork != null && entity.StartPostDate == lastWork.EntryDate && entity.Company.CompanyName == lastWork.CompanyName)
                    {
                        lastWork.LeaveDate = entity.EndPostDate;
                    }
                }
                //7、总工龄企业未维护空白项的根据职业史中的入岗时间自动生成,接害工龄企业未维护的,根据职业史中危害因素为非空白的记录推算时间,如企业维护时间的按维护时间为准。
                if (string.IsNullOrEmpty(values[13]))
                {
                    var earliestWorkHisotory = entity.EmployeeBaseInfo.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 (string.IsNullOrEmpty(values[14]))
                {
                    var workHisotries = entity.EmployeeBaseInfo.WorkHistories.OrderBy(x => x.EntryDate);
                    int monthes       = 0;
                    foreach (var item in workHisotries)
                    {
                        if (!string.IsNullOrEmpty(item.AdverseFactor))
                        {
                            DateTime?leaveDate = null;
                            if (item.LeaveDate == null)
                            {
                                leaveDate = DateTime.Now;
                                monthes  += (Math.Abs(leaveDate.Value.Month - item.EntryDate.Value.Month) + 12 * (leaveDate.Value.Year - item.EntryDate.Value.Year));
                            }
                        }
                    }
                    entity.AdverseMonthes = monthes;
                }
            }
            else
            {
                entity = null;
            }
        }