luodangjia
2024-09-24 7b55e2cd82647b87d79a778ed70fdbddb39ac00a
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.dto.CouponListDto;
import com.ruoyi.account.api.model.TAppCoupon;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserVipDetail;
import com.ruoyi.account.api.query.ExchangeRecordGoodsQuery;
import com.ruoyi.other.api.vo.CouponListVOVO;
import com.ruoyi.account.api.vo.ExchangeRecordVO;
import com.ruoyi.account.mapper.TAppCouponMapper;
import com.ruoyi.account.mapper.TAppUserMapper;
import com.ruoyi.account.service.TAppCouponService;
import com.ruoyi.account.service.TAppUserVipDetailService;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.page.BasePage;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.feignClient.OrderClient;
import com.ruoyi.order.api.query.TChargingCountQuery;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.domain.TVip;
import com.ruoyi.other.api.feignClient.OtherClient;
import com.ruoyi.other.api.feignClient.VipClient;
import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-08-06
 */
@Service
public class TAppCouponServiceImpl extends ServiceImpl<TAppCouponMapper, TAppCoupon> implements TAppCouponService {
 
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private OtherClient otherClient;
    @Autowired
    private VipClient vipClient;
    @Autowired
    private ChargingPileClient chargingPileClient;
    @Autowired
    private ChargingOrderClient chargingOrderClient;
    @Autowired
    private TAppUserVipDetailService tAppUserVipDetailService;
    @Autowired
    private TAppUserMapper tAppUserMapper;
    @Autowired
    private TokenService tokenService;
 
