xuhy
2025-01-09 712f70b2936079a131ecb1e63c6d337171618cad
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.UserMerchantCouponMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.UUIDUtil;
import com.stylefeng.guns.modular.system.warpper.MerchantCouponListWarpper;
import com.stylefeng.guns.modular.system.warpper.MerchantCouponWarpper;
import com.stylefeng.guns.modular.system.warpper.UserMerchantCouponWapper;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class UserMerchantCouponServiceImpl extends ServiceImpl<UserMerchantCouponMapper, UserMerchantCoupon> implements IUserMerchantCouponService {
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
 
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
 
    @Autowired
    private IMerchantActivityService merchantActivityService;
 
    @Autowired
    private IMerchantActivitySlaveService merchantActivitySlaveService;
 
    @Autowired
    private IUserMerchantCouponService userMerchantCouponService;
 
    @Autowired
    private IMerchantCouponService merchantCouponService;
 
    @Autowired
    private IMerchantService merchantService;
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @Autowired
    private RedisUtil redisUtil;
 
 
 
 
 
    @Override
    public ResultUtil<UserMerchantCouponWapper> getUserMerchantCoupon(Integer uid, String code) throws Exception {
        Merchant merchant = merchantService.selectOne(new EntityWrapper<Merchant>().eq("userType", 1).eq("userId", uid).ne("state", 3));
        if(merchant.getAuditStatus() == 1){
            return ResultUtil.error("账户正在审核中");
        }
        if(merchant.getAuditStatus() == 3){
            return ResultUtil.error("账户审核不通过");
        }
        if(merchant.getState() == 2){
            return ResultUtil.error("账户被冻结");
        }
        UserMerchantCoupon userMerchantCoupon = userMerchantCouponService.selectOne(new EntityWrapper<UserMerchantCoupon>().eq("code", code).eq("state", 1));
        MerchantCoupon merchantCoupon = merchantCouponService.selectById(userMerchantCoupon.getMerchantCouponId());
        if(merchantCoupon.getMerchantId().compareTo(merchant.getId()) != 0){
            return ResultUtil.error("无效的核销码");
        }
        UserMerchantCouponWapper userMerchantCoupon1 = this.baseMapper.getUserMerchantCoupon(code);
        return ResultUtil.success(userMerchantCoupon1);
    }
 
    @Override
    public ResultUtil writeOffMerchantCoupon(Integer uid, String code) throws Exception {
        UserMerchantCoupon userMerchantCoupon = this.selectOne(new EntityWrapper<UserMerchantCoupon>().eq("code", code).eq("state", 1));
        if(null == userMerchantCoupon){
            return ResultUtil.error("无效的核销码");
        }
        if(System.currentTimeMillis() > userMerchantCoupon.getEndTime().getTime() || userMerchantCoupon.getStatus() == 3){
            return ResultUtil.error("优惠券已过期");
        }
        if(userMerchantCoupon.getStatus() == 2){
            return ResultUtil.error("优惠券已使用");
        }
        userMerchantCoupon.setStatus(2);
        userMerchantCoupon.setWriteOffTime(new Date());
        userMerchantCoupon.setWriteOffUserType(1);
        userMerchantCoupon.setWriteOffUserId(uid);
        this.updateById(userMerchantCoupon);
        return ResultUtil.success();
    }
 
    @Override
    public List<Map<String, Object>> getWriteOffHistory(Integer activityId, Integer id, Integer pageNum, Integer size) throws Exception {
        pageNum = (pageNum - 1) * size;
        return this.baseMapper.getWriteOffHistory(activityId, id, pageNum, size);
    }
 
    @Override
    public List<MerchantCouponWarpper> getMyMerchantCoupon(Integer uid, Integer type) throws Exception {
        return this.baseMapper.getMyMerchantCoupon(uid, type);
    }
 
    @Override
    public void updateExpired() throws Exception {
        this.baseMapper.updateExpired();
    }
 
    @Override
    public List<MerchantCouponListWarpper> giveAwayMerchantCoupon(Integer uid, Integer orderId, Integer orderType) throws Exception {
        List<MerchantActivity> merchantActivities = new ArrayList<>();
        Integer state = 0;
        if(orderType == 1){
            OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId);
            List<OrderPrivateCar> list = orderPrivateCarService.queryOrder(uid, 8, 9);
            merchantActivities = merchantActivityService.selectList(new EntityWrapper<MerchantActivity>().eq("companyId", orderPrivateCar.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1)
                    .eq("state", 1).like("orderType", "%1%").where("((orderAmountFull is not null and orderAmountFull <= " + orderPrivateCar.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))")
                    .in("id", merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList()))
                    .notIn("id", userMerchantCouponService.selectList(new EntityWrapper<UserMerchantCoupon>().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList()))
            );
            state = orderPrivateCar.getState();
        }
 
        if(orderType == 2){
            OrderTaxi orderTaxi = orderTaxiService.selectById(orderId);
            List<OrderTaxi> list = orderTaxiService.queryOrder(uid, 8, 9);
            merchantActivities = merchantActivityService.selectList(new EntityWrapper<MerchantActivity>().eq("companyId", orderTaxi.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1)
                    .eq("state", 1).like("orderType", "%2%").where("((orderAmountFull is not null  and orderAmountFull <= " + orderTaxi.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))")
                    .in("id", merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList()))
                    .notIn("id", userMerchantCouponService.selectList(new EntityWrapper<UserMerchantCoupon>().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList()))
            );
            state = orderTaxi.getState();
        }
 
 
        if(orderType == 3){
            OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId);
            List<OrderCrossCity> list = orderCrossCityService.queryOrder(uid, 8, 9);
            merchantActivities = merchantActivityService.selectList(new EntityWrapper<MerchantActivity>().eq("companyId", orderCrossCity.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1)
                    .eq("state", 1).like("orderType", "%3%").where("((orderAmountFull is not null  and orderAmountFull <= " + orderCrossCity.getOrderMoney() + ") or (orderNumber is not null  and orderNumber <= " + list.size() + "))")
                    .in("id", merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList()))
                    .notIn("id", userMerchantCouponService.selectList(new EntityWrapper<UserMerchantCoupon>().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList()))
            );
            state = orderCrossCity.getState();
        }
 
        if(orderType == 4){
            OrderLogistics orderLogistics = orderLogisticsService.selectById(orderId);
            List<OrderLogistics> list = orderLogisticsService.selectList(new EntityWrapper<OrderLogistics>().eq("userId", uid).eq("type", orderLogistics.getType()).eq("state", 9).eq("isDelete", 1));
            merchantActivities = merchantActivityService.selectList(new EntityWrapper<MerchantActivity>().eq("companyId", orderLogistics.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1)
                    .eq("state", 1).like("orderType", "%4%").where("((orderAmountFull is not null  and orderAmountFull <= " + orderLogistics.getOrderMoney() + ") or (orderNumber is not null  and orderNumber <= " + list.size() + "))")
                    .in("id", merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList()))
                    .notIn("id", userMerchantCouponService.selectList(new EntityWrapper<UserMerchantCoupon>().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList()))
            );
            state = orderLogistics.getState();
        }
 
        if(orderType == 5){
            OrderLogistics orderLogistics = orderLogisticsService.selectById(orderId);
            List<OrderLogistics> list = orderLogisticsService.selectList(new EntityWrapper<OrderLogistics>().eq("userId", uid).eq("type", orderLogistics.getType()).eq("state", 9).eq("isDelete", 1));
            merchantActivities = merchantActivityService.selectList(new EntityWrapper<MerchantActivity>().eq("companyId", orderLogistics.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1)
                    .eq("state", 1).like("orderType", "%5%").where("((orderAmountFull is not null  and orderAmountFull <= " + orderLogistics.getOrderMoney() + ") or (orderNumber is not null  and orderNumber <= " + list.size() + "))")
                    .in("id", merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList()))
                    .notIn("id", userMerchantCouponService.selectList(new EntityWrapper<UserMerchantCoupon>().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList()))
            );
            state = orderLogistics.getState();
        }
 
        List<MerchantCouponListWarpper> list = new ArrayList<>();
        if((state == 8 || state == 9) && merchantActivities.size() > 0){
            for(MerchantActivity merchantActivity :merchantActivities){
                List<MerchantActivitySlave> merchantActivitySlaves = merchantActivitySlaveService.selectList(new EntityWrapper<MerchantActivitySlave>().eq("merchantActivityId", merchantActivity.getId()).gt("laveNumber", 0));
 
                for(MerchantActivitySlave merchantActivitySlave : merchantActivitySlaves){
                    UserMerchantCoupon userMerchantCoupon = new  UserMerchantCoupon();
                    try {
                        userMerchantCoupon.setCode(UUIDUtil.getRandomCode(16));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    userMerchantCoupon.setUserType(1);
                    userMerchantCoupon.setUserId(uid);
                    userMerchantCoupon.setMerchantActivityId(merchantActivity.getId());
                    userMerchantCoupon.setMerchantCouponId(merchantActivitySlave.getMerchantCouponId());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + merchantActivity.getEffectiveDays());
                    userMerchantCoupon.setEndTime(calendar.getTime());
                    userMerchantCoupon.setStatus(1);
                    userMerchantCoupon.setState(1);
                    userMerchantCoupon.setCreateTime(new Date());
                    this.insert(userMerchantCoupon);
 
                    MerchantCoupon merchantCoupon = merchantCouponService.selectById(merchantActivitySlave.getMerchantCouponId());
                    MerchantCouponListWarpper merchantCouponListWarpper = new MerchantCouponListWarpper();
                    merchantCouponListWarpper.setId(merchantActivitySlave.getMerchantCouponId());
                    merchantCouponListWarpper.setName(merchantCoupon.getName());
                    merchantCouponListWarpper.setType(merchantCoupon.getType());
                    merchantCouponListWarpper.setFullAmount(merchantCoupon.getFullAmount());
                    merchantCouponListWarpper.setDiscount(merchantCoupon.getDiscount());
                    list.add(merchantCouponListWarpper);
 
                    merchantActivitySlave.setLaveNumber(merchantActivitySlave.getLaveNumber() - 1);
                    if(merchantActivitySlave.getLaveNumber() == 0){
                        merchantActivity.setStatus(2);
                    }
                }
                if(merchantActivitySlaves.size() > 0){
                    merchantActivitySlaveService.updateBatchById(merchantActivitySlaves);
                }
            }
            merchantActivityService.updateBatchById(merchantActivities);
        }
        return list;
    }
 
    @Override
    public synchronized List<MerchantCouponListWarpper> getMerchantCoupon(Integer uid) throws Exception {
        UserInfo userInfo = userInfoService.selectById(uid);
        String value = redisUtil.getValue("merchantVoucher");
        List<MerchantCouponListWarpper> listWarppers = new ArrayList<>();
        if(ToolUtil.isNotEmpty(value)){
            JSONObject jsonObject = JSON.parseObject(value);
            JSONArray jsonArray = jsonObject.getJSONArray(userInfo.getPhone());
            if(null != jsonArray){
                listWarppers = jsonArray.toJavaList(MerchantCouponListWarpper.class);
            }
            jsonObject.put(userInfo.getPhone(), new JSONArray());
            redisUtil.setStrValue("merchantVoucher", jsonObject.toJSONString());
        }
 
        return listWarppers;
    }
}