UserServiceImpl.java 15 KB

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