    @Override
    public List<ExchangeRecordVO> pagelist(ExchangeRecordGoodsQuery dto) {
        List<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(2);
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<TAppCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_id",dto.getCouponId());
        wrapper.in("ways_to_obtain",integers);
        if (StringUtils.hasLength(dto.getPhone())){
            List<Long> collect = tAppUserMapper.selectList(new QueryWrapper<TAppUser>().like("phone", dto.getPhone())).stream()
                    .map(TAppUser::getId).collect(Collectors.toList());
            if(collect.isEmpty()){
                collect.add(-1L);
            }
            wrapper.in("id",collect);
        }
        if (dto.getStatus()!=null){
            wrapper.eq("status",dto.getStatus());
        }
        List<TAppCoupon> tAppCoupons = this.baseMapper.selectList(wrapper);
        List<ExchangeRecordVO> list = new ArrayList<>();
        // 订单ids
        StringBuilder stringBuilder = new StringBuilder();
        for (TAppCoupon tAppCoupon : tAppCoupons) {
            ExchangeRecordVO exchangeRecordVO = new ExchangeRecordVO();
            BeanUtils.copyProperties(tAppCoupon,exchangeRecordVO);
            list.add(exchangeRecordVO);
        }
        for (ExchangeRecordVO exchangeRecordVO : list) {
            TAppUser tAppUser = tAppUserMapper.selectById(exchangeRecordVO.getAppUserId());
            if (tAppUser!=null)exchangeRecordVO.setPhone(tAppUser.getPhone());
            switch (exchangeRecordVO.getWaysToObtain()){
                case 1:
                    if (exchangeRecordVO.getId()!=null){
                        stringBuilder.append("-").append(exchangeRecordVO.getId()).append(",");
                    }else{
                        stringBuilder.append("0").append(",");
                    }
                    break;
                case 2:
                    if (exchangeRecordVO.getId()!=null){
                        stringBuilder.append(exchangeRecordVO.getId()).append(",");
                    }else{
                        stringBuilder.append("0").append(",");
                    }
                    break;
            }
            Date date = DateUtils.toDate(exchangeRecordVO.getEndTime());
            Date date1 = DateUtils.toDate(exchangeRecordVO.getCreateTime());
            exchangeRecordVO.setEndTime1(format.format(date));
            exchangeRecordVO.setCreateTime1(format.format(date1));
            if (exchangeRecordVO.getStatus()==1){
                if (exchangeRecordVO.getDelFlag()){
                    // 删除了 就是已取消状态
                    exchangeRecordVO.setStatus(4);
                }else{
                    // 未删除 判断endTime是否过期
                    // 设置了过期时间
                    if (exchangeRecordVO.getEndTime().isBefore(java.time.LocalDateTime.now())){
                        exchangeRecordVO.setStatus(3);
                    }else{
                        // 没过期
                        exchangeRecordVO.setStatus(1);
                    }
                }
            }
            if (exchangeRecordVO.getStatus()==2){
                if (exchangeRecordVO.getDelFlag()){
                    // 删除了 就是已取消状态
                    exchangeRecordVO.setStatus(4);
                }else{
                    exchangeRecordVO.setStatus(2);
                }
            }
        }
        // 去除最后一个字符
        if (StringUtils.hasText(stringBuilder.toString())){
            String string = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
            // 远程调用 查询关联的订单编号
            List<String> data = orderClient.getCodeByOrderId(string).getData();
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setOrderId(data.get(i));
            }
        }
        return list;
    }
 
 
 
    @Override
    public PageInfo<CouponListVOVO> couponList(CouponListDto dto) {
        // todo token获取用户id
        dto.setUserId(tokenService.getLoginUserApplet().getUserId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageInfo<CouponListVOVO> pageInfo = new PageInfo<>(dto.getPageCurr(),dto.getPageSize());
        List<CouponListVOVO> list = this.baseMapper.couponList(pageInfo,dto.getUserId());
        TAppUser tAppUser = tAppUserMapper.selectById(dto.getUserId());
        Integer isVip = 0;// 是否能享受会员折扣 0否 1是普通会员 2内部会员
        // 会员折扣
        Double discount = 0.0;
        // 普通会员最高折扣金额
        Double discountMoney = 0.0;
        if (tAppUser.getVipEndTime() == null){
            // 不是会员
            isVip = 0;
        }else {
            // 判断会员有没有过期
            if (tAppUser.getVipEndTime().isAfter(java.time.LocalDateTime.now())){
                // 没过期 判断用户本月会员充电折扣次数是否还有剩余
                TAppUserVipDetail tAppUserVipDetail = tAppUserVipDetailService.list(new LambdaQueryWrapper<TAppUserVipDetail>()
                        .orderByDesc(TAppUserVipDetail::getStartTime)).get(0);
                LocalDateTime startTime = tAppUserVipDetail.getStartTime();
                LocalDateTime endTime = tAppUserVipDetail.getEndTime();
                TChargingCountQuery tChargingCountQuery = new TChargingCountQuery();
                tChargingCountQuery.setStartTime(startTime);
                tChargingCountQuery.setEndTime(endTime);
                tChargingCountQuery.setUserId(dto.getUserId());
                // 当月已享受会员折扣充电次数
                Integer data = chargingOrderClient.getChargingCount(tChargingCountQuery).getData();
                // 查询用户当前会员每月享受折扣次数和折扣比例是多少
                Integer vipId = tAppUser.getVipId();
                TVip data1 = vipClient.getInfo1(vipId).getData();
                Integer discountTimes = data1.getDiscountTimes();
                if (data>=discountTimes){
                    //  折扣次数用光了 不能再享受会员折扣
                    isVip = 0;
                }else{
                    if (data1.getType() == 2){
                        // 如果内部会员 使用该折扣
                        discount = data1.getDiscount().doubleValue();
                        isVip = 2;
                    }else{
                        // 普通会员 最高折扣金额
                        discountMoney = data1.getMaximumDeduction().doubleValue();
                        isVip = 1;
                    }
 
                }
            }else{
                // 会员过期 不能享受
                isVip = 0;
            }
        }
        for (CouponListVOVO couponListVOVO : list) {
            couponListVOVO.setValidityTime("有效期:"+format.format(couponListVOVO.getCreateTime())+" - "+format.format(couponListVOVO.getEndTime()));
        }
        List<CouponListVOVO> data = otherClient.getCouponInfoByCouponIds(list).getData();
        // 桩编号 + 是否是会员 + 内部会员折扣 + 普通会员最高折扣金额 + 预付金额
        String temp = dto.getNumber()+"-"+isVip+"-"+discount+"-"+discountMoney+"-"+dto.getPayMoney();
        // 根据当前时间段和预付金额计算服务费
        BigDecimal serviceMoney = chargingPileClient.getServiceMoney(temp).getData();
        for (CouponListVOVO datum : data) {
            // 判断优惠券类型
            switch (datum.getPreferentialMode()){
                case 1:
                    datum.setIsUse(1);
                    // 满减券 判断服务费是否满足满减条件
                    if (datum.getMeetTheConditions().compareTo(new BigDecimal(BigInteger.ZERO)) == 0){
                        // 无门槛优惠券
                        datum.setMoney(serviceMoney.subtract(datum.getDiscountAmount()));
                    }else if (serviceMoney.compareTo(datum.getMeetTheConditions())>=0){
                        // 满足条件
                        datum.setMoney(serviceMoney.subtract(datum.getDiscountAmount()));
                    }else{
                        // 不满足条件
                        datum.setMoney(serviceMoney);
                        datum.setIsUse(0);
                    }
                    break;
                case 2:
                    datum.setIsUse(1);
                    // 折扣券 判断
                    if (datum.getMeetTheConditions().compareTo(new BigDecimal(BigInteger.ZERO)) == 0){
                        // 无门槛优惠券
                        datum.setMoney(serviceMoney.subtract(datum.getDiscountAmount()));
                    }else if (serviceMoney.compareTo(datum.getMeetTheConditions())>=0){
                        // 折扣后的服务费
                        BigDecimal divide = serviceMoney.multiply(datum.getDiscount()).divide(new BigDecimal(10), 2, BigDecimal.ROUND_HALF_UP);
                        // 计算折扣了多少钱 判断是否超过最高折扣金额
                        BigDecimal subtract = serviceMoney.subtract(divide);
                        if (subtract.compareTo(datum.getMaximumDiscountAmount())>0){
                            // 超过最高折扣金额 减去最高折扣金额
                            datum.setMoney(serviceMoney.subtract(datum.getMaximumDiscountAmount()));
                        }else{
                            // 没超过最高折扣金额
                            datum.setMoney(divide);
                        }
                    }else{
                        // 不满足条件
                        datum.setMoney(serviceMoney);
                        datum.setIsUse(0);
                    }
                    break;
            }
        }
        // 通过集合的money、isUse字段 将money、isUse从大到小排序 将不可用的排在最后
        data.sort(Comparator.comparing(CouponListVOVO::getMoney).reversed().thenComparing(CouponListVOVO::getIsUse));
        pageInfo.setRecords(data);
        return pageInfo;
    }
}