UserServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. @Autowired
  67. ICompanyService companyService;
  68. @Autowired
  69. IUserRoleService userRoleService;
  70. private User getByEmail(String email){
  71. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
  72. queryWrapper.lambda().eq(User ::getEmail,email);
  73. List<User> list = this.list(queryWrapper);
  74. if(list == null || list.size()<=0){
  75. return null;
  76. }
  77. return list.get(0);
  78. }
  79. @Override
  80. public User getByUserName(String phoneNum) {
  81. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  82. wrapper.eq(User::getUserName,phoneNum);
  83. List<User> list = this.list(wrapper);
  84. if(list != null && list.size() >0){
  85. return list.get(0);
  86. }
  87. return null;
  88. }
  89. @Override
  90. public void register(RegisterParam param) {
  91. Company company = new Company();
  92. company.setState(1);
  93. company.setCompanyName(param.getPhoneNum());
  94. companyService.save(company);
  95. User userEntity = new User();
  96. userEntity.setPassword(SecurityUtil.MD5(param.getPassword()));
  97. userEntity.setEmail(param.getEmail());
  98. userEntity.setUserName(param.getPhoneNum());
  99. userEntity.setNickName(param.getPhoneNum());
  100. userEntity.setHead(ConstantUrl.DEFAULT_USER_HEAD);
  101. userEntity.setCountry(param.getCountry());
  102. userEntity.setStatus(1);
  103. userEntity.setIsNotice(1);
  104. userEntity.setRecStatus("A");
  105. userEntity.setRegisterTime(DateUserUtil.getDate(new Date()));
  106. userEntity.setCompanyId(company.getId());
  107. userEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  108. userEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  109. this.save(userEntity);
  110. company.setManagerId(userEntity.getId());
  111. companyService.saveOrUpdate(company);
  112. UserRole userRole = new UserRole();
  113. userRole.setUserId(userEntity.getId());
  114. userRole.setRoleId(6L);
  115. userRoleService.save(userRole);
  116. }
  117. @Override
  118. public void updatePassword(String phoneNum, String password) {
  119. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  120. wrapper.set(User::getPassword,password)
  121. .eq(User::getUserName,phoneNum);
  122. this.update(wrapper);
  123. }
  124. @Override
  125. public UserVo getUserInfo(String userName) {
  126. User user = this.getByUserName(userName);
  127. UserVo userVo = new UserVo();
  128. BeanUtils.copyProperties(user,userVo);
  129. //Set<Long> roleIds = userRoleService.getByUser(user);
  130. Long cameraCount = 0L;
  131. // if(!roleIds.contains(5L) && !roleIds.contains(6L) && !roleIds.contains(1L)){
  132. // cameraCount = cameraDetailService.getCountByUserId(user.getCompanyId(),user.getId(),null);
  133. // }else {
  134. // cameraCount = cameraDetailService.getCountByCompanyId(user.getCompanyId(),null);
  135. // }
  136. cameraCount = cameraDetailService.getCountByCompanyId(user.getCompanyId(),null);
  137. Long incrementNum = userIncrementService.getCountByUserId(user.getId(),0);
  138. Long incrementBindNum = userIncrementService.getCountByUserId(user.getId(),1);
  139. userVo.setCameraCount(cameraCount);
  140. userVo.setIncrementNum(incrementNum);
  141. userVo.setIncrementBindNum(incrementBindNum);
  142. return userVo;
  143. }
  144. @Override
  145. public String uploadHead(String imgdata, String userName) throws Exception {
  146. if (StringUtils.isEmpty(imgdata)){
  147. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  148. }
  149. User dbUser = this.getByUserName(userName);
  150. StringBuilder sb = new StringBuilder().append("head")
  151. .append(File.separator).append(dbUser.getUserName()).append(File.separator)
  152. .append("head").append("_").append(new Date().getTime()).append(".png");
  153. StringBuilder qiniuPath = new StringBuilder().append("head")
  154. .append("/").append(dbUser.getUserName()).append("/")
  155. .append("head").append("_").append(new Date().getTime()).append(".png");
  156. FileUtils.uploadImg(ConstantFilePath.USER_IMAGES_PATH + sb.toString(), imgdata);
  157. byte[] imageByte = FileUtils.getImageByte(imgdata);
  158. //上传头像到oss
  159. fYunFileService.uploadFile(imageByte, qiniuPath.toString());
  160. dbUser.setHead(osssPrefixUrl + qiniuPath.toString()+"?m="+System.currentTimeMillis());
  161. this.updateById(dbUser);
  162. return dbUser.getHead();
  163. }
  164. @Override
  165. public void insertAddress(ShipAddressParam param, String userName) {
  166. if (StringUtils.isEmpty(param.getShipAddress()) || StringUtils.isEmpty(param.getShipAreaPath())
  167. || StringUtils.isEmpty(param.getProvince()) || StringUtils.isEmpty(param.getCity())){
  168. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  169. }
  170. User user = getByUserName(userName);
  171. receiverInfoService.saveByParam(param,user.getId());
  172. }
  173. @Override
  174. public void updateAddress(ShipAddressParam param, String userName) {
  175. if (param.getId() == null || StringUtils.isEmpty(param.getShipAddress()) || StringUtils.isEmpty(param.getShipAreaPath())
  176. || StringUtils.isEmpty(param.getProvince()) || StringUtils.isEmpty(param.getCity())){
  177. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  178. }
  179. User user = getByUserName(userName);
  180. receiverInfoService.updateDefaultAddress(param,user.getId());
  181. }
  182. @Override
  183. public void deleteAddress(Long id) {
  184. if (id == null ){
  185. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  186. }
  187. receiverInfoService.removeById(id);
  188. }
  189. @Override
  190. public void updateEmail(String email, String userName) {
  191. if (StringUtils.isEmpty(email)){
  192. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  193. }
  194. if(!email.matches(ConstantRegex.EMAIL_REGEX)){
  195. throw new BusinessException(LoginConstant.FAILURE_CODE_3019, LoginConstant.FAILURE_MSG_3019);
  196. }
  197. User user = this.getByEmail(email);
  198. if(user!=null && !user.getUserName().equals(userName)){
  199. throw new BusinessException(LoginConstant.FAILURE_CODE_3020 , LoginConstant.FAILURE_MSG_3020);
  200. }
  201. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  202. wrapper.eq(User::getUserName,userName);
  203. wrapper.set(User::getEmail,email);
  204. this.update(wrapper);
  205. }
  206. @Override
  207. public ReceiverInfo getReceiverInfo(String userName) {
  208. User dbUser = getByUserName(userName);
  209. return receiverInfoService.getDefaultByUserId(dbUser.getId());
  210. }
  211. @Override
  212. public List<ReceiverInfo> getReceiverList(String userName) {
  213. User dbUser = getByUserName(userName);
  214. return receiverInfoService.getListByUserId(dbUser.getId());
  215. }
  216. @Override
  217. public void updateNickName(String nickName, String userName) {
  218. if (StringUtils.isEmpty(nickName)){
  219. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  220. }
  221. //检验昵称敏感词
  222. Set<String> set = Variable.sensitiveWord.getSensitiveWord(nickName, 1);
  223. if (set != null && set.size() > 0){
  224. throw new BusinessException(LoginConstant.FAILURE_CODE_3012, LoginConstant.FAILURE_MSG_3012);
  225. }
  226. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  227. wrapper.eq(User::getUserName,userName);
  228. wrapper.set(User::getNickName,nickName);
  229. this.update(wrapper);
  230. }
  231. @Override
  232. public void updateUserDetail(UserParam param, String userName) {
  233. if (param.getNickName() != null){
  234. //检验昵称敏感词
  235. Set<String> set = Variable.sensitiveWord.getSensitiveWord(param.getNickName(), 1);
  236. if (set != null && set.size() > 0){
  237. throw new BusinessException(LoginConstant.FAILURE_CODE_3012, LoginConstant.FAILURE_MSG_3012);
  238. }
  239. }
  240. if(param.getEmail() != null && !param.getEmail().matches(ConstantRegex.EMAIL_REGEX)){
  241. throw new BusinessException(LoginConstant.FAILURE_CODE_3019, LoginConstant.FAILURE_MSG_3019);
  242. }
  243. User emailUser = getByEmail(param.getEmail());
  244. if(emailUser != null && !emailUser.getUserName().equals(userName)){
  245. throw new BusinessException(LoginConstant.FAILURE_CODE_3020 , LoginConstant.FAILURE_MSG_3020);
  246. }
  247. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  248. wrapper.eq(User::getUserName,userName);
  249. wrapper.set(User::getNickName,param.getNickName());
  250. wrapper.set(User::getEmail,param.getEmail());
  251. wrapper.set(User::getIsNotice,param.getIsNotice());
  252. this.update(wrapper);
  253. }
  254. @Override
  255. public HashMap<Long, User> getByIds(List<Long> userIds) {
  256. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  257. wrapper.in(User::getId,userIds);
  258. List<User> list = this.list(wrapper);
  259. HashMap<Long,User> map = new HashMap<>();
  260. list.forEach(entity -> map.put(entity.getId(),entity));
  261. return map;
  262. }
  263. @Override
  264. public Long getCountByNickName(String nickName) {
  265. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  266. wrapper.eq(User::getNickName,nickName);
  267. return this.count(wrapper);
  268. }
  269. @Override
  270. public List<Long> getLikeUserName(String userName) {
  271. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  272. wrapper.like(User::getUserName,userName);
  273. List<Long> collect = this.list(wrapper).parallelStream().map(User::getId).collect(Collectors.toList());
  274. return new ArrayList<>(collect);
  275. }
  276. @Override
  277. public List<Long> getCompanyIdsLikeUserName(String userName) {
  278. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  279. wrapper.like(User::getUserName,userName);
  280. List<Long> collect = this.list(wrapper).parallelStream().map(User::getCompanyId).collect(Collectors.toList());
  281. return new ArrayList<>(collect);
  282. }
  283. @Override
  284. public User getByToken(String token) {
  285. try {
  286. String value = redisUtil.get(String.format(RedisKey.TOKEN_V3, token));
  287. if(StringUtils.isEmpty(value)){
  288. throw new Exception();
  289. }
  290. return JSONObject.parseObject(value,User.class);
  291. }catch (Exception e){
  292. e.printStackTrace();
  293. }
  294. String username = JwtUtil.getUsername(token);
  295. return this.getByUserName(username);
  296. }
  297. @Override
  298. public CameraVo findCameraDetailByChildName(String token, String childName) {
  299. User userVo = this.getByToken(token);
  300. CameraVo cameraVo = cameraService.getVoByChildName(childName);
  301. if(userVo != null && userVo.getId()!= null){
  302. User user = this.getById(userVo.getId());
  303. cameraVo.setNickName(user.getNickName());
  304. }else {
  305. if(cameraVo.getType().equals(9)){
  306. cameraVo.setNickName("Minion设备用户");
  307. }else if(cameraVo.getType().equals(10)){
  308. cameraVo.setNickName("Laser设备用户");
  309. }else{
  310. cameraVo.setNickName("Pro设备用户");
  311. }
  312. }
  313. List<CameraSpaceVo> voList = cameraSpaceService.getVoListByCameraId(cameraVo.getId());
  314. if(voList != null && voList.size() > 0){
  315. CameraSpaceVo cameraSpace = voList.get(0);
  316. Long space = cameraSpace.getSpace();
  317. cameraVo.setSpaceId(cameraSpace.getId());
  318. cameraVo.setSpace((long) FileSizeUtil.formetFileSize(space, FileSizeUtil.SIZETYPE_GB));
  319. cameraVo.setSpaceStr(FileSizeUtil.formatFileSize(space));
  320. cameraVo.setSpaceEndStr(DateUtil.date2String(cameraSpace.getSpaceEndTime(), DateUtil.YYYY_MM_DD_DATE_FORMAT));
  321. if(Days.daysBetween(new DateTime(), new DateTime(cameraSpace.getSpaceEndTime())).getDays() < 7){
  322. cameraVo.setIsExpire(true);
  323. }
  324. }
  325. //获取会员权益
  326. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraVo.getId());
  327. if(userIncrement != null){
  328. ResponseUserIncrement responseUserIncrement = new ResponseUserIncrement();
  329. BeanUtils.copyProperties(userIncrement,responseUserIncrement);
  330. cameraVo.setResponseUserIncrement(responseUserIncrement);
  331. }
  332. if("10".equals(cameraVo.getCameraType()) ){ //深时权益
  333. cameraVo.setUsedSpace("0");
  334. }
  335. return cameraVo;
  336. }
  337. @Override
  338. public void updateDownloadNum(long userId, int num) {
  339. this.update(new LambdaUpdateWrapper<User>().setSql("download_num = download_num + " + num).eq(User::getId, userId));
  340. }
  341. }