| @@ -7,16 +7,6 @@ import com.iformall.domain.po.WxCoupon; | |||
| public interface PushLimitService { | |||
| /** | |||
| * 根据实体查询分页列表 | |||
| * | |||
| * @param record | |||
| * @param pageIndex | |||
| * @param pageSize | |||
| * @return | |||
| */ | |||
| PageInfo<PushLimit> listAsPage(PushLimit record, Integer pageIndex, Integer pageSize); | |||
| /** | |||
| * 获取租户的PushLimit | |||
| * | |||
| @@ -25,14 +15,6 @@ public interface PushLimitService { | |||
| */ | |||
| PushLimit getPushLimit(String tenantId); | |||
| /** | |||
| * 根据Id获得实体 | |||
| * | |||
| * @param id | |||
| * @return | |||
| */ | |||
| PushLimit getById(Long id); | |||
| /** | |||
| * 保存或更新实体 | |||
| * | |||
| @@ -40,13 +22,6 @@ public interface PushLimitService { | |||
| */ | |||
| void saveOrUpdate(PushLimit record); | |||
| /** | |||
| * 根据Id删除实体 | |||
| * | |||
| * @param id | |||
| */ | |||
| void deleteById(Long id); | |||
| /** | |||
| * 检查发送时间 | |||
| * @param tenantId | |||
| @@ -8,16 +8,6 @@ import com.iformall.domain.po.WxScoreRules; | |||
| public interface WxScoreRulesService { | |||
| /** | |||
| * 根据实体查询分页列表 | |||
| * | |||
| * @param record | |||
| * @param pageIndex | |||
| * @param pageSize | |||
| * @return | |||
| */ | |||
| PageInfo<WxScoreRules> listAsPage(WxScoreRules record, Integer pageIndex, Integer pageSize); | |||
| /** | |||
| * 获取租户的ScoreRules | |||
| * @param tenantId | |||
| @@ -25,27 +15,14 @@ public interface WxScoreRulesService { | |||
| */ | |||
| WxScoreRules getScoreRules(String tenantId); | |||
| /** | |||
| * 根据Id获得实体 | |||
| * | |||
| * @param id | |||
| * @return | |||
| */ | |||
| WxScoreRules getById(Long id); | |||
| /** | |||
| /** | |||
| * 保存或更新实体 | |||
| * | |||
| * @param record | |||
| */ | |||
| void saveOrUpdate(WxScoreRules record); | |||
| /** | |||
| * 根据Id删除实体 | |||
| * | |||
| * @param id | |||
| */ | |||
| void deleteById(Long id); | |||
| /** | |||
| * 登录获取成长值 | |||
| * @param user | |||
| @@ -12,6 +12,7 @@ import com.iformall.mapper.PushLimitMapper; | |||
| import com.iformall.mapper.WxCouponActionLogMapper; | |||
| import com.iformall.service.PushLimitService; | |||
| import com.iformall.utils.DateUtils; | |||
| import com.iformall.utils.RedisUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -26,55 +27,66 @@ import java.util.Map; | |||
| public class PushLimitServiceImpl implements PushLimitService { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| private String keyPrev = "pushlimit_"; | |||
| @Autowired | |||
| PushLimitMapper pushLimitMapper; | |||
| @Autowired | |||
| WxCouponActionLogMapper wxCouponActionLogMapper; | |||
| @Override | |||
| public PageInfo<PushLimit> listAsPage(PushLimit record, Integer pageIndex, Integer pageSize) { | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> pushLimitMapper.findList(record)); | |||
| } | |||
| @Autowired | |||
| RedisUtils redisUtils; | |||
| @Override | |||
| public PushLimit getPushLimit(String tenantId) { | |||
| // get pushLime from cache | |||
| String key = keyPrev + tenantId; | |||
| // 缓存存在 | |||
| boolean hasKey = redisUtils.hasKey(key); | |||
| if (hasKey) { | |||
| PushLimit pushLimit = redisUtils.get(key, PushLimit.class); | |||
| logger.info("PushLimitServiceImpl.getPushLimit() : 从缓存中获取了疲劳度 >> " + pushLimit.toString()); | |||
| return pushLimit; | |||
| } | |||
| PushLimit pushLimit = new PushLimit(); | |||
| pushLimit.setTenantId(tenantId); | |||
| List<PushLimit> list = pushLimitMapper.findList(pushLimit); | |||
| if (list.size() > 0) { | |||
| return list.get(0); | |||
| pushLimit = list.get(0); | |||
| } else { | |||
| // 如果疲劳度设置不存在,启用默认值 | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| pushLimit.setId(idWorker.nextId()); | |||
| pushLimit.setMsgAmount(3); | |||
| pushLimit.setCouponAmount(10); | |||
| pushLimit.setCouponDay(10); | |||
| pushLimit.setTimeStart("8:00"); | |||
| pushLimit.setTimeEnd("23:00"); | |||
| Date curDate = new Date(); | |||
| pushLimit.setCreateTime(curDate); | |||
| pushLimit.setUpdateTime(curDate); | |||
| pushLimitMapper.insertSelective(pushLimit); | |||
| } | |||
| // 如果疲劳度设置不存在,启用默认值 | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| pushLimit.setId(idWorker.nextId()); | |||
| pushLimit.setMsgAmount(3); | |||
| pushLimit.setCouponAmount(10); | |||
| pushLimit.setCouponDay(10); | |||
| pushLimit.setTimeStart("8:00"); | |||
| pushLimit.setTimeEnd("23:00"); | |||
| Date curDate = new Date(); | |||
| pushLimit.setCreateTime(curDate); | |||
| pushLimit.setUpdateTime(curDate); | |||
| pushLimitMapper.insertSelective(pushLimit); | |||
| return pushLimit; | |||
| } | |||
| @Override | |||
| public PushLimit getById(Long id) { | |||
| return pushLimitMapper.selectByPrimaryKey(id); | |||
| // 插入缓存 | |||
| redisUtils.set(key, pushLimit); | |||
| logger.info("PushLimitServiceImpl.getPushLimit() : 疲劳度插入缓存 >> " + pushLimit.toString()); | |||
| return pushLimit; | |||
| } | |||
| @Override | |||
| public void saveOrUpdate(PushLimit record) { | |||
| pushLimitMapper.updateByPrimaryKeySelective(record); | |||
| } | |||
| @Override | |||
| public void deleteById(Long id) { | |||
| pushLimitMapper.deleteByPrimaryKey(id); | |||
| // 缓存存在,删除缓存 | |||
| String key = keyPrev + record.getTenantId(); | |||
| boolean hasKey = redisUtils.hasKey(key); | |||
| if (hasKey) { | |||
| redisUtils.delete(key); | |||
| logger.info("PushLimitServiceImpl.saveOrUpdate() : 从缓存中删除疲劳度 >> " + record.toString()); | |||
| } | |||
| } | |||
| @Override | |||
| @@ -1,7 +1,5 @@ | |||
| package com.iformall.service.impl; | |||
| import com.github.pagehelper.PageHelper; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.enums.EnumScoreType; | |||
| @@ -10,6 +8,7 @@ import com.iformall.mapper.WxScoreRulesMapper; | |||
| import com.iformall.service.WxCUserBasicInfoService; | |||
| import com.iformall.service.WxCUserService; | |||
| import com.iformall.service.WxScoreRulesService; | |||
| import com.iformall.utils.RedisUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -22,6 +21,8 @@ import java.util.List; | |||
| public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| private String keyPrev = "scorerules_"; | |||
| @Autowired | |||
| WxScoreRulesMapper wxScoreRulesMapper; | |||
| @Autowired | |||
| @@ -31,41 +32,51 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||
| @Autowired | |||
| WxCUserBasicInfoService wxCUserBasicInfoService; | |||
| @Override | |||
| public PageInfo<WxScoreRules> listAsPage(WxScoreRules record, Integer pageIndex, Integer pageSize) { | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxScoreRulesMapper.findList(record)); | |||
| } | |||
| @Autowired | |||
| RedisUtils redisUtils; | |||
| @Override | |||
| public WxScoreRules getScoreRules(String tenantId) { | |||
| // get pushLime from cache | |||
| String key = keyPrev + tenantId; | |||
| // 缓存存在 | |||
| if (redisUtils.hasKey(key)) { | |||
| WxScoreRules wScoreRules = redisUtils.get(key, WxScoreRules.class); | |||
| logger.info("WxScoreRulesServiceImpl.getScoreRules() : 从缓存中获取了成长值设置 >> " + wScoreRules.toString()); | |||
| return wScoreRules; | |||
| } | |||
| WxScoreRules scoreRules = new WxScoreRules(); | |||
| scoreRules.setTenantId(tenantId); | |||
| List<WxScoreRules> list = wxScoreRulesMapper.findList(scoreRules); | |||
| if (list.size() > 0) { | |||
| return list.get(0); | |||
| scoreRules = list.get(0); | |||
| } else { | |||
| // 如果成长值规则不存在,启用默认值 | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| scoreRules.setId(idWorker.nextId()); | |||
| scoreRules.setLoginCount(1); | |||
| scoreRules.setLoginScoreNumber(10); | |||
| scoreRules.setConsumptionAmount(1); | |||
| scoreRules.setConsumptionScoreNumber(50); | |||
| scoreRules.setBindCarNumber(1); | |||
| scoreRules.setBindCarScoreNumber(100); | |||
| scoreRules.setWifiNumber(1); | |||
| scoreRules.setWifiScoreNumber(10); | |||
| scoreRules.setPersonScoreNumber(100); | |||
| scoreRules.setPhoneScoreNumber(100); | |||
| wxScoreRulesMapper.insertSelective(scoreRules); | |||
| } | |||
| // 如果成长值规则不存在,启用默认值 | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| scoreRules.setId(idWorker.nextId()); | |||
| scoreRules.setLoginCount(1); | |||
| scoreRules.setLoginScoreNumber(10); | |||
| scoreRules.setConsumptionAmount(1); | |||
| scoreRules.setConsumptionScoreNumber(50); | |||
| scoreRules.setBindCarNumber(1); | |||
| scoreRules.setBindCarScoreNumber(100); | |||
| scoreRules.setWifiNumber(1); | |||
| scoreRules.setWifiScoreNumber(10); | |||
| scoreRules.setPersonScoreNumber(100); | |||
| scoreRules.setPhoneScoreNumber(100); | |||
| wxScoreRulesMapper.insertSelective(scoreRules); | |||
| // 插入缓存 | |||
| redisUtils.set(key, scoreRules); | |||
| logger.info("WxScoreRulesServiceImpl.getScoreRules() : 成长值设置插入缓存 >> " + scoreRules.toString()); | |||
| return scoreRules; | |||
| } | |||
| @Override | |||
| public WxScoreRules getById(Long id) { | |||
| return wxScoreRulesMapper.selectByPrimaryKey(id); | |||
| } | |||
| @Override | |||
| public void saveOrUpdate(WxScoreRules record) { | |||
| @@ -77,11 +88,14 @@ public class WxScoreRulesServiceImpl implements WxScoreRulesService { | |||
| } else { | |||
| wxScoreRulesMapper.updateByPrimaryKeySelective(record); | |||
| } | |||
| } | |||
| @Override | |||
| public void deleteById(Long id) { | |||
| wxScoreRulesMapper.deleteByPrimaryKey(id); | |||
| // 缓存存在,删除缓存 | |||
| String key = keyPrev + record.getTenantId(); | |||
| boolean hasKey = redisUtils.hasKey(key); | |||
| if (hasKey) { | |||
| redisUtils.delete(key); | |||
| logger.info("WxScoreRulesServiceImpl.saveOrUpdate() : 从缓存中删除成长值设置 >> " + record.toString()); | |||
| } | |||
| } | |||
| private void updateScore(Long userId, int addedScoreNumber) { | |||
| @@ -0,0 +1,95 @@ | |||
| package com.iformall.utils; | |||
| import com.alibaba.fastjson.JSON; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.data.redis.core.*; | |||
| import org.springframework.stereotype.Component; | |||
| import javax.annotation.Resource; | |||
| import java.util.concurrent.TimeUnit; | |||
| /** | |||
| * Redis工具类 | |||
| * | |||
| * @author chenshun | |||
| * @email sunlightcs@gmail.com | |||
| * @date 2017-07-17 21:12 | |||
| */ | |||
| @Component | |||
| public class RedisUtils { | |||
| @Autowired | |||
| private RedisTemplate redisTemplate; | |||
| @Resource(name="redisTemplate") | |||
| private ValueOperations<String, String> valueOperations; | |||
| @Resource(name="redisTemplate") | |||
| private HashOperations<String, String, Object> hashOperations; | |||
| @Resource(name="redisTemplate") | |||
| private ListOperations<String, Object> listOperations; | |||
| @Resource(name="redisTemplate") | |||
| private SetOperations<String, Object> setOperations; | |||
| @Resource(name="redisTemplate") | |||
| private ZSetOperations<String, Object> zSetOperations; | |||
| /** 默认过期时长,单位:秒 */ | |||
| public final static long DEFAULT_EXPIRE = 60 * 60 * 24; | |||
| /** 不设置过期时长 */ | |||
| public final static long NOT_EXPIRE = -1; | |||
| public void set(String key, Object value, long expire){ | |||
| valueOperations.set(key, toJson(value)); | |||
| if(expire != NOT_EXPIRE){ | |||
| redisTemplate.expire(key, expire, TimeUnit.SECONDS); | |||
| } | |||
| } | |||
| public void set(String key, Object value){ | |||
| set(key, value, DEFAULT_EXPIRE); | |||
| } | |||
| public <T> T get(String key, Class<T> clazz, long expire) { | |||
| String value = valueOperations.get(key); | |||
| if(expire != NOT_EXPIRE){ | |||
| redisTemplate.expire(key, expire, TimeUnit.SECONDS); | |||
| } | |||
| return value == null ? null : fromJson(value, clazz); | |||
| } | |||
| public <T> T get(String key, Class<T> clazz) { | |||
| return get(key, clazz, NOT_EXPIRE); | |||
| } | |||
| public String get(String key, long expire) { | |||
| String value = valueOperations.get(key); | |||
| if(expire != NOT_EXPIRE){ | |||
| redisTemplate.expire(key, expire, TimeUnit.SECONDS); | |||
| } | |||
| return value; | |||
| } | |||
| public String get(String key) { | |||
| return get(key, NOT_EXPIRE); | |||
| } | |||
| public boolean hasKey(String key) {return redisTemplate.hasKey(key); } | |||
| public void delete(String key) { | |||
| redisTemplate.delete(key); | |||
| } | |||
| /** | |||
| * Object转成JSON数据 | |||
| */ | |||
| private String toJson(Object object){ | |||
| if(object instanceof Integer || object instanceof Long || object instanceof Float || | |||
| object instanceof Double || object instanceof Boolean || object instanceof String){ | |||
| return String.valueOf(object); | |||
| } | |||
| return JSON.toJSONString(object); | |||
| } | |||
| /** | |||
| * JSON数据,转成Object | |||
| */ | |||
| private <T> T fromJson(String json, Class<T> clazz){ | |||
| return JSON.parseObject(json, clazz); | |||
| } | |||
| } | |||
| @@ -0,0 +1,45 @@ | |||
| package com.iformall.utils; | |||
| import org.springframework.beans.BeansException; | |||
| import org.springframework.context.ApplicationContext; | |||
| import org.springframework.context.ApplicationContextAware; | |||
| import org.springframework.stereotype.Component; | |||
| /** | |||
| * Spring Context 工具类 | |||
| * | |||
| * @author chenshun | |||
| * @email sunlightcs@gmail.com | |||
| * @date 2016年11月29日 下午11:45:51 | |||
| */ | |||
| @Component | |||
| public class SpringContextUtils implements ApplicationContextAware { | |||
| public static ApplicationContext applicationContext; | |||
| @Override | |||
| public void setApplicationContext(ApplicationContext applicationContext) | |||
| throws BeansException { | |||
| SpringContextUtils.applicationContext = applicationContext; | |||
| } | |||
| public static Object getBean(String name) { | |||
| return applicationContext.getBean(name); | |||
| } | |||
| public static <T> T getBean(String name, Class<T> requiredType) { | |||
| return applicationContext.getBean(name, requiredType); | |||
| } | |||
| public static boolean containsBean(String name) { | |||
| return applicationContext.containsBean(name); | |||
| } | |||
| public static boolean isSingleton(String name) { | |||
| return applicationContext.isSingleton(name); | |||
| } | |||
| public static Class<? extends Object> getType(String name) { | |||
| return applicationContext.getType(name); | |||
| } | |||
| } | |||