22
33import ir .bigz .springbootreal .commons .util .Utils ;
44import ir .bigz .springbootreal .dal .UserRepository ;
5+ import ir .bigz .springbootreal .dto .PageResult ;
6+ import ir .bigz .springbootreal .dto .PagedQuery ;
7+ import ir .bigz .springbootreal .dto .SqlOperation ;
58import ir .bigz .springbootreal .dto .entity .User ;
69import ir .bigz .springbootreal .dto .mapper .UserMapper ;
710import ir .bigz .springbootreal .exception .AppException ;
@@ -26,6 +29,7 @@ public class UserServiceImpl implements UserService {
2629
2730 private final UserRepository userRepository ;
2831 private final UserMapper userMapper ;
32+ private static final String USER_QUERY = "select * from users where 1=1 " ;
2933
3034
3135 public UserServiceImpl (UserRepository userRepository ,
@@ -37,12 +41,12 @@ public UserServiceImpl(UserRepository userRepository,
3741
3842 @ Override
3943 @ Transactional (propagation = Propagation .SUPPORTS , readOnly = true , rollbackFor = Exception .class )
40- @ Cacheable (value = "userCache" , key = "#userId" , condition = "#userId != null" , unless = "#result==null" )
44+ @ Cacheable (value = "userCache" , key = "#userId" , condition = "#userId != null" , unless = "#result==null" )
4145 public UserModel getUser (Long userId ) {
4246 try {
4347 Optional <User > user = userRepository .find (userId );
4448 return userMapper .userToUserModel (user .get ());
45- }catch (RuntimeException exception ){
49+ } catch (RuntimeException exception ) {
4650 throw AppException .newInstance (
4751 HttpErrorCode .ERR_10702 ,
4852 String .format ("user with id, %s not found" , userId )
@@ -54,7 +58,7 @@ public UserModel getUser(Long userId) {
5458 @ Transactional (propagation = Propagation .REQUIRED , rollbackFor = Exception .class )
5559 public UserModel addUser (UserModel userModel ) {
5660 try {
57- if (userRepository .getUserWithNationalCode (userModel .getNationalCode ()) == null ){
61+ if (userRepository .getUserWithNationalCode (userModel .getNationalCode ()) == null ) {
5862 userModel .setInsertDate (Utils .getLocalTimeNow ());
5963 userModel .setActiveStatus (true );
6064 User user = userMapper .userModelToUser (userModel );
@@ -63,7 +67,7 @@ public UserModel addUser(UserModel userModel) {
6367 }
6468 throw new RuntimeException ("user has already exist" );
6569
66- }catch (RuntimeException exception ){
70+ } catch (RuntimeException exception ) {
6771 throw AppException .newInstance (
6872 HttpErrorCode .ERR_10700 , String .format ("user has already existed with %s nationalId" , userModel .getNationalCode ())
6973 );
@@ -72,7 +76,7 @@ public UserModel addUser(UserModel userModel) {
7276
7377 @ Override
7478 @ Transactional (propagation = Propagation .REQUIRED , rollbackFor = Exception .class )
75- @ CachePut (value = "userCache" , key = "#userId" , condition = "#userId != null" , unless = "#result==null" )
79+ @ CachePut (value = "userCache" , key = "#userId" , condition = "#userId != null" , unless = "#result==null" )
7680 public UserModel updateUser (long userId , UserModel userModel ) {
7781 try {
7882 Optional <User > user = userRepository .find (userId );
@@ -81,7 +85,7 @@ public UserModel updateUser(long userId, UserModel userModel) {
8185 mapUserForUpdate (sourceUser , updateUser );
8286 sourceUser .setUpdateDate (Utils .getTimestampNow ());
8387 return userMapper .userToUserModel (sourceUser );
84- }catch (RuntimeException exception ){
88+ } catch (RuntimeException exception ) {
8589 throw AppException .newInstance (
8690 HttpErrorCode .ERR_10703 ,
8791 String .format ("user with userId %s, not found" , userId )
@@ -92,13 +96,13 @@ public UserModel updateUser(long userId, UserModel userModel) {
9296
9397 @ Override
9498 @ CacheEvict (value = "userCache" , beforeInvocation = true , key = "#userId" )
95- public String deleteUser (long userId ){
96- try {
99+ public String deleteUser (long userId ) {
100+ try {
97101 userRepository .find (userId );
98102 userRepository .delete (userId );
99103 return "Success" ;
100104
101- }catch (RuntimeException exception ){
105+ } catch (RuntimeException exception ) {
102106 throw AppException .newInstance (
103107 HttpErrorCode .ERR_10701 ,
104108 String .format ("user with id %s, not found" , userId )
@@ -112,7 +116,7 @@ public List<UserModel> getAll() {
112116 try {
113117 Stream <User > allUser = userRepository .getAll ();
114118 return allUser .map (userMapper ::userToUserModel ).collect (Collectors .toList ());
115- }catch (RuntimeException exception ){
119+ } catch (RuntimeException exception ) {
116120 throw AppException .newInstance (
117121 HttpErrorCode .ERR_10701 ,
118122 String .format ("getAll method has error: %s" , exception .getCause ())
@@ -130,7 +134,7 @@ public Page<UserModel> getUserSearchResult(UserSearchDto userSearchDto, String s
130134 Page <User > users = userRepository .getUserSearchResult (userSearchDto , order , pageable );
131135 List <UserModel > collect = users .get ().map (userMapper ::userToUserModel ).collect (Collectors .toList ());
132136 return new PageImpl <>(collect , pageable , users .getTotalElements ());
133- }catch (RuntimeException exception ){
137+ } catch (RuntimeException exception ) {
134138 throw AppException .newInstance (
135139 HttpErrorCode .ERR_10701 ,
136140 String .format ("getAll method has error: %s" , exception .getCause ())
@@ -142,38 +146,68 @@ public Page<UserModel> getUserSearchResult(UserSearchDto userSearchDto, String s
142146
143147 @ Override
144148 @ Transactional (propagation = Propagation .REQUIRED , isolation = Isolation .DEFAULT )
145- public Page <UserModel > getAllUserPage (String sortOrder , Sort .Direction direction , Integer pageNumber , Integer pageSize ){
149+ public Page <UserModel > getAllUserPage (String sortOrder , Sort .Direction direction , Integer pageNumber , Integer pageSize ) {
146150 Sort .Order order = new Sort .Order (direction , sortOrder );
147151 Pageable pageable = PageRequest .of (pageNumber , pageSize , Sort .by (order ));
148152 Page <User > all = userRepository .getAll (pageable );
149153 List <UserModel > collect = all .get ().map (userMapper ::userToUserModel ).collect (Collectors .toList ());
150154 return new PageImpl <>(collect , pageable , all .getTotalElements ());
151155 }
152156
153- private void mapUserForUpdate (User sourceUser , User updateUser ){
154- if (!sourceUser .getFirstName ().equals (updateUser .getFirstName ())){
157+ @ Override
158+ @ Transactional (propagation = Propagation .REQUIRED , isolation = Isolation .DEFAULT )
159+ public PageResult <UserModel > getUserSearchV2 (Map <String , String > queryString , PagedQuery pagedQuery ) {
160+ Map <String , Object > parametersMap = new HashMap <>();
161+ Map <String , String > conditionsMap = new HashMap <>();
162+ StringBuffer stringBuffer = new StringBuffer ();
163+ stringBuffer .append (USER_QUERY );
164+ Utils .buildNativeQueryCondition (queryString ,
165+ conditionsMap ,
166+ parametersMap ,
167+ "first_name" ,
168+ "firstName" ,
169+ SqlOperation .CONTAINS ,
170+ String .class );
171+
172+ conditionsMap .keySet ().forEach (s -> stringBuffer .append (conditionsMap .get (s )));
173+
174+ PageResult <User > userPageResult = userRepository .pageCreateQuery (stringBuffer .toString (), pagedQuery , parametersMap );
175+
176+ List <UserModel > collect = userPageResult .getResult ().stream ().map (userMapper ::userToUserModel ).collect (Collectors .toList ());
177+
178+ PageResult <UserModel > userModelPageResult = new PageResult <>(collect ,
179+ userPageResult .getPageSize (),
180+ userPageResult .getPageNumber (),
181+ userPageResult .getOffset (),
182+ userPageResult .getTotal ());
183+
184+ return userModelPageResult ;
185+ }
186+
187+ private void mapUserForUpdate (User sourceUser , User updateUser ) {
188+ if (!sourceUser .getFirstName ().equals (updateUser .getFirstName ())) {
155189 sourceUser .setFirstName (updateUser .getFirstName ());
156190 }
157- if (!sourceUser .getLastName ().equals (updateUser .getLastName ())){
191+ if (!sourceUser .getLastName ().equals (updateUser .getLastName ())) {
158192 sourceUser .setLastName (updateUser .getLastName ());
159193 }
160- if (!sourceUser .getUserName ().equals (updateUser .getUserName ())){
194+ if (!sourceUser .getUserName ().equals (updateUser .getUserName ())) {
161195 sourceUser .setUserName (updateUser .getUserName ());
162196 }
163- if (!sourceUser .getNationalCode ().equals (updateUser .getNationalCode ())){
197+ if (!sourceUser .getNationalCode ().equals (updateUser .getNationalCode ())) {
164198 sourceUser .setNationalCode (updateUser .getNationalCode ());
165199 }
166- if (!sourceUser .getEmail ().equals (updateUser .getEmail ())){
200+ if (!sourceUser .getEmail ().equals (updateUser .getEmail ())) {
167201 sourceUser .setEmail (updateUser .getEmail ());
168202 }
169- if (!sourceUser .getMobile ().equals (updateUser .getMobile ())){
203+ if (!sourceUser .getMobile ().equals (updateUser .getMobile ())) {
170204 sourceUser .setMobile (updateUser .getMobile ());
171205 }
172- if (!sourceUser .getGender ().equals (updateUser .getGender ())){
206+ if (!sourceUser .getGender ().equals (updateUser .getGender ())) {
173207 sourceUser .setGender (updateUser .getGender ());
174208 }
175209
176- if (!sourceUser .isActiveStatus () == (updateUser .isActiveStatus ())){
210+ if (!sourceUser .isActiveStatus () == (updateUser .isActiveStatus ())) {
177211 sourceUser .setActiveStatus (updateUser .isActiveStatus ());
178212 }
179213 }
0 commit comments