1 商品类别(增删改查)

1.1 GoodsCategoryMapper
package com.xzy.goods.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.GoodsCategory;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GoodsCategoryMapper extends BaseMapper<GoodsCategory> {
}
1.2 GoodsCategoryService
package com.xzy.goods.service;
import com.xzy.common.entity.GoodsCategory;
import com.xzy.goods.dao.GoodsCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GoodsCategoryService {
@Autowired
private GoodsCategoryMapper goodsCategoryMapper;
public int add(String title, String descp) {
GoodsCategory goodsCategory = new GoodsCategory();
goodsCategory.setTitle(title);
goodsCategory.setDescp(descp);
int row = goodsCategoryMapper.insert(goodsCategory);
return row;
}
public int delete(String id) {
return goodsCategoryMapper.deleteById(id);
}
public int update(String id, String title,String descp) {
GoodsCategory goodsCategory = new GoodsCategory();
goodsCategory.setId(id);
goodsCategory.setTitle(title);
goodsCategory.setDescp(descp);
return goodsCategoryMapper.updateById(goodsCategory);
}
public List<GoodsCategory> listALl() {
return goodsCategoryMapper.selectList(null);
}
}
1.3 GoodsCategoryController
package com.xzy.goods.controller;
import com.xzy.common.entity.GoodsCategory;
import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.goods.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {
@Autowired
private GoodsCategoryService goodsCategoryService = new GoodsCategoryService();
@PostMapping("/add")
public Response add(String title,String descp) {
int row = goodsCategoryService.add(title, descp);
Response response = new Response();
if(row>0){
response.code(ResponseCode.SUCCESS).msg("添加成功");
}else {
response.code(ResponseCode.ERROR).msg("添加失败");
}
return response;
}
@PostMapping("/delete")
public Response delete(String id){
Response response = new Response();
int row = goodsCategoryService.delete(id);
if(row>0){
response.code(ResponseCode.SUCCESS).msg("商品类别删除成功");
}else{
response.code(ResponseCode.ERROR).msg("商品类别删除失败");
}
return response;
}
@PostMapping("/update")
public Response update(String id, String title,String descp){
Response response = new Response();
int row = goodsCategoryService.update(id,title,descp);
if(row>0){
response.code(ResponseCode.SUCCESS).msg("商品类别修改成功");
}else{
response.code(ResponseCode.ERROR).msg("商品类别修改失败");
}
return response;
}
@GetMapping("/listAll")
public Response listAll(){
Response response = new Response();
List<GoodsCategory> goodsCategoriesList = goodsCategoryService.listALl();
response.code(ResponseCode.SUCCESS).msg("成功").data(goodsCategoriesList);
return response;
}
}
2 商品(增删改查)

2.1 GoodsMapper
package com.xzy.goods.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
List<Goods> selectListByTitleAndPlatform(@Param("title") String title, @Param("platform") Integer platform, @Param("start") Long start, @Param("size") Long size);
Long selectCountByTitleAndPlatform(@Param("title") String title, @Param("platform") Integer platform);
}
2.2 GoodsService
package com.xzy.goods.service;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.xzy.common.entity.Goods;
import com.xzy.common.util.FdfsUtils;
import com.xzy.goods.dao.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@Service
public class GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private FastFileStorageClient fastFileStorageClient;
public List<Goods> getIndexData() {
return goodsMapper.selectList(null);
}
public int add(String title, String descp, Long originPrice, Long currentPrice, MultipartFile image, String detail, String cid, Long stock) throws IOException {
String fullPath = FdfsUtils.upLoadFile(fastFileStorageClient, image.getInputStream(), image.getOriginalFilename(), image.getSize());
Goods goods = new Goods();
goods.setTitle(title);
goods.setDescp(descp);
goods.setOriginPrice(originPrice);
goods.setCurrentPrice(currentPrice);
goods.setImageUrl(fullPath);
goods.setDetail(detail);
goods.setCId(cid);
goods.setStock(stock);
return goodsMapper.insert(goods);
}
public int deleteById(String id) {
return goodsMapper.deleteById(id);
}
public int update(String id, String title, String descp, Long originPrice, Long currentPrice, String detail, Integer status, String cId, Date gmtModify) {
Goods goods = new Goods();
goods.setId(id);
goods.setTitle(title);
goods.setDescp(descp);
goods.setOriginPrice(originPrice);
goods.setCurrentPrice(currentPrice);
goods.setDetail(detail);
goods.setStatus(status);
goods.setCId(cId);
Date date = new Date();
goods.setGmtModify(date);
return goodsMapper.updateById(goods);
}
public List<Goods> listGoods(String title, Integer platform, Long current, Long size) {
Long start = (current - 1) * size;
return goodsMapper.selectListByTitleAndPlatform(title, platform, start, size);
}
public Long selectCount(String title, Integer platform){
return goodsMapper.selectCountByTitleAndPlatform(title,platform);
}
}
2.3 GoodsController
package com.xzy.goods.controller;
import com.xzy.common.entity.Goods;
import com.xzy.common.entity.vo.PageResponse;
import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("/indexData")
public Response indexData(String title) {
Response response = new Response();
List<Goods> goodsList = goodsService.getIndexData();
response.code(ResponseCode.SUCCESS).msg("成功").data(goodsList);
return response;
}
@PostMapping("/add")
public Response add(String title, String descp, Long originPrice, Long currentPrice, MultipartFile image, String detail, String cid, Long stock) throws IOException {
Response response = new Response();
int row = goodsService.add(title, descp, originPrice, currentPrice, image, detail, cid, stock);
if (row > 0) {
response.code(ResponseCode.SUCCESS).msg("商品入库成功");
} else {
response.code(ResponseCode.ERROR).msg("商品入库失败");
}
return response;
}
@PostMapping("/delete")
public Response delete(String id) {
Response response = new Response();
int row = goodsService.deleteById(id);
if (row > 0) {
response.code(ResponseCode.SUCCESS).msg("商品删除成功");
} else {
response.code(ResponseCode.ERROR).msg("商品删除失败");
}
return response;
}
@PostMapping("/update")
public Response update(String id, String title, String descp, Long originPrice, Long currentPrice, String detail, Integer status, String cId, Date gmtModify) {
Response response = new Response();
int row = goodsService.update(id, title, descp, originPrice, currentPrice, detail, status, cId, gmtModify);
if (row > 0) {
response.code(ResponseCode.SUCCESS).msg("商品修改成功");
} else {
response.code(ResponseCode.ERROR).msg("商品修改失败");
}
return response;
}
@GetMapping("/list")
public PageResponse listGoods(String title, Integer platform, Long current, Long size) {
Long count = goodsService.selectCount(title, platform);
List<Goods> goodList = goodsService.listGoods(title, platform,current,size);
PageResponse pageResponse = new PageResponse();
pageResponse.code(ResponseCode.SUCCESS).msg("成功").data(goodList).count(count);
return pageResponse;
}
}
2.4 PageResponse
package com.xzy.common.entity.vo;
import lombok.Data;
@Data
public class PageResponse {
private Integer code;
private String msg;
private Long count;
private Object data;
public PageResponse code(Integer code) {
this.code = code;
return this;
}
public PageResponse msg(String msg) {
this.msg = msg;
return this;
}
public PageResponse count(Long count) {
this.count = count;
return this;
}
public PageResponse data(Object data) {
this.data = data;
return this;
}
}
3 商品订单(增删改查)

