校园商铺平台项目(13)-店铺信息编辑DAO和Service

1 店铺信息编辑DAO

package com.tzb.o2o.dao;

import com.tzb.o2o.entity.Shop;

public interface ShopDao {
     

    Shop queryByShopId(long shopId);

    /**
     * 新增店铺
     * @param shop
     * @return
     */
     int insertShop(Shop shop);

    /**
     * 更新店铺信息
     * @param shop
     * @return
     */
     int updateShop(Shop shop);

}

  • Shop.xml
    
    <resultMap id="shopMap" type="com.tzb.o2o.entity.Shop">
        
        <id column="shop_id" property="shopId" />
        
        <result column="shop_name" property="shopName" />
        <result column="shop_desc" property="shopDesc" />
        <result column="shop_addr" property="shopAddr" />
        <result column="phone" property="phone" />
        <result column="priority" property="priority"/>
        <result column="shop_img" property="shopImg" />
        <result column="create_time" property="createTime" />
        <result column="last_edit_time" property="lastEditTime" />
        <result column="enable_status" property="enableStatus" />
        <result column="advice" property="advice" />

        
        <association column="owner_id" property="owner" javaType="PersonInfo">
            <id column="user_id" property="userId" />
            <result column="name" property="name" />
        association>

        <association column="area_id" property="area" javaType="Area">
            <id column="area_id" property="areaId" />
            <result column="area_name" property="areaName" />
        association>

        <association property="shopCategory" column="shop_category_id" javaType="ShopCategory">
            <id column="shop_category_id" property="shopCategoryId" />
            <result column="shop_category_name" property="shopCategoryName" />
        association>
    resultMap>

    
    <select id="queryByShopId" resultMap="shopMap" parameterType="long">
        SELECT
        s.shop_id,
        s.shop_name,
        s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img,
        s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status,
        s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        WHERE
        s.area_id = a.area_id
        AND s.shop_category_id = sc.shop_category_id
        AND
        s.shop_id
        = #{shopId}
    select>

1.1 单元测试

在这里插入图片描述
校园商铺平台项目(13)-店铺信息编辑DAO和Service_第1张图片
校园商铺平台项目(13)-店铺信息编辑DAO和Service_第2张图片

2 店铺编辑 Service

package com.tzb.o2o.service;

import com.tzb.o2o.dto.ShopExecution;
import com.tzb.o2o.entity.Shop;
import com.tzb.o2o.exceptions.ShopOperationException;

import java.io.InputStream;

public interface ShopService {
     

    /**
     * 根据店铺id获取店铺信息
     * @param shopId
     * @return
     */
    Shop getByShopId(long shopId);

    /**
     * 更新店铺信息,包含对图片的处理
     * @param shop
     * @param shopImgInputStream
     * @param fileName
     * @return
     * @throws ShopOperationException
     */
    ShopExecution modifyShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;

    ShopExecution addShop(Shop shop, InputStream shopImgInputStream,String fileName);
}


  • ImageUtil.java
  /**
     * sortePath是文件的路径还是目录的路径
     * 若是文件路径,删除该文件
     * 若是目录路径,则删除目录下的所有文件
     *
     * @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();
        }
    }

  • ShopServieImpl.java
@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.deleteFileOrPath(tempShop.getShopImg());
                    }
                    addShopImg(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());
            }

        }

    }

2.1 单元测试

  • ShopServiceTest.java
   @Test
    public void testModifyShop() throws ShopOperationException, FileNotFoundException {
     
        Shop shop = new Shop();
        shop.setShopId(15L);
        shop.setShopName("修改后的店铺名称");
        File shopImg = new File("D://test2.jpg");
        InputStream is = new FileInputStream(shopImg);
        ShopExecution shopExecution = shopService.modifyShop(shop, is, "test2.jpg");
        System.out.println("新图片地址:" + shopExecution.getShop().getShopImg());

    }

校园商铺平台项目(13)-店铺信息编辑DAO和Service_第3张图片
在这里插入图片描述
校园商铺平台项目(13)-店铺信息编辑DAO和Service_第4张图片

你可能感兴趣的:(#,校园商铺平台项目,校园商铺,spring)