1. Service层
为什么返回shopExecution?
原因:需要将这个shopList还有它的count整合在一起返回。而shopExecution正好满足了我们的条件。
package com.csj2018.o2o.service;
import java.io.InputStream;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.exceptions.ShopOperationException;
import com.csj2018.o2o.dto.ShopExecution;
public interface ShopService {
/**
* 根据shopCondition分页返回相应店铺列表
* @param shopCondition
* @param pageIndex
* @param pageSize
* @return
*/
public ShopExecution getShopList(Shop shopCondition,int pageIndex,int pageSize);
/**
* 通过店铺ID获取店铺信息
* @param shopId
* @return
*/
Shop getByShopId(long shopId);
/**
* 更新店铺信息,包括对图片的处理
* @param shop
* @param shopImgInputStream
* @param fileName
* @return
*/
ShopExecution modifyShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;
/**
* 注册店铺信息,包括图片处理
* @param shop
* @param shopImgInputStream
* @param fileName
* @return
*/
ShopExecution addShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;
}
2. Service实现类
2.1 参数转换
Dao层第2个参数是rowIndex,而Service是pageIndex。因为前端只认页数,而Dao层只认行数。因此这里需要做一个转换。
编写一个工具类
package com.csj2018.o2o.util;
public class PageCalculator {
public static int calculatorRowIndex(int pageIndex,int pageSize) {
return (pageIndex > 0)?(pageIndex - 1) * pageSize:0;
//同 if(pageIndex>0) {
// return pageIndex - 1;
// }else {
// return 0;
// }
}
}
当pageIndex为0和1时,rowIndex为0
2.2 实现类
package com.csj2018.o2o.service.impl;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
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 com.csj2018.o2o.dao.ShopDao;
import com.csj2018.o2o.dto.ShopExecution;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.enums.ShopStateEnum;
import com.csj2018.o2o.exceptions.ShopOperationException;
import com.csj2018.o2o.service.ShopService;
import com.csj2018.o2o.util.ImageUtil;
import com.csj2018.o2o.util.PageCalculator;
import com.csj2018.o2o.util.PathUtil;
@Service
public class ShopServiceImpl implements ShopService {
private Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);
@Autowired
private ShopDao shopDao;
@Override
public ShopExecution getShopList(Shop shopCondition,int pageIndex,int pageSize) {
int rowIndex = PageCalculator.calculatorRowIndex(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;
}
@Override
@Transactional
public ShopExecution addShop(Shop shop, InputStream shopImgInputStream, String fileName) {
// 控制判断,shop是不是包含必需的值
if (shop == null) {
logger.warn("shop== null");
return new ShopExecution(ShopStateEnum.NUll_SHOP);
}
// 增加对Shop其他引入类非空的判断
try {
// 给店铺信息赋初始值
shop.setEnableStatus(0);
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
// 添加店铺信息
int effectedNum = shopDao.insertShop(shop);
logger.warn("添加结果:" + effectedNum + "shopId:" + shop.getShopId());
if (effectedNum <= 0) {
throw new ShopOperationException("店铺创建失败");
} else {
if (shopImgInputStream != null) {
// 存储图片
try {
addShopImage(shop, shopImgInputStream, fileName);
} catch (Exception e) {
throw new ShopOperationException("addShopImg error:" + e.getMessage());
}
// 更新店铺的图片信息
effectedNum = shopDao.updateShop(shop);
if (effectedNum <= 0) {
throw new ShopOperationException("更新图片地址失败");
}
}
}
} catch (Exception e) {
throw new ShopOperationException("addShop error:" + e.getMessage());
}
return new ShopExecution(ShopStateEnum.CHECK, shop);
}
private void addShopImage(Shop shop, InputStream shopImg, String fileName) {
// 获取shop图片目录的相对路径
String dest = PathUtil.getShopImagePath(shop.getShopId());
String shopImgAddr = ImageUtil.generateThumbnail(shopImg, fileName, dest);
shop.setShopImg(shopImgAddr);
}
@Override
public Shop getByShopId(long shopId) {
return shopDao.queryByShopId(shopId);
}
@Override
public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream, String fileName)
throws ShopOperationException {
if (shop == null || shop.getShopId() == null) {
return new ShopExecution(ShopStateEnum.NUll_SHOP);
} else {
try {
// 1.判断是否需要处理图片
if (shopImgInputStream != null && fileName != null && !"".equals(fileName)) {
Shop tempShop = shopDao.queryByShopId(shop.getShopId());
if (tempShop.getShopImg() != null) {
//编写工具类,删除图片信息
ImageUtil.deleteFileOfPath(tempShop.getShopImg());
}
addShopImage(shop, shopImgInputStream, fileName);
}
// 2.更新店铺信息
shop.setLastEditTime(new Date());
int effectedNum = shopDao.updateShop(shop);
if (effectedNum <= 0) {
return new ShopExecution(ShopStateEnum.INNER_ERROR);
} else {
shop = shopDao.queryByShopId(shop.getShopId());
return new ShopExecution(ShopStateEnum.SUCCESS, shop);
}
} catch (Exception e) {
throw new ShopOperationException("modifyShop error:" + e.getMessage());
}
}
}
}
3. 单元测试
package com.csj2018.o2o.service;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.dto.ShopExecution;
import com.csj2018.o2o.entity.Area;
import com.csj2018.o2o.entity.PersonInfo;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.entity.ShopCategory;
import com.csj2018.o2o.enums.ShopStateEnum;
import com.csj2018.o2o.exceptions.ShopOperationException;
public class ShopServiceTest extends BaseTest{
@Autowired
private ShopService shopService;
@Test
public void testGetShopList() {
Shop shopCondition = new Shop();
ShopCategory sc = new ShopCategory();
sc.setShopCategoryId(1L);
shopCondition.setShopCategory(sc);
ShopExecution se = shopService.getShopList(shopCondition, 1, 5);
System.out.println("店铺列表总数:"+se.getShopList().size());
System.out.println("店铺总数为:"+se.getCount());
}
@Test
@Ignore
public void testModifyShop() throws ShopOperationException,FileNotFoundException{
Shop shop = new Shop();
shop.setShopId(1L);
shop.setShopName("修改后的店铺名称");
// File shopImg = new File("/Users/chenshanju/Downloads/liuchuanfeng.jpeg");
InputStream is = new FileInputStream("/Users/chenshanju/Downloads/liuchuanfeng.jpeg");
ShopExecution shopExecution = shopService.modifyShop(shop, is, "流川枫.jpeg");
System.out.println("新的图片地址:"+shopExecution.getShop().getShopImg());
}
@Test
@Ignore
public void testAddShop() throws FileNotFoundException {
Shop shop = new Shop();
PersonInfo owner = new PersonInfo();
Area area = new Area();
ShopCategory shopCategory = new ShopCategory();
owner.setUserId(1L);
area.setAreaId(2);
shopCategory.setShopCategoryId(1L);
shop.setOwner(owner);
shop.setArea(area);
shop.setShopCategory(shopCategory);
shop.setShopName("测试de店铺4");
shop.setShopDesc("店铺描述4");
shop.setShopAddr("测试路4号1");
shop.setPhone("1234567892");
shop.setPriority(4);
shop.setCreateTime(new Date());
shop.setEnableStatus(ShopStateEnum.CHECK.getState());
shop.setAdvice("审核中");
File shopImg = new File("/Users/chenshanju/Downloads/cat.jpg");
InputStream is = new FileInputStream(shopImg);
ShopExecution se = shopService.addShop(shop, is,shopImg.getName());
assertEquals(ShopStateEnum.CHECK.getState(), se.getState());
}
}
原文地址:https://www.cnblogs.com/csj2018/p/12230517.html
时间: 2024-11-09 16:30:20