zoukankan      html  css  js  c++  java
  • shop--7.店铺编辑和列表--更新店铺的信息,包括对店铺照片的处理,根据shopId获取shop信息

    /**
         * 根据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<String, Object> getShopById(HttpServletRequest request){
            Map<String, Object> modelMap = new HashMap<>();
            Long shopId = HttpServletRequestUtil.getLong( request, "shopId" );
            if(shopId > -1){
                try{
                    Shop shop = shopService.getShopByShopId( shopId );
                    List<Area> 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<String, Object> modifyShop(HttpServletRequest request){
            Map<String, Object> 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;
        }
    

      

  • 相关阅读:
    【linux就该这么学】-05
    【linux就该这么学】-04
    【linux就该这么学】-03
    【linux就该这么学】-02
    【linux就该这么学】-01
    【linux就该这么学】-00
    MySQL57安装与设置
    Docker(一) Docker入门教程
    Centos 7.X 安装及常规设置
    删除数组里所有与给定值相同的值
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8888898.html
Copyright © 2011-2022 走看看