校园商铺平台项目(8)- 店铺注册

1 店铺返回类型

1.1 店铺状态枚举

package com.tzb.o2o.enums;

public enum ShopStateEnum {
     
    CHECK(0, "审核中"), OFFLINE(-1, "非法店铺"),
    SUCCESS(1, "操作成功"), PASS(2, "通过认证"),
    INNER_ERROR(-1001, "内部系统错误"),
    NULL_SHOPID(-1002,"ShopId为空"),
    NULL_SHOP(-1003,"shop信息为空");

    private int state;
    private String stateInfo;

    private ShopStateEnum(int state, String stateInfo) {
     
        this.state = state;
        this.stateInfo = stateInfo;
    }

    public int getState() {
     
        return state;
    }

    public String getStateInfo() {
     
        return stateInfo;
    }

    /**
    * 依据传入的state 返回相应的 enum 值
    * */
    public static ShopStateEnum stateOf(int state){
     
        for(ShopStateEnum shopStateEnum:values()){
     
           if(shopStateEnum.getState() == state){
     
               return shopStateEnum;
           }
        }
        return null;
    }
}

1.2 店铺操作状态类

package com.tzb.o2o.dto;

import com.tzb.o2o.entity.Shop;
import com.tzb.o2o.enums.ShopStateEnum;

import java.util.List;

public class ShopExecution {
     
    // 结果状态
    private int state;

    // 状态标识
    private String stateInfo;

    // 店铺的数量
    private int count;

    // 操作的店铺(增删改店铺时用)
    private Shop shop;

    // shop列表(查询店铺列表时用)
    private List<Shop> shopList;

    public ShopExecution() {
     
    }

    // 店铺操作失败时使用的构造器
    public ShopExecution(ShopStateEnum stateEnum ) {
     
        this.state = stateEnum.getState();
        this.stateInfo = stateEnum.getStateInfo();
    }

    // 店铺操作成功时的构造器
    public ShopExecution(ShopStateEnum stateEnum,Shop shop){
     
        this.state = stateEnum.getState();
        this.stateInfo = stateEnum.getStateInfo();
        this.shop = shop;
    }

    // 店铺操作成功时的构造器
    public ShopExecution(ShopStateEnum stateEnum,List<Shop> shopList){
     
        this.state = stateEnum.getState();
        this.stateInfo = stateEnum.getStateInfo();
        this.shopList = shopList;
    }

    public int getState() {
     
        return state;
    }

    public void setState(int state) {
     
        this.state = state;
    }

    public String getStateInfo() {
     
        return stateInfo;
    }

    public void setStateInfo(String stateInfo) {
     
        this.stateInfo = stateInfo;
    }

    public int getCount() {
     
        return count;
    }

    public void setCount(int count) {
     
        this.count = count;
    }

    public Shop getShop() {
     
        return shop;
    }

    public void setShop(Shop shop) {
     
        this.shop = shop;
    }

    public List<Shop> getShopList() {
     
        return shopList;
    }

    public void setShopList(List<Shop> shopList) {
     
        this.shopList = shopList;
    }
}

1.3 自定义店铺操作异常类

-RunTimeException, 发生异常时,数据库会进行回滚
校园商铺平台项目(8)- 店铺注册_第1张图片

package com.tzb.o2o.exceptions;

public class ShopOperationException extends RuntimeException {
     
    public ShopOperationException(String msg) {
     
        super(msg);
    }
}

1.4 店铺操作 Service

校园商铺平台项目(8)- 店铺注册_第2张图片

package com.tzb.o2o.service;

import com.tzb.o2o.dto.ShopExecution;
import com.tzb.o2o.entity.Shop;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public interface ShopService {
     
    ShopExecution addShop(Shop shop, CommonsMultipartFile shopImg);
}

package com.tzb.o2o.service.impl;

import com.tzb.o2o.dao.ShopDao;
import com.tzb.o2o.dto.ShopExecution;
import com.tzb.o2o.entity.Shop;
import com.tzb.o2o.enums.ShopStateEnum;
import com.tzb.o2o.exceptions.ShopOperationException;
import com.tzb.o2o.service.ShopService;
import com.tzb.o2o.util.ImageUtil;
import com.tzb.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.Date;

@Service
public class ShopServiceImpl implements ShopService {
     

    @Autowired
    private ShopDao shopDao;

    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, CommonsMultipartFile 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 ShopOperationException("店铺创建失败");
            }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,  CommonsMultipartFile shopImg) {
     
        // 获取shop 图片目录的相对值路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest);
        shop.setShopImg(shopImgAddr);
    }
}

1.4.1 店铺操作测试类

校园商铺平台项目(8)- 店铺注册_第3张图片

