From 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 09 五月 2020 21:41:27 +0800 Subject: [PATCH] 2.1需求 --- fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java | 277 ++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 171 insertions(+), 106 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java index 442453f..bbc9950 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java @@ -1,5 +1,7 @@ package com.yeshi.fanli.service.impl.user; +import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -9,24 +11,36 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import javax.annotation.Resource; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import org.yeshi.utils.DateUtil; +import org.yeshi.utils.FileUtil; import org.yeshi.utils.HttpUtil; import org.yeshi.utils.entity.FileUploadResult; import org.yeshi.utils.tencentcloud.COSManager; +import com.aliyun.openservices.ons.api.Message; +import com.aliyun.openservices.ons.api.Producer; import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper; import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper; +import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum; +import com.yeshi.fanli.dto.mq.user.body.InviteCodeActiveMQMsg; +import com.yeshi.fanli.dto.mq.user.body.UserInfoUpdateMQMsg; +import com.yeshi.fanli.dto.mq.user.body.UserInfoUpdateMQMsg.UserInfoUpdateTypeEnum; import com.yeshi.fanli.dto.wx.WXAccountInfoDTO; import com.yeshi.fanli.entity.bus.user.UserInfo; import com.yeshi.fanli.entity.bus.user.UserInfoExtra; import com.yeshi.fanli.entity.bus.user.UserRank; import com.yeshi.fanli.entity.bus.user.UserRankRecord; import com.yeshi.fanli.entity.bus.user.WeiXinUser; +import com.yeshi.fanli.entity.bus.user.notify.UserActivedRecord; +import com.yeshi.fanli.entity.system.ConfigKeyEnum; import com.yeshi.fanli.exception.user.ThreeSaleException; import com.yeshi.fanli.exception.user.UserInfoExtraException; import com.yeshi.fanli.log.LogHelper; @@ -39,12 +53,19 @@ import com.yeshi.fanli.service.inter.user.UserInviteRecordService; import com.yeshi.fanli.service.inter.user.UserRankService; import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce; +import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService; +import com.yeshi.fanli.service.inter.user.notify.UserActivedRecordService; import com.yeshi.fanli.util.Constant; +import com.yeshi.fanli.util.FilePathEnum; import com.yeshi.fanli.util.InviteCodeFilterUtil; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.account.UserUtil; +import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory; +import com.yeshi.fanli.util.rocketmq.MQTopicName; import com.yeshi.fanli.util.wx.WXLoginUtil; import com.yeshi.fanli.vo.user.UserInfoExtraVO; + +import net.coobird.thumbnailator.Thumbnails; @Service public class UserInfoExtraServiceImpl implements UserInfoExtraService { @@ -79,6 +100,16 @@ @Resource private UserInviteRecordService userInviteRecordService; + @Resource + @Lazy + private UserActivedRecordService userActivedRecordService; + + @Resource + private UserAccountMsgNotificationService userAccountMsgNotificationService; + + @Resource(name = "producer") + private Producer producer; + @Override public UserInfoExtraVO getRankInfo(Long uid) throws UserInfoExtraException, Exception { @@ -88,10 +119,10 @@ } // 绛夌骇瀵圭収琛� - String contrast = configService.get("user_rank_contrast"); + String contrast = configService.get(ConfigKeyEnum.userRankContrast.getKey()); extraVO.setRankContrast(contrast); // 鐗瑰埆鎻愰啋 - String rankTip = configService.get("user_rank_tip"); + String rankTip = configService.get(ConfigKeyEnum.userRankTip.getKey()); extraVO.setRankTip(rankTip); Integer rankOrderNum = extraVO.getRankOrderNum(); @@ -176,7 +207,7 @@ } else { extra.setUserInfo(new UserInfo(uid)); } - + // 鏇存柊绛夌骇 if (isupdateRank) { List<UserRank> listRank = userRankService.getAllRank(); @@ -276,7 +307,6 @@ userInfoExtraMapper.insertSelective(userInfoExtra); } - @Override public UserInfoExtra saveUserInfoExtra(UserInfoExtra userInfoExtra) throws UserInfoExtraException { @@ -313,9 +343,9 @@ @Override @Transactional(rollbackFor = Exception.class) public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException { - if (uid == null || inviteCode == null) + if (uid == null || inviteCode == null) throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉鑳戒负绌�"); - + // 琚個璇蜂汉淇℃伅 UserInfo invitee = userInfoService.selectByPKey(uid); if (invitee == null) @@ -327,40 +357,14 @@ throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�"); inviteCode = inviteCode.toUpperCase(); + // 閭�璇风爜瀵瑰簲鐢ㄦ埛淇℃伅 - UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode); - if (inviter == null) + UserInfo boss = userInfoService.getUserInfoByInviteCode(inviteCode); + if (boss == null || boss.getState() != UserInfo.STATE_NORMAL) throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�"); - - // 缁戝畾鍏崇郴 - try { - threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter); - } catch (ThreeSaleException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - throw new UserInfoExtraException(1, "婵�娲诲け璐�"); - } - // 鐢熸垚閭�璇风爜 - String mycode = createInviteCode(uid); - if (StringUtil.isNullOrEmpty(mycode)) - throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触"); - - UserInfoExtra userInfoExtra = new UserInfoExtra(); - userInfoExtra.setUserInfo(invitee); - userInfoExtra.setInviteCode(mycode); - userInfoExtra.setUpdateTime(new Date()); - if (extra != null) { - userInfoExtra.setId(extra.getId()); - userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra); - } else { - userInfoExtra.setCreateTime(new Date()); - userInfoExtraMapper.insertSelective(userInfoExtra); - } - return mycode; + // 鐢熸垚閭�璇峰叧绯� + return inviteCodeActive(uid, boss.getId()); } @Override @@ -368,7 +372,7 @@ public void activationInviteWX(WXAccountInfoDTO wxAccount, Long uid, String code) throws UserInfoExtraException { if (uid == null || code == null) throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉瀹屾暣"); - + UserInfo invitee = userInfoService.selectByPKey(uid); if (invitee == null) throw new UserInfoExtraException(1, "鐢ㄦ埛涓嶅瓨鍦�"); @@ -422,8 +426,9 @@ if (asInputStream == null) { LogHelper.test("寰俊澶村儚涓嬭浇澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl); } else { - FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format( - "/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + "")); + FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, + String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(), + System.currentTimeMillis() + "")); if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) { headimgurl = result.getUrl(); } else { @@ -443,35 +448,14 @@ Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId); if (inviterId == null) throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�"); - - // 缁戝畾鍏崇郴 - try { - threeSaleSerivce.bindRelationshipByWX(invitee, inviterId); - } catch (ThreeSaleException e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - throw new UserInfoExtraException(1, "鍏崇郴缁戝畾鏃跺け璐�"); - } - - // 鐢熸垚閭�璇风爜 - String inviteCode = createInviteCode(uid); - if (StringUtil.isNullOrEmpty(inviteCode)) - throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触"); - UserInfoExtra userInfoExtra = new UserInfoExtra(); - userInfoExtra.setUserInfo(invitee); - userInfoExtra.setInviteCode(inviteCode); - userInfoExtra.setUpdateTime(new Date()); - if (extra != null) { - userInfoExtra.setId(extra.getId()); - userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra); - } else { - userInfoExtra.setCreateTime(new Date()); - userInfoExtraMapper.insertSelective(userInfoExtra); + UserInfo boss = userInfoService.selectByPKey(inviterId); + if (boss == null || boss.getState() != UserInfo.STATE_NORMAL) { + throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�"); } + + // 閭�璇风爜婵�娲� + inviteCodeActive(uid, boss.getId()); } @Override @@ -485,15 +469,6 @@ UserInfo invitee = userInfoService.selectByPKey(uid); if (invitee == null) { throw new UserInfoExtraException(1, "鐢ㄦ埛涓嶅瓨鍦�"); - } - - // 鐢ㄦ埛棰濆淇℃伅 - UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid); - if (extra != null) { - String inviteCodeHas = extra.getInviteCode(); - if (inviteCodeHas != null && inviteCodeHas.trim().length() > 0) { - throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�"); - } } String wxUnionId = weiXinUser.getUnionid(); @@ -535,8 +510,9 @@ if (asInputStream == null) { LogHelper.test("寰俊澶村儚涓嬭浇澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl); } else { - FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format( - "/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + "")); + FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, + String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(), + System.currentTimeMillis() + "")); if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) { headimgurl = result.getUrl(); } else { @@ -557,10 +533,33 @@ if (inviterId == null) { throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�"); } - + + UserInfo boss = userInfoService.selectByPKey(inviterId); + if (boss == null || boss.getState() != UserInfo.STATE_NORMAL) { + throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�"); + } + + // 閭�璇风爜婵�娲� + inviteCodeActive(uid, boss.getId()); + } + + + /** + * 閭�璇峰叧绯荤敓鎴愭縺娲荤爜 + * @param uid + * @param bossId + * @return + * @throws UserInfoExtraException + */ + private String inviteCodeActive(Long uid, Long bossId) throws UserInfoExtraException{ + UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid); + if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode())) { + throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�"); + } + // 缁戝畾鍏崇郴 try { - threeSaleSerivce.bindRelationshipByWX(invitee, inviterId); + threeSaleSerivce.bindInviteRelationship(uid, bossId); } catch (ThreeSaleException e) { try { LogHelper.errorDetailInfo(e); @@ -571,12 +570,12 @@ } // 鐢熸垚閭�璇风爜 - String inviteCode = createInviteCode(uid);; - if (StringUtil.isNullOrEmpty(inviteCode)) + String inviteCode = createInviteCode(uid); + if (StringUtil.isNullOrEmpty(inviteCode)) throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触"); - + UserInfoExtra userInfoExtra = new UserInfoExtra(); - userInfoExtra.setUserInfo(invitee); + userInfoExtra.setUserInfo(new UserInfo(uid)); userInfoExtra.setInviteCode(inviteCode); userInfoExtra.setUpdateTime(new Date()); if (extra != null) { @@ -586,7 +585,19 @@ userInfoExtra.setCreateTime(new Date()); userInfoExtraMapper.insertSelective(userInfoExtra); } + + userActivedRecordService.add(new UserActivedRecord(uid)); + +// if (!Constant.IS_TEST) { // 鍙戦�佹縺娲绘垚鍔熸秷鎭� + Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteCodeActive, + new InviteCodeActiveMQMsg(uid, new Date())); + producer.send(message); +// } + + return inviteCode; } + + @Override public void updateActiveTime(Long uid, Date date) { @@ -617,12 +628,12 @@ public void updateGoldCoin(Long id, Integer goldCoin) { userInfoExtraMapper.updateGoldCoin(id, goldCoin); } - + @Override public void addGoldCoinByUid(Long uid, Integer goldCoin) { userInfoExtraMapper.addGoldCoinByUid(uid, goldCoin); } - + @Override public void updateByPrimaryKeySelective(UserInfoExtra record) { userInfoExtraMapper.updateByPrimaryKeySelective(record); @@ -640,17 +651,17 @@ // vip閭�璇风爜浼樺厛 if (!StringUtil.isNullOrEmpty(userInfoExtra.getInviteCodeVip())) return userInfoExtra.getInviteCodeVip(); - + if (!StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) return userInfoExtra.getInviteCode(); - + // 鏄惁婊¤冻鐢熶骇閭�璇风爜 int relationshipNum = threeSaleSerivce.getSuccessRelationshipNum(uid); if (relationshipNum > 0) { try { String inviteCode = createInviteCode(uid); if (!StringUtil.isNullOrEmpty(inviteCode)) { - UserInfoExtra extra= new UserInfoExtra(); + UserInfoExtra extra = new UserInfoExtra(); extra.setId(userInfoExtra.getId()); extra.setInviteCode(inviteCode); userInfoExtraMapper.updateByPrimaryKeySelective(extra); @@ -666,12 +677,13 @@ /** * 鐢熶骇閭�璇风爜 + * * @param uid * @return */ private String createInviteCode(Long uid) { String inviteCode = null; - for (int i = 1; i <= 5; i ++) { + for (int i = 1; i <= 5; i++) { inviteCode = UserUtil.getInviteCode(i, uid); long countCode = userInfoExtraMapper.countByInviteCode(inviteCode); if (countCode <= 0) @@ -679,8 +691,7 @@ } return inviteCode; } - - + @Override public long countByRankId(Long rankId) { return userInfoExtraMapper.countByRankId(rankId); @@ -724,11 +735,15 @@ public UserInfo getUserByInviteCode(String inviteCode) throws UserInfoExtraException { if (StringUtil.isNullOrEmpty(inviteCode)) throw new UserInfoExtraException(1, "閭�璇风爜涓嶈兘涓虹┖"); - + UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode); if (inviter == null) throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�"); + if (inviter.getState() != UserInfo.STATE_NORMAL) { + throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�"); + } + return inviter; } @@ -774,48 +789,98 @@ if (inviter == null) { throw new UserInfoExtraException(1, "瀵瑰簲鐨勯個璇峰叧绯讳笉瀛樺湪"); } + + if (inviter.getState() != UserInfo.STATE_NORMAL) { + throw new UserInfoExtraException(1, "瀵瑰簲鐨勯個璇峰叧绯讳笉瀛樺湪"); + } + return inviter; } - @Override - public void updateAutoExtract(Integer days) { - userInfoExtraMapper.updateAutoExtract(days); - } - @Transactional + @Transactional(rollbackFor = Exception.class) @Override public void updateInviteCodeVip(String inviteCodeVip, Long uid) throws UserInfoExtraException { if (inviteCodeVip == null || inviteCodeVip.length() < 4 || inviteCodeVip.length() > 12) throw new UserInfoExtraException(1, "閭�璇风爜蹇呴』涓�4鍒�12浣�"); - + UserInfoExtra extra = getUserInfoExtra(uid); if (extra == null) throw new UserInfoExtraException(2, "鐢ㄦ埛淇℃伅涓嶅瓨鍦�"); if (StringUtil.isNullOrEmpty(extra.getInviteCode())) throw new UserInfoExtraException(3, "閭�璇风爜灏氭湭婵�娲�"); - + if (!StringUtil.isNullOrEmpty(extra.getInviteCodeVip())) throw new UserInfoExtraException(4, "鍙兘淇敼涓�娆�"); - + // 杞崲鎴愬ぇ鍐� inviteCodeVip = inviteCodeVip.toUpperCase(); - + // 楠岃瘉鐗规畩鐮� - if(InviteCodeFilterUtil.isSpecialCode(inviteCodeVip)) + if (InviteCodeFilterUtil.isSpecialCode(inviteCodeVip)) throw new UserInfoExtraException(5, "閭�璇风爜宸插瓨鍦�"); - + if (inviteCodeVip.equalsIgnoreCase(extra.getInviteCode())) throw new UserInfoExtraException(5, "涓嶈兘涓哄師閭�璇风爜"); - + long count = userInfoExtraMapper.countByInviteCode(inviteCodeVip); if (count > 0) throw new UserInfoExtraException(6, "閭�璇风爜宸插瓨鍦�"); - + UserInfoExtra update = new UserInfoExtra(); update.setId(extra.getId()); update.setInviteCodeVip(inviteCodeVip); update.setUpdateTime(new Date()); userInfoExtraMapper.updateByPrimaryKeySelective(update); + + if (!Constant.IS_TEST) { // 鍙戦�侀個璇风爜鏇存敼鎴愬姛鐨勬秷鎭� + Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userInfoUpdate, + new UserInfoUpdateMQMsg(uid, UserInfoUpdateTypeEnum.inviteCode, inviteCodeVip, + new Date())); + producer.send(message); + } + + // 娑堟伅 + userAccountMsgNotificationService.createChangeInviteCode(uid, extra.getInviteCode(), inviteCodeVip); } + + @Override + public void uploadERCode(MultipartFile file, Long uid) { + UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid); + if (extra == null) + return; + + // 鍘嬬缉 + String targetPath = FileUtil.getCacheDir() + "/erCode_" + uid + "_" + System.currentTimeMillis() + ".jpg"; + try { + Thumbnails.of(file.getInputStream()).size(200, 200).toFile(targetPath); + } catch (IOException e) { + LogHelper.errorDetailInfo(e); + return; + } + + // 鎵ц涓婁紶 + String filePath = FilePathEnum.userERcode.getPath() + UUID.randomUUID().toString().replace("-", "") + ".jpg"; + String fileLink = COSManager.getInstance().uploadFile(new File(targetPath), filePath).getUrl(); + + // 鍒犻櫎鏈湴鍥剧墖 + if (new File(targetPath).exists()) { + new File(targetPath).delete(); + } + + // 鍘熶簩缁寸爜 + String erCode = extra.getErCode(); + + UserInfoExtra update = new UserInfoExtra(); + update.setId(extra.getId()); + update.setErCode(fileLink); + userInfoExtraMapper.updateByPrimaryKeySelective(update); + + // 鍘熶簩缁寸爜 + if (!StringUtil.isNullOrEmpty(erCode)) { + COSManager.getInstance().deleteFile(erCode); + } + } + } -- Gitblit v1.8.0