<file_sep>package com.lusheng.bookcrossing.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lusheng.bookcrossing.dao.BaseDao;
import com.lusheng.bookcrossing.model.PointsChangeRecord;
import com.lusheng.bookcrossing.service.PointsChangeRecordService;
@Service("pointsChangeRecordService")
public class PointsChangeRecordServiceImpl extends BaseServiceImpl<PointsChangeRecord> implements PointsChangeRecordService{

	@Resource(name="pointsChangeRecordDao")
	@Override
	public void setDao(BaseDao<PointsChangeRecord> dao) {
		super.setDao(dao);
	}
	@Override
	public List<PointsChangeRecord> getPointsChangeRecordsByUidHasPage(
			Integer uid, int pageNum, int pageSize) {
		String hql="from PointsChangeRecord p where p.user.uid =? order by p.time desc";
		return this.findEntityHasPageByHQL(hql, (pageNum-1)*pageSize, pageSize, uid);
	}
	@Override
	public int getPointsChangeRecordsCountByUid(Integer uid) {
		String hql="select count(*) from PointsChangeRecord p where p.user.uid =?";
		return (int)(long) this.uniqueResult(hql, uid);
	}

}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.User;
@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<User>{

}
<file_sep>package com.lusheng.bookcrossing.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lusheng.bookcrossing.dao.BaseDao;
import com.lusheng.bookcrossing.model.DriftRecord;
import com.lusheng.bookcrossing.service.DriftRecordService;

@Service("driftRecordService")
public class DriftRecordServiceImpl extends BaseServiceImpl<DriftRecord>
		implements DriftRecordService {
	@Resource(name = "driftRecordDao")
	@Override
	public void setDao(BaseDao<DriftRecord> dao) {
		super.setDao(dao);
	}

	@Override
	public DriftRecord getDriftingRecordByBid(Integer bid) {
		String hql = "from DriftRecord d where d.book.bid=? and d.status=?";
		List<DriftRecord> driftRecords = this.findEntityByHQL(hql, bid,
				DriftRecord.DRIFTING_STATUS);
		if (driftRecords == null || driftRecords.size() == 0) {
			return null;
		}
		return driftRecords.get(0);
	}

	@Override
	public DriftRecord getLastDriftedRecordByBid(Integer bid) {
		String hql = "from DriftRecord d where d.book.bid=? and d.status=? order by d.startTime desc";
		List<DriftRecord> driftRecords = this.findEntityByHQL(hql, bid,
				DriftRecord.DRIFTED_STATUS);
		if (driftRecords == null || driftRecords.size() == 0) {
			return null;
		}
		return driftRecords.get(0);
	}

	@Override
	public List<DriftRecord> getDriftRecordsByBidHasPage(Integer bid,
			int pageNum, int pageSize) {
		String hql = "from DriftRecord d where d.book.bid=?";
		return this.findEntityHasPageByHQL(hql, (pageNum - 1) * pageSize,
				pageSize, bid);
	}

	@Override
	public int getDriftRecordsCountByBid(Integer bid) {
		String hql = "select count(*) from DriftRecord d where d.book.bid=?";
		return (int) (long) this.uniqueResult(hql, bid);
	}

	@Override
	public List<DriftRecord> getDriftRecordsByUidHasPageHasPage(Integer uid, int pageNum,
			int pageSize) {
		String hql="from DriftRecord d where d.preUser.uid=? or d.nextUser.uid=?";
		return this.findEntityHasPageByHQL(hql, (pageNum - 1) * pageSize,
				pageSize, uid,uid);
	}

	@Override
	public int getDriftRecordsCountByUid(Integer uid) {
		String hql = "select count(*) from DriftRecord d where d.preUser.uid=? or d.nextUser.uid=?";
		return (int) (long) this.uniqueResult(hql, uid,uid);
	}

}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.Review;
@Repository("reviewDao")
public class ReviewDaoImpl extends BaseDaoImpl<Review>{

}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.DriftRecord;
@Repository("driftRecordDao")
public class DriftRecordDaoImpl extends BaseDaoImpl<DriftRecord>{

}
<file_sep>package com.lusheng.bookcrossing.uitls;

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonUtils {
	private static final String FLAG_KEY = "flag";
	private static final String DATA_KEY = "data";
	public static final String ERROR_CODE_KEY = "errorCode";
	public static final String ERROR_STR_KEY = "errorStr";
	private static  Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

	public static String getSuccessMsg(Object data) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put(FLAG_KEY, true);
		if (data != null) {
			map.put(DATA_KEY, data);
		}
		return gson.toJson(map);
	}

	public static String getErrorMsg(int errorCode, String errorStr) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put(FLAG_KEY, false);
		map.put(ERROR_CODE_KEY, errorCode);
		map.put(ERROR_STR_KEY, errorStr);
		return gson.toJson(map);
	}
	public static String getServerBugErrorMsg(){
		return getErrorMsg(Config.SERVER_BUG_ERROR_CODE, Config.SERVER_BUG_ERROR_STR);
	}
}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.Book;
@Repository("bookDao")
public class BookDaoImpl extends BaseDaoImpl<Book>{
}
<file_sep>package com.lusheng.bookcrossing.uitls;
import java.util.UUID;
public class UUIDUtils {
	public  static String randomUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
}
<file_sep>package com.lusheng.bookcrossing.action.manager;

