审计记录

src/com/w1hd/zzhnc/service/GoodsService.java 3.9 KB
zxt@theyeasy.com committed
1 2
package com.w1hd.zzhnc.service;

zxt@theyeasy.com committed
3 4 5 6 7
import java.sql.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
zxt@theyeasy.com committed
8
import org.springframework.data.redis.core.RedisTemplate;
zxt@theyeasy.com committed
9
import org.springframework.stereotype.Service;
zxt@theyeasy.com committed
10 11
import org.testng.util.Strings;

zxt@theyeasy.com committed
12
import com.google.common.collect.Lists;
zxt@theyeasy.com committed
13 14 15
import com.w1hd.zzhnc.dao.GoodsDao;
import com.w1hd.zzhnc.model.Goods;
import com.w1hd.zzhnc.util.PageResults;
zxt@theyeasy.com committed
16
import com.w1hd.zzhnc.util.RedisUtil;
zxt@theyeasy.com committed
17 18 19

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
zxt@theyeasy.com committed
20 21 22 23

@Service
public class GoodsService {

zxt@theyeasy.com committed
24 25 26
	@Autowired
	GoodsDao goodsDao;

zxt@theyeasy.com committed
27
	final String GOODSID_ = RedisUtil.PROJECTNAME.concat("_").concat("goods");
zxt@theyeasy.com committed
28

zxt@theyeasy.com committed
29 30 31
	public List<Goods> getHomeData() {
		Example ex = new Example(Goods.class);
		ex.createCriteria().andEqualTo("deleted", false).andEqualTo("isHome", true);
32
		ex.setOrderByClause("page_views desc");
33 34
		RowBounds r = new RowBounds(0, 10);
		return goodsDao.selectByExample(ex);
zxt@theyeasy.com committed
35 36
	}

zxt@theyeasy.com committed
37
	@SuppressWarnings("unchecked")
zxt@theyeasy.com committed
38 39 40 41
	public Goods update(Goods goods) {
		goods.setUpdateTime(new Date(System.currentTimeMillis()));
		if (goods.getId() == null || goods.getId() == 0) {
			goods.setCreatetime(new Date(System.currentTimeMillis()));
zxt@theyeasy.com committed
42
			goods.setDeleted(false);
zxt@theyeasy.com committed
43 44
			goodsDao.insert(goods);
		} else {
zxt@theyeasy.com committed
45
			if (goods.getCreatetime() == null) {
zxt@theyeasy.com committed
46 47
				goods.setCreatetime(new java.util.Date());
			}
zxt@theyeasy.com committed
48
			goods.setDeleted(false);
zxt@theyeasy.com committed
49
			Goods old = goods(goods.getId()).getRows().get(0);
zxt@theyeasy.com committed
50
			if (old != null) {
zxt@theyeasy.com committed
51 52 53
				goods.setPageViews(old.getPageViews() + 1);
			}
			redisTemplate.delete(GOODSID_ + goods.getId());
zxt@theyeasy.com committed
54 55 56 57 58
			goodsDao.updateByPrimaryKey(goods);
		}
		return goods;
	}

zxt@theyeasy.com committed
59 60 61
	@Autowired
	RedisTemplate redisTemplate;

zxt@theyeasy.com committed
62 63
	public PageResults<Goods> seacrh(Integer id, String key, Integer sellerId, Integer page, Integer size,
			Integer sort) {
zxt@theyeasy.com committed
64 65
		Example ex = new Example(Goods.class);
		Criteria c = ex.createCriteria();
zxt@theyeasy.com committed
66
		if (id != null && id > 0) {
zxt@theyeasy.com committed
67
			return goods(id);
zxt@theyeasy.com committed
68 69
		} else {
			if (!Strings.isNullOrEmpty(key)) {
zxt@theyeasy.com committed
70 71 72
				if ("副食".equals(key) || "小商品".equals(key) || "服装".equals(key) || "汽摩配件".equals(key)) {
					c.andEqualTo("categoryName", key);
				} else {
zxt@theyeasy.com committed
73 74
					c.andCondition(" (description like \"%" + key + "%\" or name like \"%" + key
							+ "%\" or seller_name like \"%" + key + "%\" or seller_address like \"%" + key + "%\" )");
zxt@theyeasy.com committed
75 76
				}

zxt@theyeasy.com committed
77 78 79 80
			}
			if (sellerId != null && sellerId > 0) {
				c.andEqualTo("sellerId", sellerId);
			}
zxt@theyeasy.com committed
81
		}
zxt@theyeasy.com committed
82

zxt@theyeasy.com committed
83
		c.andEqualTo("deleted", false);
zxt@theyeasy.com committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		switch (sort) {
		case 1:// 根据更新时间排序
			ex.setOrderByClause("update_time desc");
			break;
		case 2:// 根据转发数排序
			ex.setOrderByClause("share_count desc");
			break;
		case 3:// 根据浏览数排序
			ex.setOrderByClause("page_views desc");
			break;
		default:
			ex.setOrderByClause("update_time desc,share_count desc,page_views desc");
			break;
		}

99
		RowBounds row = new RowBounds((page - 1) * size, size);
zxt@theyeasy.com committed
100
		List<Goods> list = goodsDao.selectByExampleAndRowBounds(ex, row);
zxt@theyeasy.com committed
101
		int count = goodsDao.selectCountByExample(ex);
zxt@theyeasy.com committed
102
		PageResults<Goods> pageResults = new PageResults<>();
103 104 105 106
		pageResults.setTotal(count);
		pageResults.setPage(page);
		pageResults.setPageSize(size);
		pageResults.setRows(list);
zxt@theyeasy.com committed
107 108 109
		return pageResults;
	}

zxt@theyeasy.com committed
110 111 112 113 114 115 116 117
	@SuppressWarnings("unchecked")
	public PageResults<Goods> goods(Integer id) {
		PageResults<Goods> pageResults = new PageResults<>();
		pageResults.setTotal(1);
		pageResults.setPage(1);
		pageResults.setPageSize(10);
		List<Goods> list = Lists.newArrayList();
		Goods goods = (Goods) redisTemplate.opsForValue().get(GOODSID_ + id);
zxt@theyeasy.com committed
118
		if (goods == null) {
zxt@theyeasy.com committed
119 120 121 122 123 124 125 126 127
			goods = goodsDao.selectByPrimaryKey(id);
		}
		goods.setPageViews(goods.getPageViews() + 1);
		redisTemplate.opsForValue().set(GOODSID_ + id, goods);
		list.add(goods);
		pageResults.setRows(list);
		return pageResults;
	}

128 129 130 131
	public Object delete(Integer id) {
		return goodsDao.deleteByPrimaryKey(id) > 0;
	}

zxt@theyeasy.com committed
132
}