GoodsServiceImpl.java 3.4 KB
Newer Older
Y
yingjun 已提交
1 2 3 4 5 6 7 8 9
package com.yingjun.ssm.service.impl;

import com.yingjun.ssm.cache.RedisCache;
import com.yingjun.ssm.dao.GoodsDao;
import com.yingjun.ssm.dao.OrderDao;
import com.yingjun.ssm.dao.UserDao;
import com.yingjun.ssm.entity.Goods;
import com.yingjun.ssm.entity.User;
import com.yingjun.ssm.enums.ResultEnum;
Y
yingjun 已提交
10
import com.yingjun.ssm.exception.BizException;
Y
yingjun 已提交
11
import com.yingjun.ssm.service.GoodsService;
Y
yingjun 已提交
12 13 14 15 16 17 18 19 20 21
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Y
yingjun 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

@Service
public class GoodsServiceImpl implements GoodsService {

	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private RedisCache cache;

	@Override
	public List<Goods> getGoodsList(int offset, int limit) {
Y
yingjun 已提交
38 39
		String cache_key = RedisCache.CAHCENAME + "|getGoodsList|" + offset + "|" + limit;
		List<Goods> result_cache = cache.getListCache(cache_key, Goods.class);
Y
yingjun 已提交
40 41 42
		if (result_cache != null) {
			LOG.info("get cache with key:" + cache_key);
		} else {
Y
yingjun 已提交
43 44
			// 缓存中没有再去数据库取,并插入缓存(缓存时间为60秒)
			result_cache = goodsDao.queryAll(offset, limit);
Y
yingjun 已提交
45
			cache.putListCacheWithExpireTime(cache_key, result_cache, RedisCache.CAHCETIME);
Y
yingjun 已提交
46
			LOG.info("put cache with key:" + cache_key);
Y
yingjun 已提交
47 48 49 50 51 52 53
			return result_cache;
		}
		return result_cache;
	}

	@Transactional
	@Override
Y
yingjun 已提交
54
	public void buyGoods(long userPhone, long goodsId, boolean useProcedure) {
Y
yingjun 已提交
55 56 57
		// 用户校验
		User user = userDao.queryByPhone(userPhone);
		if (user == null) {
Y
yingjun 已提交
58
			throw new BizException(ResultEnum.INVALID_USER.getMsg());
Y
yingjun 已提交
59 60
		}
		if (useProcedure) {
Y
yingjun 已提交
61
			// 通过存储方式的方法进行操作
Y
yingjun 已提交
62 63 64 65 66 67
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", user.getUserId());
			map.put("goodsId", goodsId);
			map.put("title", "抢购");
			map.put("result", null);
			goodsDao.bugWithProcedure(map);
Y
yingjun 已提交
68
			int result = MapUtils.getInteger(map, "result", -1);
Y
yingjun 已提交
69 70
			if (result <= 0) {
				// 买卖失败
Y
yingjun 已提交
71
				throw new BizException(ResultEnum.INNER_ERROR.getMsg());
Y
yingjun 已提交
72
			} else {
Y
yingjun 已提交
73
				// 买卖成功
Y
yingjun 已提交
74 75 76 77 78 79 80 81 82 83
				// 此时缓存中的数据不是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定)
				cache.deleteCacheWithPattern("getGoodsList*");
				LOG.info("delete cache with key: getGoodsList*");
				return;
			}
		} else {

			int inserCount = orderDao.insertOrder(user.getUserId(), goodsId, "普通买卖");
			if (inserCount <= 0) {
				// 买卖失败
Y
yingjun 已提交
84
				throw new BizException(ResultEnum.DB_UPDATE_RESULT_ERROR.getMsg());
Y
yingjun 已提交
85 86 87 88 89
			} else {
				// 减库存
				int updateCount = goodsDao.reduceNumber(goodsId);
				if (updateCount <= 0) {
					// 减库存失败
Y
yingjun 已提交
90
					throw new BizException(ResultEnum.DB_UPDATE_RESULT_ERROR.getMsg());
Y
yingjun 已提交
91 92 93 94 95 96 97 98 99 100 101 102
				} else {
					// 买卖成功
					// 此时缓存中的数据不再是最新的,需要对缓存进行清理(具体的缓存策略还是要根据具体需求制定)
					cache.deleteCacheWithPattern("getGoodsList*");
					LOG.info("delete cache with key: getGoodsList*");
					return;
				}
			}
		}
	}

}