package com.wtkj.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.wtkj.entity.*; import com.wtkj.service.*; import com.wutong.file.entity.FileAttach; import com.wutong.file.feign.IFileClient; import io.seata.spring.annotation.GlobalTransactional; import lombok.AllArgsConstructor; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.Func; import org.springblade.system.user.feign.IUserClient; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.Duration; import java.util.ArrayList; import java.util.List; import static com.wtkj.config.NacosConfigValue.expireIn; import static com.wtkj.config.NacosConfigValue.scanCount; /** * @author Blizzard * @create at 2023-09-19 17:35 * @describe */ @AllArgsConstructor @Service public class QrcodeServiceImpl implements IQrcodeService { private final BladeRedis redis; private final IUserClient userClient; private final IProjectAuthService projectAuthService; private final IProjectGroupService projectGroupService; private final IContactOuterService contactOuterService; private final IProjectService projectService; private final IFileShareService fileShareService; private final IFileClient fileClient; @Override public JSONObject createQrcode() { JSONObject res = new JSONObject(); String kv = IdWorker.getIdStr(); Long userId = AuthUtil.getUserId(); redis.setEx(kv, 0, Duration.ofSeconds(expireIn)); res.put("qrcodeId", kv); res.put("createUser", userId); return res; } @Override @GlobalTransactional(rollbackFor = Exception.class) public boolean inviteToDept(Long createUser, Integer type, String qrcodeId, Long userId, Long deptId) { boolean flag = false; boolean exist = isExist(qrcodeId); //二维码可用前提下 if (exist) { //1.绑定机构 R rpc = userClient.bindUser(userId, deptId); //2.保存外部联系人 if (rpc.isSuccess()) { flag = rpc.getData(); } if (type == 2) { ContactOuter contact = new ContactOuter(); contact.setRelatedDeptId(deptId); contact.setRelatedUserId(userId); contact.setUserId(createUser); contactOuterService.save(contact); } } return flag; } @Override @Transactional(rollbackFor = Exception.class) public boolean inviteToProject(String qrcodeId, Long deptId, Long projectId) { boolean flag = false; boolean exist = isExist(qrcodeId); if (exist) { //1.权限 2.联系人 3.项目组 ProjectAuth auth = new ProjectAuth(); auth.setProjectId(projectId); auth.setTopDept(deptId); auth.setUserDept(Func.firstLong(AuthUtil.getDeptId())); auth.setUserId(auth.getUserId()); projectAuthService.save(auth); Project project = projectService.getById(projectId); if (project != null) { ProjectGroup group = new ProjectGroup(); group.setProjectId(projectId); group.setInviteDept(project.getCompetentUnit()); group.setBeInvitedDept(deptId); flag = projectGroupService.save(group); } } return flag; } @Override public JSONObject createGroupQrcode(Long projectId) { JSONObject res = new JSONObject(); String kv = IdWorker.getIdStr(); redis.setEx(kv, 0, Duration.ofSeconds(expireIn)); res.put("createUser", AuthUtil.getUser()); res.put("projectId", projectId); res.put("qrcodeId", kv); return res; } @Override public Long savaShare(FileShare share) { Long key = null; boolean save = fileShareService.save(share); if (save) { key = share.getId(); redis.setEx(String.valueOf(key), 0, Duration.ofDays(3)); } return key; } @Override public List getShare(Long id) { List res = new ArrayList<>(); boolean available = isAvailable(String.valueOf(id)); if (available) { FileShare byId = fileShareService.getById(id); if (byId != null) { String bladeFileIds = byId.getBladeFileIds(); R> rpc = fileClient.fileList(bladeFileIds); if (rpc.isSuccess()) { res = rpc.getData(); } } } return res; } /** * 判断二维码是否有效和是否达到扫描上限 */ private boolean isExist(String key) { boolean flag = false; Boolean exists = redis.exists(key); //如果存在 if (exists) { Long count = redis.incrBy(key, 1); if (count > scanCount) { throw new ServiceException("二维码只能扫描" + scanCount + "次数"); } else { flag = true; } } return flag; } private boolean isAvailable(String key) { boolean flag = false; Boolean exists = redis.exists(key); //如果存在 if (exists) { Long count = redis.incrBy(key, 1); if (count > 20) { throw new ServiceException("二维码只能扫描" + 20 + "次数"); } else { flag = true; } } return flag; } }