微信点餐系统

部分业务逻辑开发

1、ProduceCategoryService
DAO —ProductCategoryRepository:

1
2
3
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
List<ProductCategory> findAllByCategoryTypeIn(List<Integer> ids);
}

1
2
3
4
5
6
7
public interface ProductCategoryService {

ProductCategory findOne(Integer id);
List<ProductCategory> findAll();
List<ProductCategory> findAllByCategoryTypeIn(List<Integer> types);
ProductCategory save(ProductCategory productCategory);
}
Service ---ProductCategoryServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    @Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

@Autowired
private ProductCategoryRepository productCategoryRepository;


@Override
public ProductCategory findOne(Integer id) {
ProductCategory result = productCategoryRepository.findOne(id);
return result;
}

@Override
public List<ProductCategory> findAll() {
return productCategoryRepository.findAll();
}

@Override
public List<ProductCategory> findAllByCategoryTypeIn(List<Integer> types) {
return productCategoryRepository.findAllByCategoryTypeIn(types);
}

@Override
public ProductCategory save(ProductCategory productCategory) {
return productCategoryRepository.save(productCategory);
}
}
测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    @RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryServiceImplTest {

@Autowired
private ProductCategoryServiceImpl productCategoryService;

@Test
public void findOne() throws Exception {
ProductCategory result = productCategoryService.findOne(1);
Assert.assertNotNull(result);
}

@Test
public void findAll() throws Exception {
List<ProductCategory> result = productCategoryService.findAll();
Assert.assertNotEquals(0,result.size());
}

@Test
public void findAllByCategoryTypeIn() throws Exception {
List<Integer> types = Arrays.asList(111,211);
List<ProductCategory> result = productCategoryService.findAllByCategoryTypeIn(types);
Assert.assertNotEquals(0,result.size());
}

@Test
public void save() throws Exception {
ProductCategory result = productCategoryService.save(new ProductCategory("清凉一夏",11));
}

}
2、买家商品

entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Entity
@DynamicUpdate
@Data
public class ProductInfo {
@Id
private String productId;

private String productName;

private double productPrice;

private Integer productStock;

private String productDescription;

private String productIcon;

private Integer productStatus;

private Integer categoryType;
}
dao:
1
2
3
public interface ProductInfoRepository extends JpaRepository<ProductInfo,String> {
List<ProductInfo> findByProductStatus(Integer status);
}
service:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

@Autowired
private ProductInfoRepository repository;

/**
* 获取所有商品详情
* @param pageable
* @return
*/
@Override
public Page<ProductInfo> findAll(Pageable pageable) {
Page<ProductInfo> result = repository.findAll(pageable);
return result;
}

/**
* 获取单个商品信息
* @param id
* @return
*/
@Override
public ProductInfo findOne(String id) {
return repository.findOne(id);
}

/**
* 获取所有在架商品信息
* @return
*/
@Override
public List<ProductInfo> findUpAll() {

return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
}

/**
* 保存一个商品信息
* @param productInfo
* @return
*/
@Override
public ProductInfo save(ProductInfo productInfo) {
return repository.save(productInfo);
}
}
controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

@Autowired
private ProductInfoService productInfoService;
@Autowired
private ProductCategoryService productCategoryService;

@GetMapping("/list")
public ResultVO list(){


//查询所有上架商品
List<ProductInfo> productInfoList = productInfoService.findUpAll();

//查询商品类目
List<Integer> categoryTypeList = productInfoList.stream()
.map(e -> e.getCategoryType())
.collect(Collectors.toList());
List<ProductCategory> productCategoryList = productCategoryService.findAllByCategoryTypeIn(categoryTypeList);
//数据封装
List<ProductVO> productVOList = new ArrayList<>();
for (ProductCategory p:productCategoryList
) {
ProductVO productVO = new ProductVO();
productVO.setProductType(p.getCategoryType());
productVO.setProductName(p.getCategoryName());
List<ProductInfoVO> productInfoVOList = new ArrayList<>();
for (ProductInfo productInfo: productInfoList
) {
if (productInfo.getCategoryType().equals(p.getCategoryType())){
ProductInfoVO productInfoVO = new ProductInfoVO();
BeanUtils.copyProperties(productInfo,productInfoVO);
productInfoVOList.add(productInfoVO);
}

}
productVO.setProductInfoVOList(productInfoVOList);
productVOList.add(productVO);
}




return ResultVOUtils.success(productVOList);
}
}
其中的介个VO:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Data
public class ResultVO<T> {
/**
* 信息码
*/
private Integer code;
/**
* 信息
*/
private String msg;
/**
* 数据
*/
private T data;
}
1
2
3
4
5
6
7
8
9
10
@Data
public class ProductVO {

@JsonProperty("name")
private String productName;
@JsonProperty("type")
private Integer productType;
@JsonProperty("food")
private List<ProductInfoVO> productInfoVOList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
    @Data
public class ProductInfoVO {
@JsonProperty("id")
private String productId;
@JsonProperty("name")
private String productName;
@JsonProperty("price")
private BigDecimal productPrice;
@JsonProperty("description")
private String productDescription;
@JsonProperty("icon")
private String productIcon;
}
ResultVOUtils:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ResultVOUtils {

public static ResultVO success(Object object){
ResultVO resultVO = new ResultVO();
resultVO.setCode(0);
resultVO.setMsg("success");
resultVO.setData(object);
return resultVO;

}

public static ResultVO success() {
return success(null);
}

public ResultVO failure(Integer code,String message) {
ResultVO resultVO = new ResultVO();
resultVO.setCode(code);
resultVO.setMsg(message);
return resultVO;
}
}
其他业务逻辑代码略....
文章目录