package com.ads.business.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import com.ads.business.dao.DocDao; import com.ads.business.dao.DocRoleDao; import com.ads.business.dao.TypeRoleDao; import com.ads.business.entity.*; import com.ads.business.service.*; import com.ads.business.service.data.GetLoginOutData; import com.ads.business.service.model.request.DocListRequest; import com.ads.business.service.model.request.DocRequest; import com.ads.business.service.model.response.CheckAuthResponse; import com.ads.business.service.model.response.DocListResponse; import com.ads.business.service.model.response.DocResponse; import com.ads.business.service.model.response.PdfDetailResponse; import com.ads.common.data.GetPictureConfigInData; import com.ads.common.data.JsonResult; import com.ads.common.data.PageInfo; import com.ads.common.exception.BusinessException; import com.ads.common.util.OnlineUserUtils; import com.ads.common.util.PageUtils; import com.ads.common.util.PdfUtil; import com.ads.common.util.UtilMessage; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author hou * @since 2021-10-21 */ @Slf4j @Service public class DocServiceImpl extends ServiceImpl implements DocService { @Resource private DocTypeService docTypeService; @Value("${doc.linkUrl}") private String linkUrl; @Resource private DocHistoryService docHistoryService; @Resource private TypeRoleService typeRoleService; @Autowired private GetPictureConfigInData pictureConfig; @Resource private DocRoleService docRoleService; @Resource private DocRoleDao docRoleDao; @Resource private DocConversationRelService docConversationRelService; @Resource private ConversationUserRelService conversationUserRelService; @Resource private DocTypeConversationRelService docTypeConversationRelService; @Resource private ConversationService conversationService; @Resource private TypeRoleDao typeRoleDao; @Override @Transactional(rollbackFor = Exception.class) public String addDoc(DocRequest inData) { String message = checkBean(inData, false); if (StringUtils.isNotEmpty(message)) { return message; } int count = this.count(new LambdaQueryWrapper() .eq(Doc::getTypeId, inData.getTypeId()) .eq(Doc::getName, inData.getName())); if (count > 0) { throw new BusinessException("同一分类下,文件名称已存在!"); } Doc doc = new Doc(); BeanUtil.copyProperties(inData, doc); GetLoginOutData userInfo = OnlineUserUtils.get(); if (null != userInfo) { doc.setCreateUser(Long.valueOf(userInfo.getUserId())); } doc.setCreateTime(new Date()); doc.setUpdateTime(new Date()); this.baseMapper.insert(doc); ArrayList docRoles = new ArrayList<>(); if (CollectionUtil.isNotEmpty(inData.getRole())) { inData.getRole().stream().distinct().forEach(e -> { DocRole docRole = new DocRole(); docRole.setDocId(doc.getId()); docRole.setMobile(e); docRoles.add(docRole); }); docRoleDao.insertBatch(docRoles); } Doc updoc = new Doc(); updoc.setId(doc.getId()); updoc.setLinkUrl(linkUrl + doc.getId() + "&showmenu=false"); this.updateById(updoc); DocHistory docHistory = new DocHistory(); BeanUtil.copyProperties(doc, docHistory); docHistory.setId(null); docHistory.setDocId(doc.getId()); docHistoryService.save(docHistory); //新增文件会话关系 if (CollectionUtil.isNotEmpty(inData.getConversationList())) { List list = new ArrayList<>(); inData.getConversationList().stream().forEach(conversationRequest -> { DocConversationRel docConversationRel = new DocConversationRel(); docConversationRel.setDocId(doc.getId()); docConversationRel.setChatId(conversationRequest.getChatId()); docConversationRel.setOpenConversationId(conversationRequest.getOpenConversationId()); list.add(docConversationRel); }); docConversationRelService.saveBatch(list); } return null; } @Override @Transactional(rollbackFor = Exception.class) public String updateDoc(DocRequest inData) { String message = checkBean(inData, true); if (StringUtils.isNotEmpty(message)) { return message; } Doc doc = this.baseMapper.selectById(Long.valueOf(inData.getId())); Boolean flag = false; if (!inData.getDocUrl().equals(doc.getDocUrl())) { flag = true; } int count = this.count(new LambdaQueryWrapper() .eq(Doc::getTypeId, inData.getTypeId()) .eq(Doc::getName, inData.getName()) .notIn(Doc::getId, inData.getId())); if (count > 0) { throw new BusinessException("同一分类下,文件名称已存在!"); } BeanUtil.copyProperties(inData, doc); doc.setId(Long.valueOf(inData.getId())); GetLoginOutData userInfo = OnlineUserUtils.get(); if (null != userInfo) { doc.setUpdateUser(Long.valueOf(userInfo.getUserId())); } doc.setUpdateTime(new Date()); this.baseMapper.updateById(doc); docRoleService.remove(Wrappers.lambdaQuery(DocRole.class).eq(DocRole::getDocId, doc.getId())); if (CollectionUtil.isNotEmpty(inData.getRole())) { ArrayList docRoles = new ArrayList<>(); inData.getRole().stream().distinct().forEach(e -> { DocRole docRole = new DocRole(); docRole.setDocId(doc.getId()); docRole.setMobile(e); docRoles.add(docRole); }); docRoleDao.insertBatch(docRoles); } if (flag) { DocHistory docHistory = new DocHistory(); BeanUtil.copyProperties(doc, docHistory); docHistory.setCreateTime(new Date()); docHistory.setCreateUser(Long.valueOf(userInfo.getUserId())); docHistory.setDocId(doc.getId()); docHistory.setId(null); docHistoryService.save(docHistory); } //根据文件ID删除 docConversationRelService.remove(new LambdaQueryWrapper() .eq(DocConversationRel::getDocId, inData.getId())); //新增文件会话关系 if (CollectionUtil.isNotEmpty(inData.getConversationList())) { //新增 List list = new ArrayList<>(); inData.getConversationList().stream().forEach(conversationRequest -> { DocConversationRel docConversationRel = new DocConversationRel(); docConversationRel.setDocId(doc.getId()); docConversationRel.setChatId(conversationRequest.getChatId()); docConversationRel.setOpenConversationId(conversationRequest.getOpenConversationId()); list.add(docConversationRel); }); docConversationRelService.saveBatch(list); } return null; } @Override public DocResponse detail(Long docId) { DocResponse outData = new DocResponse(); Doc doc = this.baseMapper.selectById(docId); if (null != doc) { BeanUtil.copyProperties(doc, outData); outData.setFileName(doc.getDocUrl().substring(doc.getDocUrl().lastIndexOf("/") + 1)); } List list = docRoleService.list(Wrappers.lambdaQuery(DocRole.class).eq(DocRole::getDocId, doc.getId())); if (CollectionUtil.isNotEmpty(list)) { List roles = list.stream().map(DocRole::getMobile).collect(Collectors.toList()); outData.setRole(roles); } List docRels = docConversationRelService.list(new LambdaQueryWrapper().eq(DocConversationRel::getDocId, docId)); if (CollectionUtil.isNotEmpty(docRels)) { outData.setConversationList(docRels); } return outData; } @Override public JsonResult pdfDetail(HttpServletRequest request, Long docId, String userNo) { GetLoginOutData userInfo = OnlineUserUtils.get(); if (null == userInfo) { return JsonResult.fail("您没有登录"); } if (StringUtils.isEmpty(userInfo.getMobile())) { return JsonResult.fail(1000, "请重新登录"); } //水印 姓名+手机号后四位 String watermark = userInfo.getName() + userInfo.getMobile().substring(userInfo.getMobile().length() - 5); Doc doc = this.baseMapper.selectById(docId); if (null == doc) { throw new BusinessException("当前文件信息不存在!"); } if (doc.getStatus() == 0) { return JsonResult.fail(1000, "当前文件已禁用,请启用后查看!"); } DocType docType = docTypeService.getById(doc.getTypeId()); if (ObjectUtil.isEmpty(docType)) { return JsonResult.fail("当前文件说在分类信息不存在"); } if (docType.getStatus() == 0) { return JsonResult.fail(1000, "当前文件所在分类已禁用,请启用后查看!"); } //userInfo.setMobile("18896995753"); //userInfo.setMobile("15261532462"); if (StringUtils.isEmpty(userInfo.getMobile())) { return JsonResult.fail(1000, "请退出重新登录钉钉"); } log.info("获取文件详情信息--校验会话权限"); List typeList = docTypeConversationRelService.list(new LambdaQueryWrapper() .eq(DocTypeConversationRel::getTypeId, docType.getTypeId())); List typeRoleMobiles = typeRoleDao.getListByTypeId(Long.valueOf(docType.getTypeId())); //手机号不为空,群组为空 if (CollectionUtil.isNotEmpty(typeRoleMobiles) && CollectionUtil.isEmpty(typeList)) { if (!typeRoleMobiles.contains(userInfo.getMobile())) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } Boolean res = checkAuthDetail(userInfo, doc); if (res) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } } //手机号不为空,群组不为空 if (CollectionUtil.isNotEmpty(typeRoleMobiles) && CollectionUtil.isNotEmpty(typeList)) { List conversationMobiles = docTypeConversationRelService.getListByTypeId(docType.getTypeId()); conversationMobiles.addAll(typeRoleMobiles); if (!conversationMobiles.contains(OnlineUserUtils.get().getMobile())) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } Boolean res = checkAuthDetail(userInfo, doc); if (res) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } } //手机号为空,群组不为空 if (CollectionUtil.isEmpty(typeRoleMobiles) && CollectionUtil.isNotEmpty(typeList)) { List conversationMobiles = docTypeConversationRelService.getListByTypeId(docType.getTypeId()); if (!conversationMobiles.contains(OnlineUserUtils.get().getMobile())) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } Boolean res = checkAuthDetail(userInfo, doc); if (res) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } } //手机号为空,群组为空 if (CollectionUtil.isEmpty(typeRoleMobiles) && CollectionUtil.isEmpty(typeList)) { Boolean res = checkAuthDetail(userInfo, doc); if (res) { return JsonResult.fail(1000, "您没有当前文件的查看权限"); } } log.info("校验权限结束!"); String fileName = doc.getName(); String docUrl = doc.getDocUrl(); String path = request.getScheme() + "://" + request.getServerName() + ":" + "19050" + request.getContextPath() + "/"; String filePath = pictureConfig.getServerBasePath() + "/" + pictureConfig.getUploadPath() + "/" + userNo + "/" + docId + fileName + ".pdf"; String createPath = pictureConfig.getBasePath() + "/" + pictureConfig.getUploadPath() + "/" + userNo + "/" + docId + fileName + ".pdf"; File fromFile = new File(createPath); //判断目标文件所在的目录是否存在 if (!fromFile.getParentFile().exists()) { //如果目标文件所在的目录不存在,则创建父目录 fromFile.getParentFile().mkdirs(); } if (fromFile.exists()) { fromFile.delete(); } try { PdfUtil.setWatermark(new BufferedOutputStream(new FileOutputStream(new File(createPath))), docUrl, watermark); } catch (FileNotFoundException e) { e.printStackTrace(); throw new BusinessException("生成pdf文件水印有异常!"); } log.info("pdfDetail--文件路径:{}", path + filePath); PdfDetailResponse rs = new PdfDetailResponse(); rs.setPath(path + filePath); rs.setFileName(fileName); return JsonResult.success(rs, UtilMessage.GET_MESSAGE_SUCCESS); } @Transactional(rollbackFor = Exception.class) @Override public JsonResult updateSwitch(Long id, Integer status) { Doc docdb = this.getById(id); if (ObjectUtil.isEmpty(docdb)) { throw new BusinessException(UtilMessage.DATA_EMPTY); } if (docdb.getStatus().equals(status) && status == 1) { throw new BusinessException("文件分类已启用,请勿重复操作!"); } if (docdb.getStatus().equals(status) && status == 0) { throw new BusinessException("文件分类已禁用,请勿重复操作!"); } Doc doc = new Doc(); doc.setId(id); doc.setStatus(status); this.updateById(doc); String msg; if (status == 1) { msg = UtilMessage.ENABLE_MESSAGE_SUCCESS; } else { msg = UtilMessage.DISABLE_MESSAGE_SUCCESS; } return JsonResult.success(null, msg); } /** * 获取文件列表信息 * * @param request 列表查询参数信息 * @param type 1 移动端 * @return 文件参数结果信息 */ @Override public JsonResult docList(DocListRequest request, Integer type) { if (null == request) { return JsonResult.fail("请输入文件分类信息"); } if (type == 1) { DocType docType = docTypeService.getById(request.getTypeId()); if (ObjectUtil.isEmpty(docType)) { return JsonResult.fail("文件分类信息不存在"); } if (docType.getStatus() == 0) { return JsonResult.fail(1000, "当前文件分类已禁用,请启用后查看!"); } if (StringUtils.isEmpty(request.getTypeId())) { return JsonResult.fail("请输入文件分类信息"); } GetLoginOutData userInfo = OnlineUserUtils.get(); if (null == userInfo) { return JsonResult.fail("您没有登录"); } //userInfo.setMobile("18896995753"); //userInfo.setMobile("15261532462"); if (StringUtils.isEmpty(userInfo.getMobile())) { return JsonResult.fail(1000, "请退出重新登录钉钉"); } List docList = this.list(new LambdaQueryWrapper().eq(Doc::getTypeId, request.getTypeId()).eq(Doc::getStatus, 1)); log.info("获取文件列表信息--校验会话分类权限"); List typeList = docTypeConversationRelService.list(new LambdaQueryWrapper() .eq(DocTypeConversationRel::getTypeId, request.getTypeId())); List typeRoleMobiles = typeRoleDao.getListByTypeId(Long.valueOf(request.getTypeId())); //手机号不为空,群组为空 if (CollectionUtil.isNotEmpty(typeRoleMobiles) && CollectionUtil.isEmpty(typeList)) { if (!typeRoleMobiles.contains(userInfo.getMobile())) { return JsonResult.fail(1000, "您没有当前文件分类的查看权限"); } List newList = checkAuth(userInfo, docList); if (CollectionUtil.isEmpty(newList)) { return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } else { return JsonResult.success(new PageInfo<>(newList), UtilMessage.GET_MESSAGE_SUCCESS); } } //手机号不为空,群组不为空 if (CollectionUtil.isNotEmpty(typeRoleMobiles) && CollectionUtil.isNotEmpty(typeList)) { List conversationMobiles = docTypeConversationRelService.getListByTypeId(docType.getTypeId()); conversationMobiles.addAll(typeRoleMobiles); if (!conversationMobiles.contains(OnlineUserUtils.get().getMobile())) { return JsonResult.fail(1000, "您没有当前文件分类的查看权限"); } List newList = checkAuth(userInfo, docList); if (CollectionUtil.isEmpty(newList)) { return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } else { return JsonResult.success(new PageUtils(newList, newList.size(), request.getPageSize(), request.getPage()) , UtilMessage.GET_MESSAGE_SUCCESS); } } //手机号为空,群组不为空 if (CollectionUtil.isEmpty(typeRoleMobiles) && CollectionUtil.isNotEmpty(typeList)) { List conversationMobiles = docTypeConversationRelService.getListByTypeId(docType.getTypeId()); if (!conversationMobiles.contains(OnlineUserUtils.get().getMobile())) { return JsonResult.fail(1000, "您没有当前文件分类的查看权限"); } List newList = checkAuth(userInfo, docList); if (CollectionUtil.isEmpty(newList)) { return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } else { return JsonResult.success(new PageUtils(newList, newList.size(), request.getPageSize(), request.getPage()) , UtilMessage.GET_MESSAGE_SUCCESS); } } //手机号为空,群组为空 if (CollectionUtil.isEmpty(typeRoleMobiles) && CollectionUtil.isEmpty(typeList)) { List newList = checkAuth(userInfo, docList); if (CollectionUtil.isEmpty(newList)) { return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } else { return JsonResult.success(new PageUtils(newList, newList.size(), request.getPageSize(), request.getPage()) , UtilMessage.GET_MESSAGE_SUCCESS); } } return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } else { Long currPage = request.getPage() == null ? 1 : request.getPage().longValue(); Long pageSize = request.getPageSize() == null ? 10 : request.getPageSize().longValue(); Page page = new Page<>(currPage, pageSize); QueryWrapper wrapper = new QueryWrapper(); if (StringUtils.isNotBlank(request.getTypeId())) { wrapper.eq("hdt.type_id", Long.valueOf(request.getTypeId())); } if (StringUtils.isNotBlank(request.getName())) { wrapper.like("hd.name", request.getName()); } Integer status; if (type == 1) { status = type; } else { status = request.getStatus(); } if (null != status) { wrapper.eq("hd.status", status); } wrapper.orderByDesc("hd.create_time"); if (type == 1) { List list = this.baseMapper.findList(wrapper); if (CollectionUtil.isEmpty(list)) { return JsonResult.success(new PageUtils(new Page<>(1, 0)), UtilMessage.GET_MESSAGE_SUCCESS); } String mobile = OnlineUserUtils.get().getMobile(); log.info("获取文件列表信息----mobile={}", mobile); List docIds = list.stream().map(DocListResponse::getId).collect(Collectors.toList()); List docRoles = docRoleDao.selectList(Wrappers.lambdaQuery(DocRole.class).in(DocRole::getDocId, docIds)); Map> docRoleMap = docRoles.stream().collect(Collectors.groupingBy(DocRole::getDocId)); List responses = new ArrayList<>(); list.forEach(e -> { List roleList = docRoleMap.get(Long.valueOf(e.getId())); if (CollectionUtil.isEmpty(roleList)) { responses.add(e); } else { List mobiles = roleList.stream().map(DocRole::getMobile).collect(Collectors.toList()); if (mobiles.contains(OnlineUserUtils.get().getMobile())) { responses.add(e); } } }); return JsonResult.success(new PageUtils(new Page(1, responses.size() , responses.size()).setRecords(responses)), UtilMessage.GET_MESSAGE_SUCCESS); } else { return JsonResult.success(new PageUtils(this.baseMapper.findPage(page, wrapper)), UtilMessage.GET_MESSAGE_SUCCESS); } } } private List checkAuth(GetLoginOutData userInfo, List docList) { List newList = new ArrayList<>(); //校验分类下手机号 if (CollectionUtil.isNotEmpty(docList)) { for (Doc doc : docList) { //群组 List docConversationRels = docConversationRelService.list(new LambdaQueryWrapper() .eq(DocConversationRel::getDocId, doc.getId())); //权限手机号 List docRoleMobiles = docRoleDao.getMobileList(doc.getId()); //手机号不为空,群组为空 if (CollectionUtil.isNotEmpty(docRoleMobiles) && CollectionUtil.isEmpty(docConversationRels)) { if (!docRoleMobiles.contains(userInfo.getMobile())) { if (docList.size() == 1) { return newList; } continue; } } //手机号不为空,群组不为空 if (CollectionUtil.isNotEmpty(docRoleMobiles) && CollectionUtil.isNotEmpty(docConversationRels)) { //群组内所有人手机号 List docConversationMobiles = docConversationRelService.getListByDocId(doc.getId()); docRoleMobiles.addAll(docConversationMobiles); if (!docRoleMobiles.contains(userInfo.getMobile())) { if (docList.size() == 1) { return newList; } continue; } } //手机号为空,群组不为空 if (CollectionUtil.isEmpty(docRoleMobiles) && CollectionUtil.isNotEmpty(docConversationRels)) { //群组内所有人手机号 List docConversationMobiles = docConversationRelService.getListByDocId(doc.getId()); if (!docConversationMobiles.contains(userInfo.getMobile())) { if (docList.size() == 1) { return newList; } continue; } } CheckAuthResponse rs = new CheckAuthResponse(); rs.setId(doc.getId().toString()); rs.setTypeId(doc.getTypeId().toString()); rs.setFileSize(doc.getFileSize()); rs.setName(doc.getName()); //手机号为空,群组为空 根据分类权限 newList.add(rs); } } return newList; } private boolean checkAuthDetail(GetLoginOutData userInfo, Doc doc) { //校验分类下手机号 //群组 List docConversationRels = docConversationRelService.list(new LambdaQueryWrapper() .eq(DocConversationRel::getDocId, doc.getId())); //权限手机号 List docRoleMobiles = docRoleDao.getMobileList(doc.getId()); //手机号不为空,群组为空 if (CollectionUtil.isNotEmpty(docRoleMobiles) && CollectionUtil.isEmpty(docConversationRels)) { if (!docRoleMobiles.contains(userInfo.getMobile())) { return true; } } //手机号不为空,群组不为空 if (CollectionUtil.isNotEmpty(docRoleMobiles) && CollectionUtil.isNotEmpty(docConversationRels)) { //群组内所有人手机号 List docConversationMobiles = docConversationRelService.getListByDocId(doc.getId()); docRoleMobiles.addAll(docConversationMobiles); if (!docRoleMobiles.contains(userInfo.getMobile())) { return true; } } //手机号为空,群组不为空 if (CollectionUtil.isEmpty(docRoleMobiles) && CollectionUtil.isNotEmpty(docConversationRels)) { //群组内所有人手机号 List docConversationMobiles = docConversationRelService.getListByDocId(doc.getId()); if (!docConversationMobiles.contains(userInfo.getMobile())) { return true; } } CheckAuthResponse rs = new CheckAuthResponse(); rs.setId(doc.getId().toString()); rs.setTypeId(doc.getTypeId().toString()); rs.setFileSize(doc.getFileSize()); rs.setName(doc.getName()); //手机号为空,群组为空 根据分类权限 return false; } /** * 验证实体的必填项是否已经填写 * * @param inData 实体参数 * @return message 验证结果集 */ private String checkBean(DocRequest inData, Boolean mark) { String message = ""; if (mark && null == inData.getId()) { message = "编辑文件时,Id不能为空!"; } if (null == inData.getTypeId()) { message = "文件分类不能为空!"; } if (StringUtils.isEmpty(inData.getName())) { message = "文件名称不能为空!"; } if (StringUtils.isEmpty(inData.getFileSize())) { message = "上传的文件不能为空!"; } return message; } }