imooc mysql电商项目_【电商项目】---订单模块

@Service

public class ItemServiceImpl implements ItemService {

@Autowired

private ItemsMapper itemsMapper;

@Autowired

private ItemsImgMapper itemsImgMapper;

@Autowired

private ItemsSpecMapper itemsSpecMapper;

@Autowired

private ItemsParamMapper itemsParamMapper;

@Autowired

private ItemsCommentsMapper itemsCommentsMapper;

@Autowired

private ItemsMapperCustom itemsMapperCustom;

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public Items queryItemById(String itemId) {

return itemsMapper.selectByPrimaryKey(itemId);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public List queryItemImgList(String itemId) {

Example itemsImgExp = new Example(ItemsImg.class);

Example.Criteria criteria = itemsImgExp.createCriteria();

criteria.andEqualTo("itemId", itemId);

return itemsImgMapper.selectByExample(itemsImgExp);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public List queryItemSpecList(String itemId) {

Example itemsSpecExp = new Example(ItemsSpec.class);

Example.Criteria criteria = itemsSpecExp.createCriteria();

criteria.andEqualTo("itemId", itemId);

return itemsSpecMapper.selectByExample(itemsSpecExp);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public ItemsParam queryItemParam(String itemId) {

Example itemsParamExp = new Example(ItemsParam.class);

Example.Criteria criteria = itemsParamExp.createCriteria();

criteria.andEqualTo("itemId", itemId);

return itemsParamMapper.selectOneByExample(itemsParamExp);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public CommentLevelCountsVO queryCommentCounts(String itemId) {

Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);

Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);

Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);

Integer totalCounts = goodCounts + normalCounts + badCounts;

CommentLevelCountsVO countsVO = new CommentLevelCountsVO();

countsVO.setTotalCounts(totalCounts);

countsVO.setGoodCounts(goodCounts);

countsVO.setNormalCounts(normalCounts);

countsVO.setBadCounts(badCounts);

return countsVO;

}

@Transactional(propagation = Propagation.SUPPORTS)

Integer getCommentCounts(String itemId, Integer level) {

ItemsComments condition = new ItemsComments();

condition.setItemId(itemId);

if (level != null) {

condition.setCommentLevel(level);

}

return itemsCommentsMapper.selectCount(condition);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public PagedGridResult queryPagedComments(String itemId,

Integer level,

Integer page,

Integer pageSize) {

Map map = new HashMap<>();

map.put("itemId", itemId);

map.put("level", level);

// mybatis-pagehelper

/**

* page: 第几页

* pageSize: 每页显示条数

*/

PageHelper.startPage(page, pageSize);

List list = itemsMapperCustom.queryItemComments(map);

for (ItemCommentVO vo : list) {

vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));

}

return setterPagedGrid(list, page);

}

private PagedGridResult setterPagedGrid(List> list, Integer page) {

PageInfo> pageList = new PageInfo<>(list);

PagedGridResult grid = new PagedGridResult();

grid.setPage(page);

grid.setRows(list);

grid.setTotal(pageList.getPages());

grid.setRecords(pageList.getTotal());

return grid;

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public PagedGridResult searhItems(String keywords, String sort, Integer page, Integer pageSize) {

Map map = new HashMap<>();

map.put("keywords", keywords);

map.put("sort", sort);

PageHelper.startPage(page, pageSize);

List list = itemsMapperCustom.searchItems(map);

return setterPagedGrid(list, page);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public PagedGridResult searhItems(Integer catId, String sort, Integer page, Integer pageSize) {

Map map = new HashMap<>();

map.put("catId", catId);

map.put("sort", sort);

PageHelper.startPage(page, pageSize);

List list = itemsMapperCustom.searchItemsByThirdCat(map);

return setterPagedGrid(list, page);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public List queryItemsBySpecIds(String specIds) {

String ids[] = specIds.split(",");

List specIdsList = new ArrayList<>();

Collections.addAll(specIdsList, ids);

return itemsMapperCustom.queryItemsBySpecIds(specIdsList);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public ItemsSpec queryItemSpecById(String specId) {

return itemsSpecMapper.selectByPrimaryKey(specId);

}

@Transactional(propagation = Propagation.SUPPORTS)

@Override

public String queryItemMainImgById(String itemId) {

ItemsImg itemsImg = new ItemsImg();

itemsImg.setItemId(itemId);

itemsImg.setIsMain(YesOrNo.YES.type);

ItemsImg result = itemsImgMapper.selectOne(itemsImg);

return result != null ? result.getUrl() : "";

}

@Transactional(propagation = Propagation.REQUIRED)

@Override

public void decreaseItemSpecStock(String specId, int buyCounts) {

// synchronized 不推荐使用,集群下无用,性能低下

// 锁数据库: 不推荐,导致数据库性能低下

// 分布式锁 zookeeper redis

// lockUtil.getLock(); -- 加锁

// 1. 查询库存

// int stock = 10;

// 2. 判断库存,是否能够减少到0以下

// if (stock - buyCounts < 0) {

// 提示用户库存不够

// 10 - 3 -3 - 5 = -1

// }

// lockUtil.unLock(); -- 解锁

int result = itemsMapperCustom.decreaseItemSpecStock(specId, buyCounts);

if (result != 1) {

throw new RuntimeException("订单创建失败,原因:库存不足!");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值