admin
2019-02-21 7e57a20c0ccde504c2f2b2b9fd4a9fd7c89d5e92
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
package org.fanli.service.user.service.impl.account;
 
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.fanli.facade.user.entity.account.BindingAccount;
import org.fanli.facade.user.entity.account.BindingAccountService;
import org.fanli.facade.user.entity.money.UserMoneyDetail;
import org.fanli.facade.user.entity.money.extract.AlipayAccountValidNormalHistory;
import org.fanli.facade.user.exception.account.BindingAccountException;
import org.fanli.facade.user.exception.money.AlipayAccountException;
import org.fanli.facade.user.exception.money.AlipayTransferException;
import org.fanli.facade.user.exception.money.UserMoneyDetailException;
import org.fanli.facade.user.util.factory.UserMoneyDetailFactory;
import org.fanli.service.user.dao.account.UserInfoMapper;
import org.fanli.service.user.dao.money.AlipayAccountValidNormalHistoryMapper;
import org.fanli.service.user.dao.money.BindingAccountMapper;
import org.fanli.service.user.dao.money.UserMoneyDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.StringUtil;
 
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.yeshi.fanli.base.Constant;
import com.yeshi.fanli.base.entity.user.UserInfo;
import com.yeshi.fanli.base.log.LogHelper;
 
import net.sf.json.JSONObject;
 
@Service
public class BindingAccountServiceImpl implements BindingAccountService {
 
    @Resource
    private BindingAccountMapper bindingAccountMapper;
    @Resource
    private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserMoneyDetailMapper userMoneyDetailMapper;
 
    public List<BindingAccount> getBindingAccountByUid(long uid) {
        return bindingAccountMapper.selectByUid(uid);
    }
 
    public void addBindingAccount(BindingAccount addAccount) throws BindingAccountException {
        BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(addAccount.getUserInfo().getId(),
                addAccount.getType());
 
        if (bindingAccount == null) {
            bindingAccountMapper.insertSelective(addAccount);
        } else {
            throw new BindingAccountException(Constant.BA_EXIST);
        }
    }
 
    public Integer deleteBindingAccount(BindingAccount account) {
        BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(account.getUserInfo().getId(),
                account.getType());
        if (bindingAccount != null) {
            bindingAccountMapper.deleteByPrimaryKey(bindingAccount.getId());
            return 1;
        } else
            return 0;
    }
 
    public BindingAccount getBindingAccountByUidAndType(long uid, int type) {
        return bindingAccountMapper.selectByUidAndType(uid, type);
    }
 
    @Override
    public BindingAccount changeAlipayBinding(Long uid, String name, String account) {
        BindingAccount bindingAccount = getBindingAccountByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
        if (bindingAccount == null)// 创建账号
        {
            bindingAccount = new BindingAccount();
            bindingAccount.setAccount(account);
            bindingAccount.setName(name);
            bindingAccount.setType(BindingAccount.TYPE_ALIPAY);
            bindingAccount.setUserInfo(new UserInfo(uid));
            bindingAccountMapper.insertSelective(bindingAccount);
        } else {
            BindingAccount update = new BindingAccount();
            update.setId(bindingAccount.getId());
            update.setName(name);
            update.setAccount(account);
            bindingAccountMapper.updateByPrimaryKeySelective(update);
 
            bindingAccount.setName(name);
            bindingAccount.setAccount(account);
        }
        return bindingAccount;
    }
 
    @Override
    public int deleteByPrimaryKey(Long id) {
        return bindingAccountMapper.deleteByPrimaryKey(id);
    }
 
