From b0b94a084ed6c1a685caebb8dfb1a0d65fdd9ef6 Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期二, 16 七月 2024 10:57:42 +0800 Subject: [PATCH] 提交 --- server/service/src/main/java/com/doumee/service/business/impl/UsersServiceImpl.java | 382 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 378 insertions(+), 4 deletions(-) diff --git a/server/service/src/main/java/com/doumee/service/business/impl/UsersServiceImpl.java b/server/service/src/main/java/com/doumee/service/business/impl/UsersServiceImpl.java index 91ee428..e92832d 100644 --- a/server/service/src/main/java/com/doumee/service/business/impl/UsersServiceImpl.java +++ b/server/service/src/main/java/com/doumee/service/business/impl/UsersServiceImpl.java @@ -1,17 +1,28 @@ package com.doumee.service.business.impl; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.doumee.biz.system.SystemDictDataBiz; +import com.doumee.biz.zbom.model.IamUserRoleModel; +import com.doumee.biz.zbom.model.IamUserTypeModel; +import com.doumee.config.Jwt.JwtPayLoad; +import com.doumee.config.Jwt.JwtTokenUtil; +import com.doumee.core.annotation.excel.ExcelImporter; +import com.doumee.core.constants.ResponseStatus; +import com.doumee.core.exception.BusinessException; +import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.Utils; +import com.doumee.dao.admin.request.ShopImport; +import com.doumee.dao.admin.request.UserImport; import com.doumee.dao.business.ShopMapper; +import com.doumee.core.wx.WxMiniConfig; +import com.doumee.dao.business.SmsEmailMapper; import com.doumee.dao.business.UsersMapper; -import com.doumee.dao.business.model.Multifile; -import com.doumee.dao.business.model.News; -import com.doumee.dao.business.model.Shop; -import com.doumee.dao.business.model.Users; +import com.doumee.dao.business.model.*; import com.doumee.dao.system.model.SystemUser; +import com.doumee.dao.web.response.AccountResponse; import com.doumee.service.business.UsersService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; @@ -20,12 +31,23 @@ import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.apache.catalina.User; import org.apache.commons.lang3.StringUtils; +import me.chanjar.weixin.common.error.WxErrorException; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Objects; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * 鍛樺伐淇℃伅琛⊿ervice瀹炵幇 @@ -35,10 +57,16 @@ @Service public class UsersServiceImpl implements UsersService { + + ExecutorService executor = Executors.newFixedThreadPool(1); + @Autowired + private RedisTemplate<String, Object> redisTemplate; @Autowired private UsersMapper usersMapper; @Autowired private ShopMapper shopMapper; + @Autowired + private SmsEmailMapper smsEmailMapper; @Autowired private SystemDictDataBiz systemDictDataBiz; @@ -262,4 +290,350 @@ QueryWrapper<Users> wrapper = new QueryWrapper<>(users); return usersMapper.selectCount(wrapper); } + + + + + @Override + public AccountResponse phoneLogin(String phone,String code){ + if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(code)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + Users users = usersMapper.selectOne(new QueryWrapper<Users>().lambda().eq(Users::getPhone,phone).last("limit 1")); + if(Objects.isNull(users)){ + throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏈煡璇㈠埌璐︽埛淇℃伅"); + } + if(!Constants.equalsInteger(users.getIsdeleted(), Constants.ZERO)){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"褰撳墠璐︽埛宸插垹闄�"); + } + if(!StringUtils.equals(users.getStatus(),Constants.ZERO+"")){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"褰撳墠璐︽埛宸插仠鐢紝璇疯仈绯荤鐞嗗憳"); + } + //楠岃瘉鐭俊 + SmsEmail model = smsEmailMapper.selectOne(new QueryWrapper<SmsEmail>().lambda() + .eq(SmsEmail::getType, Constants.ZERO) + .eq(SmsEmail::getPhone, phone) + .eq(SmsEmail::getRemark, code) + .eq(SmsEmail::getIsdeleted, Constants.ZERO) + ); + if(model == null){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝楠岃瘉鐮佷笉姝g‘锛岃閲嶆柊鍙戦�佸啀璇曪紒"); + } + if(!Constants.equalsInteger(model.getStatus(),Constants.ZERO)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝楠岃瘉鐮佸凡澶辨晥锛岃閲嶆柊鍙戦�佸啀璇曪紒"); + } + model.setStatus(Constants.ONE); + model.setEditDate(new Date()); + if(model.getCreateDate() !=null && + System.currentTimeMillis() - model.getCreateDate().getTime() > 3*60*1000){ + smsEmailMapper.updateById(model); + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝楠岃瘉鐮佸凡澶辨晥锛岃閲嶆柊鍙戦�佸啀璇晘"); + } + smsEmailMapper.updateById(model); + //鍒涘缓token + JwtPayLoad payLoad = new JwtPayLoad(users.getId(),Constants.ONE); + String token = JwtTokenUtil.generateToken(payLoad); + AccountResponse accountResponse = new AccountResponse(); + accountResponse.setToken(token); + accountResponse.setUsers(users); + return accountResponse; + } + + @Override + public AccountResponse wxLogin(String code){ + try { + //鑾峰彇寰俊鏁忔劅鏁版嵁 + WxMaJscode2SessionResult session = WxMiniConfig.wxPersonnelService.getUserService().getSessionInfo(code); + String openId = session.getOpenid(); + if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(openId)) { + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鑾峰彇openid澶辫触锛佽鑱旂郴绠$悊鍛�"); + } + AccountResponse accountResponse = new AccountResponse(); + accountResponse.setSessionKey(session.getSessionKey()); + Users users = usersMapper.selectOne(new QueryWrapper<Users>().lambda().eq(Users::getOpenid,openId).last("limit 1")); + if(Objects.isNull(users)){ + return accountResponse; + } + if(!Constants.equalsInteger(users.getIsdeleted(),Constants.ZERO)){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"褰撳墠璐︽埛宸插垹闄�"); + } + if(!StringUtils.equals(users.getStatus(),Constants.ZERO+"")){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"褰撳墠璐︽埛宸插仠鐢紝璇疯仈绯荤鐞嗗憳"); + } + //鍒涘缓token + JwtPayLoad payLoad = new JwtPayLoad(users.getId(),Constants.ONE); + String token = JwtTokenUtil.generateToken(payLoad); + accountResponse.setToken(token); + accountResponse.setUsers(users); + return accountResponse; + } catch (WxErrorException e) { + e.printStackTrace(); + } + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"寰俊鐧诲綍寮傚父锛佽鑱旂郴绠$悊鍛�"); + } + + + @Override + public void bindingOpenid(String code,Long userId){ + try { + //鑾峰彇寰俊鏁忔劅鏁版嵁 + WxMaJscode2SessionResult session = WxMiniConfig.wxPersonnelService.getUserService().getSessionInfo(code); + String openId = session.getOpenid(); + if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(openId)) { + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鑾峰彇openid澶辫触锛佽鑱旂郴绠$悊鍛�"); + } + usersMapper.update(new UpdateWrapper<Users>().lambda().set(Users::getOpenid,null).eq(Users::getOpenid,openId)); + Users users = usersMapper.selectById(userId); + users.setOpenid(openId); + users.setEditDate(new Date()); + usersMapper.updateById(users); + } catch (WxErrorException e) { + e.printStackTrace(); + } + } + + @Override + @Transactional(rollbackFor = {BusinessException.class,Exception.class}) + public String importBatch(MultipartFile file){ + Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.IMPORTING_USERS); + if(importing!=null && importing){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝宸插瓨鍦ㄥ鍏ユ垨鑰呬笂涓嬬骇閲嶇疆浠诲姟姝e湪鎵ц涓紝璇风◢鍚庡啀璇曪紒"); + } + redisTemplate.opsForValue().set(Constants.RedisKeys.IMPORTING_USERS,true); + try { + LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); + ExcelImporter ie = null; + List<UserImport> dataList =null; + try { + ie = new ExcelImporter(file,0,0); + dataList = ie.getDataList(UserImport.class,null); + } catch (Exception e) { + e.printStackTrace(); + } + if(dataList == null || dataList.size() ==0){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝褰曞叆鏁版嵁涓虹┖锛�"); + } + Date date =new Date(); + List<UserImport> finalDataList = dataList; + Callable<String> task = () -> { + dealUserDataBiz(finalDataList,date,loginUserInfo); + return "寮傛浠诲姟瀹屾垚"; + }; + executor.submit(task); + + return "瀵煎叆鎴愬姛"; + }catch (BusinessException e){ + throw e; + }catch (Exception e){ + throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"淇℃伅瀵煎叆澶辫触锛岃绋嶅悗閲嶈瘯"); + } + } + + private void dealUserDataBiz(List<UserImport> dataList, Date date, LoginUserInfo loginUserInfo) { + try { + List<Users> list = usersMapper.selectJoinList(Users.class,new MPJLambdaWrapper<Users>() + .selectAll(Users.class) + ); + List<Users> insertList = new ArrayList<>(); + List<Users> updateList = new ArrayList<>(); + for(int i=0;i<dataList.size();i++){ + UserImport model = dataList.get(i); + if(StringUtils.isBlank(model.getName()) + &&StringUtils.isBlank(model.getCode()) ){ + continue; + } + Users user = checkModelParam(model,insertList,updateList,i,loginUserInfo,list,date); + } + if(insertList .size() >0){ + int temp = 0; + while(temp < insertList.size()){ + int index; + if(temp + 500 <= insertList.size()){ + index = temp+500; + }else{ + index = insertList.size(); + } + usersMapper.insert(insertList.subList(temp,index)); + temp = index; + } + } + if(updateList .size() >0){ + for(Users s : updateList){ + //閬嶅巻鏇存柊鏁版嵁 + usersMapper.updateById(s); + } + } + }catch (BusinessException e){ + throw e; + }catch (Exception e){ + throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"淇℃伅瀵煎叆澶辫触锛岃绋嶅悗閲嶈瘯"); + }finally { + executor.shutdown(); + redisTemplate.delete(Constants.RedisKeys.IMPORTING_USERS); + } + } + private Users getUserByUserID(String userId, List<Users> list) { + if(list ==null || list.size() ==0){ + return null; + } + for(Users s :list){ + if(StringUtils.equals(s.getIamId(),userId)){ + return s; + } + } + return null; + } + + private Users checkModelParam(UserImport model, List<Users> insertList, List<Users> updateList, int index, LoginUserInfo loginUserInfo, List<Users> list, Date date) { + + if(StringUtils.isBlank(model.getUserId())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝绗�"+(index+3)+"琛屻�愮紪鐮併�戜俊鎭笉瀹屾暣锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒"); + } + if(StringUtils.isBlank(model.getName())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝绗�"+(index+3)+"琛屻�愬鍚嶃�戜俊鎭笉瀹屾暣锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒"); + } + if(StringUtils.isBlank(model.getAccountNo())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝绗�"+(index+3)+"琛屻�愮櫥褰曡处鍙枫�戜俊鎭笉瀹屾暣锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒"); + } + if(StringUtils.isBlank(model.getPhone())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"瀵逛笉璧凤紝绗�"+(index+3)+"琛屻�愭墜鏈哄彿銆戜俊鎭笉瀹屾暣锛岃妫�鏌ヨ〃鏍煎唴瀹癸紒"); + } + Users users = getUserByUserID(model.getUserId(),list); + if(users == null){ + users = new Users(); + users.setCreator(loginUserInfo.getId()); + users.setCreateDate(date); + initUsersInfo(users,model,date,loginUserInfo); + insertList.add(users); + }else { + initUsersInfo(users,model,date,loginUserInfo); + updateList.add(users); + } + return users; + } + + private void initUsersInfo(Users users, UserImport model,Date date,LoginUserInfo loginUserInfo ){ + users.setEditor(loginUserInfo.getId()); + users.setEditDate(date); + users.setName(model.getName()); + users.setIamUsername(model.getAccountNo()); + users.setPhone(model.getPhone()); + users.setStatus(model.getStatus()); + users.setEmail(model.getEmail()); + users.setIamId(model.getUserId()); + users.setCode(model.getCode()); + users.setSex(model.getSex()); + users.setStatus(model.getStatus()); + users.setIsdeleted(Constants.ZERO); + users.setRoleIds(""); + users.setRoleNames(""); + users.setRoleCodes(""); + users.setUserTypes(""); + users.setIamOrgId(""); + getDepartidByParam(users,model.getOrgIds()); + setRolesInfo(users,model); + getUserTypesByParam(users,model.getTypeCodes()); + } + + private String getUserTypesByParam(Users users,String typeCodes) { + if( typeCodes !=null){ + String[] codes =typeCodes.split(","); + for(String t : codes){ + if(StringUtils.isBlank(users.getUserTypes())){ + users.setUserTypes(""); + } + users.setUserTypes(users.getUserTypes()+"["+t+"];"); + } + } + return users.getUserTypes(); + } + + private void setRolesInfo(Users users, UserImport model) { + if( model.getRoleCodes() !=null){ + String[] codes = model.getRoleCodes().split(","); + for(String t : codes){ + if(StringUtils.isBlank(users.getRoleCodes())){ + users.setRoleCodes(""); + } + users.setRoleCodes(users.getRoleCodes()+"["+t+"];"); + } + } + if( model.getRoleIds() !=null){ + String[] codes = model.getRoleIds().split(","); + for(String t : codes){ + if(StringUtils.isBlank(users.getRoleIds())){ + users.setRoleIds(""); + } + users.setRoleIds(users.getRoleIds()+"["+t+"];"); + } + } + if( model.getRoleNames() !=null){ + String[] codes = model.getRoleNames().split(","); + for(String t : codes){ + if(StringUtils.isBlank(users.getRoleNames())){ + users.setRoleNames(""); + } + users.setRoleNames(users.getRoleNames()+"["+t+"];"); + } + } + } + + private Shop getDepartidByParam(Users users , String orgIds) { + if( orgIds ==null){ + return null; + } + String[] orgArray = orgIds.split(","); + if( orgArray !=null && orgArray.length>0){ + for(String t : orgArray){ + Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().lambda() + .eq(Shop::getOrgId,t) + .last("limit 1")); + if(shop!=null) { + users.setIamOrgId(t); + users.setDepartmentId(shop.getId()); + return shop; + } + } + } + users.setIamOrgId(orgIds+"--灏氭湭鍚屾鍒扮粍缁�"); + + return null; + } + + + + @Override + public Users usersDetail(Long userId){ + Users users = usersMapper.selectById(userId); + if(Objects.isNull(users)){ + throw new BusinessException(ResponseStatus.DATA_EMPTY); + } + String prefix = StringUtils.trimToNull(systemDictDataBiz.queryByCode(Constants.OBJCET_STORAGE,Constants.RESOURCE_PATH).getCode()) + + StringUtils.trimToNull(systemDictDataBiz.queryByCode(Constants.OBJCET_STORAGE,Constants.USERS_FILE).getCode()); + if(StringUtils.isNotBlank(users.getImgurl())){ + users.setImgurlFull(prefix + users.getImgurl()); + } + if(StringUtils.isNotBlank(users.getQrcodeImg())){ + users.setQrcodeImgFull(prefix + users.getQrcodeImg()); + } + if(StringUtils.isNotBlank(users.getCompanyQrcodeImg())){ + users.setCompanyQrcodeImgFull(prefix + users.getCompanyQrcodeImg()); + } + if(Objects.nonNull(users.getDepartmentId())){ + Shop shop = shopMapper.selectById(users.getDepartmentId()); + if(Objects.nonNull(shop)){ + users.setShopName(shop.getName()); + if(StringUtils.isNotBlank(shop.getAddress())){ + users.setShopAddress(shop.getAddress()); + }else{ + users.setShopAddress(shop.getProvinceName()+shop.getCityName()+shop.getAreaName()+shop.getTown()+shop.getRegAddr()); + } + } + } + return users; + } + + + } + -- Gitblit v1.9.3