package com.taoke.autopay.service.impl; import com.taoke.autopay.dao.KeyOrderMapper; import com.taoke.autopay.dao.WxUserOrderCountMapper; import com.taoke.autopay.entity.OrderChannelEnum; 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.KeyOrderService; import com.taoke.autopay.service.UserSettingService; import com.taoke.autopay.service.WxUserOrderCountService; import com.taoke.autopay.utils.TimeUtil; 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; @Resource private UserSettingService userSettingService; @Resource private KeyOrderMapper keyOrderMapper; @Transactional(rollbackFor = Exception.class) @Override public void addOrderCount(Long uid, OrderCountTypeEnum orderType, OrderChannelEnum orderChannel, String day, int count, Integer maxCount) throws WxOrderCountException { // 统计用户总次数 WxUserOrderCountMapper.DaoQuery daoQuery=new WxUserOrderCountMapper.DaoQuery(); daoQuery.uid = uid; if(orderType!=null) { daoQuery.orderType = orderType.getType(); } daoQuery.orderChannel = orderChannel; Long totalCount = wxUserOrderCountMapper.sumOrderCount(daoQuery); if(totalCount==null){ totalCount = 0L; } if(totalCount>Integer.MAX_VALUE){ totalCount = (long)Integer.MAX_VALUE; } int submitCount = userSettingService.getLimitCountByTotalCount(orderType.getType(), (int)totalCount.longValue(), orderChannel); if(maxCount==null){ maxCount =Integer.MAX_VALUE; } maxCount = Math.min(submitCount, maxCount); WxUserOrderCount info = new WxUserOrderCount(); info.setDay(day); if(orderType!=null) { info.setOrderType(orderType.getType()); } info.setUid(uid); info.setOrderChannel(orderChannel); 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 { if (maxCount != null && maxCount < count + 0) { throw new WxOrderCountException(String.format("今日超过最大提交次数(%s次)", maxCount)); } info.setCreateTime(new Date()); info.setOrderCount(count); wxUserOrderCountMapper.insertSelective(info); } } @Override public void isOrderCountLimit(Long uid, OrderCountTypeEnum orderType,OrderChannelEnum orderChannel, String day, int count, Integer maxCount) throws WxOrderCountException{ WxUserOrderCountMapper.DaoQuery daoQuery=new WxUserOrderCountMapper.DaoQuery(); daoQuery.uid = uid; if(orderType!=null) { daoQuery.orderType = orderType.getType(); } daoQuery.orderChannel = orderChannel; Long totalCount = wxUserOrderCountMapper.sumOrderCount(daoQuery); if(totalCount==null){ totalCount = 0L; } if(totalCount>Integer.MAX_VALUE){ totalCount = (long)Integer.MAX_VALUE; } int submitCount = userSettingService.getLimitCountByTotalCount(orderType.getType(), (int)totalCount.longValue(),orderChannel); if(maxCount==null){ maxCount =Integer.MAX_VALUE; } maxCount = Math.min(submitCount, maxCount); // 统计今天的提交次数 KeyOrderMapper.DaoQuery orderQuery=new KeyOrderMapper.DaoQuery(); orderQuery.uid = uid; orderQuery.minCreateTime =new Date( TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(System.currentTimeMillis(),"yyyyMMdd"),"yyyyMMdd")); if(orderType!=null&&orderType!=OrderCountTypeEnum.SUBMIT_TOKEN_COUNT){ orderQuery.orderType=orderType.getType(); } long todayCount = keyOrderMapper.count(orderQuery); if (maxCount != null && maxCount < count + todayCount) { throw new WxOrderCountException(String.format("今日超过最大提交次数(%s次)", maxCount)); } } @Override public WxUserOrderCount get(Long uid, OrderCountTypeEnum orderType,OrderChannelEnum orderChannel, String day) { WxUserOrderCountMapper.DaoQuery daoQuery = new WxUserOrderCountMapper.DaoQuery(); daoQuery.uid = uid; daoQuery.day = day; if(orderType!=null) { daoQuery.orderType = orderType.getType(); } daoQuery.orderChannel = orderChannel; List list = wxUserOrderCountMapper.list(daoQuery); if (list.size() > 0) { return list.get(0); } return null; } @Override public long sum(Long uid, OrderCountTypeEnum orderType, OrderChannelEnum orderChannel,String day) { WxUserOrderCountMapper.DaoQuery daoQuery=new WxUserOrderCountMapper.DaoQuery(); daoQuery.uid = uid; if(orderType!=null) { daoQuery.orderType = orderType.getType(); } daoQuery.orderChannel = orderChannel; daoQuery.day = day; Long totalCount = wxUserOrderCountMapper.sumOrderCount(daoQuery); if(totalCount==null){ return 0; } return totalCount; } }