Пример #1
0
        public HandleState Import(List <SysUserViewModel> data)
        {
            try
            {
                eFMSDataContext    dc           = (eFMSDataContext)DataContext.DC;
                List <SysUser>     sysUsers     = new List <SysUser>();
                List <SysEmployee> sysEmployees = new List <SysEmployee>();
                foreach (var item in data)
                {
                    var objUser     = new SysUserModel();
                    var objEmployee = new SysEmployee();
                    objEmployee.Id = Guid.NewGuid().ToString();
                    objEmployee.DatetimeCreated = objEmployee.DatetimeModified = DateTime.Now;
                    objEmployee.EmployeeNameEn  = item.EmployeeNameEn;
                    objEmployee.EmployeeNameVn  = item.EmployeeNameVn;
                    objEmployee.Tel             = item.Tel;
                    objEmployee.Title           = item.Title;
                    objEmployee.StaffCode       = item.StaffCode;
                    objEmployee.Email           = item.Email;
                    sysEmployees.Add(objEmployee);
                    objUser.Username      = item.Username;
                    objUser.UserType      = item.UserType;
                    objUser.WorkingStatus = !string.IsNullOrEmpty(item.WorkingStatus)? item.WorkingStatus : "Working";
                    objUser.Active        = item.Status?.ToLower() == "active" ? true : (item.Status?.ToLower() == "inactive" ? false : false);
                    objUser.EmployeeId    = objEmployee.Id;
                    objUser.Id            = Guid.NewGuid().ToString();
                    objUser.UserCreated   = objUser.UserModified = currentUser.UserID;
                    objUser.Password      = BCrypt.Net.BCrypt.HashPassword(Constants.PERMISSION_RANGE_OWNER);
                    objUser.Description   = item.Description;
                    objUser.Password      = SystemConstants.Password;
                    objUser.Password      = BCrypt.Net.BCrypt.HashPassword(objUser.Password);
                    sysUsers.Add(objUser);
                }

                dc.SysEmployee.AddRange(sysEmployees);
                dc.SysUser.AddRange(sysUsers);
                dc.SaveChanges();
                return(new HandleState());
            }
            catch (Exception ex)
            {
                return(new HandleState(ex.Message));
            }
        }
