校园商铺-6店铺编辑列表和列表功能-6店铺列表展示之Service层的实现

1. Service层

为什么返回shopExecution?
原因:需要将这个shopList还有它的count整合在一起返回。而shopExecution正好满足了我们的条件。

package com.csj2018.o2o.service;

import java.io.InputStream;

import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.exceptions.ShopOperationException;
import com.csj2018.o2o.dto.ShopExecution;
public interface ShopService {
    /**
     * 根据shopCondition分页返回相应店铺列表
     * @param shopCondition
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public ShopExecution getShopList(Shop shopCondition,int pageIndex,int pageSize);
    /**
     * 通过店铺ID获取店铺信息
     * @param shopId
     * @return
     */
    Shop getByShopId(long shopId);
    /**
     * 更新店铺信息,包括对图片的处理
     * @param shop
     * @param shopImgInputStream
     * @param fileName
     * @return
     */
    ShopExecution modifyShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;
    /**
     * 注册店铺信息,包括图片处理
     * @param shop
     * @param shopImgInputStream
     * @param fileName
     * @return
     */
    ShopExecution addShop(Shop shop,InputStream shopImgInputStream,String fileName) throws ShopOperationException;
}

2. Service实现类

2.1 参数转换

Dao层第2个参数是rowIndex,而Service是pageIndex。因为前端只认页数,而Dao层只认行数。因此这里需要做一个转换。
编写一个工具类

package com.csj2018.o2o.util;

public class PageCalculator {
    public static int calculatorRowIndex(int pageIndex,int pageSize) {
        return (pageIndex > 0)?(pageIndex - 1) * pageSize:0;    

//同     if(pageIndex>0) {
//          return pageIndex - 1;
//      }else {
//          return 0;
//      }
    }
}

当pageIndex为0和1时,rowIndex为0

2.2 实现类

package com.csj2018.o2o.service.impl;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csj2018.o2o.dao.ShopDao;
import com.csj2018.o2o.dto.ShopExecution;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.enums.ShopStateEnum;
import com.csj2018.o2o.exceptions.ShopOperationException;
import com.csj2018.o2o.service.ShopService;
import com.csj2018.o2o.util.ImageUtil;
import com.csj2018.o2o.util.PageCalculator;
import com.csj2018.o2o.util.PathUtil;

@Service
public class ShopServiceImpl implements ShopService {
    private Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);

    @Autowired
    private ShopDao shopDao;
    @Override
    public ShopExecution getShopList(Shop shopCondition,int pageIndex,int pageSize) {
        int rowIndex = PageCalculator.calculatorRowIndex(pageIndex, pageSize);
        List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
        int count = shopDao.queryShopCount(shopCondition);
        ShopExecution se = new ShopExecution();
        if(shopList != null) {
            se.setShopList(shopList);
            se.setCount(count);
        }else {
            se.setState(ShopStateEnum.INNER_ERROR.getState());
        }
        return se;
    }

    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, InputStream shopImgInputStream, String fileName) {

        // 控制判断,shop是不是包含必需的值
        if (shop == null) {
            logger.warn("shop== null");
            return new ShopExecution(ShopStateEnum.NUll_SHOP);
        }
        // 增加对Shop其他引入类非空的判断
        try {
            // 给店铺信息赋初始值
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            // 添加店铺信息
            int effectedNum = shopDao.insertShop(shop);
            logger.warn("添加结果:" + effectedNum + "shopId:" + shop.getShopId());
            if (effectedNum <= 0) {
                throw new ShopOperationException("店铺创建失败");
            } else {
                if (shopImgInputStream != null) {

                    // 存储图片
                    try {
                        addShopImage(shop, shopImgInputStream, fileName);
                    } 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 addShopImage(Shop shop, InputStream shopImg, String fileName) {
        // 获取shop图片目录的相对路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg, fileName, dest);
        shop.setShopImg(shopImgAddr);
    }

    @Override
    public Shop getByShopId(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    @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.deleteFileOfPath(tempShop.getShopImg());
                    }
                    addShopImage(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());
            }
        }
    }
}

3. 单元测试

package com.csj2018.o2o.service;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.dto.ShopExecution;
import com.csj2018.o2o.entity.Area;
import com.csj2018.o2o.entity.PersonInfo;
import com.csj2018.o2o.entity.Shop;
import com.csj2018.o2o.entity.ShopCategory;
import com.csj2018.o2o.enums.ShopStateEnum;
import com.csj2018.o2o.exceptions.ShopOperationException;

public class ShopServiceTest extends BaseTest{
    @Autowired
    private ShopService shopService;
    @Test
    public void testGetShopList() {
        Shop shopCondition = new Shop();
        ShopCategory sc = new ShopCategory();
        sc.setShopCategoryId(1L);
        shopCondition.setShopCategory(sc);
        ShopExecution se = shopService.getShopList(shopCondition, 1, 5);
        System.out.println("店铺列表总数:"+se.getShopList().size());
        System.out.println("店铺总数为:"+se.getCount());
    }