public interface IManagerAction {
	String BASE_PATH = "manager";
	String ERROR = "error";
	String SUCCESS = "success";
	String SUCCESS_CODE = "1";
	String ERROR_CODE = "0";
}
<file_sep>package com.lusheng.bookcrossing.action.mobile;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lusheng.bookcrossing.model.Book;
import com.lusheng.bookcrossing.model.DriftRecord;
import com.lusheng.bookcrossing.model.User;
import com.lusheng.bookcrossing.service.BookService;
import com.lusheng.bookcrossing.service.DriftRecordService;
import com.lusheng.bookcrossing.uitls.Config;
import com.lusheng.bookcrossing.uitls.JsonUtils;
import com.lusheng.bookcrossing.uitls.ParameterUtils;
@Controller
@RequestMapping("mobile")
public class DriftRecordAction {
	@Autowired
	private BookService bookService;
	@Autowired
	private DriftRecordService driftRecordService;
	@ResponseBody
	@RequestMapping("/lookDriftRecordByBid")
	public String lookDriftRecordByBid(HttpServletRequest request){
		Integer bid=ParameterUtils.getIntNoCareException(request, "bid", -1);
		int pageNum=ParameterUtils.getIntNoCareException(request, "pageNum", 1);
		int pageSize=ParameterUtils.getIntNoCareException(request, "pageSize", 10);
		if(bid==-1){
			return JsonUtils.getErrorMsg(Config.INVALID_PRAMETERS_ERROR_CODE, "bid格式不对");
		}
		Book book=bookService.getEntity(bid);
		if(book==null){
			return JsonUtils.getErrorMsg(Config.OTHER_ERROR_CODE, "不存在此漂书");
		}
		int count=driftRecordService.getDriftRecordsCountByBid(bid);
		pageSize=pageSize==0?10:pageSize;
		int pageCount=(count-1)/pageSize+1;
		List<DriftRecord> driftRecords=driftRecordService.getDriftRecordsByBidHasPage(bid, pageNum, pageSize);
		Map<String,Object> map=new LinkedHashMap<String, Object>();
		map.put("pageNum", pageNum);
		map.put("pageCount", pageCount);
		map.put("driftRecords", wrapDriftRecords(driftRecords));
		return JsonUtils.getSuccessMsg(map);
	}
	@ResponseBody
	@RequestMapping("/lookMyDriftRecord")
	public String lookMyDriftRecord(HttpServletRequest request){
		Integer uid=(Integer) request.getAttribute("uid");
		int pageNum=ParameterUtils.getIntNoCareException(request, "pageNum", 1);
		int pageSize=ParameterUtils.getIntNoCareException(request, "pageSize", 10);
		int count=driftRecordService.getDriftRecordsCountByUid(uid);
		pageSize=pageSize==0?10:pageSize;
		int pageCount=(count-1)/pageSize+1;
		List<DriftRecord> driftRecords=driftRecordService.getDriftRecordsByUidHasPageHasPage(uid, pageNum, pageSize);
		Map<String,Object> map=new LinkedHashMap<String, Object>();
		map.put("pageNum", pageNum);
		map.put("pageCount", pageCount);
		map.put("driftRecords", wrapDriftRecords(driftRecords));
		return JsonUtils.getSuccessMsg(map);
	}
	private List<DriftRecordWrapper> wrapDriftRecords(List<DriftRecord> driftRecords){
		List<DriftRecordWrapper> driftRecordWrappers=new ArrayList<DriftRecordAction.DriftRecordWrapper>();
		for(DriftRecord driftRecord:driftRecords){
			driftRecordWrappers.add(new DriftRecordWrapper(driftRecord));
		}
		return driftRecordWrappers;
	}
	private static class DriftRecordWrapper extends DriftRecord{
		public Integer preUid;
		public String preUsername;
		public Integer nextUid;
		public String nextUsername;
		public Integer bid;
		public String bookname;
		public DriftRecordWrapper(DriftRecord driftRecord){
			setDrid(driftRecord.getDrid());
			setStartTime(driftRecord.getStartTime());
			setEndTime(driftRecord.getEndTime());
			setStatus(driftRecord.getStatus());
			User preUser=driftRecord.getPreUser();
			if(preUser!=null){
				preUid=preUser.getUid();
				preUsername=preUser.getUsername();
			}
			User nextUser=driftRecord.getNextUser();
			if(nextUser!=null){
				nextUid=nextUser.getUid();
				nextUsername=nextUser.getUsername();
			}
			Book book = driftRecord.getBook();
			if(book!=null){
				bid = book.getBid();
				bookname=book.getBookname();
			}
		}
	}
	
}
<file_sep>package com.lusheng.bookcrossing.model;

