package com.qfedu.fmmall.service.impl;
import com.qfedu.fmmall.dao.ProductImgMapper;
import com.qfedu.fmmall.dao.ProductMapper;
import com.qfedu.fmmall.dao.ProductSkuMapper;
import com.qfedu.fmmall.entity.Product;
import com.qfedu.fmmall.entity.ProductImg;
import com.qfedu.fmmall.entity.ProductSku;
import com.qfedu.fmmall.entity.ProductVO;
import com.qfedu.fmmall.service.ProductService;
import com.qfedu.fmmall.vo.ResStatus;
import com.qfedu.fmmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List;
/**
* @author beyondx
* @date Created in 2022/08/16/ 11:00
*/
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductMapper productMapper;
@Autowired
private ProductImgMapper productImgMapper;
@Autowired
private ProductSkuMapper productSkuMapper;
@Override
public ResultVO listRecommendProducts() {
List<ProductVO> productVOS = productMapper.selectRecommendProducts();
ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productVOS);
return resultVO;
}
@Transactional(propagation = Propagation.SUPPORTS) // 事务的隔离级别
@Override
public ResultVO getProductBasicInfo(String productId) {
// 1.商品基本信息
Example example = new Example(Product.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("productId", productId);
criteria.andEqualTo("productStatus", 1); // 状态为1表示 上架商品; 上午商品推荐的时候, 就需要有这个
List<Product> products = productMapper.selectByExample(example);
if(products.size() > 0) {
// 只有 有了 商品基本信息, 才有必要查询 里面的; 这里相当于 容错
// size() > 0商品存在; 才有必要查 商品图片, 商品套餐
// 2.商品图片
Example example1 = new Example(ProductImg.class);
Example.Criteria criteria1 = example.createCriteria();
criteria1.andEqualTo("itemId", productId);
List<ProductImg> productImgs = productImgMapper.selectByExample(example);
// 3.商品套餐
Example example2 = new Example(ProductSku.class);
Example.Criteria criteria2 = example2.createCriteria();
criteria2.andEqualTo("productId", productId);
criteria2.andEqualTo("status", 1);
List<ProductSku> productSkus = productSkuMapper.selectByExample(example2);
HashMap<String, Object> basicInfo = new HashMap<>();
basicInfo.put("product", products.get(0));
basicInfo.put("productImgs", productImgs);
basicInfo.put("productSkus", productSkus);
return new ResultVO(ResStatus.OK, "success", basicInfo);
} else {
// 商品不存在
return new ResultVO(ResStatus.NO, "查询的商品不存在!", null);
}
}
}
挖坑
了解一下 tkMapper, 在实际项目中, 使用普遍吗