- 商品数据表
create table 'mmall_product'(
'id' int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
'category_id' int(11) NOT NULL COMMENT '分类id,对应mmal_category分类表的主键',
'name' varchar(100) NOT NULL COMMENT '商品名称',
'subtitle' varchar(200) DEFAULT NULL COMMENT '商品副标题',
'main_image' varchar(500) DEFAULT NULL COMMENT '产品主图,url相对地址',
'sub_images' text COMMENT '图片地址,json格式,扩展用',
'detail' text COMMENT '商品详情',
'price' decimal(20,2) NOT NULL COMMENT '价格,单位-元保留两位小数',
'stock' int(11) NOT NULL COMMENT '库存数量',
'status int(6) DEFAULT '1' COMMENT '商品状态:1-在售,2-下架,3-删除',
'create_time' datetime DEFAULT NULL COMMENT '创建时间',
'update_time' datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY ('id')
)ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
- 设置公共常量类
//设置公共量
public class Const {
public static final String CURRENT_USER = "currentUser";
public static final String EMAIL="email";
public static final String USERNAME="username";
public interface Role{
int ROLE_CUSTOMER=0;//普通用户
int ROLE_ADMIN=1;//管理员
}
}
一、增加与修改商品
- 思路:
1、判断商品是否有图片,如果有就用String[ ] subImagArray = product.getSubImages().split(",")以逗号分割存进数组里,然后取数组的第一个值作为主图。
2、判断传来的id是否为空,不为空则进行更新操作,为空则进行增加操作。
- controller(参数Product)
//增加商品和修改商品
//新增商品http://localhost:8080/manage/product/save.do?categoryId=1&name=三星洗衣机&subtitle=三星大促销&subImages=test.jpg,11.jpg,2.jpg,3.jpg&detail=detailtext&price=1000&stock=100&status=1
//修改商品 http://localhost:8080/manage/product/save.do?id=3&categoryId=1&name=三星洗衣机&subtitle=三星大促销&subImages=test.jpg&detail=detailtext&price=1000&stock=100&status=1
@RequestMapping("save.do")
@ResponseBody
public ServerResponse productSave(HttpSession session, Product product) {
User user = (User) session.getAttribute(Const.CURRENT_USER);
if (user == null) {
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
}
if (iUserService.checkAdminRole(user).isSuccess()) {
return iProductService.saveOrUpdateProduct(product);
} else {
return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
}
}
- Impl(参数Product)
public ServerResponse saveOrUpdateProduct(Product product) {
if (product != null) {
//判断商品的图片是否为空
if (org.apache.commons.lang3.StringUtils.isNotBlank(product.getSubImages())) {
//不为空则把传过来的商品图片地址用逗号进行分割
String[] subImagArray = product.getSubImages().split(",");
if (subImagArray.length > 0) {
//取第一个商品的图片当做主图
product.setMainImage(subImagArray[0]);
}
}
//如果传过来的id不为空,则对商品进行修改更新操作
if (product.getId() != null) {
int rowCount = productMapper.updateByPrimaryKey(product);
if (rowCount > 0) {
return ServerResponse.createBySuccess("修改商品成功");
}
return ServerResponse.createByErrorMessage("修改商品失败");
} else {
//否则进行增加商品操作
int rowCount = productMapper.insert(product);
if (rowCount > 0) {
return ServerResponse.createBySuccess("增加商品成功");
}
return ServerResponse.createByErrorMessage("增加商品失败");
}
}
return ServerResponse.createByErrorMessage("新增或修改商品参数不正确");
}
二、商品上下架
- 思路:从前台获取修改的商品id与商品上下架状态对数据库进行修改
- controller(参数:商品id,商品状态status)
//设置商品上下架(是否在售)
//http://localhost:8080/manage/product/set_sale_status.do?productId=1&status=1
@RequestMapping("set_sale_status.do")
@ResponseBody
public ServerResponse set_sale_status(HttpSession session, Integer productId, Integer status) {
User user = (User) session.getAttribute(Const.CURRENT_USER);
if (user == null) {
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
}
if (iUserService.checkAdminRole(user).isSuccess()) {
return iProductService.set_sale_status(productId, status);
} else {
return ServerResponse.createByErrorMessage("不是管理员,无权限操作");
}
}
- impl(参数:商品id,商品状态status)
//商品上下架
public ServerResponse set_sale_status(Integer p