import java.util.ArrayList;
import java.util.List;

public class Category {
	private Integer cid;
	private String cname;
	private Category parent;
	private List<Category> subCategories=new ArrayList<Category>();
	public List<Category> getSubCategories() {
		return subCategories;
	}
	public void setSubCategories(List<Category> subCategories) {
		this.subCategories = subCategories;
	}
	public Integer getCid() {
		return cid;
	}
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Category getParent() {
		return parent;
	}
	public void setParent(Category parent) {
		this.parent = parent;
	}
	@Override
	public String toString() {
		return "Category [cid=" + cid + ", cname=" + cname + ", parent="
				+ parent + "]";
	}
}
<file_sep>package com.lusheng.bookcrossing.action.manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lusheng.bookcrossing.model.Manager;
import com.lusheng.bookcrossing.service.ManagerService;
import com.lusheng.bookcrossing.uitls.ParameterUtils;
import com.lusheng.bookcrossing.uitls.TextUtils;

@RequestMapping("manager")
@Controller
public class ManagePersonInfoAction implements IManagerAction{
	@Autowired
	private ManagerService managerService;
	@RequestMapping("/login")
	public String login(HttpServletRequest request) {
		String accountNumber = ParameterUtils.getTrimString(request, "accountNumber");
		String password = ParameterUtils.getTrimString(request, "password");
		if (TextUtils.isEmpty(accountNumber)) {
			request.setAttribute(ERROR, "账号不能为空");
			return "forward:/manager/login.jsp";
		}
		if (TextUtils.isEmpty(password)) {
			request.setAttribute(ERROR, "密码不能为空");
			return "forward:/manager/login.jsp";
		}
		Manager manager = managerService.login(accountNumber, password);
		if (manager != null) {
			request.getSession().setAttribute("manager", manager);
			return BASE_PATH + "/home";
		} else {
			request.setAttribute(ERROR, "账号或密码不正确");
			return "forward:/manager/login.jsp";
		}
	}

	@RequestMapping("/home")
	public String goHome() {
		return BASE_PATH + "/home";
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/manager/login.jsp";
	}

	@RequestMapping(value = "/changePassword", method = RequestMethod.GET)
	public String goChangePassword() {
		return BASE_PATH + "/changePassword";
	}