Пример #2
0
        public List <SysUserImportModel> CheckValidImport(List <SysUserImportModel> list)
        {
            eFMSDataContext dc = (eFMSDataContext)DataContext.DC;

            list.ForEach(item =>
            {
                //check empty username
                string userName    = item.Username;
                item.UsernameValid = true;
                if (string.IsNullOrEmpty(userName))
                {
                    item.Username      = stringLocalizer[SystemLanguageSub.MSG_USER_USERNAME_EMPTY];
                    item.IsValid       = false;
                    item.UsernameValid = false;
                }
                else
                {
                    var isFound = DataContext.Get().Any(x => x.Username == userName);
                    if (isFound)
                    {
                        item.IsValid       = false;
                        item.UsernameValid = false;
                        item.Username      = stringLocalizer[SystemLanguageSub.MSG_USER_USERNAME_EXISTED, item.Username].Value;
                    }
                }
                //check empty Name EN
                string nameEN            = item.EmployeeNameEn;
                item.EmployeeNameEnValid = true;
                if (string.IsNullOrEmpty(nameEN))
                {
                    item.EmployeeNameEn      = stringLocalizer[SystemLanguageSub.MSG_USER_NAMEEN_EMPTY];
                    item.IsValid             = false;
                    item.EmployeeNameEnValid = false;
                }

                //check empty Full Name
                string nameVN            = item.EmployeeNameVn;
                item.EmployeeNameVnValid = true;
                if (string.IsNullOrEmpty(nameVN))
                {
                    item.EmployeeNameVn      = stringLocalizer[SystemLanguageSub.MSG_USER_NAMEVN_EMPTY];
                    item.IsValid             = false;
                    item.EmployeeNameVnValid = false;
                }
                //check empty and existed staff code
                string staffCode    = item.StaffCode;
                item.StaffCodeValid = true;
                if (string.IsNullOrEmpty(staffCode))
                {
                    item.StaffCode      = stringLocalizer[SystemLanguageSub.MSG_USER_STAFFCODE_EMPTY];
                    item.IsValid        = false;
                    item.StaffCodeValid = false;
                }
                else
                {
                    var isFound = sysEmployeeService.Get().Any(x => x.StaffCode == staffCode);
                    if (isFound)
                    {
                        item.IsValid        = false;
                        item.StaffCodeValid = false;
                        item.StaffCode      = stringLocalizer[SystemLanguageSub.MSG_USER_STAFFCODE_EXISTED, item.StaffCode].Value;
                    }
                }

                ////chek empty title
                //string title = item.Title;
                //item.TitleValid = true;
                //if (string.IsNullOrEmpty(title))
                //{
                //    item.Title = stringLocalizer[SystemLanguageSub.MSG_USER_TITLE_EMPTY];
                //    item.IsValid = false;
                //    item.TitleValid = false;
                //}
                //check empty and valid User Type
                string userType    = item.UserType;
                item.UserTypeValid = true;
                if (string.IsNullOrEmpty(userType))
                {
                    item.UserType      = stringLocalizer[SystemLanguageSub.MSG_USER_USERTYPE_EMPTY];
                    item.IsValid       = false;
                    item.UserTypeValid = false;
                }
                else if (!userType.Equals("Normal User") && !userType.Equals("Local Admin") && !userType.Equals("Super Admin"))
                {
                    item.UserType      = stringLocalizer[SystemLanguageSub.MSG_USER_USERTYPE_NOTFOUND];
                    item.IsValid       = false;
                    item.UserTypeValid = false;
                }

                //check empty and valid working status
                string workingStatus    = item.WorkingStatus;
                item.WorkingStatusValid = true;
                //if (string.IsNullOrEmpty(workingStatus))
                //{
                //    item.WorkingStatus = stringLocalizer[SystemLanguageSub.MSG_USER_WORKINGSTATUS_EMPTY];
                //    item.IsValid = false;
                //    item.WorkingStatusValid = false;
                //}
                if (workingStatus != null)
                {
                    if (!workingStatus.Equals("Working") && !workingStatus.Equals("Maternity leave") && !workingStatus.Equals("Off"))
                    {
                        item.WorkingStatus      = stringLocalizer[LanguageSub.MSG_DATA_NOT_FOUND];
                        item.IsValid            = false;
                        item.WorkingStatusValid = false;
                    }
                }
                //check empty and valid status
                string status    = item.Status;
                item.StatusValid = true;
                //if (string.IsNullOrEmpty(status))
                //{
                //    item.Status = stringLocalizer[SystemLanguageSub.MSG_USER_STATUS_EMPTY];
                //    item.IsValid = false;
                //    item.StatusValid = false;
                //}
                if (status != null)
                {
                    if (!status.Equals("Active") && !status.Equals("Inactive"))
                    {
                        item.Status      = string.Format(stringLocalizer[SystemLanguageSub.MSG_USER_STATUS_NOTFOUND], item.Status);
                        item.IsValid     = false;
                        item.StatusValid = false;
                    }
                }
                //check empty and valid email
                string email    = item.Email;
                item.EmailValid = true;
                if (string.IsNullOrEmpty(email))
                {
                    item.Email      = stringLocalizer[SystemLanguageSub.MSG_USER_EMAIL_EMPTY];
                    item.IsValid    = false;
                    item.EmailValid = false;
                }
                var checkDupStaffCode = list.GroupBy(x => x.StaffCode)
                                        .Where(t => t.Count() > 1)
                                        .Select(y => y.Key)
                                        .ToList();
                var checkDupUserName = list.GroupBy(x => x.Username)
                                       .Where(t => t.Count() > 1)
                                       .Select(y => y.Key)
                                       .ToList();
                if (checkDupStaffCode.Count > 0)
                {
                    item.IsValid        = false;
                    item.StaffCode      = stringLocalizer[SystemLanguageSub.MSG_USER_STAFFCODE_DUPLICATE, item.StaffCode].Value;
                    item.StaffCodeValid = false;
                }

                if (checkDupUserName.Count > 0)
                {
                    item.IsValid       = false;
                    item.UsernameValid = false;
                    item.Username      = stringLocalizer[SystemLanguageSub.MSG_USER_USERNAME_DUPLICATE, item.Username].Value;
                }
            });
            return(list);
        }