<build>

        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    
                    <include>**/*.*include>
                includes>
            resource>
            <resource>
                <directory>src/test/resourcesdirectory>
                <includes>
                    
                    <include>**/*.*include>
                includes>
            resource>
        resources>

public class ShopServiceTest extends BaseTest {
     

    @Autowired
    private ShopService shopService;

    @Test
    public void testAddShop() throws FileNotFoundException {
     
        Shop shop = new Shop();
        PersonInfo owner = new PersonInfo();
        Area area = new Area();
        ShopCategory shopCategory = new ShopCategory();
        owner.setUserId(1L);
        area.setAreaId(2);
        shopCategory.setShopCategoryId(1L);
        shop.setOwner(owner);
        shop.setArea(area);
        shop.setShopCategory(shopCategory);
        shop.setShopName("测试的店铺3");
        shop.setShopDesc("测试店铺的描述信息3");
        shop.setShopAddr("测试店铺地址3");
        shop.setPhone("8888");
        shop.setCreateTime(new Date());
        shop.setEnableStatus(ShopStateEnum.CHECK.getState());
        shop.setAdvice("审核中");

        File shopImg = new File("D:\\test.jpg");

        InputStream is = new FileInputStream(shopImg);

        ShopExecution se = shopService.addShop(shop,is,shopImg.getName());

        assertEquals(ShopStateEnum.CHECK.getState(),se.getState());

    }


}

校园商铺平台项目(8)- 店铺注册_第4张图片
校园商铺平台项目(8)- 店铺注册_第5张图片

1.5 店铺管理 Controller

1.5.1 HttpServletRequest工具类

package com.tzb.o2o.util;

import javax.servlet.http.HttpServletRequest;

public class HttpServletRequestUtil {
     

    public static int getInt(HttpServletRequest request, String key) {
     
        try {
     
            return Integer.decode(request.getParameter(key));
        } catch (Exception e) {
     
            return -1;
        }
    }

    public static long getLong(HttpServletRequest request, String key) {
     
        try {
     
            return Long.valueOf(request.getParameter(key));
        } catch (Exception e) {
     
            return -1;
        }
    }

    public static Double getDouble(HttpServletRequest request, String key) {
     
        try {
     
            return Double.valueOf(request.getParameter(key));
        } catch (Exception e) {
     
            return -1d;
        }
    }

    public static boolean getBoolean(HttpServletRequest request, String key) {
     
        try {
     
            return Boolean.valueOf(request.getParameter(key));
        } catch (Exception e) {
     
            return false;
        }
    }

    public static String getString(HttpServletRequest request, String key) {
     
        try {
     
            String result = request.getParameter(key);
            if (null != result) {
     
                result = result.trim();
            }
            if ("".equals(result)) {
     
                result = null;
            }
            return result;
        } catch (Exception e) {
     
            return null;
        }
    }
}

1.5.2 店铺管理控制器

 
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.8.7version>
        dependency>
@Controller
@RequestMapping("shopadmin")
public class ShopManagementController {
     

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "registershop", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> registerShop(HttpServletRequest request) {
     

        Map<String, Object> modelMap = new HashMap<>();

        // 1.接收并转化相应的参数,包括店铺信息和图片信息
        // shopStr,和前端约定好的变量
        String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;

        try {
     
            shop = mapper.readValue(shopStr, Shop.class);
        } catch (Exception 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 ,和前段约定好的变量
            shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        } else {
     
            modelMap.put("success", false);
            modelMap.put("errMsg", "上传图片不能为空");
            return modelMap;
        }

        // 2. 注册店铺
        if (null != shop && null != shopImg) {
     
            PersonInfo owner = new PersonInfo();

            // session TODO
            owner.setUserId(1L);
            shop.setOwner(owner);

            ShopExecution se = null;
            try {
     
                se = shopService.addShop(shop, shopImg.getInputStream(), shopImg.getOriginalFilename());
                if (se.getState() == ShopStateEnum.CHECK.getState()) {
     
                    modelMap.put("success", true);
                } else {
     
                    modelMap.put("success", false);
                    modelMap.put("errMsg", se.getStateInfo());
                }
            } catch (IOException e) {
     
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
                return modelMap;
            }
            return modelMap;
        } else {
     
            modelMap.put("success", false);
            modelMap.put("errMsg", "请输入店铺信息");
            return modelMap;
        }
        // 3.返回结果
    }

   /* private static void inputStreamToFile(InputStream ins, File file) {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = ins.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            throw new RuntimeException("调用 inputStreamToFile 产生异常:" + e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (ins != null) {
                    ins.close();
                }
            } catch (Exception e) {
                throw new RuntimeException("调用 inputStreamToFile 产生IO异常:" + e.getMessage());
            }
        }
    }*/
}

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