	@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
	public String changePassword(HttpServletRequest request) {
		String oldPwd = ParameterUtils.getTrimString(request, "oldPwd");
		String newPwd = ParameterUtils.getTrimString(request, "newPwd");
		if (TextUtils.isEmpty(oldPwd)) {
			request.setAttribute(ERROR, "原始密码不能为空");
			return BASE_PATH + "/changePassword";
		}
		if (TextUtils.isEmpty(newPwd)) {
			request.setAttribute(ERROR, "新密码不能为空");
			return BASE_PATH + "/changePassword";
		}
		Manager manager = (Manager) request.getSession()
				.getAttribute("manager");
		if (!TextUtils.equals(manager.getPassword(), oldPwd)) {
			request.setAttribute(ERROR, "原始密码错误");
			return BASE_PATH + "/changePassword";
		}
		if (TextUtils.equals(oldPwd, newPwd)) {
			request.setAttribute(ERROR, "新密码不能与原始密码相同");
			return BASE_PATH + "/changePassword";
		}
		try {
			managerService.changePassword(manager.getMid(), newPwd);
			request.setAttribute(SUCCESS, "密码修改成功");
			manager.setPassword(newPwd);
		} catch (Exception e) {
			request.setAttribute(ERROR, "密码修改失败");
		}
		return BASE_PATH + "/changePassword";
	}
}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.PointsChangeRecord;
@Repository("pointsChangeRecordDao")
public class PointsChangeRecordDaoImpl extends BaseDaoImpl<PointsChangeRecord>{

}
<file_sep>package com.lusheng.bookcrossing.service;

import java.util.List;

import com.lusheng.bookcrossing.model.Review;

public interface ReviewService extends BaseService<Review>{
	List<Review> getReviewsByBid(Integer bid);
	List<Review> getReviewsByUidHasPage(Integer uid,int pageNum,int pageSize);
	int getReviewsCountByUid(Integer uid);
	void deleteReviewByRid(Integer rid);
	void praiseReview(Integer rid,Integer uid);
	void trampleReview(Integer rid,Integer uid);
}
<file_sep>package com.lusheng.bookcrossing.service;

import com.lusheng.bookcrossing.model.PraiseTrampleRecord;

public interface PraiseTrampleRecordService extends BaseService<PraiseTrampleRecord>{
	PraiseTrampleRecord getPraiseTrampleRecordByUidAndRid(Integer uid,Integer rid);
}
<file_sep>package com.lusheng.bookcrossing.dao.impl;

import org.springframework.stereotype.Repository;

import com.lusheng.bookcrossing.model.PraiseTrampleRecord;
@Repository("praiseTrampleRecordDao")
public class PraiseTrampleRecordDaoImpl extends BaseDaoImpl<PraiseTrampleRecord>{

}
<file_sep>package com.lusheng.bookcrossing.action.manager;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lusheng.bookcrossing.model.Book;
import com.lusheng.bookcrossing.model.Category;
import com.lusheng.bookcrossing.model.User;
import com.lusheng.bookcrossing.service.BookService;
import com.lusheng.bookcrossing.service.CategoryService;
import com.lusheng.bookcrossing.uitls.ParameterUtils;
import com.lusheng.bookcrossing.uitls.TextUtils;
import com.lusheng.bookcrossing.uitls.UUIDUtils;

@RequestMapping("manager")
@Controller
public class ManageBookAction implements IManagerAction{
	private static final int PAGE_SIZE = 5;
	@Autowired
	private BookService bookService;
	@Autowired
	private CategoryService categoryService;
	@RequestMapping("/shareBook")
	public void shareBook(HttpServletRequest request){
		Book book;
		User user=new User();
		user.setUid(22);
		String imagePath="/file/image/avatar/test.jpg";
		for(int i=0;i<100;i++){
			book=new Book();
			book.setAuthor("作者"+i);
			book.setBookcrossingId(UUIDUtils.randomUUID());
			book.setBookname("书名"+i);
			book.setShareUser(user);
			book.setImagePath(imagePath);
			book.setPress("出版社"+i);
			book.setPublishTime(new Date(System.currentTimeMillis()-i*1000*60*60*24));
			book.setShareTime(new Date(System.currentTimeMillis()-i*1000000));
			book.setStatus(Book.NO_AUDIT);
			bookService.saveEntity(book);
		}
	}
	@RequestMapping("/auditBook")
	public String auditBook(HttpServletRequest request){
		int status=ParameterUtils.getIntNoCareException(request, "status", Book.NO_AUDIT);
		int pageNum=ParameterUtils.getIntNoCareException(request, "pageNum", 1);
		List<Book> books=bookService.getBooksByStatusHasPage(status, pageNum, PAGE_SIZE);
		int bookCount=bookService.getBookCountByStatus(status);
		int pageCount=(bookCount-1)/PAGE_SIZE+1;
		request.setAttribute("status", status);
		request.setAttribute("books", books);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("pageCount", pageCount);
		if(status==Book.NO_AUDIT){
			List<Category> categories = categoryService.getFirstLevelCategories();
			request.setAttribute("categories", categories);
		}
		return BASE_PATH+"/auditBook";
	}
	@RequestMapping("/auditPass")
	public void auditPass(HttpServletRequest request,PrintWriter out){
		int bid;
		int cid;
		try {
			bid=ParameterUtils.getInt(request, "bid");
			cid=ParameterUtils.getInt(request, "cid");
		} catch (Exception e) {
			out.write(ERROR_CODE);
			out.flush();
			return;
		}
		try {
			bookService.auditPass(bid, cid);
			out.write(SUCCESS_CODE);
		} catch (Exception e) {
			out.write(ERROR_CODE);
		}
		out.flush();
	}
	@RequestMapping("/auditNoPass")
	public void auditNoPass(HttpServletRequest request,PrintWriter out){
		String nopassReason=ParameterUtils.getTrimString(request, "nopassReason");
		int bid;
		try {
			bid=ParameterUtils.getInt(request, "bid");
		} catch (Exception e) {
			out.write(ERROR_CODE);
			out.flush();
			return;
		}
		try {
			bookService.auditNoPass(bid, nopassReason);
			out.write(SUCCESS_CODE);
		} catch (Exception e) {
			out.write(ERROR_CODE);
		}
		out.flush();
	}
	
