GoodsService.java 1.7 KB
package com.w1hd.zzhnc.service;

import java.sql.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.util.Strings;

import com.w1hd.zzhnc.dao.GoodsDao;
import com.w1hd.zzhnc.model.Goods;
import com.w1hd.zzhnc.util.PageResults;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class GoodsService {

	@Autowired
	GoodsDao goodsDao;

	public List<Goods> getHomeData() {
		Example ex = new Example(Goods.class);
		ex.createCriteria().andEqualTo("deleted", false).andEqualTo("isHome", true);
		RowBounds r = new RowBounds(0, 5);
		return goodsDao.selectByExampleAndRowBounds(ex, r);
	}

	public Goods update(Goods goods) {
		goods.setUpdateTime(new Date(System.currentTimeMillis()));
		if (goods.getId() == null || goods.getId() == 0) {
			goods.setCreatetime(new Date(System.currentTimeMillis()));
			goodsDao.insert(goods);
		} else {
			goodsDao.updateByPrimaryKey(goods);
		}
		return goods;
	}

	public PageResults<Goods> seacrh(String key, Integer sellerId, Integer page, Integer size) {
		Example ex = new Example(Goods.class);
		Criteria c = ex.createCriteria();
		if (!Strings.isNullOrEmpty(key)) {
			c.andCondition(" (description like \"%" + key + "%\" or name like \"%" + key + "%\")");
		}
		if (sellerId != null && sellerId > 0) {
			c.andEqualTo("sellerId", sellerId);
		}
		c.andEqualTo("deleted", false);
		RowBounds row = new RowBounds(page, size);
		List<Goods> list = goodsDao.selectByExampleAndRowBounds(ex, row);
		PageResults<Goods> pageResults = new PageResults<>();
		return pageResults;
	}

}