admin
2019-11-19 3890e3b68a50b491e649b9431371668c31339edc
添加原子方法调用
2个文件已修改
2个文件已添加
78 ■■■■ 已修改文件
fanli/src/main/java/com/yeshi/fanli/manager/util/AtomMethodCallInterface.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/manager/util/AtomMethodCallManager.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/mapping/integral/UserGetIntegralFromOrderRecordMapper.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/UserGetIntegralFromOrderRecordServiceImpl.java 31 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/manager/util/AtomMethodCallInterface.java
New file
@@ -0,0 +1,5 @@
package com.yeshi.fanli.manager.util;
public interface AtomMethodCallInterface {
    public void excute();
}
fanli/src/main/java/com/yeshi/fanli/manager/util/AtomMethodCallManager.java
New file
@@ -0,0 +1,40 @@
package com.yeshi.fanli.manager.util;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.yeshi.fanli.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
public class AtomMethodCallManager {
    @Resource
    private JedisPool jedisPool;
    /**
     * 事件执行
     *
     * @param key
     *            事件唯一标识,要与具体的业务逻辑相结合
     * @param call
     */
    public void excute(String key, AtomMethodCallInterface call) {
        String redisKey = "atom-" + StringUtil.Md5(key);
        Jedis jedis = jedisPool.getResource();
        try {
            if (jedis.setnx(redisKey, "1") <= 0) {
                return;
            }
            jedis.expire(redisKey, 60);
            call.excute();
        } finally {
            jedis.del(redisKey);
            jedisPool.returnResource(jedis);
        }
    }
}
fanli/src/main/java/com/yeshi/fanli/mapping/integral/UserGetIntegralFromOrderRecordMapper.xml
@@ -90,7 +90,7 @@
    <select id="countByRecordInfo" resultType="java.lang.Long">
    <select id="countByRecordInfo" resultType="java.lang.Long" useCache="false" flushCache="true">
        select
        count(*)
        from yeshi_ec_integral_get_from_order where 1=1
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/UserGetIntegralFromOrderRecordServiceImpl.java
@@ -10,14 +10,22 @@
import com.yeshi.fanli.dao.mybatis.integral.UserGetIntegralFromOrderRecordMapper;
import com.yeshi.fanli.entity.integral.UserGetIntegralFromOrderRecord;
import com.yeshi.fanli.exception.integral.UserGetIntegralFromOrderRecordException;
import com.yeshi.fanli.manager.util.AtomMethodCallInterface;
import com.yeshi.fanli.manager.util.AtomMethodCallManager;
import com.yeshi.fanli.service.inter.user.integral.UserGetIntegralFromOrderRecordService;
import com.yeshi.fanli.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class UserGetIntegralFromOrderRecordServiceImpl implements UserGetIntegralFromOrderRecordService {
    @Resource
    private UserGetIntegralFromOrderRecordMapper userGetIntegralFromOrderRecordMapper;
    @Resource
    private AtomMethodCallManager atomMethodCallManager;
    @Override
    public long countByEventTypeAndUidAndOrderInfo(int eventType, Long uid, String orderNo, int orderSourceType) {
@@ -36,14 +44,23 @@
                || StringUtil.isNullOrEmpty(record.getOrderNo()) || record.getOrderSourceType() == null)
            throw new UserGetIntegralFromOrderRecordException(1, "信息不完整");
        long count = countByEventTypeAndUidAndOrderInfo(record.getEventType(), record.getUid(), record.getOrderNo(),
                record.getOrderSourceType());
        if (count > 0)
            throw new UserGetIntegralFromOrderRecordException(2, "记录已存在");
        String key = String.format("rs-%s",
                StringUtil.Md5(String.format("UserGetIntegralFromOrderRecord-addRecord-%s-%s-%s-%s", record.getUid(),
                        record.getEventType(), record.getOrderNo(), record.getOrderSourceType())));
        if (record.getCreateTime() == null)
            record.setCreateTime(new Date());
        userGetIntegralFromOrderRecordMapper.insertSelective(record);
        atomMethodCallManager.excute(key, new AtomMethodCallInterface() {
            @Override
            public void excute() {
                long count = countByEventTypeAndUidAndOrderInfo(record.getEventType(), record.getUid(),
                        record.getOrderNo(), record.getOrderSourceType());
                if (count > 0)
                    return;
                if (record.getCreateTime() == null)
                    record.setCreateTime(new Date());
                userGetIntegralFromOrderRecordMapper.insertSelective(record);
            }
        });
    }
}