UserServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.fdkankan.common.constant.ConstantRegex;
  8. import com.fdkankan.common.constant.ConstantUrl;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.util.*;
  11. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  12. import com.fdkankan.ucenter.common.constants.ConstantFilePath;
  13. import com.fdkankan.redis.constant.RedisKey;
  14. import com.fdkankan.redis.util.RedisUtil;
  15. import com.fdkankan.sensitive.Variable;
  16. import com.fdkankan.ucenter.constant.LoginConstant;
  17. import com.fdkankan.ucenter.entity.*;
  18. import com.fdkankan.ucenter.mapper.IUserMapper;
  19. import com.fdkankan.ucenter.service.*;
  20. import com.fdkankan.ucenter.util.DateUserUtil;
  21. import com.fdkankan.ucenter.vo.request.RegisterParam;
  22. import com.fdkankan.ucenter.vo.request.ShipAddressParam;
  23. import com.fdkankan.ucenter.vo.request.UserParam;
  24. import com.fdkankan.ucenter.vo.response.*;
  25. import java.io.File;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Set;
  31. import java.util.stream.Collectors;
  32. import javax.annotation.Resource;
  33. import org.apache.commons.lang3.StringUtils;
  34. import org.joda.time.DateTime;
  35. import org.joda.time.Days;
  36. import org.springframework.beans.BeanUtils;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.beans.factory.annotation.Value;
  39. import org.springframework.stereotype.Service;
  40. /**
  41. * <p>
  42. * 用户信息表 服务实现类
  43. * </p>
  44. *
  45. * @author
  46. * @since 2022-07-01
  47. */
  48. @Service
  49. public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements IUserService {
  50. @Value("${fyun.host}")
  51. private String osssPrefixUrl;
  52. @Autowired
  53. ICameraDetailService cameraDetailService;
  54. @Autowired
  55. IUserIncrementService userIncrementService;
  56. @Autowired
  57. IReceiverInfoService receiverInfoService;
  58. @Autowired
  59. RedisUtil redisUtil;
  60. @Autowired
  61. FYunFileServiceInterface fYunFileService;
  62. @Autowired
  63. ICameraService cameraService;
  64. @Autowired
  65. ICameraSpaceService cameraSpaceService;
  66. private User getByEmail(String email){
  67. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  68. queryWrapper.lambda().eq(User ::getEmail,email);
  69. List<User> list = this.list(queryWrapper);
  70. if(list == null || list.size()<=0){
  71. return null;
  72. }
  73. return list.get(0);
  74. }
  75. @Override
  76. public User getByUserName(String phoneNum) {
  77. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  78. wrapper.eq(User::getUserName,phoneNum);
  79. List<User> list = this.list(wrapper);
  80. if(list != null && list.size() >0){
  81. return list.get(0);
  82. }
  83. return null;
  84. }
  85. @Override
  86. public void register(RegisterParam param) {
  87. User userEntity = new User();
  88. userEntity.setPassword(SecurityUtil.MD5(param.getPassword()));
  89. userEntity.setEmail(param.getEmail());
  90. userEntity.setUserName(param.getPhoneNum());
  91. userEntity.setNickName(param.getPhoneNum());
  92. userEntity.setHead(ConstantUrl.DEFAULT_USER_HEAD);
  93. userEntity.setCountry(param.getCountry());
  94. userEntity.setStatus(1);
  95. userEntity.setIsNotice(1);
  96. userEntity.setRecStatus("A");
  97. userEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  98. userEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  99. this.save(userEntity);
  100. }
  101. @Override
  102. public void updatePassword(String phoneNum, String password) {
  103. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  104. wrapper.set(User::getPassword,password)
  105. .eq(User::getUserName,phoneNum);
  106. this.update(wrapper);
  107. }
  108. @Override
  109. public UserVo getUserInfo(String userName) {
  110. User user = this.getByUserName(userName);
  111. UserVo userVo = new UserVo();
  112. BeanUtils.copyProperties(user,userVo);
  113. Long cameraCount = cameraDetailService.getCountByUserId(user.getId(),null);
  114. Long incrementNum = userIncrementService.getCountByUserId(user.getId(),0);
  115. Long incrementBindNum = userIncrementService.getCountByUserId(user.getId(),1);
  116. userVo.setCameraCount(cameraCount);
  117. userVo.setIncrementNum(incrementNum);
  118. userVo.setIncrementBindNum(incrementBindNum);
  119. return userVo;
  120. }
  121. @Override
  122. public String uploadHead(String imgdata, String userName) throws Exception {
  123. if (StringUtils.isEmpty(imgdata)){
  124. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  125. }
  126. User dbUser = this.getByUserName(userName);
  127. StringBuilder sb = new StringBuilder().append("head")
  128. .append(File.separator).append(dbUser.getUserName()).append(File.separator)
  129. .append("head").append("_").append(new Date().getTime()).append(".png");
  130. StringBuilder qiniuPath = new StringBuilder().append("head")
  131. .append("/").append(dbUser.getUserName()).append("/")
  132. .append("head").append("_").append(new Date().getTime()).append(".png");
  133. FileUtils.uploadImg(ConstantFilePath.USER_IMAGES_PATH + sb.toString(), imgdata);
  134. byte[] imageByte = FileUtils.getImageByte(imgdata);
  135. //上传头像到oss
  136. fYunFileService.uploadFile(imageByte, qiniuPath.toString());
  137. dbUser.setHead(osssPrefixUrl + qiniuPath.toString()+"?m="+System.currentTimeMillis());
  138. this.updateById(dbUser);
  139. return dbUser.getHead();
  140. }
  141. @Override
  142. public void insertAddress(ShipAddressParam param, String userName) {
  143. if (StringUtils.isEmpty(param.getShipAddress()) || StringUtils.isEmpty(param.getShipAreaPath())
  144. || StringUtils.isEmpty(param.getProvince()) || StringUtils.isEmpty(param.getCity())){
  145. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  146. }
  147. User user = getByUserName(userName);
  148. receiverInfoService.saveByParam(param,user.getId());
  149. }
  150. @Override
  151. public void updateAddress(ShipAddressParam param, String userName) {
  152. if (param.getId() == null || StringUtils.isEmpty(param.getShipAddress()) || StringUtils.isEmpty(param.getShipAreaPath())
  153. || StringUtils.isEmpty(param.getProvince()) || StringUtils.isEmpty(param.getCity())){
  154. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  155. }
  156. User user = getByUserName(userName);
  157. receiverInfoService.updateDefaultAddress(param,user.getId());
  158. }
  159. @Override
  160. public void deleteAddress(Long id) {
  161. if (id == null ){
  162. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  163. }
  164. receiverInfoService.removeById(id);
  165. }
  166. @Override
  167. public void updateEmail(String email, String userName) {
  168. if (StringUtils.isEmpty(email)){
  169. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  170. }
  171. if(!email.matches(ConstantRegex.EMAIL_REGEX)){
  172. throw new BusinessException(LoginConstant.FAILURE_CODE_3019, LoginConstant.FAILURE_MSG_3019);
  173. }
  174. User user = this.getByEmail(email);
  175. if(user!=null && !user.getUserName().equals(userName)){
  176. throw new BusinessException(LoginConstant.FAILURE_CODE_3020 , LoginConstant.FAILURE_MSG_3020);
  177. }
  178. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  179. wrapper.eq(User::getUserName,userName);
  180. wrapper.set(User::getEmail,email);
  181. this.update(wrapper);
  182. }
  183. @Override
  184. public ReceiverInfo getReceiverInfo(String userName) {
  185. User dbUser = getByUserName(userName);
  186. return receiverInfoService.getDefaultByUserId(dbUser.getId());
  187. }
  188. @Override
  189. public List<ReceiverInfo> getReceiverList(String userName) {
  190. User dbUser = getByUserName(userName);
  191. return receiverInfoService.getListByUserId(dbUser.getId());
  192. }
  193. @Override
  194. public void updateNickName(String nickName, String userName) {
  195. if (StringUtils.isEmpty(nickName)){
  196. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  197. }
  198. //检验昵称敏感词
  199. Set<String> set = Variable.sensitiveWord.getSensitiveWord(nickName, 1);
  200. if (set != null && set.size() > 0){
  201. throw new BusinessException(LoginConstant.FAILURE_CODE_3012, LoginConstant.FAILURE_MSG_3012);
  202. }
  203. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  204. wrapper.eq(User::getUserName,userName);
  205. wrapper.set(User::getNickName,nickName);
  206. this.update(wrapper);
  207. }
  208. @Override
  209. public void updateUserDetail(UserParam param, String userName) {
  210. if (param.getNickName() != null){
  211. //检验昵称敏感词
  212. Set<String> set = Variable.sensitiveWord.getSensitiveWord(param.getNickName(), 1);
  213. if (set != null && set.size() > 0){
  214. throw new BusinessException(LoginConstant.FAILURE_CODE_3012, LoginConstant.FAILURE_MSG_3012);
  215. }
  216. }
  217. if(param.getEmail() != null && !param.getEmail().matches(ConstantRegex.EMAIL_REGEX)){
  218. throw new BusinessException(LoginConstant.FAILURE_CODE_3019, LoginConstant.FAILURE_MSG_3019);
  219. }
  220. User emailUser = getByEmail(param.getEmail());
  221. if(emailUser != null && !emailUser.getUserName().equals(userName)){
  222. throw new BusinessException(LoginConstant.FAILURE_CODE_3020 , LoginConstant.FAILURE_MSG_3020);
  223. }
  224. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  225. wrapper.eq(User::getUserName,userName);
  226. wrapper.set(User::getNickName,param.getNickName());
  227. wrapper.set(User::getEmail,param.getEmail());
  228. wrapper.set(User::getIsNotice,param.getIsNotice());
  229. this.update(wrapper);
  230. }
  231. @Override
  232. public HashMap<Long, User> getByIds(List<Long> userIds) {
  233. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  234. wrapper.in(User::getId,userIds);
  235. List<User> list = this.list(wrapper);
  236. HashMap<Long,User> map = new HashMap<>();
  237. list.forEach(entity -> map.put(entity.getId(),entity));
  238. return map;
  239. }
  240. @Override
  241. public Long getCountByNickName(String nickName) {
  242. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  243. wrapper.eq(User::getNickName,nickName);
  244. return this.count(wrapper);
  245. }
  246. @Override
  247. public List<Long> getLikeUserName(String userName) {
  248. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  249. wrapper.like(User::getUserName,userName);
  250. List<Long> collect = this.list(wrapper).parallelStream().map(User::getId).collect(Collectors.toList());
  251. return new ArrayList<>(collect);
  252. }
  253. @Override
  254. public User getByToken(String token) {
  255. try {
  256. String value = redisUtil.get(String.format(RedisKey.TOKEN_V3, token));
  257. if(StringUtils.isEmpty(value)){
  258. throw new Exception();
  259. }
  260. return JSONObject.parseObject(value,User.class);
  261. }catch (Exception e){
  262. e.printStackTrace();
  263. }
  264. String username = JwtUtil.getUsername(token);
  265. return this.getByUserName(username);
  266. }
  267. @Override
  268. public CameraVo findCameraDetailByChildName(String token, String childName) {
  269. User userVo = this.getByToken(token);
  270. CameraVo cameraVo = cameraService.getVoByChildName(childName);
  271. if(userVo != null && userVo.getId()!= null){
  272. User user = this.getById(userVo.getId());
  273. cameraVo.setNickName(user.getNickName());
  274. }else {
  275. if(cameraVo.getType().equals(9)){
  276. cameraVo.setNickName("Minion设备用户");
  277. }else if(cameraVo.getType().equals(10)){
  278. cameraVo.setNickName("Laser设备用户");
  279. }else{
  280. cameraVo.setNickName("Pro设备用户");
  281. }
  282. }
  283. List<CameraSpaceVo> voList = cameraSpaceService.getVoListByCameraId(cameraVo.getId());
  284. if(voList != null && voList.size() > 0){
  285. CameraSpaceVo cameraSpace = voList.get(0);
  286. Long space = cameraSpace.getSpace();
  287. cameraVo.setSpaceId(cameraSpace.getId());
  288. cameraVo.setSpace((long) FileSizeUtil.formetFileSize(space, FileSizeUtil.SIZETYPE_GB));
  289. cameraVo.setSpaceStr(FileSizeUtil.formatFileSize(space));
  290. cameraVo.setSpaceEndStr(DateUtil.date2String(cameraSpace.getSpaceEndTime(), DateUtil.YYYY_MM_DD_DATE_FORMAT));
  291. if(Days.daysBetween(new DateTime(), new DateTime(cameraSpace.getSpaceEndTime())).getDays() < 7){
  292. cameraVo.setIsExpire(true);
  293. }
  294. }
  295. //获取会员权益
  296. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraVo.getId());
  297. if(userIncrement != null){
  298. ResponseUserIncrement responseUserIncrement = new ResponseUserIncrement();
  299. BeanUtils.copyProperties(userIncrement,responseUserIncrement);
  300. cameraVo.setResponseUserIncrement(responseUserIncrement);
  301. }
  302. if("10".equals(cameraVo.getCameraType()) ){ //深时权益
  303. cameraVo.setUsedSpace("0");
  304. }
  305. return cameraVo;
  306. }
  307. @Override
  308. public void updateDownloadNum(long userId, int num) {
  309. this.update(new LambdaUpdateWrapper<User>().setSql("download_num = download_num + " + num).eq(User::getId, userId));
  310. }
  311. }