admin
2024-08-04 bc56870059cca013649077af0e53891cba8dbfd1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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.UserSettingService;
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;
 
    @Resource
    private UserSettingService userSettingService;
 
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrderCount(Long uid, OrderCountTypeEnum orderType, String day, int count, Integer maxCount) throws WxOrderCountException {
        // 统计用户总次数
        WxUserOrderCountMapper.DaoQuery daoQuery=new WxUserOrderCountMapper.DaoQuery();
        daoQuery.uid = uid;
        daoQuery.orderType=orderType.getType();
        Long totalCount = wxUserOrderCountMapper.sumOrderCount(daoQuery);
        if(totalCount==null){
            totalCount = 0L;
        }
        if(totalCount>Integer.MAX_VALUE){
            totalCount = (long)Integer.MAX_VALUE;
        }
       int submitCount = userSettingService.getLimitCountByTotalCount((int)totalCount.longValue());
        if(maxCount==null){
            maxCount =Integer.MAX_VALUE;
        }
        maxCount = Math.min(submitCount, maxCount);
        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 {
            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, String day, int count, Integer maxCount) throws WxOrderCountException{
 
        WxUserOrderCountMapper.DaoQuery daoQuery=new WxUserOrderCountMapper.DaoQuery();
        daoQuery.uid = uid;
        daoQuery.orderType=orderType.getType();
        Long totalCount = wxUserOrderCountMapper.sumOrderCount(daoQuery);
        if(totalCount==null){
            totalCount = 0L;
        }
        if(totalCount>Integer.MAX_VALUE){
            totalCount = (long)Integer.MAX_VALUE;
        }
        int submitCount = userSettingService.getLimitCountByTotalCount((int)totalCount.longValue());
        if(maxCount==null){
            maxCount =Integer.MAX_VALUE;
        }
        maxCount = Math.min(submitCount, maxCount);
        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));
            }
        }
    }
 
    @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;
    }
}