1
Sunshine
2024-11-05 1ec0f818f512186b3af02637906632264fe97119
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
package com.nova.sankuai.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nova.sankuai.domain.api.tongbao.TBResposeInfo;
import com.nova.sankuai.domain.api.tongbao.TBUtil;
import com.nova.sankuai.domain.api.weiyaquanyi.WeiYaConfigProperties;
import com.nova.sankuai.domain.api.weiyaquanyi.WeiYaKeyUtil;
import com.nova.sankuai.domain.api.weiyaquanyi.WeiYaRespVO;
import com.nova.sankuai.domain.api.weiyaquanyi.WeiYaUtil;
import com.nova.sankuai.domain.entity.CustomerUser;
import com.nova.sankuai.domain.entity.TongbaoLog;
import com.nova.sankuai.domain.entity.VipCard;
import com.nova.sankuai.domain.vo.MemberRightAutoLoginVo;
import com.nova.sankuai.infra.config.CommonException;
import com.nova.sankuai.infra.mapper.CustomerUserMapper;
import com.nova.sankuai.infra.mapper.TongbaoLogMapper;
import com.nova.sankuai.infra.mapper.VipCardMapper;
import com.nova.sankuai.security.UserDetail;
import com.nova.sankuai.service.IMemberRightService;
import com.nova.sankuai.service.IShanTaiService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.Objects;
 
/**
 * @Description 会员权益ServiceImpl
 * @Author CWR
 * @Date 2022/4/4 16:04
 */
 
@Slf4j
@Service
@AllArgsConstructor
public class MemberRightServiceImpl implements IMemberRightService {
 
    private final WeiYaUtil weiYaUtil;
    private final WeiYaConfigProperties properties;
 
    private final CustomerUserMapper customerUserMapper;
 
    private final VipCardMapper vipCardMapper;
 
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
 
    private final TongbaoLogMapper tongbaoLogMapper;
 
    private final IShanTaiService shanTaiService;
    /**
     * 渠道商用户开通会员卡回调
     *
     * @param userId          渠道侧会员id
     * @param channelMemberId 渠道侧会员卡id
     * @return
     */
    @Override
    public WeiYaRespVO channelMemberNotify(String userId, String channelMemberId) {
        return this.weiYaUtil.channelMemberNotify(userId, channelMemberId);
    }
 
    /**
     * 联登接口(AES加密请求参数并返回)
     *
     * @param vo 请求参数
     * @return
     */
    @Override
    public String autoLogin(MemberRightAutoLoginVo vo) throws JsonProcessingException {
        vo.setChannelCode(properties.getChannelCode());
        String json = OBJECT_MAPPER.writeValueAsString(vo);
        return WeiYaKeyUtil.encryptAES(json, properties.getAesSecretKey());
    }
 
 
    @Override
    public String getChannelMemberId(UserDetail user) {
        if (user == null) {
            throw new CommonException("用户不存在");
        }
        CustomerUser customerUser = customerUserMapper.selectOne(Wrappers.<CustomerUser>lambdaQuery()
                .eq(CustomerUser::getPhone, user.getPhone()));
        if (customerUser == null) {
            throw new CommonException("用户不存在");
        }
        int compare = DateUtil.compare(new Date(), customerUser.getVipEndDate());
        if (compare > 0) {
            throw new CommonException("当前用户不存在有效的会员权益");
        }
        if (StringUtils.isNotEmpty(customerUser.getVipCardId())) {
            return customerUser.getVipCardId();
        } else {
            VipCard vipCard = vipCardMapper.getVipLowestCard();
            return vipCard.getCardId();
        }
    }
 
    @Override
    public String membershipInterests(String phone) {
        //获取当前用户
        CustomerUser customerUser = customerUserMapper.selectOne(new LambdaQueryWrapper<CustomerUser>()
                .eq(CustomerUser::getPhone, phone));
        //判断当前用户是否已经开通会员权益 true为开通
        boolean ownership = isOwnership(customerUser);
        String h5Url = "";
        String serialNo = TBUtil.getOrder();
        if (!ownership) {
            //创建订单
            TBResposeInfo tbRespose = TBUtil.createOrder(customerUser, serialNo);
            if (tbRespose == null) {
                throw new CommonException("通宝会员权益创建订单失败");
            }
            //存储数据记录
            insertLog(customerUser, tbRespose, serialNo);
            //修改用户
            updateCustomer(customerUser, tbRespose);
            h5Url = TBUtil.getH5Url(tbRespose.getOrderNumber(), customerUser);
        } else {
            //判断该用户是不是绑卡闪态
            if (Objects.nonNull(customerUser)){
                h5Url = shanTaiService.getEquityUrl(customerUser);
            }
            if (Objects.isNull(h5Url)){
                h5Url = TBUtil.getH5Url(customerUser.getOrderNumber(), customerUser);
            }
        }
        return h5Url;
 
    }
 
    @Override
    public String manualMembershipInterests(String phone) {
        //获取当前用户
        CustomerUser customerUser = customerUserMapper.selectOne(new LambdaQueryWrapper<CustomerUser>()
                .eq(CustomerUser::getPhone, phone));
        boolean ownership = isOwnership(customerUser);
        if (!ownership) {
            String serialNo = TBUtil.getOrder();
            //创建订单
            TBResposeInfo tbRespose = TBUtil.createOrder(customerUser, serialNo);
            if (tbRespose == null) {
                throw new CommonException("通宝会员权益创建订单失败");
            }
            //存储数据记录
            insertLog(customerUser, tbRespose, serialNo);
            //修改用户
            updateCustomer(customerUser, tbRespose);
            return "操作成功";
        } else {
            throw new CommonException("该用户已开通会员权益");
        }
    }
 
    public boolean isOwnership(CustomerUser customerUser) {
        if (customerUser == null) {
            throw new CommonException("当前用户不存在");
        }
        Date date = new Date();
        if (customerUser.getVipStartDate() == null && customerUser.getVipEndDate() == null
                && customerUser.getSeniorVipStartDate() == null && customerUser.getSeniorVipEndDate() == null) {
            throw new CommonException("当前用户不是会员");
        } else if (date.getTime() > customerUser.getVipEndDate().getTime()) {
            throw new CommonException("当前用户会员已过期");
        }
        if (StringUtils.isEmpty(customerUser.getOrderNumber())) {
            return false;
        }
        return true;
    }
 
    public void insertLog(CustomerUser customerUser, TBResposeInfo tbRespose, String serialNo) {
        TongbaoLog tongbaoLog = new TongbaoLog();
        tongbaoLog.setCustomerUserId(customerUser.getId());
        tongbaoLog.setCreationDate(new Date());
        tongbaoLog.setOrderValidEndDay(tbRespose.getOrderValidEndDay());
        tongbaoLog.setOrderNumber(tbRespose.getOrderNumber());
        tongbaoLog.setPhone(customerUser.getPhone());
        tongbaoLog.setName(customerUser.getName());
        tongbaoLog.setSerialNo(serialNo);
        tongbaoLogMapper.insert(tongbaoLog);
    }
 
 
    public void updateCustomer(CustomerUser customerUser, TBResposeInfo tbRespose) {
        customerUserMapper.update(null, Wrappers.<CustomerUser>lambdaUpdate()
                .set(CustomerUser::getOrderNumber, tbRespose.getOrderNumber())
                .set(CustomerUser::getOrderValidEndDay, tbRespose.getOrderValidEndDay())
                .eq(CustomerUser::getId, customerUser.getId()));
    }
 
}