@Service
public class ShopServiceImpl implements ShopService {
@Autowired
private ShopDao shopDao;
@Override
@Transactional
public ShopExecution addShop(Shop shop, File shopImg) {
if (shop == null) {
return new ShopExecution(ShopStateEnum.NULL_SHOP);
}
try {
shop.setEnableStatus(0);
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
int effectedNum = shopDao.insertShop(shop);
if (effectedNum <= 0) {
throw new RuntimeException("店铺创建失败");
} else {
if (shopImg != null) {
try {
// 存储图片
addShopImg(shop, shopImg);
} 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 addShopImg(Shop shop, File shopImg) {
// 获取shop图片目录的相对自路径
String dest = PathUtil.getShopImagePath(shop.getShopId());
String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest);
shop.setShopImg(shopImgAddr);
}
}public class ShopOperationException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 8385422554610647351L;
public ShopOperationException(String msg) {
super(msg);
}
}
SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需
了解课程