3.1 GoodsOrderMapper
package com.xzy.goods.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xzy.common.entity.GoodsOrder;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GoodsOrderMapper extends BaseMapper<GoodsOrder> {
}
3.2 GoodsOrderService
package com.xzy.goods.service;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzy.common.entity.Address;
import com.xzy.common.entity.Goods;
import com.xzy.common.entity.GoodsOrder;
import com.xzy.common.entity.Member;
import com.xzy.goods.dao.AddressMapper;
import com.xzy.goods.dao.GoodsMapper;
import com.xzy.goods.dao.GoodsOrderMapper;
import com.xzy.um.dao.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.beans.Transient;
@Service
public class GoodsOrderService {
@Autowired
private GoodsOrderMapper goodsOrderMapper;
@Autowired
private MemberMapper memberMapper;
@Autowired
private AddressMapper addressMapper;
@Autowired
private GoodsMapper goodsMapper;
@Transient
public int add(String memberId, String goodsId, Long buyNumber) {
Member member = memberMapper.selectById(memberId);
QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
addressQueryWrapper.eq("u_id", memberId);
Address address = addressMapper.selectOne(addressQueryWrapper);
Goods goods = goodsMapper.selectById(goodsId);
goods.setStock(goods.getStock() - buyNumber);
goodsMapper.updateById(goods);
GoodsOrder goodOrder = new GoodsOrder();
goodOrder.setUId(member.getId());
goodOrder.setUName(member.getNickname());
goodOrder.setGId(goods.getId());
goodOrder.setGTitle(goods.getTitle());
goodOrder.setAddrId(address.getId());
goodOrder.setBuyNumber(buyNumber);
goodOrder.setCurrentPrice(goods.getCurrentPrice());
goodOrder.setTotalPrice(goods.getCurrentPrice() * buyNumber);
goodOrder.setStatus(0);
Date currentDate = new Date();
goodOrder.setGmtModify(currentDate);
goodOrder.setGmtModify(currentDate);
return goodsOrderMapper.insert(goodOrder);
}
}
3.3 GoodsOrderController
package com.xzy.goods.controller;
import com.xzy.common.entity.vo.Response;
import com.xzy.common.entity.vo.ResponseCode;
import com.xzy.common.util.JwtUtils;
import com.xzy.goods.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/order")
@CrossOrigin
public class GoodsOrderController {
@Autowired
private GoodsOrderService goodsOrderService;
@PostMapping("/add")
public Response add(String goodsId, Long buyNumber, HttpServletRequest request){
String memberId = JwtUtils.getIdByJwtToken(request);
Response response = new Response();
int row = goodsOrderService.add(memberId,goodsId,buyNumber);
if(row > 0){
response.code(ResponseCode.SUCCESS).msg("创建订单成功");
}else{
response.code(ResponseCode.ERROR).msg("创建订单失败");
}
return response;
}
}