-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
386 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...y-project/lottery-common/src/main/java/com/flameking/lottery/common/util/SpringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.flameking.lottery.common.util; | ||
|
||
import org.apache.commons.lang3.ObjectUtils; | ||
import org.springframework.aop.framework.AopContext; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware{ | ||
/** Spring应用上下文环境 */ | ||
private static ConfigurableListableBeanFactory beanFactory; | ||
|
||
private static ApplicationContext applicationContext; | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException | ||
{ | ||
SpringUtils.beanFactory = beanFactory; | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException | ||
{ | ||
SpringUtils.applicationContext = applicationContext; | ||
} | ||
|
||
/** | ||
* 获取对象 | ||
* | ||
* @param name | ||
* @return Object 一个以所给名字注册的bean的实例 | ||
* @throws BeansException | ||
* | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T getBean(String name) throws BeansException | ||
{ | ||
return (T) beanFactory.getBean(name); | ||
} | ||
|
||
/** | ||
* 获取类型为requiredType的对象 | ||
* | ||
* @param clz | ||
* @return | ||
* @throws BeansException | ||
* | ||
*/ | ||
public static <T> T getBean(Class<T> clz) throws BeansException | ||
{ | ||
T result = (T) beanFactory.getBean(clz); | ||
return result; | ||
} | ||
|
||
/** | ||
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true | ||
* | ||
* @param name | ||
* @return boolean | ||
*/ | ||
public static boolean containsBean(String name) | ||
{ | ||
return beanFactory.containsBean(name); | ||
} | ||
|
||
/** | ||
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) | ||
* | ||
* @param name | ||
* @return boolean | ||
* @throws NoSuchBeanDefinitionException | ||
* | ||
*/ | ||
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException | ||
{ | ||
return beanFactory.isSingleton(name); | ||
} | ||
|
||
/** | ||
* @param name | ||
* @return Class 注册对象的类型 | ||
* @throws NoSuchBeanDefinitionException | ||
* | ||
*/ | ||
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException | ||
{ | ||
return beanFactory.getType(name); | ||
} | ||
|
||
/** | ||
* 如果给定的bean名字在bean定义中有别名,则返回这些别名 | ||
* | ||
* @param name | ||
* @return | ||
* @throws NoSuchBeanDefinitionException | ||
* | ||
*/ | ||
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException | ||
{ | ||
return beanFactory.getAliases(name); | ||
} | ||
|
||
/** | ||
* 获取aop代理对象 | ||
* | ||
* @param invoker | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> T getAopProxy(T invoker) | ||
{ | ||
return (T) AopContext.currentProxy(); | ||
} | ||
|
||
/** | ||
* 获取当前的环境配置,无配置返回null | ||
* | ||
* @return 当前的环境配置 | ||
*/ | ||
public static String[] getActiveProfiles() | ||
{ | ||
return applicationContext.getEnvironment().getActiveProfiles(); | ||
} | ||
|
||
/** | ||
* 获取当前的环境配置,当有多个环境配置时,只获取第一个 | ||
* | ||
* @return 当前的环境配置 | ||
*/ | ||
public static String getActiveProfile() | ||
{ | ||
final String[] activeProfiles = getActiveProfiles(); | ||
return ObjectUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...va/com/flameking/lottery/domain/activity/repository/IUserTakeActivityCountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.flameking.lottery.domain.activity.repository; | ||
|
||
import com.flameking.lottery.domain.activity.model.vo.ActivityBillVO; | ||
|
||
public interface IUserTakeActivityCountRepository { | ||
boolean subtractionLeftCount(ActivityBillVO bill); | ||
} |
8 changes: 8 additions & 0 deletions
8
...in/java/com/flameking/lottery/domain/activity/repository/IUserTakeActivityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.flameking.lottery.domain.activity.repository; | ||
|
||
import com.flameking.lottery.domain.activity.model.aggregates.PartakeReq; | ||
import com.flameking.lottery.domain.activity.model.vo.ActivityBillVO; | ||
|
||
public interface IUserTakeActivityRepository { | ||
boolean takeActivity(PartakeReq partake, ActivityBillVO bill, Long takeId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...com/flameking/lottery/infrastructure/repository/impl/UserTakeActivityCountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.flameking.lottery.infrastructure.repository.impl; | ||
|
||
import com.flameking.lottery.domain.activity.model.vo.ActivityBillVO; | ||
import com.flameking.lottery.domain.activity.repository.IUserTakeActivityCountRepository; | ||
import com.flameking.lottery.infrastructure.entity.UserTakeActivityCount; | ||
import com.flameking.lottery.infrastructure.service.IUserTakeActivityCountService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class UserTakeActivityCountRepository implements IUserTakeActivityCountRepository { | ||
@Autowired | ||
private IUserTakeActivityCountService userTakeActivityCountService; | ||
|
||
public boolean subtractionLeftCount(ActivityBillVO bill) { | ||
if (null == bill.getUserTakeLeftCount()) { | ||
UserTakeActivityCount userTakeActivityCount = new UserTakeActivityCount(); | ||
userTakeActivityCount.setUId(bill.getUId()); | ||
userTakeActivityCount.setActivityId(bill.getActivityId()); | ||
userTakeActivityCount.setTotalCount(bill.getTakeCount()); | ||
userTakeActivityCount.setLeftCount(bill.getTakeCount() - 1); | ||
return userTakeActivityCountService.create(userTakeActivityCount); | ||
} else { | ||
UserTakeActivityCount userTakeActivityCount = new UserTakeActivityCount(); | ||
userTakeActivityCount.setUId(bill.getUId()); | ||
userTakeActivityCount.setActivityId(bill.getActivityId()); | ||
return userTakeActivityCountService.updateLeftCount(userTakeActivityCount); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...java/com/flameking/lottery/infrastructure/repository/impl/UserTakeActivityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.flameking.lottery.infrastructure.repository.impl; | ||
|
||
import com.flameking.lottery.domain.activity.model.aggregates.PartakeReq; | ||
import com.flameking.lottery.domain.activity.model.vo.ActivityBillVO; | ||
import com.flameking.lottery.domain.activity.repository.IUserTakeActivityRepository; | ||
import com.flameking.lottery.infrastructure.entity.UserTakeActivity; | ||
import com.flameking.lottery.infrastructure.service.IUserTakeActivityService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class UserTakeActivityRepository implements IUserTakeActivityRepository { | ||
@Autowired | ||
private IUserTakeActivityService userTakeActivityService; | ||
|
||
@Override | ||
public boolean takeActivity(PartakeReq partake, ActivityBillVO bill, Long takeId) { | ||
UserTakeActivity userTakeActivity = new UserTakeActivity(); | ||
userTakeActivity.setUId(bill.getUId()); | ||
userTakeActivity.setTakeId(takeId); | ||
userTakeActivity.setActivityId(bill.getActivityId()); | ||
userTakeActivity.setActivityName(bill.getActivityName()); | ||
userTakeActivity.setTakeDate(partake.getPartakeDate()); | ||
|
||
if (null == bill.getUserTakeLeftCount()) { | ||
userTakeActivity.setTakeCount(1); | ||
} else { | ||
userTakeActivity.setTakeCount(bill.getTakeCount() - bill.getUserTakeLeftCount()); | ||
} | ||
String uuid = bill.getUId() + "_" + bill.getActivityId() + "_" + bill.getTakeCount(); | ||
userTakeActivity.setUuid(uuid); | ||
|
||
return userTakeActivityService.create(userTakeActivity); | ||
} | ||
} |
Oops, something went wrong.