shop--7.店铺信息编辑--Service层(包括更新店铺,对照片的处理)
Service
1 /** 2 * 通过店铺Id获取店铺信息 3 * 4 *@param shopId 5 *@param shopImgInputStream 6 *@return 7 * 8 */ 9 Shop getByShopId(long shopId); 10 11 /** 12 * 更新店铺信息,包括对图片的处理 13 * 14 *@param shop 15 *@param shopImgInputStream 16 *@param fileName 17 * 18 */ 19 ShopExecution modifyShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;
ServiceImpl
1 @Override 2 public Shop getByShopId(long shopId) { 3 return shopDao.queryByShopId(shopId); 4 } 5 @Override 6 public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream, String fileName) 7 throws ShopOperationException { 8 if(shop ==null || shop.getShopId() == null) { 9 return new ShopExecution(ShopStateEnum.NULL_SHOP); 10 }else { 11 try { 12 //1.判断是否需要处理图片 13 if(shopImgInputStream != null && fileName!=null &&!"".equals(fileName)) {//是否有图片传进来 14 Shop tempShop = shopDao.queryByShopId(shop.getShopId()); 15 //判断之前的店铺信息中是否有shop图片,如果有的话,就先删除,没有的话,就直接加进去 16 if(tempShop.getShopImg()!=null) { 17 ImageUtil.deleteFileOrPath(tempShop.getShopImg()); 18 } 19 addShopImg(shop, shopImgInputStream, fileName); 20 } 21 //2.更新店铺信息 22 shop.setLastEditTime(new Date()); 23 int effectdNum = shopDao.updateShop(shop); 24 if(effectdNum <= 0) { 25 return new ShopExecution(ShopStateEnum.INNER_ERROR); 26 }else { 27 shop = shopDao.queryByShopId(shop.getShopId()); 28 return new ShopExecution(ShopStateEnum.SUCCESS,shop); 29 }}catch (Exception e) { 30 throw new ShopOperationException("modifyShop error: " + e.getMessage()); 31 } 32 } 33 34 }
工具类ImageUtil中新增的delete
1 /** 2 * 更新店铺时候用的 需要删除图片 3 * storePath是文件的路径还是目录的路径 4 * 如果storePath是文件路径则删除该文件 5 * 如果storePath是目录路径则删除目录下的所有文件 6 *@param storePath 7 */ 8 public static void deleteFileOrPath(String storePath) { 9 File fileOrPath = new File(PathUtil.getImgBasePath() + storePath); 10 if(fileOrPath.exists()) { 11 if(fileOrPath.isDirectory()) {//如果是目录,就删除所有的文件 12 File files[] = fileOrPath.listFiles(); 13 for(int i = 0;i <files.length;i++) { 14 files[i].delete(); 15 } 16 } 17 fileOrPath.delete(); 18 } 19 }