admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoServiceImpl.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.io.Serializable;
import java.math.BigDecimal;
@@ -18,10 +20,13 @@
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.core.task.TaskExecutor;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.NumberUtil;
import org.yeshi.utils.tencentcloud.COSManager;
@@ -35,17 +40,28 @@
import com.yeshi.fanli.dao.mybatis.share.ShareMapper;
import com.yeshi.fanli.dao.user.UserInfoDao;
import com.yeshi.fanli.entity.bus.user.BindingAccount;
import com.yeshi.fanli.entity.bus.user.ForbiddenUserIdentifyCode;
import com.yeshi.fanli.entity.bus.user.ForbiddenUserIdentifyCode.ForbiddenUserIdentifyCodeTypeEnum;
import com.yeshi.fanli.entity.bus.user.InviteUser;
import com.yeshi.fanli.entity.bus.user.UserExtraTaoBaoInfo;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.WeiXinUser;
import com.yeshi.fanli.entity.system.System;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.exception.ThreeSaleException;
import com.yeshi.fanli.exception.user.ForbiddenUserIdentifyCodeException;
import com.yeshi.fanli.exception.user.UserInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoService;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionAuthRecordService;
import com.yeshi.fanli.service.inter.user.BindingAccountService;
import com.yeshi.fanli.service.inter.user.ForbiddenUserIdentifyCodeService;
import com.yeshi.fanli.service.inter.user.SpreadUserImgService;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserExtraTaoBaoInfoService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserRankService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
@@ -55,17 +71,19 @@
import com.yeshi.fanli.util.wx.WXLoginUtil;
import com.yeshi.fanli.vo.user.UserInfoVO;
import net.coobird.thumbnailator.Thumbnails;
@Service
public class UserInfoServiceImpl implements UserInfoService {
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Resource
   private UserInfoDao userInfoDao;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private HongBaoService hongBaoService;
   @Resource
   private ConfigService configService;
@@ -93,6 +111,27 @@
   @Resource
   private HongBaoV2CountService hongBaoV2CountService;
   @Resource
   private UserRankService userRankService;
   @Resource
   private UserActiveLogService userActiveLogService;
   @Resource
   private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
   @Resource
   private TaoBaoUnionAuthRecordService taoBaoUnionAuthRecordService;
   @Resource
   private SpreadUserImgService spreadUserImgService;
   @Resource
   private ForbiddenUserIdentifyCodeService forbiddenUserIdentifyCodeService;
   @Resource
   private BindingAccountService bindingAccountService;
   public UserInfo getUserByLoginTypeAndOpenId(int loginType, String openid, String appid) {
      List<UserInfo> list = null;
@@ -159,8 +198,6 @@
            return form;
         }
         if (form.getSystem().getId() != 7)
            hongBaoService.setNewUserHongBao(form, 1);
         LogHelper.userInfo("添加用户:" + form);
         if (form.getLoginType() == Constant.WEIXIN) {
            // final UserInfo temp = form;
@@ -449,7 +486,7 @@
         find.setLoginType(Constant.WEIXIN);
         find.setRank(0);
         find.setWxOpenId(weiXinUser.getOpenid());
         System system = new System(Constant.FANLI);
         BusinessSystem system = new BusinessSystem(Constant.FANLI);
         system.setPlatform(1);
         find.setSystem(system);
      }
@@ -625,28 +662,24 @@
   }
   @Override
   public BigDecimal getMyHB(Long id) {
      return userInfoMapper.getMyHB(id);
   }
   @Override
   public UserInfo getUserByIdWithMybatis(long uid) {
      UserInfo user = userInfoMapper.selectByPKey(uid);
      return UserUtil.filterForClientUser(user);
   }
   @Override
   public long queryCount( Integer userState, String key, Integer keyType, String userRank, Integer days, String startTime, String endTime) {
   public long queryCount(Integer userState, String key, Integer keyType, String userRank, Integer days,
         String startTime, String endTime) {
      return userInfoMapper.queryCount(userState, key, keyType, userRank, days, startTime, endTime);
   }
   @Override
   public List<UserInfoVO> query(long start, int count,  Integer userState, String key, Integer keyType,
   public List<UserInfoVO> query(long start, int count, Integer userState, String key, Integer keyType,
         String userRank, Integer days, String startTime, String endTime, Integer orderField, Integer orderMode) {
      List<UserInfoVO> userList = userInfoMapper.query(start, count,userState, key, keyType, userRank, days, startTime, endTime,
            orderField, orderMode);
      List<UserInfoVO> userList = userInfoMapper.query(start, count, userState, key, keyType, userRank, days,
            startTime, endTime, orderField, orderMode);
      if (userList == null || userList.size() == 0) {
         return null;
      }
@@ -658,37 +691,52 @@
      return userList;
   }
   public void convertUserInfoVOData(UserInfoVO userInfoVO) {
      String f_alipayAccount = userInfoVO.getF_alipayAccount();
      String rankName = userInfoVO.getRankName();
      if (rankName == null || rankName.trim().length() == 0) {
         userInfoVO.setRankName("青铜");
         userInfoVO.setRankPicture("http://img.flqapp.com/resource/rank/rank_picture_new_1.png");
      }
      String f_alipayAccount = userInfoVO.getAlipayAccountInvalid();
      if (f_alipayAccount != null && f_alipayAccount.trim().length() > 0) {
         userInfoVO.setAlipayAccountState(1);
      }
      String f_phone = userInfoVO.getF_phone();
      String f_phone = userInfoVO.getPhoneInvalid();
      if (f_phone != null && f_phone.trim().length() > 0) {
         userInfoVO.setPhoneState(1);
      }
      String f_taobaoUid = userInfoVO.getF_taobaoUid();
      String f_taobaoUid = userInfoVO.getTaobaoUidInvalid();
      if (f_taobaoUid != null && f_taobaoUid.trim().length() > 0) {
         userInfoVO.setTaobaoUidState(1);
      }
      String f_wxUnionId = userInfoVO.getF_wxUnionId();
      String f_wxUnionId = userInfoVO.getWxUnionIdInvalid();
      if (f_wxUnionId != null && f_wxUnionId.trim().length() > 0) {
         userInfoVO.setWxUnionIdState(1);
      }
      // 最近登录时间
      Long lastLoginTime = userInfoVO.getLastLoginTime();
      if (lastLoginTime != null && lastLoginTime == 0) {
         userInfoVO.setLastLoginTime(null);
      }
      /*
       * 显示用户微信 淘宝 老版本存在 String wxUnionId = userInfoVO.getWxUnionId(); if
       * (wxUnionId == null || wxUnionId.trim().length() == 0) {
       * userInfoVO.setWxName(null); }
       *
       * String taoBaoUid = userInfoVO.getTaoBaoUid(); if (taoBaoUid == null
       * || taoBaoUid.trim().length() == 0) { userInfoVO.setTbName(null); }
       */
      Long uid = userInfoVO.getId();
      /* 绑定收款账号信息 */
      List<BindingAccount> account = bindingAccountMapper.selectByUid(uid);
      if (account != null && account.size() > 0) {
@@ -707,10 +755,10 @@
            userInfoVO.setAccountBindIdWX(bindingAccount.getId());
         }
      }
      // 最近一次下单时间
      Date lastOrderTime = hongBaoV2CountService.getLastHongBaoTime(uid);
      if (lastOrderTime != null) {
      if (lastOrderTime != null && lastOrderTime.getTime() != 0) {
         userInfoVO.setLastOrderTime(lastOrderTime.getTime());
      }
@@ -755,7 +803,6 @@
      int secondTeamCount = shareMapper.mySecondTeamCount(uid + "");
      userInfoVO.setCountLevelTwo(secondTeamCount);
   }
   @Override
   public double querySumMoney(String key, Integer userType, Integer days, String startTime, String endTime) {
@@ -817,5 +864,134 @@
   public UserInfo getInfoByPhoneOrInviteCode(String phone, String inviteCode) {
      return userInfoMapper.getInfoByPhoneOrInviteCode(phone, inviteCode);
   }
   @Override
   public UserInfo getEffectiveUserInfoByPhone(String phone) {
      return userInfoMapper.getEffectiveUserInfoByPhone(phone);
   }
   @Override
   public UserInfo getEffectiveUserInfoByWXUnionId(String unionId) {
      return userInfoMapper.getEffectiveUserInfoByWXUnionId(unionId);
   }
   @Override
   public UserInfo getUserInfo(Long uid) throws UserInfoException {
      if (uid == null) {
         throw new UserInfoException(1, "请求参数为空");
      }
      UserInfo userInfo = userInfoMapper.selectByPKey(uid);
      if (userInfo == null) {
         throw new UserInfoException(1, "用户不存在");
      }
      if (userInfo.getState() == UserInfo.STATE_FORBIDDEN) {
         throw new UserInfoException(Constant.CODE_FORBIDDEN_USER, Constant.FORBIDDEN_USER_REASON_DESC);
      }
      if (userInfo.getState() != UserInfo.STATE_NORMAL) {
         throw new UserInfoException(1, "用户不存在");
      }
      // 淘宝昵称 组织
      boolean clearOpenid = true;
      UserExtraTaoBaoInfo extraTaoBaoInfo = userExtraTaoBaoInfoService.getByUid(uid);
      if (extraTaoBaoInfo != null) {
         String specialId = extraTaoBaoInfo.getSpecialId();
         String relationId = extraTaoBaoInfo.getRelationId();
         String taoBaoNickName = extraTaoBaoInfo.getTaoBaoNickName();
         if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId)) {
            clearOpenid = false;
            userInfo.setOpenid(extraTaoBaoInfo.getTaoBaoUid());
            if (!StringUtil.isNullOrEmpty(taoBaoNickName)) {
               userInfo.setTbName(taoBaoNickName);
            } else {
               String taoBaoUserNick = taoBaoUnionAuthRecordService.getTaoBaoUserNick(uid);
               if (!StringUtil.isNullOrEmpty(taoBaoUserNick)) {
                  userInfo.setTbName(taoBaoUserNick);
               }
            }
         }
      }
      if (clearOpenid) {
         userInfo.setOpenid(null);
      }
      return userInfo;
   }
   @Override
   public BigDecimal getBalance(Long uid) {
      UserInfo user = userInfoMapper.selectByPKey(uid);
      if (user != null)
         return user.getMyHongBao();
      return null;
   }
   @Override
   public void uploadPortrait(MultipartFile file, Long uid) throws UserInfoException, IOException {
      UserInfo userInfo = userInfoMapper.selectByPKey(uid);
      if (userInfo == null) {
         throw new UserInfoException(1, "用户不存在");
      }
      // 谷歌压缩图片
      String targetPath = FileUtil.getCacheDir() + "/uploadPortrait_" + uid + "_" + System.currentTimeMillis()
            + ".jpg";
      Thumbnails.of(file.getInputStream()).size(200, 200).toFile(targetPath);
      // 执行上传
      String filePath = "/img/user/" + 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 portrait = userInfo.getPortrait();
      // 更新信息
      userInfo.setPortrait(fileLink);
      userInfoMapper.updateByPrimaryKeySelective(userInfo);
      // 删除原头像
      if (!StringUtil.isNullOrEmpty(portrait) && portrait.contains("/img/user/")) {
         COSManager.getInstance().deleteFile(portrait);
      }
      executor.execute(new Runnable() {
         @Override
         public void run() {
            try {
               // 删除分享头像信息
               spreadUserImgService.deleteImgUrl(uid);
            } catch (Exception e) {
               try {
                  LogHelper.errorDetailInfo(e);
               } catch (Exception e1) {
                  e1.printStackTrace();
               }
            }
         }
      });
   }
   @Override
   public void saveUserInfo(String nickName, Long uid) throws UserInfoException {
      UserInfo userInfo = userInfoMapper.selectByPKey(uid);
      if (userInfo == null) {
         throw new UserInfoException(1, "用户不存在");
      }
      userInfo.setNickName(nickName);
      userInfoMapper.updateByPrimaryKeySelective(userInfo);
   }
}