| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221 |
- package com.izouma.nineth.service;
- import cn.binarywang.wx.miniapp.api.WxMaService;
- import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
- import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
- import cn.hutool.core.convert.Convert;
- import com.alibaba.fastjson.JSON;
- import cn.hutool.core.collection.CollUtil;
- import com.alibaba.fastjson.JSONObject;
- import com.alipay.api.AlipayApiException;
- import com.alipay.api.AlipayClient;
- import com.alipay.api.DefaultAlipayClient;
- import com.alipay.api.request.AlipayUserCertifyOpenCertifyRequest;
- import com.alipay.api.request.AlipayUserCertifyOpenInitializeRequest;
- import com.alipay.api.request.AlipayUserCertifyOpenQueryRequest;
- import com.alipay.api.response.AlipayUserCertifyOpenCertifyResponse;
- import com.alipay.api.response.AlipayUserCertifyOpenInitializeResponse;
- import com.alipay.api.response.AlipayUserCertifyOpenQueryResponse;
- import com.github.kevinsawicki.http.HttpRequest;
- import com.huifu.adapay.core.exception.BaseAdaPayException;
- import com.izouma.nineth.TokenHistory;
- import com.izouma.nineth.config.AlipayProperties;
- import com.izouma.nineth.config.Constants;
- import com.izouma.nineth.config.GeneralProperties;
- import com.izouma.nineth.config.RedisKeys;
- import com.izouma.nineth.domain.Collection;
- import com.izouma.nineth.domain.*;
- import com.izouma.nineth.dto.*;
- import com.izouma.nineth.dto.oasis.OasisLoginDTO;
- import com.izouma.nineth.enums.AssetStatus;
- import com.izouma.nineth.enums.AuthStatus;
- import com.izouma.nineth.enums.AuthorityName;
- import com.izouma.nineth.enums.HeatType;
- import com.izouma.nineth.event.RegisterEvent;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.*;
- import com.izouma.nineth.security.Authority;
- import com.izouma.nineth.security.JwtTokenUtil;
- import com.izouma.nineth.security.JwtUserFactory;
- import com.izouma.nineth.service.sms.SmsService;
- import com.izouma.nineth.service.storage.StorageService;
- import com.izouma.nineth.utils.*;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import me.chanjar.weixin.common.error.WxErrorException;
- import me.chanjar.weixin.mp.api.WxMpService;
- import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
- import me.chanjar.weixin.mp.bean.result.WxMpUser;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.RandomStringUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.rocketmq.spring.core.RocketMQTemplate;
- import org.springframework.beans.BeanUtils;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageImpl;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.jpa.domain.Specification;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.stereotype.Service;
- import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
- import javax.persistence.criteria.CriteriaBuilder;
- import javax.persistence.criteria.CriteriaQuery;
- import javax.persistence.criteria.Predicate;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.text.SimpleDateFormat;
- import java.time.Duration;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- @Service
- @Slf4j
- @AllArgsConstructor
- public class UserService {
- private UserRepo userRepo;
- private WxMaService wxMaService;
- private WxMpService wxMpService;
- private SmsService smsService;
- private StorageService storageService;
- private JwtTokenUtil jwtTokenUtil;
- private FollowService followService;
- private FollowRepo followRepo;
- private IdentityAuthRepo identityAuthRepo;
- private SysConfigService sysConfigService;
- private UserBankCardRepo userBankCardRepo;
- private InviteRepo inviteRepo;
- private CacheService cacheService;
- private TokenHistoryRepo tokenHistoryRepo;
- private CollectionRepo collectionRepo;
- private AdapayMerchantService adapayMerchantService;
- private RocketMQTemplate rocketMQTemplate;
- private GeneralProperties generalProperties;
- private RedisTemplate<String, Object> redisTemplate;
- private PasswordEncoder passwordEncoder;
- private WeakPassRepo weakPassRepo;
- private UserBalanceRepo userBalanceRepo;
- private ContentAuditService contentAuditService;
- private HeatInfoRepo heatInfoRepo;
- private ShowroomRepo showroomRepo;
- private TradingAccountRepo tradingAccountRepo;
- private AlipayClient alipayClient;
- private SnowflakeIdWorker snowflakeIdWorker;
- private FaceAuthRepo faceAuthRepo;
- private AuctionPassRecordRepo auctionPassRecordRepo;
- private AssetRepo assetRepo;
- private ShowCollectionRepo showCollectionRepo;
- private ShowroomService showroomService;
- private NewsLikeRepo newsLikeRepo;
- private UserPropertyRepo userPropertyRepo;
- public User update(User user) {
- if (!SecurityUtils.hasRole(AuthorityName.ROLE_ADMIN)) {
- if (!SecurityUtils.getAuthenticatedUser().getId().equals(user.getId())) {
- throw new BusinessException("无权限");
- }
- }
- User orig = userRepo.findById(user.getId()).orElseThrow(new BusinessException("无记录"));
- ObjUtils.merge(orig, user);
- orig = save(orig);
- userRepo.updateAssetMinter(orig.getId());
- userRepo.updateAssetOwner(orig.getId());
- userRepo.updateCollectionMinter(orig.getId());
- userRepo.updateCollectionOwner(orig.getId());
- userRepo.updateOrderMinter(orig.getId());
- userRepo.updateHistoryFromUser(orig.getId());
- userRepo.updateHistoryToUser(orig.getId());
- userRepo.updateShowroomToUser(orig.getId());
- cacheService.clearCollection();
- return orig;
- }
- public User save(User user) {
- if (user.getId() != null) {
- cacheService.clearUserMy(user.getId());
- cacheService.clearUser(user.getId());
- }
- return userRepo.save(user);
- }
- public User update(Long userId, String nickname, String avatar, String sex, String bg, String intro,
- Boolean useCollectionPic, Boolean riskWarning, Integer level) {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- if (StringUtils.isNotBlank(nickname)) {
- if (!nickname.equals(user.getNickname())) {
- if (!contentAuditService.auditText(nickname)) {
- throw new BusinessException("昵称包含非法内容");
- }
- }
- user.setNickname(nickname);
- }
- if (StringUtils.isNotBlank(avatar)) {
- user.setAvatar(avatar);
- }
- if (StringUtils.isNotBlank(sex)) {
- user.setSex(sex);
- }
- if (StringUtils.isNotBlank(bg)) {
- user.setBg(bg);
- }
- if (StringUtils.isNotBlank(intro)) {
- if (!intro.equals(user.getIntro())) {
- if (!contentAuditService.auditText(nickname)) {
- throw new BusinessException("简介包含非法内容");
- }
- }
- user.setIntro(intro);
- }
- if (useCollectionPic != null) {
- user.setUseCollectionPic(useCollectionPic);
- }
- if (riskWarning != null) {
- user.setRiskWarning(riskWarning);
- }
- if (level != null) {
- user.setLevel(level);
- }
- user = save(user);
- userRepo.updateAssetMinter(userId);
- userRepo.updateAssetOwner(userId);
- userRepo.updateCollectionMinter(userId);
- userRepo.updateCollectionOwner(userId);
- userRepo.updateOrderMinter(userId);
- userRepo.updateHistoryFromUser(userId);
- userRepo.updateHistoryToUser(userId);
- userRepo.updateShowroomToUser(userId);
- return user;
- }
- @Cacheable(value = "userList", key = "#pageQuery.hashCode()")
- public PageWrapper<User> all(PageQuery pageQuery) {
- Specification<User> specification = JpaUtils.toSpecification(pageQuery, User.class);
- specification = specification.and((Specification<User>) (root, criteriaQuery, criteriaBuilder) -> {
- List<Predicate> and = new ArrayList<>();
- and.add(criteriaBuilder.equal(root.get("del"), false));
- if (!pageQuery.getQuery().containsKey("admin")) {
- and.add(criteriaBuilder.equal(root.get("admin"), false));
- }
- if (pageQuery.getQuery().containsKey("hasRole")) {
- String roleName = (String) pageQuery.getQuery().get("hasRole");
- if (roleName.equals("ROLE_MINTER")) {
- and.add(criteriaBuilder.equal(root.get("minter"), true));
- } else {
- and.add(criteriaBuilder
- .isMember(Authority.get(AuthorityName.valueOf(roleName)), root.get("authorities")));
- }
- }
- if (pageQuery.getQuery().containsKey("vip")) {
- boolean vip = (boolean) pageQuery.getQuery().get("vip");
- if (vip) {
- and.add(criteriaBuilder.greaterThan(root.get("vipPurchase"), 0));
- } else {
- and.add(criteriaBuilder.lessThanOrEqualTo(root.get("vipPurchase"), 0));
- }
- }
- return criteriaBuilder.and(and.toArray(new Predicate[0]));
- });
- Page<User> page = userRepo.findAll(specification, JpaUtils.toPageRequest(pageQuery));
- return PageWrapper.of(page);
- }
- public User create(UserRegister userRegister) {
- User user = new User();
- BeanUtils.copyProperties(userRegister, user);
- user.setShareRatio(sysConfigService.getBigDecimal("share_ratio"));
- user.setAuthStatus(AuthStatus.NOT_AUTH);
- if (StringUtils.isNotBlank(userRegister.getPassword())) {
- checkPasswordStrength(userRegister.getPassword());
- user.setPassword(passwordEncoder.encode(userRegister.getPassword()));
- }
- return save(user);
- }
- public User phoneRegister(String phone, String code, String password, String inviteCode, Long invitor,
- Long collectionId, Long showroomId) {
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- Invite invite = null;
- if (StringUtils.isNotBlank(inviteCode)) {
- invite = inviteRepo.findFirstByCode(inviteCode).orElse(null);
- }
- smsService.verify(phone, code);
- Collection collection;
- if (collectionId != null) {
- collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
- // if (!collection.isOnShelf() || !collection.isSalable()) {
- // collectionId = null;
- // } else if (collection.isScheduleSale()) {
- // if (collection.getStartTime().isAfter(LocalDateTime.now())) {
- // collectionId = null;
- // }
- // }
- // 只看是否开去分享
- if (ObjectUtils.isEmpty(collection.getOpenQuota()) || !collection.getOpenQuota()) {
- collectionId = null;
- }
- }
- User user = create(UserRegister.builder()
- .username(name)
- .nickname(name)
- .password(password)
- .avatar(Constants.DEFAULT_AVATAR)
- .phone(phone)
- .invitorPhone(Optional.ofNullable(invite).map(Invite::getPhone).orElse(null))
- .invitorName(Optional.ofNullable(invite).map(Invite::getName).orElse(null))
- .inviteCode(Optional.ofNullable(invite).map(Invite::getCode).orElse(null))
- .collectionInvitor(invitor)
- .collectionId(collectionId)
- .build());
- if (invite != null) {
- inviteRepo.increaseNum(invite.getId());
- }
- if (ObjectUtils.isNotEmpty(showroomId)) {
- //通过展厅的注册数量
- int weight = sysConfigService.getInt("heat_register_weight");
- heatInfoRepo.save(HeatInfo.builder()
- .showroomId(showroomId)
- .userId(user.getId())
- .type(HeatType.REGISTER)
- .value(weight)
- .build());
- showroomRepo.addHeatAndRegister(showroomId, weight, 1);
- }
- return user;
- }
- public String mqRegister(String phone, String code, String password, String inviteCode, Long invitor,
- Long collectionId, Long showroomId) {
- rocketMQTemplate.convertAndSend(generalProperties.getRegisterTopic(),
- new RegisterEvent(phone, code, password, inviteCode, invitor, collectionId, showroomId));
- return phone;
- }
- public Object getRegisterResult(String phone) {
- return redisTemplate.opsForValue().get("register::" + phone);
- }
- public void del(Long id) {
- User user = userRepo.findById(id).orElseThrow(new BusinessException("用户不存在"));
- user.setDel(true);
- if (StringUtils.isNoneEmpty(user.getOpenId())) {
- user.setOpenId(user.getOpenId() + "###" + RandomStringUtils.randomAlphabetic(8));
- }
- if (StringUtils.isNoneEmpty(user.getPhone())) {
- user.setPhone(user.getPhone() + "###" + RandomStringUtils.randomAlphabetic(8));
- }
- save(user);
- //删除实名认证
- identityAuthRepo.softDeleteByUserId(id);
- }
- public User loginByPhone(String phone, String code) {
- User user = userRepo.findByPhoneAndDelFalse(phone).orElse(null);
- smsService.verify(phone, code);
- if (user == null) {
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- user = create(UserRegister.builder()
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .username(name)
- .nickname(name)
- .avatar(Constants.DEFAULT_AVATAR)
- .phone(phone)
- .build());
- }
- return user;
- }
- public User loginByPhonePwd(String phone, String password) {
- if (StringUtils.isEmpty(phone)) {
- throw new BusinessException("手机号错误");
- }
- User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("账号或密码错误"));
- if (StringUtils.isEmpty(user.getPassword())) {
- throw new BusinessException("账号或密码错误");
- }
- if (StringUtils.isNoneEmpty(user.getPassword()) &&
- !passwordEncoder.matches(password, user.getPassword())) {
- throw new BusinessException("账号或密码错误");
- }
- return user;
- }
- public User loginByUsernamePwd(String username, String password) {
- if (StringUtils.isEmpty(username)) {
- throw new BusinessException("用户名错误");
- }
- User user = userRepo.findByUsernameAndDelFalse(username).orElseThrow(new BusinessException("账号或密码错误"));
- if (StringUtils.isEmpty(user.getPassword())
- || !passwordEncoder.matches(password, user.getPassword())) {
- throw new BusinessException("账号或密码错误");
- }
- return user;
- }
- public User loginMp(String code) throws WxErrorException {
- WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
- WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
- User user = userRepo.findByOpenIdAndDelFalse(wxMpUser.getOpenId()).orElse(null);
- if (user == null) {
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- user = User.builder()
- .username(name)
- .nickname(name)
- .avatar(wxMpUser.getHeadImgUrl())
- .sex(wxMpUser.getSexDesc())
- .country(wxMpUser.getCountry())
- .province(wxMpUser.getProvince())
- .city(wxMpUser.getCity())
- .openId(wxMpUser.getOpenId())
- .language(wxMpUser.getLanguage())
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .authStatus(AuthStatus.NOT_AUTH)
- .build();
- save(user);
- }
- return user;
- }
- public String code2openId(String code) throws WxErrorException {
- WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
- return wxMpService.oauth2getUserInfo(accessToken, null).getOpenId();
- }
- public User loginMa(String code) {
- try {
- WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
- String openId = result.getOpenid();
- String sessionKey = result.getSessionKey();
- User userInfo = userRepo.findByOpenIdAndDelFalse(openId).orElse(null);
- ;
- if (userInfo != null) {
- return userInfo;
- }
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- userInfo = User.builder()
- .username(name)
- .nickname(name)
- .openId(openId)
- .avatar(Constants.DEFAULT_AVATAR)
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .authStatus(AuthStatus.NOT_AUTH)
- .build();
- userInfo = save(userInfo);
- return userInfo;
- } catch (WxErrorException e) {
- e.printStackTrace();
- }
- throw new BusinessException("登录失败");
- }
- public User getMaUserInfo(String sessionKey, String rawData, String signature,
- String encryptedData, String iv) {
- // 用户信息校验
- if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
- throw new BusinessException("获取用户信息失败");
- }
- // 解密用户信息
- WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
- User user = userRepo.findByOpenIdAndDelFalse(wxUserInfo.getOpenId()).orElse(null);
- String avatarUrl = Constants.DEFAULT_AVATAR;
- try {
- String path = "image/avatar/" +
- new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) +
- RandomStringUtils.randomAlphabetic(8) +
- ".jpg";
- avatarUrl = storageService.uploadFromUrl(wxUserInfo.getAvatarUrl(), path);
- } catch (Exception e) {
- log.error("获取头像失败", e);
- }
- if (user == null) {
- user = User.builder()
- .username(UUID.randomUUID().toString())
- .nickname(wxUserInfo.getNickName())
- .openId(wxUserInfo.getOpenId())
- .avatar(avatarUrl)
- .sex(wxUserInfo.getGender())
- .country(wxUserInfo.getCountry())
- .province(wxUserInfo.getProvince())
- .city(wxUserInfo.getCity())
- .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
- .build();
- user = save(user);
- } else {
- user.setAvatar(avatarUrl);
- user.setNickname(wxUserInfo.getNickName());
- user.setSex(wxUserInfo.getGender());
- user.setCountry(wxUserInfo.getCountry());
- user.setProvince(wxUserInfo.getProvince());
- user.setCity(wxUserInfo.getCity());
- user = save(user);
- }
- return user;
- }
- public String setPassword(Long userId, String password) {
- checkPasswordStrength(password);
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- user.setPassword(passwordEncoder.encode(password));
- user = save(user);
- return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
- }
- public String setPassword(Long userId, String code, String password) {
- checkPasswordStrength(password);
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- smsService.verify(user.getPhone(), code);
- return setPassword(userId, password);
- }
- public String forgotPassword(String phone, String password, String code) {
- checkPasswordStrength(password);
- User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("手机号未注册"));
- smsService.verify(user.getPhone(), code);
- return setPassword(user.getId(), password);
- }
- public static void checkPasswordStrength(String password) {
- if (StringUtils.isBlank(password)) throw new BusinessException("密码不能为空");
- if (!Pattern.matches("^[a-zA-Z0-9!@#$%^&*]+$", password)) throw new BusinessException("密码含非法字符");
- int upper = 0;
- int lower = 0;
- int digit = 0;
- int special = 0;
- char ch;
- for (int i = 0; i < password.length(); i++) {
- ch = password.charAt(i);
- if (Character.isUpperCase(ch))
- upper++;
- else if (Character.isLowerCase(ch))
- lower++;
- else if (Character.isDigit(ch))
- digit++;
- else {
- if (ch == '<' || ch == '>') {
- throw new BusinessException("密码包含非法字符");
- } else
- special++;
- }
- }
- if (upper > 0 && lower > 0 && digit > 0 && password.length() >= 8) {
- return;
- }
- throw new BusinessException("密码长度至少为8位,且必须包含大小写字母和数字");
- }
- public void bindPhone(Long userId, String phone) {
- User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
- if (StringUtils.isNoneEmpty(user.getPhone())) {
- throw new BusinessException("该账号已绑定手机");
- }
- userRepo.findByPhoneAndDelFalse(phone).ifPresent(user1 -> {
- if (!user1.getId().equals(userId)) {
- throw new BusinessException("该手机号已绑定其他账号");
- }
- });
- user.setPhone(phone);
- save(user);
- }
- public UserDTO toDTO(User user) {
- return toDTO(user, true);
- }
- public UserDTO toDTO(User user, boolean join) {
- UserDTO userDTO = new UserDTO();
- BeanUtils.copyProperties(user, userDTO);
- if (user.getAuthorities() != null) {
- userDTO.setAuthorities(new HashSet<>(user.getAuthorities()));
- }
- if (join) {
- if (SecurityUtils.getAuthenticatedUser() != null) {
- userDTO.setFollow(followService.isFollow(SecurityUtils.getAuthenticatedUser().getId(), user.getId()));
- }
- }
- return userDTO;
- }
- public List<UserDTO> toDTO(List<User> users) {
- List<Follow> follows = new ArrayList<>();
- if (SecurityUtils.getAuthenticatedUser() != null) {
- follows.addAll(followRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
- }
- return users.stream().parallel().map(user -> {
- UserDTO dto = toDTO(user, false);
- if (!follows.isEmpty()) {
- dto.setFollow(follows.stream().anyMatch(f -> f.getFollowUserId().equals(user.getId())));
- }
- return dto;
- }).collect(Collectors.toList());
- }
- public Page<UserDTO> toDTO(Page<User> users) {
- List<UserDTO> userDTOS = toDTO(users.getContent());
- return new PageImpl<>(userDTOS, users.getPageable(), users.getTotalElements());
- }
- @CacheEvict(value = "user", allEntries = true)
- public void setTradeCode(Long userId, String token, String tradeCode) {
- String phone = smsService.verifyToken(token);
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- if (!StringUtils.equals(phone, user.getPhone())) {
- throw new BusinessException("验证码无效");
- }
- user.setTradeCode(passwordEncoder.encode(tradeCode));
- save(user);
- }
- public void verifyTradeCode(Long userId, String tradeCode) {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- if (!passwordEncoder.matches(tradeCode, user.getTradeCode())) {
- throw new BusinessException("交易密码错误");
- }
- }
- public Map<String, Object> searchByPhone(String phone) {
- if (AuthStatus.SUCCESS != SecurityUtils.getAuthenticatedUser().getAuthStatus()) {
- throw new BusinessException("实名认证后才能赠送");
- }
- User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("用户不存在或未认证"));
- if (AuthStatus.SUCCESS != user.getAuthStatus()) {
- throw new BusinessException("用户不存在或未认证");
- }
- String realName = identityAuthRepo.findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(
- user.getId(), AuthStatus.SUCCESS)
- .map(IdentityAuth::getRealName).orElse("").replaceAll(".*(?=.)", "**");
- Map<String, Object> map = new HashMap<>();
- map.put("id", user.getId());
- map.put("avatar", user.getAvatar());
- map.put("phone", user.getPhone().replaceAll("(?<=.{3}).*(?=.{4})", "**"));
- map.put("realName", realName);
- return map;
- }
- public Map<String, Object> searchByPhoneAdmin(String phoneStr) {
- List<String> phone = Arrays.stream(phoneStr.replaceAll("\n", " ")
- .replaceAll("\r\n", " ")
- .split(" "))
- .map(String::trim)
- .filter(s -> !StringUtils.isEmpty(s))
- .collect(Collectors.toList());
- List<User> users = userRepo.findByPhoneInAndDelFalse(phone);
- Map<String, Object> map = new HashMap<>();
- map.put("users", users);
- List<String> notFound = phone.stream().filter(p -> users.stream().noneMatch(u -> p.equals(u.getPhone())))
- .collect(Collectors.toList());
- map.put("notFound", notFound);
- return map;
- }
- public void addBankCard(Long userId, String bankNo, String phone, String code) throws BaseAdaPayException {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- IdentityAuth identityAuth = identityAuthRepo
- .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(userId, AuthStatus.SUCCESS)
- .orElseThrow(new BusinessException("用户未认证"));
- if (identityAuth.isOrg()) {
- //throw new BusinessException("企业认证用户请绑定对公账户");
- }
- if (!StringUtils.isBlank(user.getSettleAccountId())) {
- throw new BusinessException("此账号已绑定");
- }
- BankValidate bankValidate = BankUtils.validate(bankNo);
- if (!bankValidate.isValidated()) {
- throw new BusinessException("暂不支持此卡");
- }
- smsService.verify(phone, code);
- // adapayMerchantService.createMemberForAll(userId.toString(), user.getPhone(), identityAuth.getRealName(), identityAuth.getIdNo());
- // user.setMemberId(user.getId().toString());
- // save(user);
- //
- // String accountId = adapayMerchantService.createSettleAccountForAll
- // (user.getMemberId(), identityAuth.getRealName(),
- // identityAuth.getIdNo(), phone, bankNo);
- // user.setSettleAccountId(Optional.ofNullable(accountId).orElse("1"));
- // save(user);
- user.setMemberId(user.getId().toString());
- user.setSettleAccountId("1");
- save(user);
- userBankCardRepo.save(UserBankCard.builder()
- .bank(bankValidate.getBank())
- .bankName(bankValidate.getBankName())
- .bankNo(bankNo)
- .cardType(bankValidate.getCardType())
- .cardTypeDesc(bankValidate.getCardTypeDesc())
- .userId(userId)
- .phone(phone)
- .realName(identityAuth.getRealName())
- .idNo(identityAuth.getIdNo())
- .build());
- userBalanceRepo.unlock(userId);
- }
- public void removeBankCard(Long userId) throws BaseAdaPayException {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- // if (StringUtils.isNotBlank(user.getSettleAccountId()) && StringUtils.isNotBlank(user.getMemberId())) {
- // adapayMerchantService.delSettleAccountForAll(user.getMemberId());
- // user.setSettleAccountId(null);
- // save(user);
- // userBankCardRepo.deleteByUserId(userId);
- // cacheService.clearUserMy(userId);
- // } else {
- // throw new BusinessException("未绑定");
- // }
- user.setSettleAccountId(null);
- save(user);
- userBankCardRepo.deleteByUserId(userId);
- cacheService.clearUserMy(userId);
- }
- public void removeAuth(Long userId) {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- if (user.getAuthStatus() == AuthStatus.SUCCESS) {
- user.setAuthStatus(AuthStatus.NOT_AUTH);
- save(user);
- identityAuthRepo.deleteAll(identityAuthRepo.findByUserIdAndDelFalse(userId));
- cacheService.clearUserMy(userId);
- }
- }
- public Map<String, Object> batchRegister(String phones, String defaultPassword) {
- List<String> exist = new ArrayList<>();
- List<String> err = new ArrayList<>();
- List<String> success = new ArrayList<>();
- Arrays.stream(phones.replaceAll(",", " ")
- .replaceAll(",", " ")
- .replaceAll("\n", " ")
- .replaceAll("\r\n", " ")
- .split(" ")).forEach(phone -> {
- if (userRepo.findByPhoneAndDelFalse(phone).isPresent()) {
- exist.add(phone);
- } else {
- if (!Pattern.matches("^1[3-9]\\d{9}$", phone)) {
- err.add(phone);
- } else {
- try {
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- User user = create(UserRegister.builder()
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .username(name)
- .nickname(name)
- .password(defaultPassword)
- .avatar(Constants.DEFAULT_AVATAR)
- .phone(phone)
- .build());
- success.add(phone);
- } catch (Exception e) {
- log.error("注册失败", e);
- err.add(phone);
- }
- }
- }
- });
- Map<String, Object> map = new HashMap<>();
- map.put("exist", exist);
- map.put("error", err);
- map.put("success", success);
- return map;
- }
- public Map<String, Object> invite(PageQuery pageQuery) {
- Page<User> all = this.all(pageQuery).toPage();
- List<Long> userIds = all.map(User::getId).getContent();
- List<TokenHistory> page = tokenHistoryRepo.userBuy(userIds);
- Map<Long, BigDecimal> buy = page.stream()
- .collect(Collectors.groupingBy(TokenHistory::getToUserId,
- Collectors.mapping(TokenHistory::getPrice,
- Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
- Page<InvitePhoneDTO> users = all.map(user -> {
- InvitePhoneDTO dto = new InvitePhoneDTO(user);
- dto.setTotal(buy.get(user.getId()) == null ? BigDecimal.ZERO : buy.get(user.getId()));
- return dto;
- });
- BigDecimal total = buy.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
- Map<String, Object> map = new HashMap<>();
- map.put("user", users);
- map.put("total", total);
- return map;
- }
- @Async
- public void checkSettleAccountAsync() {
- checkSettleAccount();
- }
- public void checkSettleAccount() {
- List<User> list = userRepo.findBySettleAccountIdIsNotNull();
- AtomicInteger count = new AtomicInteger();
- list.forEach(user -> {
- try {
- Thread.sleep(500);
- IdentityAuth identityAuth = identityAuthRepo
- .findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(user.getId(), AuthStatus.SUCCESS)
- .orElseThrow(new BusinessException("用户未认证"));
- UserBankCard userBankCard = userBankCardRepo.findByUserId(user.getId()).stream().findAny()
- .orElseThrow(new BusinessException("未绑卡"));
- adapayMerchantService.createMemberForAll(
- user.getId().toString(), Optional.ofNullable(userBankCard.getPhone()).orElse(user.getPhone()),
- identityAuth.getRealName(), identityAuth.getIdNo());
- adapayMerchantService.createSettleAccountForAll(
- user.getId().toString(), identityAuth.getRealName(),
- identityAuth.getIdNo(), Optional.ofNullable(userBankCard.getPhone()).orElse(user.getPhone()),
- userBankCard.getBankNo());
- userBankCard.setPhone(Optional.ofNullable(userBankCard.getPhone()).orElse(user.getPhone()));
- userBankCardRepo.save(userBankCard);
- } catch (Exception e) {
- user.setSettleAccountId(null);
- save(user);
- userBankCardRepo.deleteByUserId(user.getId());
- }
- count.getAndIncrement();
- log.info("checkSettleAccount {}/{}", count.get(), list.size());
- });
- }
- @Cacheable(value = "myUserInfo", key = "#id")
- public User my(Long id) {
- User user = userRepo.findById(id).orElseThrow(new BusinessException("用户不存在"));
- user.setPassword(null);
- user.setTradeCode(null);
- return user;
- }
- public Page<Minter> toMinterDTO(Page<User> users) {
- List<User> origins = users.getContent();
- List<Minter> minters = new ArrayList<>();
- origins.forEach(user -> {
- Minter minter = Minter.builder()
- .id(user.getId())
- .name(user.getNickname())
- .avatar(user.getAvatar())
- .build();
- minters.add(minter);
- });
- return new PageImpl<>(minters, users.getPageable(), users.getTotalElements());
- }
- @Async
- public List<User> scanWeakPassword() {
- String[] weakPass = new String[]{
- "000000", "111111", "11111111", "112233", "123123", "123321", "123456", "12345678", "654321", "666666",
- "888888", "abcdef", "abcabc", "abc123", "a1b2c3", "aaa111", "123qwe", "qwerty", "qweasd", "admin",
- "password", "p@ssword", "passwd", "iloveyou", "5201314", "asdfghjkl", "66666666", "88888888"};
- boolean hasNext = true;
- int pageNum = 0;
- List<User> list = new ArrayList<>();
- while (hasNext) {
- Page<User> page = userRepo.findAll((Specification<User>) (root, query, criteriaBuilder) ->
- criteriaBuilder.isNotNull(root.get("password")), PageRequest.of(pageNum++, 200, Sort.by("id")));
- page.getContent().parallelStream().forEach(user -> {
- BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
- boolean match = false;
- for (String pass : weakPass) {
- if (encoder.matches(pass, user.getPassword())) {
- match = true;
- log.info("检测到弱密码userId={}, pass={}", user.getId(), pass);
- weakPassRepo.save(new WeakPass(user.getId(), pass));
- user.setPassword(null);
- save(user);
- redisTemplate.opsForValue().set(RedisKeys.JWT_TOKEN + user.getId(), "1");
- break;
- }
- }
- if (match) {
- list.add(user);
- }
- });
- hasNext = page.hasNext();
- }
- return list;
- }
- public List<InvitorDTO> findInviteOrderByCount(Long collectionId) {
- redisTemplate.opsForValue().get(RedisKeys.INVITOR_LIST + collectionId);
- List<InvitorDTO> dtos;
- dtos = JSONObject.parseArray((String) redisTemplate.opsForValue()
- .get(RedisKeys.INVITOR_LIST + collectionId), InvitorDTO.class);
- if (dtos == null) {
- dtos = new ArrayList<>();
- List<Object[]> objects = userRepo.customSearch(collectionId);
- for (Object[] object : objects) {
- InvitorDTO invitorDTO = new InvitorDTO((BigInteger) object[0], (String) object[1], (BigInteger) object[2]);
- dtos.add(invitorDTO);
- }
- redisTemplate.opsForValue()
- .set(RedisKeys.INVITOR_LIST + collectionId, JSONObject.toJSONString(dtos), Duration
- .ofSeconds(60 * 10));
- }
- return dtos;
- }
- public InvitorDetailDTO findMyInviteRecord(Long userId, Long collectionId) {
- InvitorDetailDTO result = new InvitorDetailDTO();
- // if (!SecurityUtils.getAuthenticatedUser().getId().equals(userId)) {
- // throw new BusinessException("无法查询他人邀请记录");
- // }
- List<InvitorDTO> invitorDTOS = findInviteOrderByCount(collectionId);
- InvitorDTO dto = invitorDTOS.stream()
- .filter(invitorDTO -> invitorDTO.getUserId().equals(BigInteger.valueOf(userId)))
- .findFirst().orElse(null);
- if (dto != null) {
- result.setIndex(invitorDTOS.indexOf(dto) + 1);
- result.setUserId(BigInteger.valueOf(userId));
- result.setNickName(dto.getNickName());
- if (result.getIndex() != 1) {
- result.setLastCount(invitorDTOS.get(invitorDTOS.indexOf(dto) - 1).getCount());
- }
- } else {
- result.setUserId(BigInteger.valueOf(userId));
- result.setNickName(SecurityUtils.getAuthenticatedUser().getNickname());
- }
- List<InvitedUserDTO> invitedUserDTOS = userRepo.findInvitedDTO(collectionId, userId);
- result.setInvitedUserDTOS(invitedUserDTOS);
- result.setCount(BigInteger.valueOf(invitedUserDTOS.size()));
- return result;
- }
- public void enableWallet(Long userId) {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
- if (user.isWalletEnabled()) {
- return;
- }
- if (!sysConfigService.getBoolean("enable_wallet")) {
- throw new BusinessException("绿魔卡功能暂未开启");
- }
- IdentityAuth identityAuth = identityAuthRepo.findByUserId(userId).stream().findFirst().orElse(null);
- if (identityAuth == null) {
- throw new BusinessException("请先完成实名认证");
- }
- // long age = ChronoUnit.YEARS.between(LocalDate.parse(identityAuth.getIdNo().substring(6, 14),
- // DateTimeFormatter.ofPattern("yyyyMMdd")), LocalDate.now());
- // if (!((age >= 22 && age <= 55))) {
- // throw new BusinessException("仅22至55周岁藏家可申请绿魔卡");
- // }
- // BigDecimal amount = sysConfigService.getBigDecimal("wallet_enable_amount");
- // if (Optional.ofNullable(orderRepo.sumUserPrice(userId)).orElse(BigDecimal.ZERO).compareTo(amount) < 0) {
- // throw new BusinessException("申请绿魔卡需满" + amount + "绿洲石");
- // }
- user.setWalletEnabled(true);
- save(user);
- }
- public Page<CompanyDTO> companyList(PageQuery pageQuery) {
- Page<User> users = this.all(pageQuery).toPage();
- List<Map<String, Object>> companyNums = showroomRepo.countNum("COMPANY");
- Map<Long, Integer> showroomNum = new HashMap<>();
- companyNums.forEach(value -> showroomNum.put(Convert.convert(Long.class, value.get("user_id")),
- Convert.convert(Integer.class, value.get("num"))));
- List<Map<String, Object>> companyBoxNums = showroomRepo.countNum("COMPANY_BOX");
- Map<Long, Integer> boxNum = new HashMap<>();
- companyBoxNums.forEach(value -> boxNum.put(Convert.convert(Long.class, value.get("user_id")),
- Convert.convert(Integer.class, value.get("num"))));
- return users.map(user -> {
- CompanyDTO dto = new CompanyDTO(user);
- dto.setShowroomNum(showroomNum.get(user.getId()) == null ? 0 : showroomNum.get(user.getId()));
- dto.setBoxShowroomNum(boxNum.get(user.getId()) == null ? 0 : boxNum.get(user.getId()));
- return dto;
- });
- }
- public Object loginTrading(String phone, String password, String tradeCode) {
- if (StringUtils.isEmpty(phone)) {
- throw new BusinessException("手机号错误");
- }
- User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("账号或密码错误"));
- TradingAccount tradingAccount = tradingAccountRepo.findById(user.getId())
- .orElseThrow(new BusinessException("账号或密码错误"));
- if (StringUtils.isEmpty(user.getPassword())) {
- throw new BusinessException("账号或密码错误");
- }
- if (StringUtils.isNoneEmpty(user.getPassword()) &&
- !passwordEncoder.matches(password, user.getPassword())) {
- throw new BusinessException("账号或密码错误");
- }
- if (StringUtils.isNoneEmpty(user.getPassword()) &&
- !passwordEncoder.matches(tradeCode, user.getTradeCode())) {
- throw new BusinessException("支付密码错误");
- }
- Map<String, Object> map = new HashMap<>();
- map.put("user", user);
- map.put("token", jwtTokenUtil.generateToken(JwtUserFactory.create(user)));
- map.put("account", tradingAccount);
- return map;
- }
- public Object myTrading(Long id) {
- User user = userRepo.findById(id).orElseThrow(new BusinessException("账号或密码错误"));
- TradingAccount tradingAccount = tradingAccountRepo.findById(user.getId())
- .orElseThrow(new BusinessException("账号或密码错误"));
- Map<String, Object> map = new HashMap<>();
- map.put("user", user);
- map.put("account", tradingAccount);
- return map;
- }
- public String prepareAliAuth(String type, Long userId, String name, String no) throws AlipayApiException {
- Long id = snowflakeIdWorker.nextId();
- AlipayUserCertifyOpenInitializeRequest request = new AlipayUserCertifyOpenInitializeRequest();
- JSONObject biz = new JSONObject();
- biz.put("outer_order_no", id + "");
- biz.put("biz_code", "FACE");
- JSONObject identity_param = new JSONObject();
- identity_param.put("identity_type", "CERT_INFO");
- identity_param.put("cert_type", type);
- identity_param.put("cert_name", name);
- identity_param.put("cert_no", no);
- biz.put("identity_param", identity_param);
- JSONObject merchant_config = new JSONObject();
- merchant_config.put("return_url", "alipays://platformapi/startapp?appId=20000067&url=" +
- URLEncoder.encode(generalProperties.getHost() + "/user/faceAuthNotify/" + id, StandardCharsets.UTF_8));
- biz.put("merchant_config", merchant_config);
- log.info(JSON.toJSONString(biz, true));
- request.setBizContent(biz.toJSONString());
- AlipayUserCertifyOpenInitializeResponse response = alipayClient.execute(request);
- if (response.isSuccess()) {
- String certifyId = response.getCertifyId();
- faceAuthRepo.save(FaceAuth.builder()
- .id(id)
- .userId(userId)
- .name(name)
- .idNo(no)
- .certifyId(certifyId)
- .build());
- return certifyId;
- }
- throw new BusinessException(response.getMsg());
- }
- public String getAliAuthUrl(String certify_id) throws AlipayApiException {
- AlipayUserCertifyOpenCertifyRequest request = new AlipayUserCertifyOpenCertifyRequest();
- JSONObject bizContentObj = new JSONObject();
- bizContentObj.put("certify_id", certify_id);
- request.setBizContent(bizContentObj.toString());
- AlipayUserCertifyOpenCertifyResponse response = alipayClient.pageExecute(request, "GET");
- if (response.isSuccess()) {
- return response.getBody();
- }
- throw new BusinessException(response.getMsg());
- }
- public User oneKeyLogin(String umengKey, String token) {
- String phone = UmengUtils.getMobile(umengKey, token);
- if (StringUtils.isBlank(phone)) {
- throw new BusinessException("登录失败,请尝试其他方式");
- }
- User user = userRepo.findByPhoneAndDelFalse(phone).orElse(null);
- if (user == null) {
- String name = "0x" + RandomStringUtils.randomAlphabetic(8);
- user = create(UserRegister.builder()
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .username(name)
- .nickname(name)
- .avatar(Constants.DEFAULT_AVATAR)
- .phone(phone)
- .build());
- }
- return user;
- }
- public Map<String, Object> checkFaceAuth(String certifyId) throws AlipayApiException {
- AlipayUserCertifyOpenQueryRequest request = new AlipayUserCertifyOpenQueryRequest();
- JSONObject biz = new JSONObject();
- biz.put("certify_id", certifyId);
- request.setBizContent(biz.toJSONString());
- AlipayUserCertifyOpenQueryResponse response = alipayClient.execute(request);
- Map<String, Object> map = new HashMap<>();
- if (response.isSuccess()) {
- System.out.println("调用成功");
- } else {
- System.out.println("调用失败");
- }
- return map;
- }
- public void faceAuthNotify(Long id) {
- faceAuthRepo.findById(id).ifPresent(faceAuth -> {
- try {
- AlipayUserCertifyOpenQueryRequest request = new AlipayUserCertifyOpenQueryRequest();
- JSONObject biz = new JSONObject();
- biz.put("certify_id", faceAuth.getCertifyId());
- request.setBizContent(biz.toJSONString());
- AlipayUserCertifyOpenQueryResponse response = alipayClient.execute(request);
- if (response.isSuccess()) {
- JSONObject res = JSONObject.parseObject(response.getBody());
- JSONObject data = res.getJSONObject("alipay_user_certify_open_query_response");
- if (StringUtils.equals(data.getString("passed"), "T")) {
- User user = userRepo.findById(faceAuth.getUserId()).orElse(null);
- if (user != null) {
- IdentityAuth identityAuth = identityAuthRepo.save(IdentityAuth.builder()
- .userId(user.getId())
- .idNo(faceAuth.getIdNo())
- .realName(faceAuth.getName())
- .status(AuthStatus.SUCCESS)
- .build());
- identityAuthRepo.deleteDuplicated(user.getId(), identityAuth.getId());
- user.setAuthStatus(AuthStatus.SUCCESS);
- user.setAuthId(identityAuth.getId());
- save(user);
- }
- }
- }
- } catch (AlipayApiException e) {
- throw new RuntimeException(e);
- }
- });
- }
- public Map<String, Object> oasisInfo(Long userId) {
- Map<String, Object> map = new HashMap<>();
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("未找到用户信息"));
- map.put("nickName", user.getNickname());
- List<Asset> assets = assetRepo.findAllByOwnerIdAndStatusAndOasisIdNotNull(userId, AssetStatus.NORMAL);
- List<OasisLoginDTO> oasisLoginDTOS = new ArrayList<>();
- assets.forEach(asset -> {
- OasisLoginDTO oasisLoginDTO = new OasisLoginDTO();
- oasisLoginDTO.setOasisId(asset.getOasisId());
- oasisLoginDTO.setAssetId(asset.getId());
- oasisLoginDTO.setSource(asset.getStatus().getDescription());
- Collection collection = collectionRepo.findFirstByOnShelfAndAssetId(true, asset.getId());
- if (collection != null) {
- oasisLoginDTO.setUrl(generalProperties.getHost() + "/9th/productDetail/" + collection
- .getId() + "?id=" + collection.getId());
- } else {
- oasisLoginDTO.setUrl("未公开展示");
- }
- Showroom showroom = showroomRepo.findByOasisId(oasisLoginDTO.getOasisId())
- .orElseThrow(new BusinessException("无记录 "));
- List<ShowCollection> origin = showCollectionRepo.findAllByShowroomIdOrderBySort(showroom.getId());
- List<ShowCollection> neo = new ArrayList<>();
- if (origin != null) {
- origin.forEach(orig -> collectionRepo.findById(orig.getCollectionId())
- .ifPresent(collection1 -> {
- orig.setStatus(showroomService.getStatus(collection1));
- orig.setPrice(collection1.getPrice());
- neo.add(orig);
- }));
- }
- showroom.setCollections(neo);
- User showRoomUser = SecurityUtils.getAuthenticatedUser();
- if (showRoomUser != null && !showRoomUser.isAdmin()) {
- List<NewsLike> likes = newsLikeRepo.findByUserIdAndShowroomId(showRoomUser
- .getId(), showroom.getId());
- showroom.setLiked(CollUtil.isNotEmpty(likes));
- }
- oasisLoginDTO.setShowroom(showroom);
- oasisLoginDTOS.add(oasisLoginDTO);
- });
- map.put("oasisInfo", oasisLoginDTOS);
- map.put("avatar", SecurityUtils.getAuthenticatedUser().getAvatar());
- return map;
- }
- /**
- * 流拍5次直接删号处罚
- */
- @Scheduled(cron = "0 0/10 * * * ?")
- public void delUser() {
- List<Long> userIds = auctionPassRecordRepo.checkUserId();
- if (CollUtil.isNotEmpty(userIds)) {
- log.info("流拍处罚:{}", userIds);
- userRepo.softDeleteIn(userIds);
- //清缓存
- userIds.forEach(id -> {
- cacheService.clearUserMy(id);
- cacheService.clearUser(id);
- });
- }
- }
- public void savePoint(User user) {
- //给积分
- Long invitor = user.getCollectionInvitor();
- if (ObjectUtils.isEmpty(user.getCollectionId()) || ObjectUtils.isEmpty(invitor)) {
- return;
- }
- Collection collection = collectionRepo.findById(user.getCollectionId()).orElse(null);
- if (collection == null) {
- return;
- }
- if (user.getVipPoint() < 1) {
- //有效新用户1个限购
- user.setVipPoint(100);
- userRepo.save(user);
- cacheService.clearUserMy(user.getId());
- cacheService.clearUser(user.getId());
- }
- //指标数量
- int assignment = collection.getAssignment();
- if (assignment < 1) {
- return;
- }
- int inviteNum = userRepo.countAllByCollectionIdAndCollectionInvitorAndSettleAccountIdIsNotNull(
- user.getCollectionId(), invitor);
- int point = inviteNum / assignment;
- if (point < 1) {
- return;
- }
- User parent = userRepo.findById(invitor).orElse(null);
- if (parent == null) {
- return;
- }
- if (parent.getVipPoint() < 1) {
- //老用户可有一个限购
- parent.setVipPoint(100);
- userRepo.save(parent);
- cacheService.clearUserMy(user.getId());
- cacheService.clearUser(user.getId());
- }
- UserProperty userProperty = userPropertyRepo.findById(invitor).orElse(new UserProperty(invitor, 0));
- if (userProperty.getMaxCount() < 10 && userProperty.getMaxCount() != point) {
- userProperty.setMaxCount(point);
- userPropertyRepo.save(userProperty);
- }
- }
- }
|