package com.taoke.autopay.service.impl;
|
|
import com.taoke.autopay.dao.WxUserOrderCountMapper;
|
import com.taoke.autopay.entity.OrderCountTypeEnum;
|
import com.taoke.autopay.entity.WxUserOrderCount;
|
import com.taoke.autopay.exception.WxOrderCountException;
|
import com.taoke.autopay.factory.OrderFactory;
|
import com.taoke.autopay.service.WxUserOrderCountService;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author hxh
|
* @title: WxUserOrderCountServiceImpl
|
* @description: 用户订单计数服务
|
* @date 2024/6/28 19:18
|
*/
|
@Service
|
public class WxUserOrderCountServiceImpl implements WxUserOrderCountService {
|
|
@Resource
|
private WxUserOrderCountMapper wxUserOrderCountMapper;
|
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public void addOrderCount(Long uid, OrderCountTypeEnum orderType, String day, int count, Integer maxCount) throws WxOrderCountException {
|
WxUserOrderCount info = new WxUserOrderCount();
|
info.setDay(day);
|
info.setOrderType(orderType.getType());
|
info.setUid(uid);
|
info.setId(OrderFactory.createId(info));
|
// 判断是否存在
|
WxUserOrderCount old = wxUserOrderCountMapper.selectByPrimaryKeyForUpdate(info.getId());
|
|
|
if (old != null) {
|
if (maxCount != null && maxCount < count + old.getOrderCount()) {
|
throw new WxOrderCountException(String.format("今日超过最大提交次数(%s次)", maxCount));
|
}
|
WxUserOrderCount update = new WxUserOrderCount();
|
update.setId(info.getId());
|
update.setOrderCount(old.getOrderCount() + count);
|
update.setUpdateTime(new Date());
|
wxUserOrderCountMapper.updateByPrimaryKeySelective(update);
|
} else {
|
info.setCreateTime(new Date());
|
info.setOrderCount(count);
|
wxUserOrderCountMapper.insertSelective(info);
|
}
|
}
|
|
@Override
|
public WxUserOrderCount get(Long uid, OrderCountTypeEnum orderType, String day) {
|
WxUserOrderCountMapper.DaoQuery daoQuery = new WxUserOrderCountMapper.DaoQuery();
|
daoQuery.uid = uid;
|
daoQuery.day = day;
|
daoQuery.orderType = orderType.getType();
|
List<WxUserOrderCount> list = wxUserOrderCountMapper.list(daoQuery);
|
if (list.size() > 0) {
|
return list.get(0);
|
}
|
return null;
|
}
|
}
|