Example #1
0
            public async Task <EmployeeVm> Handle(Query request, CancellationToken cancellationToken)
            {
                var student = await _context.GetParameterValue <string>("SuUserType", "Student", cancellationToken);

                StringBuilder sql = new StringBuilder();

                sql.AppendLine("select e.company_code as person_company,");
                if (request.UserType == student)
                {
                    sql.AppendLine(" e.student_id ");
                }
                else
                {
                    sql.AppendLine(" e.employee_code ");
                }
                sql.AppendLine("from su_user_type t ");
                if (request.UserType == student)
                {
                    sql.AppendLine("inner join sh_student e on e.company_code = t.company_code ");
                    sql.AppendLine("and e.student_id  = t.student_id ");
                }
                else
                {
                    sql.AppendLine("inner join db_employee e on e.company_code = t.company_code ");
                    sql.AppendLine("and e.employee_code  = t.employee_code ");
                }

                sql.AppendLine("where t.user_id = @User");

                var emp = await _context.QueryFirstOrDefaultAsync <EmployeeVm>(sql.ToString(), new { User = request.UserId }, cancellationToken);

                return(emp);
            }
Example #2
0
            public async Task <PageDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var student = await _context.GetParameterValue <string>("SuUserType", "Student", cancellationToken);

                StringBuilder sql = new StringBuilder();

                sql.AppendLine("	select u.user_id as id,u.user_name as \"username\" ");
                if (request.UserType == student)
                {
                    sql.AppendLine("          ,e.student_code as \"employeeCode\" ");
                    sql.AppendLine("	      , get_name(@Lang,e.student_name_tha,e.student_name_eng) as \"employeeName\" ");
                }
                else
                {
                    sql.AppendLine("          ,e.employee_code as \"employeeCode\" ");
                    sql.AppendLine("	      , get_name(@Lang,e.t_name_concat,e.e_name_concat) as \"employeeName\" ");
                }

                sql.AppendLine("	      , u.active,u.force_change_password as \"forceChange\",case  when u.lockout_end >= current_timestamp then true else false end as locked	");
                sql.AppendLine("	      , u.xmin as \"rowVersion\"	");
                sql.AppendLine("	from su_user u 	");
                sql.AppendLine("	inner join su_user_type t on t.user_id  = u.user_id 	");
                if (request.UserType == student)
                {
                    sql.AppendLine("	inner join sh_student e on e.company_code = t.company_code 	");
                    sql.AppendLine("	                       and e.student_id = t.student_id 	");
                }
                else
                {
                    sql.AppendLine("	inner join db_employee e on e.company_code = t.company_code 	");
                    sql.AppendLine("	                        and e.employee_code = t.employee_code 	");
                    sql.AppendLine("                            and group_type_code = @UserGroup ");
                }

                sql.AppendLine("    where 1=1 ");

                if (!string.IsNullOrEmpty(request.Username))
                {
                    sql.AppendLine("and u.user_name ilike concat('%',@Username,'%') ");
                }

                if (request.UserType == student && !string.IsNullOrEmpty(request.Employee))
                {
                    sql.AppendLine("and replace(concat(e.student_code,e.student_name_tha,e.student_name_eng),' ','') ilike  concat('%',replace(@Employee,' ',''),'%') ");
                }
                else if (request.UserType != student && !string.IsNullOrEmpty(request.Employee))
                {
                    sql.AppendLine("and replace(concat(e.employee_code,e.t_name_concat,e.e_name_concat),' ','') ilike  concat('%',replace(@Employee,' ',''),'%') ");
                }

                switch (request.Status)
                {
                case "A":
                    sql.AppendLine("  AND u.active = true ");
                    break;

                case "I":
                    sql.AppendLine("  AND coalesce(u.active,false) = false");
                    break;

                case "F":
                    sql.AppendLine("  AND u.force_change_password = true ");
                    break;

                case "L":
                    sql.AppendLine("  AND u.lockout_end is not null ");
                    sql.AppendLine("  AND current_timestamp <= lockout_end  ");
                    break;
                }

                return(await _context.GetPage(sql.ToString(), new
                {
                    Company = this._user.Company,
                    Username = request.Username,
                    Employee = request.Employee,
                    Status = request.Status,
                    UserGroup = request.UserGroup,
                    Lang = _user.Language
                }, (RequestPageQuery)request, cancellationToken));
            }
Example #3
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var exists = await _context.Set <SuUser>().AnyAsync(o => o.UserName.ToLower() == request.UserName.ToLower(), cancellationToken);

                if (exists)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "message.STD00004", "label.SURT06.Username");
                }

                var studentType = await _context.GetParameterValue <string>("SuUserType", "Student", cancellationToken);

                var        saveResult = new Result();
                DbEmployee employee   = null;

                string password = string.Empty;


                employee = await _context.Set <DbEmployee>().FirstOrDefaultAsync(o => o.CompanyCode == _user.Company && o.EmployeeCode == request.EmployeeCode, cancellationToken);

                password = employee.EmployeeCode;


                request.CreatedBy      = _user.UserName;
                request.CreatedDate    = DateTime.Now;
                request.CreatedProgram = _user.ProgramCode;
                request.UpdatedBy      = _user.UserName;
                request.UpdatedDate    = DateTime.Now;
                request.UpdatedProgram = _user.ProgramCode;
                var result = await _identity.CreateUserAsync(request, password);

                var userType = new SuUserType
                {
                    UserId      = result.UserId,
                    CompanyCode = _user.Company,
                    UserType    = request.UserType
                };

                if (request.UserType == studentType)
                {
                    userType.StudentId = request.StudentId;
                }
                else
                {
                    userType.EmployeeCode = request.EmployeeCode;
                }
                _context.Set <SuUserType>().Add(userType);
                await _context.SaveChangesAsync(cancellationToken);

                saveResult.haveEmail = false;
                if (request.UserType != studentType && employee != null && !string.IsNullOrWhiteSpace(employee?.Email))
                {
                    try
                    {
                        var param = new Dictionary <string, string>();
                        param.Add("[UserName]", request.UserName);
                        param.Add("[Password]", password);
                        await _email.SendEmailWithTemplateAsysnc("SU002", employee.Email, null, param);

                        saveResult.haveEmail = true;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "SURT06 send create user email fail.");
                    }
                }

                saveResult.Id = result.UserId;
                return(saveResult);
            }