    @Test
    @Ignore
    public void testModifyShop() throws ShopOperationException,FileNotFoundException{
        Shop shop = new Shop();
        shop.setShopId(1L);
        shop.setShopName("修改后的店铺名称");
//      File shopImg = new File("/Users/chenshanju/Downloads/liuchuanfeng.jpeg");
        InputStream is = new FileInputStream("/Users/chenshanju/Downloads/liuchuanfeng.jpeg");
        ShopExecution shopExecution = shopService.modifyShop(shop, is, "流川枫.jpeg");
        System.out.println("新的图片地址:"+shopExecution.getShop().getShopImg());
    }
    @Test
    @Ignore
    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("测试de店铺4");
        shop.setShopDesc("店铺描述4");
        shop.setShopAddr("测试路4号1");
        shop.setPhone("1234567892");
        shop.setPriority(4);
        shop.setCreateTime(new Date());
        shop.setEnableStatus(ShopStateEnum.CHECK.getState());
        shop.setAdvice("审核中");
        File shopImg = new File("/Users/chenshanju/Downloads/cat.jpg");
        InputStream is = new FileInputStream(shopImg);
        ShopExecution se = shopService.addShop(shop, is,shopImg.getName());
        assertEquals(ShopStateEnum.CHECK.getState(), se.getState());
    }
}

原文地址:https://www.cnblogs.com/csj2018/p/12230517.html

时间: 2024-11-09 16:30:20

校园商铺-6店铺编辑列表和列表功能-6店铺列表展示之Service层的实现的相关文章

SSM到Spring Boot -从零开发校园商铺平台

第1章 开发准备   1-1 课程导学   1-2 开发准备第2章 项目设计和框架搭建   2-1 系统功能模块划分   2-2 实体类设计与表创建A   2-3 实体类设计与表创建B   2-4 实体类设计与表创建C   2-5 配置Maven   2-6 逐层完成SSM的各项配置   2-7 验证Dao   2-8 验证Sevice   2-9 验证Controller   2-10 彩蛋时间第3章 Logback配置与使用   3-1 logback介绍   3-2 logback的配置 

校园商铺-6店铺编辑列表和列表功能-2店铺信息编辑之Controller层的实现

1.店铺编辑范围 除了店铺名称.店铺分类不可更改,其他信息(区域.地址.电话.缩略图.店铺简介)都可以更改. 2.编写获取店铺信息的controller @RequestMapping(value="/getshopbyid",method=RequestMethod.GET) @ResponseBody private Map<String,Object> getShopById(HttpServletRequest request){ Map<String,Obj

校园商铺-7商品类别模块-2商品类别列表从后到前

商品类别模块为什么使用shopId,而不是Shop实体类? 因为我们获取productCategory时,并不需要获取除了shopId之外的信息,因此不用Shop实体类. public class ProductCategory { private Long productCategoryId; private Long shopId; private String productCategoryName; private Integer priority; private Date creat

shop--8.店铺列表展示--Service层

在service层中, 有一个问题,就是: dao层中的分页查询传参,传入的是rowIndex(从第几行开始查询), pageSize(查询多少条记录) 而在service层中分页查询传的是pageIndex(第几页),pageSize 所以在第一个参数要有一个转换才可以 所以编写一个工具类,将pageIndex转为rowIndex PageCalculator 1 /** 2 * 将pageIndex(第几页)转换为查询结果中的第几行rowIndex 3 * 如果pageIndex是1 pag

校园商铺-9前端展示系统-8店铺详情页的开发

1.controller层 package com.csj2018.o2o.web.frontend; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springf

校园商铺-4店铺注册功能模块-6店铺注册之Controller层的改造

不合理的地方: 1. 并不需要将InputStream转换成File类型,直接将InputStream传进入交给CommonsMultipartfile去处理就可以了 如果做这样的转换,每次都需要生成一个空白的文件,然后还需要向文件中写入请求传送过来的文件流,这样不仅仅产生很多垃圾文件,同时有可能造成写入失败,抛出异常的风险,大大地加大了系统的不稳定性. 2. 问题:一开始设计ShopService接口addShop方法的时候,第二个参数不早早设定为InputStream? 原因:体现实际的开发

校园商铺-4店铺注册功能模块-11店铺类别区域信息的获取

在js中用到的getshopinitinfo还未实现,现在实现这个方法 var initUrl = '/o2o/shopadmin/getshopinitinfo';//获取店铺的初始信息,还未定义 店铺类别 dao层 package com.csj2018.o2o.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.csj2018.o2o.entity.ShopCategory;

Java期末项目——校园商铺平台(一)

需求分析: 1.1背景 随着高等院校规模的扩大,学生数量的增加,学生的需求日益多样化,加之现在高等学校大多位于郊区,校园商铺发挥着越来越重要的作用.大学校园经济有一定的特殊性,主要体现在,消费群体单一稳定性,购买行为的多次重复性.校园商铺不仅给在现实生提供了方便快捷的商品购买的同时,也是一个很好的盈利方式.在开展校园商铺的同时,要考虑到学校的实际情况,以及是否能盈利. 1.2非功能性需求分析 首先在性能要求上,简约的设计界面,操作灵敏快捷,方便用户体验.网页反应速度快.易打开.网页界面稳定等.在

毕设利器 SSM到Spring Boot 从零开发校园商铺平台

第1章 开发准备本章包含课程介绍,同时讲解开发网站所需要准备的事情,并且带领大家从零开始搭建一个Maven Web. 第2章 项目设计和框架搭建本章主要先带领大家过一遍项目1.0的大致功能点,并依据功能点划分,设计出合理的实体类以及表来.此外,本章还要完成搭建SSM框架底子的任务,并从后到前完成一个功能点的Dao.Service.Controller的开发与UT验证 第3章 Logback配置与使用本章将讲解logback相关的配置,并验证,运用到程序当中 第4章 店铺注册功能模块本章将开始项目