	@RequestMapping("/changeCategory")
	public void changeCategory(HttpServletRequest request,PrintWriter out){
		int bid;
		int cid;
		try {
			bid=ParameterUtils.getInt(request, "bid");
			cid=ParameterUtils.getInt(request, "cid");
		} catch (Exception e) {
			out.write(ERROR_CODE);
			out.flush();
			return;
		}
		try {
			bookService.changeCategory(bid, cid);
			out.write(SUCCESS_CODE);
		} catch (Exception e) {
			out.write(ERROR_CODE);
		}
		out.flush();
	}
	@RequestMapping("/searchBook")
	public String searchBook(HttpServletRequest request){
		String searchContent=ParameterUtils.getTrimString(request, "searchContent");
		int searchFieldCode=ParameterUtils.getIntNoCareException(request, "searchFieldCode", Book.SEARCH_BOOKCROSSINGID_FIELD_CODE);
		int searchType=ParameterUtils.getIntNoCareException(request, "searchType", Book.ACCURATE_SEARCH_TPYE);
		int pageNum=ParameterUtils.getIntNoCareException(request, "pageNum", 1);
		List<Book> books=new ArrayList<Book>();
		int pageCount = 0;
		if(!TextUtils.isEmpty(searchContent)){
			if(searchFieldCode==Book.SEARCH_BOOKCROSSINGID_FIELD_CODE){
				Book book=bookService.getPassAuditBookByBookcrossingId(searchContent);
				if(book!=null){
					books.add(book);
					if(books.size()==1){
						pageCount=1;
					}
				}
			}else if(searchFieldCode==Book.SEARCH_BOOKNAME_FIELD_CODE){
				books.addAll(bookService.getPassAuditBooksByNameHasPage(searchContent, searchType, pageNum, PAGE_SIZE));
				pageCount=(bookService.getPassAuditBooksCountByName(searchContent, searchType)-1)/PAGE_SIZE+1;
			}
		}
		request.setAttribute("searchContent", searchContent);
		request.setAttribute("searchFieldCode", searchFieldCode);
		request.setAttribute("searchType", searchType);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("books", books);
		request.setAttribute("pageCount", pageCount);
		return BASE_PATH+"/searchBook";
	}
	@RequestMapping("/listBook")
	public String listBook(HttpServletRequest request){
		int pid=ParameterUtils.getIntNoCareException(request, "pid", -1);
		int cid=ParameterUtils.getIntNoCareException(request, "cid", -1);
		int pageNum=ParameterUtils.getIntNoCareException(request, "pageNum", 1);
		List<Category> categories=categoryService.getFirstLevelCategories();
		List<Category> subCategories = null;
		List<Book> books = null;
		int pageCount = 0;
		if(pid!=-1&&cid!=-1){
			subCategories=categoryService.getSecondLevelCategories(pid);
			books=bookService.getPassAuditBooksByCategoryHasPage(cid, pageNum, PAGE_SIZE);
			pageCount=(bookService.getPassAuditBooksCountByCategory(cid)-1)/PAGE_SIZE+1;
		}
		request.setAttribute("pid", pid);
		request.setAttribute("cid", cid);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("categories", categories);
		request.setAttribute("subCategories", subCategories);
		request.setAttribute("books", books);
		request.setAttribute("pageCount", pageCount);
		return BASE_PATH+"/listBook";
	}
	@RequestMapping("/bookInfo")
	public String bookInfo(HttpServletRequest request){
		int bid=ParameterUtils.getIntNoCareException(request, "bid", 0);
		Book book=bookService.getEntity(bid);
		List<Category> categories=categoryService.getFirstLevelCategories();
		request.setAttribute("book", book);
		request.setAttribute("categories", categories);
		return BASE_PATH+"/bookInfo";
	}
}
<file_sep>package com.lusheng.bookcrossing.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lusheng.bookcrossing.dao.BaseDao;
import com.lusheng.bookcrossing.model.PraiseTrampleRecord;
import com.lusheng.bookcrossing.model.Review;
import com.lusheng.bookcrossing.model.User;
import com.lusheng.bookcrossing.service.ReviewService;
import com.lusheng.bookcrossing.service.UserService;
@Service("reviewService")
public class ReviewServiceImpl extends BaseServiceImpl<Review> implements ReviewService{
	@Autowired
	private UserService userService;
	@Resource(name="praiseTrampleRecordDao")
	private BaseDao<PraiseTrampleRecord> praiseTrampleRecordDao;
	@Resource(name="reviewDao")
	@Override
	public void setDao(BaseDao<Review> dao) {
		super.setDao(dao);
	}

