博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shop--7.店铺编辑和列表--更新店铺的信息,包括对店铺照片的处理,根据shopId获取shop信息...
阅读量:4671 次
发布时间:2019-06-09

本文共 5465 字,大约阅读时间需要 18 分钟。

/**     * 根据shopId获取shop信息     * @param shopId     * @return     */    public Shop getShopByShopId(long shopId);    /**     * 更新店铺的信息,包括对店铺照片的处理     * @param shop     * @param shopImg     * @return     */    public ShopExecution modifyShop(Shop shop, CommonsMultipartFile shopImg);

 

serviceImpl实现

 

@Override    public Shop getShopByShopId(long shopId) {        return shopDao.queryByShopId( shopId );    }    @Override    public ShopExecution modifyShop(Shop shop, CommonsMultipartFile shopImg) {        if(shop == null || shop.getShopId() == null){            return new ShopExecution(ShopStateEnum.NULL_SHOP);        } else{            try{                //1.判断是否需要修改图片                if(shopImg != null){                    Shop tempShop = shopDao.queryByShopId( shop.getShopId() );                    //判断之前的店铺信息中是否有shop图片,如果有的话,就先删除,没有的话,就直接加进去                    if(tempShop != null){                        ImageUtil.deleteFileOrPath( tempShop.getShopImg() );                    }                    addShopImg( shop, shopImg );                }                //2.更新店铺信息                shop.setLastEditTime( new Date(  ) );                int effectNum = shopDao.updateShop( shop );                if(effectNum <= 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());            }        }    }

  

工具类ImageUtil中新增的delete

/**     *storePath是文件路径  或  目录路径     * 如果storePath是文件路径,则删除文件     * 如果storePath是目录路径,则删除该目录下的所有文件     * @param storePath     */    public static void deleteFileOrPath(String storePath){       File fileOrPath = new File(PathUtil.getImgBasePath() + storePath);       if(fileOrPath.exists()){           if(fileOrPath.isDirectory()){               File files[] = fileOrPath.listFiles();               for(File file : files){                   file.delete();               }           }           fileOrPath.delete();       }    }

  

controller层的实现

其中,修改店铺信息的代码与注册店铺信息的代码相似

 

/**     * 根据店铺信息获取店铺     * @param request     * @return     */    @RequestMapping(value="/getshopbyid", method=RequestMethod.GET)    @ResponseBody    private Map
getShopById(HttpServletRequest request){ Map
modelMap = new HashMap<>(); Long shopId = HttpServletRequestUtil.getLong( request, "shopId" ); if(shopId > -1){ try{ Shop shop = shopService.getShopByShopId( shopId ); List
areaList = areaService.getAreaList(); modelMap.put("shop", shop); modelMap.put( "areaList", areaList ); modelMap.put( "success", true ); } catch(Exception e){ modelMap.put("success", false); modelMap.put("errMsg", e.toString()); } }else{ modelMap.put("success", false); modelMap.put( "errMsg", "empty shopId" ); } return modelMap; } /** * 修改店铺信息 * @param request * @return */ @RequestMapping(value="/modifyshop", method= RequestMethod.POST ) @ResponseBody public Map
modifyShop(HttpServletRequest request){ Map
modelMap = new HashMap<>(); //判断验证码是否正确 if(!CodeUtil.checkVerifyCode(request)){ modelMap.put( "success", false ); modelMap.put( "errMsg", "验证码错误!!!!!" ); return modelMap; } //1.接收并转化相应的参数,包括店铺信息和图片信息 String shopStr = HttpServletRequestUtil.getString( request, "shopStr" ); ObjectMapper mapper = new ObjectMapper(); // create once, reuse Shop shop = null; try { shop = mapper.readValue(shopStr, Shop.class); } catch (IOException e) { modelMap.put("success", false); modelMap.put("errMsg", e.getMessage()); return modelMap; } CommonsMultipartFile shopImg = null; //在本次会话的上下文获取上传的文件 CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //如果文件上传的有值 if(commonsMultipartResolver.isMultipart( request )){ MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request; shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg"); } //2.注册店铺 if(shop != null && shop.getShopId() != null){ try{ ShopExecution shopExecution = shopService.modifyShop(shop, shopImg); if(shopExecution.getState() == ShopStateEnum.SUCCESS.getState()){ modelMap.put( "success", true ); }else{ modelMap.put( "success", false ); modelMap.put( "errMsg", shopExecution.getStateInfo()); } } catch(RuntimeException e){ modelMap.put("success", false); modelMap.put("errMsg", e.toString()); } }else{ modelMap.put("success", false); modelMap.put("errMsg", "请输入店铺Id"); } //3.返回结果 return modelMap; }

  

 

转载于:https://www.cnblogs.com/SkyeAngel/p/8888898.html

你可能感兴趣的文章
[Algorithm] Find first missing positive integer
查看>>
[Angular] @ViewChild and template #refs to get Element Ref
查看>>
[Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
查看>>
[Angular] Configurable Angular Components - Content Projection and Input Templates
查看>>
[PWA] 17. Cache the photo
查看>>
[RxJS] ReplaySubject with buffer
查看>>
[Firebase] 3. Firebase Simple Login Form
查看>>
AI 线性代数
查看>>
ltp-ddt realtime_cpu_load涉及的cyclictest 交叉编译
查看>>
MySQL中Checkpoint技术
查看>>
【MT】牛津的MT教程
查看>>
Meta标签中的format-detection属性及含义
查看>>
PowerDesigner教程系列(四)概念数据模型
查看>>
DataGradView操作之,列头右键菜单隐藏和显示字段功能
查看>>
windows中使用Git工具连接GitHub(配置篇)
查看>>
示例 - 10行代码在C#中获取页面元素布局信息
查看>>
Linux 发行版本简介 (zz)
查看>>
POJ 2387 Til the Cows Come Home(Dijkstra)
查看>>
关于使用Tomcat服务器出现413错误的解决办法(Request Entity Too Large)
查看>>
离线使用iPhone SDK文档的方法
查看>>