码迷,mamicode.com
首页 > 其他好文 > 详细

商铺项目(店铺注册功能模块(二))

时间:2017-10-18 19:58:45      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:set   red   package   enable   this   创建失败   ons   entity   blog   

技术分享

package com.ouyan.o2o.dto;

import java.util.List;

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

/**
 * 封装执行后结果
 */
public class ShopExecution {

    // 结果状态
    private int state;

    // 状态标识
    private String stateInfo;

    // 店铺数量
    private int count;

    // 操作的shop(增删改店铺的时候用)
    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;
    }

}

技术分享

package com.ouyan.o2o.enums;

/**
 * 使用枚举表述常量数据字典
 */
public enum ShopStateEnum {

    CHECK(0, "审核中"), OFFLINE(-1, "非法商铺"), SUCCESS(1, "操作成功"), PASS(2, "通过认证"), INNER_ERROR(
            -1001, "操作失败"), NULL_SHOPID(-1002, "ShopId为空"), NULL_SHOP_INFO(
            -1003, "传入了空的信息");

    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;
    }

    public static ShopStateEnum stateOf(int index) {
        for (ShopStateEnum state : values()) {
            if (state.getState() == index) {
                return state;
            }
        }
        return null;
    }

}

接下来咱们来实现service层(要注意只有抛出RuntimeException或者继承RuntimeException的异常时,事务才会终止):

技术分享

package com.ouyan.o2o.exceptions;

public class ShopOperationException extends RuntimeException{
    /**
     * 
     */
    private static final long serialVersionUID = -2174235994752246501L;

    public ShopOperationException(String msg){
        super(msg);
    }
}

技术分享

package com.ouyan.o2o.service;

import java.io.File;

import com.ouyan.o2o.dto.ShopExecution;
import com.ouyan.o2o.entity.Shop;

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

技术分享

package com.ouyan.o2o.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ouyan.o2o.dao.ShopDao;
import com.ouyan.o2o.dto.ShopExecution;
import com.ouyan.o2o.entity.Shop;
import com.ouyan.o2o.enums.ShopStateEnum;
import com.ouyan.o2o.exceptions.ShopOperationException;
import com.ouyan.o2o.service.ShopService;
import com.ouyan.o2o.util.PathUtil;
import com.ouyan.o2o.util.imageUtil;
@ServiceImpl
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_INFO);
        }
        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, File shopImg) {
        //获取shop图片的相对路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = imageUtil.generateThumbnail(shopImg, dest);
        shop.setShopImg(shopImgAddr);
    }
}

技术分享

package com.ouyan.o2o.service;

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ouyan.o2o.BaseTest;
import com.ouyan.o2o.dto.ShopExecution;
import com.ouyan.o2o.entity.Area;
import com.ouyan.o2o.entity.PersonInfo;
import com.ouyan.o2o.entity.Shop;
import com.ouyan.o2o.entity.ShopCategory;
import com.ouyan.o2o.enums.ShopStateEnum;
@Service
public class ShopServiceTest extends BaseTest{
    @Autowired
    private ShopService shopService;
    @Test
    public void testAddShop(){
        Shop shop = new Shop();
        PersonInfo owner = new PersonInfo();
        Area area = new Area();
        ShopCategory shopCategory = new ShopCategory();
        owner.setUserId(1L);
        area.setAreaId(2L);
        shopCategory.setShopCategoryId(33L);
        shop.setOwner(owner);
        shop.setArea(area);
        shop.setShopCategory(shopCategory);
        shop.setShopName("测试的店铺1");
        shop.setShopDesc("test1");
        shop.setShopAddr("test1");
        shop.setPhone("test1");
        shop.setCreateTime(new Date());
        shop.setEnableStatus(ShopStateEnum.CHECK.getState());
        shop.setAdvice("审核中");
        File shopImg = new File("d:/timg.jpg");
        ShopExecution se = shopService.addShop(shop, shopImg);
        assertEquals(ShopStateEnum.CHECK.getState(),se.getState());
    }
}

 

商铺项目(店铺注册功能模块(二))

标签:set   red   package   enable   this   创建失败   ons   entity   blog   

原文地址:http://www.cnblogs.com/XJJD/p/7688183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!