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,"账号不存在");
|
}
|
|
}
|
}
|