    @Transactional
    @Override
    public void validAlipayAccount(Long uid, String account, String name)
            throws AlipayTransferException, AlipayApiException, AlipayAccountException {
        if (uid == null)
            throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "用户ID不能为空");
        if (StringUtil.isNullOrEmpty(account))
            throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "账号不能为空");
        if (StringUtil.isNullOrEmpty(name))
            throw new AlipayAccountException(AlipayAccountException.CODE_NO_PARAMS, "真实姓名不能为空");
        //
        List<BindingAccount> bindingAccountList = bindingAccountMapper.selectByAccount(account);
        if (bindingAccountList != null && bindingAccountList.size() > 0) {
            if (bindingAccountList.get(0).getUserInfo().getId().longValue() != uid)
                throw new AlipayAccountException(AlipayAccountException.CODE_ALREADY_BIND,
                        "该支付宝账号已被其他账号绑定,请更换其他的支付宝账号来绑定");
        }
 
        // TODO 做频率验证-每月验证一次
        AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid);
        if (latest != null) {
            Calendar caLatest = Calendar.getInstance();
            caLatest.setTimeInMillis(latest.getCreateTime().getTime());
            Calendar nowLatest = Calendar.getInstance();
            if (caLatest.get(Calendar.MONTH) == nowLatest.get(Calendar.MONTH))// 上次更改和现在是同一个月
                throw new AlipayAccountException(AlipayAccountException.CODE_TIMES_LIMIT, "每月只能更换绑定一次支付宝账号,请次月再试。");
        }
 
        UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0)
            throw new AlipayAccountException(AlipayAccountException.CODE_NO_MONEY, "你的账户目前没有余额,无需绑定提现帐号。");
 
        // 需要转账验证
        BigDecimal money = new BigDecimal("0.1");
        transferAlipayWithVerify(account, name);
        // 扣款
        userInfoMapper.subHongBaoByUid(uid, money);
        // 转账成功
        // 插入转账成功表
        AlipayAccountValidNormalHistory history = new AlipayAccountValidNormalHistory();
        history.setAccount(account);
        history.setCreateTime(new Date());
        history.setName(name);
        history.setUid(uid);
        alipayAccountValidNormalHistoryMapper.insertSelective(history);
        // 新版资金
        try {
            UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(history, money);
            userMoneyDetailMapper.insertSelective(userMoneyDetail);
        } catch (UserMoneyDetailException e) {
            try {
                LogHelper.errorDetailInfo(e);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
 
        // userNotificationService.alipayAccountValidRight(uid, money, account);
 
    }
 
    private void transferAlipayWithVerify(String account, String name)
            throws AlipayTransferException, AlipayApiException {
        String privateKey = Constant.alipayConfig.getPrivateKey();
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                Constant.alipayConfig.getAppId(), privateKey, "json", "gbk", null, "RSA2");
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String appName = Constant.systemCommonConfig.getProjectChineseName();
        JSONObject json = new JSONObject();
        json.put("out_biz_no", uuid);
        json.put("payee_type", "ALIPAY_LOGONID");
        json.put("payee_account", account);
        json.put("amount", "0.1");
        json.put("payer_show_name", appName + "支付宝验证");
        json.put("payee_real_name", name);
        json.put("remark", "来自" + appName + "的支付宝验证打款");
        request.setBizContent(json.toString());
        AlipayFundTransToaccountTransferResponse response = null;
        response = alipayClient.execute(request);
        // 成功转账
        if (response != null && response.isSuccess() && "10000".equals(response.getCode())) {
            return;
        } else// 转账失败
        {
            throw new AlipayTransferException(Integer.parseInt(response.getCode()), response.getSubCode(),
                    response.getSubMsg());
        }
    }
 
    @Transactional
    @Override
    public BindingAccount changeAlipayBindingWithVerify(Long uid, String name, String account)
            throws AlipayTransferException, AlipayApiException, AlipayAccountException {
 
        try {
            validAlipayAccount(uid, account, name);
        } catch (AlipayTransferException e1) {
            throw new AlipayTransferException(e1.getCode(), e1.getSubCode(), e1.getMsg());
        } catch (AlipayApiException e1) {
            throw new AlipayApiException(e1.getErrCode(), e1.getErrMsg());
        } catch (AlipayAccountException e1) {
            throw new AlipayAccountException(e1.getCode(), e1.getMsg());
        }
 
        BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
        if (bindingAccount == null)// 创建账号
        {
            bindingAccount = new BindingAccount();
            bindingAccount.setAccount(account);
            bindingAccount.setName(name);
            bindingAccount.setType(BindingAccount.TYPE_ALIPAY);
            bindingAccount.setUserInfo(new UserInfo(uid));
            bindingAccountMapper.insertSelective(bindingAccount);
        } else {
            BindingAccount updateBindingAccount = new BindingAccount();
            updateBindingAccount.setId(bindingAccount.getId());
            updateBindingAccount.setName(name);
            updateBindingAccount.setAccount(account);
            bindingAccountMapper.updateByPrimaryKeySelective(updateBindingAccount);
            bindingAccount.setName(updateBindingAccount.getName());
            bindingAccount.setAccount(updateBindingAccount.getAccount());
        }
 
        return bindingAccount;
    }
 
    @Override
    public boolean canVerifyAlipayAccount(Long uid) throws BindingAccountException {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0)
            throw new BindingAccountException(1, "你的账户目前没有余额,无需绑定提现帐号。");
 
        AlipayAccountValidNormalHistory latest = alipayAccountValidNormalHistoryMapper.selectLatestByUid(uid);
        if (latest != null) {
            Calendar caLatest = Calendar.getInstance();
            caLatest.setTimeInMillis(latest.getCreateTime().getTime());
            Calendar nowLatest = Calendar.getInstance();
            if (caLatest.get(Calendar.MONTH) == nowLatest.get(Calendar.MONTH))// 上次更改和现在是同一个月
                throw new BindingAccountException(2, "每月仅可修改1次提现账号,请下月再试吧。");
        }
        return true;
    }
 
}