xiaoyong931011
2021-07-16 4f60ffbbc73de024fd11146a7c73698044b2504b
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
package com.xzx.gc.user.controller;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.PayEnum;
import com.xzx.gc.common.dto.log.OperationAppLog;
import com.xzx.gc.common.request.BaseController;
import com.xzx.gc.common.utils.MqUtil;
import com.xzx.gc.entity.AccountBindInfo;
import com.xzx.gc.entity.AccountInfo;
import com.xzx.gc.entity.OtherUserInfo;
import com.xzx.gc.user.dto.AccountBindExtraDto;
import com.xzx.gc.user.mapper.AccountBindInfoMapper;
import com.xzx.gc.user.mapper.AccountMapper;
import com.xzx.gc.user.mapper.OtherUserMapper;
import com.xzx.gc.user.service.AccountBindService;
import com.xzx.gc.user.service.AccountService;
import com.xzx.gc.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Api(tags = "账号绑定第三方信息")
@RequestMapping("/account/bind")
@RestController
@Validated
public class AccountBindController extends BaseController {
 
    @Autowired
    private AccountMapper accountMapper;
 
    @Autowired
    private AccountBindService accountBindService;
 
    @Autowired
    private AccountBindInfoMapper accountBindInfoMapper;
 
    @Autowired
    OtherUserMapper otherUserMapper;
 
    @Autowired
    private AccountService accountService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private MqUtil mqUtil;
 
 
    @ApiOperation( value = "查询绑定信息")
    @PostMapping("/list")
    public Result<List<AccountBindExtraDto>> bindList(HttpServletRequest request) {
        Result result=new Result();
        String userId = getUserId(request);
        AccountInfo accountInfo1 = accountService.findByUserId(userId);
        if(accountInfo1!=null){
            AccountBindInfo accountBindInfo=new AccountBindInfo();
            accountBindInfo.setAccountId(accountInfo1.getAccountId());
            accountBindInfo.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));
            List<AccountBindInfo> select = accountBindInfoMapper.select(accountBindInfo);
            if(CollUtil.isNotEmpty(select)){
                List<AccountBindExtraDto> list=new ArrayList<>();
 
                //按绑定类型分组
                Map<Short, List<AccountBindInfo>> collect = select.stream().collect(Collectors.groupingBy(AccountBindInfo::getAccountType));
                for (Short aShort : collect.keySet()) {
                         AccountBindExtraDto dto=new AccountBindExtraDto();
                        if(PayEnum.银行卡.getValue().equals(aShort.toString())){
                            dto.setAccountType(PayEnum.银行卡.getValue());
                            List<AccountBindInfo> accountBindInfos = collect.get(aShort);
                            accountBindInfos.stream().filter(x-> {
                                x.setAccountNumber(StrUtil.replace(x.getAccountNumber(), 6, 10, Convert.toChar("*")));
                                return true;
                            }).collect(Collectors.toList());
                            dto.setList(accountBindInfos);
                            list.add(dto);
                        }else{
                            long count = list.stream().filter(x -> PayEnum.支付宝加微信.getValue().equals(x.getAccountType())).count();
                            dto.setAccountType(PayEnum.支付宝加微信.getValue());
                            if(count==0){
                                dto.setList(collect.get(aShort));
                                list.add(dto);
                            }else{
                                List<AccountBindExtraDto> collect1 = list.stream().filter(x -> PayEnum.支付宝加微信.getValue().equals(x.getAccountType())).collect(Collectors.toList());
                                List<AccountBindInfo> list1 = collect1.get(0).getList();
                                list1.addAll(collect.get(aShort));
                            }
                        }
                }
                result.setData(list);
            }
        }
        return result;
    }
 
 
    @ApiOperation( value = "第三方账号绑定")
    @PostMapping("")
    public Result<String> bind(HttpServletRequest request,@Validated @RequestBody AccountBindInfo accountBindInfo) {
        if(PayEnum.微信.getValue().equals(Convert.toStr(accountBindInfo.getAccountType()))){
            Validator.validateNotEmpty(accountBindInfo.getAccountNumber(),"微信OPENID不能为空");
        }else if(PayEnum.支付宝.getValue().equals(Convert.toStr(accountBindInfo.getAccountType()))){
            Validator.validateNotEmpty(accountBindInfo.getAccountNumber(),"支付宝账号不能为空");
            Validator.validateNotEmpty(accountBindInfo.getAccountName(),"真实姓名不能为空");
        }else if(PayEnum.银行卡.getValue().equals(Convert.toStr(accountBindInfo.getAccountType()))){
            Validator.validateNotEmpty(accountBindInfo.getAccountNumber(),"银行卡号不能为空");
            Validator.validateNotEmpty(accountBindInfo.getAccountName(),"开户人姓名不能为空");
            String bankName = accountBindInfo.getBankName();
            Validator.validateNotEmpty(bankName,"开户行不能为空");
            String[] split = bankName.split(",");
            if(split.length==1){
                return Result.error(-1,"开户支行不能为空");
            }
        }else{
            return Result.error(-1,"账号类型不正确");
        }
 
        Result result=new Result();
        AccountInfo accountInfo1 = accountService.findByUserIdForbidden(getUserId(request));
        if(accountInfo1!=null){
            accountBindInfo.setAccountId(accountInfo1.getAccountId());
            AccountBindInfo byAccountIdAndType = accountBindService.findByAccountIdAndType(accountBindInfo.getAccountId(), accountBindInfo.getAccountType());
            if(byAccountIdAndType==null){
                accountBindService.add(accountBindInfo);
            }else{
                accountBindInfo.setId(byAccountIdAndType.getId());
                accountBindService.updateById(accountBindInfo);
            }
 
            //如果是微信支付 更新OpenId
            if(PayEnum.微信.getValue().equals(Convert.toStr(accountBindInfo.getAccountType()))) {
                OtherUserInfo otherUserInfo = new OtherUserInfo();
                otherUserInfo.setUserId(getUserId(request));
                otherUserInfo.setOpenId(accountBindInfo.getAccountNumber());
                otherUserMapper.updateByPrimaryKeySelective(otherUserInfo);
            }
 
            String mobilePhone = userService.findOtherByUserId(getUserId(request),0);
            OperationAppLog build = OperationAppLog.builder().appPrograme(getFrontClient(request)).opreateName(mobilePhone)
                    .methodName(Constants.USER_MODUL_NAME).operateAction("第三方账号绑定-"+getUserId(request)).build();
            mqUtil.sendApp(build);
 
            return result;
        }else{
            return Result.error(-1,"账号不存在");
        }
 
    }
}