Administrator
2026-06-13 befbb28b2810ed108d2744bceee2bb9b3edaa9bc
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.enumerates.ScoreFlowTypeEnum;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.*;
import cc.mrbird.febs.mall.controller.dependentStation.enums.SalesServiceEnums;
import cc.mrbird.febs.mall.conversion.MallMemberConversion;
import cc.mrbird.febs.mall.conversion.MallShopApplyConversion;
import cc.mrbird.febs.mall.conversion.MallStoreConversion;
import cc.mrbird.febs.mall.dto.*;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.*;
import cc.mrbird.febs.mall.vo.*;
import cc.mrbird.febs.pay.model.BrandWCPayRequestData;
import cc.mrbird.febs.pay.service.IXcxPayService;
import cc.mrbird.febs.pay.util.MD5;
import cc.mrbird.febs.vip.VipSettingUnAliveSettingBo;
import cc.mrbird.febs.vip.entity.MallVipConfig;
import cc.mrbird.febs.vip.mapper.MallVipConfigMapper;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author wzy
 * @date 2021-09-16
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ApiMallMemberServiceImpl extends ServiceImpl<MallMemberMapper, MallMember> implements IApiMallMemberService {
 
    private final MallMemberWalletMapper mallMemberWalletMapper;
    private final ICommonService commonService;
    private final RedisUtils redisUtils;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallShopApplyMapper mallShopApplyMapper;
    private final MallAgentRecordMapper mallAgentRecordMapper;
    private final CouponGoodsMapper couponGoodsMapper;
    private final MallMemberCouponMapper mallMemberCouponMapper;
    private final MallAddressInfoMapper mallAddressInfoMapper;
 
 
    @Value("${spring.profiles.active}")
    private String active;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public FebsResponse register(RegisterDto registerDto) {
        String account = registerDto.getAccount();
        MallMember mallMember = this.baseMapper.selectInfoByAccount(account);
        if (mallMember != null) {
            throw new FebsException("Registered");
        }
        /**
         * 验证两次密码是否一致
         */
        String password = registerDto.getPassword();
        String confirmPassword = registerDto.getConfirmPassword();
        if (!password.equals(confirmPassword)) {
            throw new FebsException("The two passwords are inconsistent");
        }
        mallMember = new MallMember();
        mallMember.setName(registerDto.getLastName() + registerDto.getFirstName());
        mallMember.setPhone(registerDto.getPhoneNumber());
        mallMember.setEmail(registerDto.getAccount());
        mallMember.setPassword(SecureUtil.md5(password));
        mallMember.setAccountStatus(MallMember.ACCOUNT_STATUS_ENABLE);
        mallMember.setAccountType(MallMember.ACCOUNT_TYPE_NORMAL);
        this.baseMapper.insert(mallMember);
 
        MallAddressInfo mallAddressInfo = new MallAddressInfo();
        mallAddressInfo.setMemberId(mallMember.getId());
        mallAddressInfo.setName(registerDto.getLastName());
        mallAddressInfo.setFristName(registerDto.getFirstName());
        mallAddressInfo.setPhone(registerDto.getPhoneNumber());
        mallAddressInfoMapper.insert(mallAddressInfo);
 
 
        MallMemberWallet wallet = new MallMemberWallet();
        wallet.setBalance(BigDecimal.ZERO);
        wallet.setMemberId(mallMember.getId());
        mallMemberWalletMapper.insert(wallet);
        return new FebsResponse().success().message("registered successfully");
    }
 
    @Override
    public FebsResponse toLogin(LoginDto loginDto) {
        String md5Pwd = SecureUtil.md5(loginDto.getPassword());
 
        MallMember mallMember = this.baseMapper.selectInfoByAccountAndPwd(loginDto.getAccount(), md5Pwd);
        if (mallMember == null) {
            throw new FebsException("The user does not exist or the account password is incorrect");
        }
 
        if (MallMember.ACCOUNT_STATUS_DISABLED.equals(mallMember.getAccountStatus())) {
            throw new FebsException("The account is abnormal and is temporarily restricted from logging in");
        }
 
        String redisKey = AppContants.APP_LOGIN_PREFIX + mallMember.getId();
        String existToken = redisUtils.getString(redisKey);
        if (StrUtil.isNotBlank(existToken)) {
            Object o = redisUtils.get(existToken);
            if (ObjectUtil.isNotEmpty(o)) {
                redisUtils.del(existToken);
            }
        }
 
        String token = IdUtil.simpleUUID();
        redisUtils.set(token, JSONObject.toJSONString(mallMember), 360000);
        redisUtils.set(redisKey, token, 360000);
        Map<String, Object> authInfo = new HashMap<>();
        authInfo.put("token", token);
        authInfo.put("rasToken", generateAsaToken(token));
        return new FebsResponse().success().data(authInfo);
    }
 
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, AppContants.PUBLIC_KEY);
//        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);
        //去掉时间戳
        return rsa.encryptBase64(token, KeyType.PublicKey);
    }
 
    @Override
    public FebsResponse forgetPwd(ForgetPwdDto forgetPwdDto) {
 
        Long id = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = this.baseMapper.selectById(id);
        if (mallMember == null) {
            throw new FebsException("Setup failed");
        }
 
        if (!forgetPwdDto.getNewPassword().equals(forgetPwdDto.getNewPasswordAgain())){
            throw new FebsException("Setup failed");
        }
 
        String pwd = SecureUtil.md5(forgetPwdDto.getNewPassword());
        mallMember.setPassword(pwd);
 
        this.baseMapper.update(
                null,
                Wrappers.lambdaUpdate(MallMember.class)
                .set(MallMember::getPassword, pwd)
                .eq(MallMember::getId, id)
        );
        return new FebsResponse().success().message("Setup successful");
    }
 
    @Override
    public FebsResponse logout() {
        Long id = LoginUserUtil.getLoginUser().getId();
 
        String redisKey = AppContants.APP_LOGIN_PREFIX + id;
        String existToken = redisUtils.getString(redisKey);
        if (StrUtil.isNotBlank(existToken)) {
            Object o = redisUtils.get(existToken);
            if (ObjectUtil.isNotEmpty(o)) {
                redisUtils.del(existToken);
            }
        }
        redisUtils.del(AppContants.APP_LOGIN_PREFIX + id);
        redisUtils.del(AppContants.XCX_LOGIN_PHONE_PREFIX + id);
        return new FebsResponse().success().message("Log out");
    }
 
    @Override
    public FebsResponse findMemberInfo() {
        Long id = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = this.baseMapper.selectById(id);
        MallMemberVo mallMemberVo = new MallMemberVo();
        mallMemberVo.setName(mallMember.getName());
        mallMemberVo.setEmail(mallMember.getEmail());
        mallMemberVo.setPhoneNumber(mallMember.getPhone());
        mallMemberVo.setAvatar(mallMember.getAvatar());
        return new FebsResponse().success().data(mallMemberVo);
    }
 
 
 
    @Override
    public FebsResponse modifyMemberInfo(ModifyMemberInfoDto modifyMemberInfoDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = this.baseMapper.selectById(memberId);
        if (StrUtil.isNotBlank(modifyMemberInfoDto.getName())) {
            mallMember.setName(modifyMemberInfoDto.getName());
        }
 
        this.baseMapper.updateById(mallMember);
        return new FebsResponse().success().message("success");
    }
 
 
 
    @Override
    public void addMoneyFlow(Long memberId, BigDecimal amount, Integer type, String orderNo, String description, String remark, Long rtMemberId, Integer status, Integer flowType) {
        MallMoneyFlow flow = new MallMoneyFlow();
        flow.setMemberId(memberId);
        flow.setAmount(amount);
        flow.setType(type);
        flow.setOrderNo(orderNo);
        flow.setDescription(description);
        flow.setRemark(remark);
        flow.setRtMemberId(rtMemberId);
        flow.setStatus(status);
        flow.setFlowType(flowType);
        mallMoneyFlowMapper.insert(flow);
    }
 
 
 
    @Override
    public void bindPhone(AccountAndCodeDto accountAndCodeDto) {
        boolean b = commonService.verifyCode(accountAndCodeDto.getAccount(), accountAndCodeDto.getCode());
        if (!b) {
            throw new FebsException("验证码错误");
        }
 
        Long id = LoginUserUtil.getLoginUser().getId();
        MallMember member = this.baseMapper.selectById(id);
 
        member.setPhone(accountAndCodeDto.getAccount());
        this.baseMapper.updateById(member);
    }
 
 
 
    @Override
    public void shopApply(ShopApplyDto shopApplyDto) {
        MallMember member = LoginUserUtil.getLoginUser();
 
        MallShopApply hasApply = mallShopApplyMapper.selectNewestApplyByMemberId(member.getId());
        if (hasApply != null) {
            if (!hasApply.getStatus().equals(MallShopApply.APPLY_DISAGREE)) {
                throw new FebsException("请勿重复提交申请");
            }
        }
 
        MallShopApply mallShopApply = new MallShopApply();
        BeanUtil.copyProperties(shopApplyDto, mallShopApply);
 
        mallShopApply.setStatus(MallShopApply.APPLY_ING);
        mallShopApply.setMemberId(member.getId());
        mallShopApplyMapper.insert(mallShopApply);
    }
 
    @Override
    public MallShopApply findNewestApply() {
        MallMember member = LoginUserUtil.getLoginUser();
 
        return mallShopApplyMapper.selectNewestApplyByMemberId(member.getId());
    }
 
 
 
    @Override
    public List<ShopListVo> findShopListVo(ShopListDto shopListDto) {
        Page<MallShopApply> page = new Page<>(shopListDto.getPageNow(), shopListDto.getPageSize());
 
        MallShopApply shopApply = new MallShopApply();
        shopApply.setStatus(MallShopApply.APPLY_AGREE);
        IPage<MallShopApply> pageResult = mallShopApplyMapper.selectShopApplyInPage(shopApply, page);
 
        List<MallShopApply> list = pageResult.getRecords();
        if (CollUtil.isEmpty(list)) {
            list = new ArrayList<>();
        }
        return MallShopApplyConversion.INSTANCE.entitiesToVOs(list);
    }
 
 
    private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
 
 
 
    @Override
    public void updateMemberAgent(Long memberId,String levelCode) {
        mallAgentRecordMapper.updateStateByMemberId(memberId);
        MallAgentRecord mallAgentRecord = mallAgentRecordMapper.selectById(memberId);
        //更新用户表中的LEVEL
        MallMember mallMember = this.baseMapper.selectById(mallAgentRecord.getMemberId());
        mallMember.setLevel(levelCode);
        this.baseMapper.updateById(mallMember);
    }
 
 
 
    @Override
    public FebsResponse memberPayCoupon(MallMemberCouponDto mallMemberCouponDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        mallMemberCouponDto.setMemberId(memberId);
        mallMemberCouponDto.setExpireTime(DateUtil.date());
//        List<Long> couponIds = couponGoodsMapper.selectByGoodId(mallMemberCouponDto.getGoodsId());
        List<Long> goodsIdList = mallMemberCouponDto.getGoodsIdList();
        List<Long> couponIds = couponGoodsMapper.selectByGoodIdList(goodsIdList);
 
        List<MallMemberCouponVo> mallMemberCouponVos = new ArrayList<>();
        if(CollUtil.isNotEmpty(couponIds)){
            mallMemberCouponVos = mallMemberCouponMapper.selectListCreateInPage(mallMemberCouponDto,couponIds);
        }
        return new FebsResponse().success().data(mallMemberCouponVos);
    }
 
 
    @Override
    public FebsResponse setInvite(ApiSetInviteDto apiSetInviteDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = this.baseMapper.selectById(memberId);
        if(ObjectUtil.isNotEmpty(mallMember.getReferrerId())){
            throw new FebsException("已绑定");
        }
        String inviteId = apiSetInviteDto.getInviteId();
        if(inviteId.equals(mallMember.getInviteId())){
            return new FebsResponse().success();
        }
        MallMember member = this.baseMapper.selectInfoByInviteId(inviteId);
        if (member != null) {
            if (StrUtil.isNotBlank(member.getReferrerIds()) && member.getReferrerIds().contains(mallMember.getInviteId())) {
                log.info("下级不能邀请上级");
                return new FebsResponse().success();
            }
            mallMember.setReferrerId(member.getInviteId());
 
            //推荐人和推荐人链
            boolean flag = false;
            String parentId = mallMember.getReferrerId();
            if (StrUtil.isBlank(parentId)) {
                flag = true;
            }
            String ids = "";
            while (!flag) {
                if (StrUtil.isBlank(ids)) {
                    ids += parentId;
                } else {
                    ids += ("," + parentId);
                }
                MallMember parentMember = this.baseMapper.selectInfoByInviteId(parentId);
                if (parentMember == null) {
                    break;
                }
                parentId = parentMember.getReferrerId();
                if (StrUtil.isBlank(parentMember.getReferrerId())) {
                    flag = true;
                }
            }
            if (StrUtil.isNotBlank(ids)) {
                mallMember.setReferrerIds(ids);
            }
            this.baseMapper.updateById(mallMember);
        }
        return new FebsResponse().success();
    }
 
    private  String getXcxLoginUrl(String code) {
        String wechatLoginUrl =xcxProperties.getWecharLoginUrl();
        return String.format(wechatLoginUrl, xcxProperties.getXcxAppid(), xcxProperties.getXcxSecret(), code);
    }
 
 
 
    @Override
    public FebsResponse salesService() {
        MallSalesServiceVo mallSalesServiceVo = new MallSalesServiceVo();
 
        List<DataDictionaryCustom> dataDictionaryCustoms = dataDictionaryCustomMapper.selectDicByType(
                SalesServiceEnums.WHATSAPP.getType()
        );
        if (dataDictionaryCustoms.size() > 0){
 
            for (DataDictionaryCustom dataDictionaryCustom : dataDictionaryCustoms){
                if (SalesServiceEnums.WHATSAPP.getCode().equals(dataDictionaryCustom.getCode())){
                    mallSalesServiceVo.setWhatsApp(dataDictionaryCustom.getValue());
                }
                if (SalesServiceEnums.PHONE.getCode().equals(dataDictionaryCustom.getCode())){
                    mallSalesServiceVo.setPhone(dataDictionaryCustom.getValue());
                }
                if (SalesServiceEnums.EMAIL.getCode().equals(dataDictionaryCustom.getCode())){
                    mallSalesServiceVo.setEmail(dataDictionaryCustom.getValue());
                }
                if (SalesServiceEnums.WORKINGHOURS.getCode().equals(dataDictionaryCustom.getCode())){
                    mallSalesServiceVo.setWorkingHours(dataDictionaryCustom.getValue());
                }
                if (SalesServiceEnums.ADDRESS.getCode().equals(dataDictionaryCustom.getCode())){
                    mallSalesServiceVo.setAddress(dataDictionaryCustom.getValue());
                }
            }
 
        }
        return new FebsResponse().success().data(mallSalesServiceVo);
    }
 
    @Override
    public FebsResponse moneyChange() {
        List<MallMoneyChangeVo> vos = new ArrayList<>();
        List<DataDictionaryCustom> dataDictionaryCustoms = dataDictionaryCustomMapper.selectDicByType(
                SalesServiceEnums.USD.getType()
        );
        if (dataDictionaryCustoms.size() > 0){
            for (DataDictionaryCustom dataDictionaryCustom : dataDictionaryCustoms){
                MallMoneyChangeVo vo = new MallMoneyChangeVo();
                    vo.setMoneyChange(dataDictionaryCustom.getValue());
                    vo.setCode(dataDictionaryCustom.getCode());
                    vo.setMoneyCode(dataDictionaryCustom.getDescription());
                    vos.add(vo);
            }
 
        }
        return new FebsResponse().success().data(vos);
 
    }
 
    public static void main(String[] args) {
        Long userld = 173L;
        String shopAccount = "luohu";
        String shopPwd = "123456";
 
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
 
        objectObjectHashMap.put("shopAccount",shopAccount);
        objectObjectHashMap.put("shopPwd",shopPwd);
        objectObjectHashMap.put("userId",userld.toString());
 
        //sign= MD5(shopAccount+shopPwd+userId)
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(shopAccount);
        stringBuffer.append(shopPwd);
        stringBuffer.append(userld);
        String sign = MD5.MD5Encode(stringBuffer.toString());
        objectObjectHashMap.put("sign",sign);
 
        System.out.println(sign);
 
        System.out.println(objectObjectHashMap);
        String url = "https://data.muchun.co/api/getCustomerCheckRecords";
        String result = HttpCurlUtil.sendPost(url, objectObjectHashMap);
        Integer retCode = JSONUtil.parseObj(result).getInt("retCode");
        String message = JSONUtil.parseObj(result).getStr("message");
        if(0 != retCode){
            System.out.println(message);
        }
 
        JSONArray data = JSONUtil.parseObj(result).getJSONArray("data");
        System.out.println(data);
    }
 
 
}