1. Service层的实现
1. ShopService接口
/**
* 根据shopCondition分页返回相应列表数据
* @param shopCondition
* @param pageIndex
* @param pageSize
* @return
*/
public ShopExecution getShopList(
Shop shopCondition,int pageIndex,int pageSize);
2. ShopServiceImpl实现类
封装util用于获取rowIndex:
public class PageCaculator {
/**
* 从第(pageIndex-1)*pageSize个数据开始选取pageSize个数据
* @param pageIndex 页码
* @param pageSize 选取的数据
* @return
*/
public static int calculateRowIndex(int pageIndex,int pageSize){
return (pageIndex>0)?(pageIndex-1)*pageSize:0;
}
}
@Override
public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
int rowIndex =
PageCaculator.calculateRowIndex(pageIndex,pageSize);
List<Shop> shopList =
shopDao.queryShopList(shopCondition,rowIndex,pageSize);
int count = shopDao.queryShopCount(shopCondition);
ShopExecution se = new ShopExecution();
if(shopList!=null){
se.setShopList(shopList);
se.setCount(count);
}else {
se.setState(ShopStateEnum.INNER_ERROR.getState());
}
return se;
}
3. ShopServiceTest测试类
@Test
public void testGetShopList(){
Shop shopCondition = new Shop();
ShopCategory sc = new ShopCategory();
sc.setShopCategoryId(3L);
shopCondition.setShopCategory(sc);
ShopExecution shopExecution
= shopService.getShopList(shopCondition,1,2);
System.out.println("店铺列表数为:"+shopExecution.getShopList().size());
System.out.println("店铺列表总数为:"+shopExecution.getCount());
}
2. Controller层的实现
/**
*
*
* @Title: getShopList
*
* @Description: 从session中获取当前person拥有的商铺列表
*
* @param request
* @return
*
* @return: Map<String,Object>
*/
@RequestMapping(value = "/getshoplist", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopList(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 现在还没有做登录模块,因此session中并没有用户的信息,先模拟一下登录 要改造TODO
PersonInfo personInfo = new PersonInfo();
personInfo.setUserId(2L);
personInfo.setName("茶花女");
request.getSession().setAttribute("user", personInfo);
// 从session中获取user信息
personInfo = (PersonInfo) request.getSession().getAttribute("user");
try {
Shop shopCondition = new Shop();
shopCondition.setOwner(personInfo);
ShopExecution se = shopService.getShopList(shopCondition, 1, 99);
modelMap.put("success", true);
modelMap.put("shopList", se.getShopList());
modelMap.put("user", personInfo);
} catch (ShopOperationException e) {
e.printStackTrace();
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
}
return modelMap;
}
/**
*
*
* @Title: shopManagement
*
* @Description: 从商铺列表页面中,点击“进入”按钮进入
* 某个商铺的管理页面的时候,对session中的数据的校验从而进行页面的跳转,是否跳转到店铺列表页面或者可以直接操作该页面
*
* 访问形式如下
* http://ip:port/o2o/shopadmin/shopmanagement?shopId=xxx
*
* @return
*
* @return: Map<String,Object>
*/
@RequestMapping(value = "/getshopmanageInfo", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getShopManageInfo(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 获取shopId
long shopId = HttpServletRequestUtil.getLong(request, "shopId");
// 如果shopId不合法
if (shopId < 0) {
// 尝试从当前session中获取
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
if (currentShop == null) {
// 如果当前session中也没有shop信息,告诉view层 重定向
modelMap.put("redirect", true);
modelMap.put("url", "/o2o/shopadmin/shoplist");
}else{
// 告诉view层 进入该页面
modelMap.put("redirect", false);
modelMap.put("shopId", currentShop.getShopId());
}
} else { // shopId合法的话
Shop shop = new Shop();
shop.setShopId(shopId);
// 将currentShop放到session中
request.getSession().setAttribute("currentShop", shop);
modelMap.put("redirect", false);
}
return modelMap;
}