	@Override
	public List<Review> getReviewsByBid(Integer bid) {
		String hql="from Review r where r.book.bid=?";
		return this.findEntityByHQL(hql, bid);
	}

	@Override
	public List<Review> getReviewsByUidHasPage(Integer uid, int pageNum,
			int pageSize) {
		String hql="from Review r where r.user.uid=? order by r.time desc";
		return this.findEntityHasPageByHQL(hql, (pageNum-1)*pageSize, pageSize, uid);
	}

	@Override
	public int getReviewsCountByUid(Integer uid) {
		String hql="select count(*) from Review r where r.user.uid=?";
		return (int)(long) this.uniqueResult(hql, uid);
	}

	@Override
	public void deleteReviewByRid(Integer rid) {
		String hql1="delete from PraiseTrampleRecord p where p.review.rid=?";
		this.batchEntityByHQL(hql1, rid);
		String hql2="delete from Review r where r.rid=?";
		this.batchEntityByHQL(hql2, rid);
	}

	@Override
	public void praiseReview(Integer rid, Integer uid) {
		Review review=this.getEntity(rid);
		if(review==null){
			return;
		}
		review.setPraiseNum(review.getPraiseNum()+1);
		if(review.getPraiseNum()==10){
			userService.changePointsByUid(review.getUser().getUid(), 20,"对于漂书"+review.getBook().getBookname()+"的书评得到10个赞");
		}
		PraiseTrampleRecord praiseTrampleRecord=new PraiseTrampleRecord();
		praiseTrampleRecord.setReview(review);
		User user=new User();
		user.setUid(uid);
		praiseTrampleRecord.setUser(user);
		praiseTrampleRecord.setType(PraiseTrampleRecord.PRAISE_TYPE);
		praiseTrampleRecordDao.saveEntity(praiseTrampleRecord);
	}

	@Override
	public void trampleReview(Integer rid, Integer uid) {
		Review review=this.getEntity(rid);
		if(review==null){
			return;
		}
		review.setTrampleNum(review.getTrampleNum()+1);
		if(review.getTrampleNum()==10){
			userService.changePointsByUid(review.getUser().getUid(), -20,"对于漂书"+review.getBook().getBookname()+"的书评得到10个踩");
		}
		PraiseTrampleRecord praiseTrampleRecord=new PraiseTrampleRecord();
		praiseTrampleRecord.setReview(review);
		User user=new User();
		user.setUid(uid);
		praiseTrampleRecord.setUser(user);
		praiseTrampleRecord.setType(PraiseTrampleRecord.TRAMPLE_TYPE);
		praiseTrampleRecordDao.saveEntity(praiseTrampleRecord);
	}
}
<file_sep># bookcrossing
毕业设计
