Search in sources :

Example 1 with PmphGroupFile

use of com.bc.pmpheep.back.po.PmphGroupFile in project pmph by BCSquad.

the class MigrationStageEight method groupFile.

protected void groupFile() {
    // 要迁移的旧库表名
    String tableName = "bbs_downloadinfo";
    // 增加new_pk字段
    JdbcHelper.addColumn(tableName);
    // 取得该表中所有数据
    List<Map<String, Object>> maps = JdbcHelper.queryForList(tableName);
    // 迁移成功的条目数
    int count = 0;
    List<Map<String, Object>> excel = new LinkedList<>();
    Map<String, Object> result = new LinkedHashMap<>();
    int correctCount = 0;
    int[] state = { 0, 0, 0 };
    StringBuilder reason = new StringBuilder();
    StringBuilder dealWith = new StringBuilder();
    String sql = "SELECT new_pk FROM bbs_groupusers WHERE groupID = ? AND userID = ?";
    /* 开始遍历查询结果 */
    for (Map<String, Object> map : maps) {
        PmphGroupFile groupFile = new PmphGroupFile();
        String groupID = (String) map.get("groupID");
        String fileName = (String) map.get("fileName");
        Integer donloadCount = (Integer) map.get("donloadCount");
        String serverPath = (String) map.get("serverPath");
        String serverName = (String) map.get("serverName");
        String userID = (String) map.get("userID");
        Long groupId = JdbcHelper.getPrimaryKey("bbs_group", "groupID", groupID);
        if (null == groupId) {
            map.put(SQLParameters.EXCEL_EX_HEADER, "未找到文件对应小组");
            excel.add(map);
            logger.error("未找到文件对应小组,此结果将被记录在Excel中");
            if (state[0] == 0) {
                reason.append("没有找到文件所对应的小组。");
                dealWith.append("放弃迁移。");
                state[0] = 1;
            }
            continue;
        }
        groupFile.setGroupId(groupId);
        Long memberId = JdbcHelper.getJdbcTemplate().queryForObject(sql, Long.class, groupID, userID);
        if (null == memberId) {
            map.put(SQLParameters.EXCEL_EX_HEADER, "未找到文件对应上传者");
            excel.add(map);
            logger.error("未找到文件对应上传者,此结果将被记录在Excel中");
            if (state[1] == 0) {
                reason.append("没有找到文件所对应的上传者。");
                dealWith.append("放弃迁移。");
                state[1] = 1;
            }
            continue;
        }
        // file_id设为固定字符串,稍后更新
        groupFile.setFileId("LOST");
        groupFile.setMemberId(memberId);
        groupFile.setFileName(fileName);
        groupFile.setFileSize(0D);
        groupFile.setDownload(donloadCount);
        /* 如果创建时间为空,则上传时间等于最后一次下载时间 */
        Timestamp gmtCreate;
        if (null == map.get("createTime")) {
            gmtCreate = Timestamp.valueOf(map.get("lastTime").toString());
        } else {
            gmtCreate = Timestamp.valueOf(map.get("createTime").toString());
        }
        groupFile.setGmtCreate(gmtCreate);
        /* 保存GroupFile实例 */
        String id = (String) map.get("ID");
        groupFile = groupFileService.add(groupFile);
        long pk = groupFile.getId();
        // 更新旧表中new_pk字段
        JdbcHelper.updateNewPrimaryKey(tableName, pk, "ID", id);
        count++;
        /* 以下读取小组文件并保存在mongoDB中,读取失败时导出到Excel中 */
        String path = serverPath.concat(serverName);
        String mongoId;
        try {
            mongoId = fileService.migrateFile(path, FileType.GROUP_FILE, pk);
        } catch (IOException ex) {
            logger.error("文件读取异常,路径<{}>,异常信息:{}", path, ex.getMessage());
            map.put(SQLParameters.EXCEL_EX_HEADER, "文件读取异常");
            excel.add(map);
            if (state[2] == 0) {
                reason.append("小组文件丢失。");
                dealWith.append("放弃迁移。");
                state[2] = 1;
            }
            continue;
        }
        groupFile.setFileId(mongoId);
        String fullpath = SQLParameters.FILE_PATH + path;
        File file = new File(fullpath);
        double fileSize = file.length() / 1024;
        groupFile.setFileSize(fileSize);
        groupFileService.updatePmphGroupFile(groupFile);
        if (null == map.get("exception")) {
            correctCount++;
        }
    }
    if (excel.size() > 0) {
        try {
            excelHelper.exportFromMaps(excel, "后台小组文件表", "pmph_group_file");
        } catch (IOException ex) {
            logger.error("异常数据导出到Excel失败", ex);
        }
    }
    if (correctCount != maps.size()) {
        result.put(SQLParameters.EXCEL_HEADER_TABLENAME, "pmph_group_file");
        result.put(SQLParameters.EXCEL_HEADER_DESCRIPTION, "后台小组文件表");
        result.put(SQLParameters.EXCEL_HEADER_SUM_DATA, maps.size());
        result.put(SQLParameters.EXCEL_HEADER_MIGRATED_DATA, count);
        result.put(SQLParameters.EXCEL_HEADER_CORECT_DATA, correctCount);
        result.put(SQLParameters.EXCEL_HEADER_TRANSFERED_DATA, count - correctCount);
        result.put(SQLParameters.EXCEL_HEADER_NO_MIGRATED_DATA, maps.size() - count);
        result.put(SQLParameters.EXCEL_HEADER_EXCEPTION_REASON, reason.toString());
        result.put(SQLParameters.EXCEL_HEADER_DEAL_WITH, dealWith.toString());
        SQLParameters.STATISTICS_RESULT.add(result);
    }
    logger.info("'{}'表迁移完成,异常条目数量:{}", tableName, excel.size());
    logger.info("原数据库中共有{}条数据,迁移了{}条数据", maps.size(), count);
    // 记录信息
    Map<String, Object> msg = new HashMap<>();
    msg.put("result", "" + tableName + "  表迁移完成" + count + "/" + maps.size());
    SQLParameters.STATISTICS.add(msg);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PmphGroupFile(com.bc.pmpheep.back.po.PmphGroupFile) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PmphGroupFile(com.bc.pmpheep.back.po.PmphGroupFile) File(java.io.File)

Example 2 with PmphGroupFile

use of com.bc.pmpheep.back.po.PmphGroupFile in project pmph by BCSquad.

the class PmphGroupFileServiceImpl method addPmphGroupFile.

/**
 * @param pmphGroupFile
 *            实体对象
 * @return 带主键的 PmphGroupFile
 * @throws CheckedServiceException
 * @update Mryang 2017.10.13 15:30
 */
@Override
public String addPmphGroupFile(Long[] ids, MultipartFile file, String sessionId) throws CheckedServiceException, IOException {
    PmphUser pmphUser = SessionUtil.getPmphUserBySessionId(sessionId);
    if (ObjectUtil.isNull(pmphUser) || ObjectUtil.isNull(pmphUser.getId())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "用户为空");
    }
    if (ObjectUtil.isNull(ids) || ids.length == 0) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "参数不能为空");
    }
    if (ObjectUtil.isNull(file)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "文件不能为空");
    }
    Long userId = pmphUser.getId();
    List<PmphGroupFile> list = new ArrayList<>();
    for (Long id : ids) {
        if (ObjectUtil.isNull(id)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "小组id不能为空");
        }
        PmphGroupMemberVO pmphGroupMemberVO = pmphGroupMemberService.getPmphGroupMemberByMemberId(id, userId, false);
        PmphGroupFile pmphGroupFile = new PmphGroupFile(id, pmphGroupMemberVO.getId(), "0" + pmphGroupMemberVO.getId(), file.getOriginalFilename(), 0, null);
        Long fileLenth = file.getSize();
        pmphGroupFile.setFileSize(Double.valueOf(fileLenth / 1024));
        pmphGroupFileDao.addPmphGroupFile(pmphGroupFile);
        list.add(pmphGroupFile);
    }
    // list.get(0).getId()获取上传的第一条数据的id
    String fileId = fileService.save(file, FileType.GROUP_FILE, list.get(0).getId());
    for (PmphGroupFile pmphGroupFile : list) {
        pmphGroupFile.setFileId(fileId);
        pmphGroupFileDao.updatePmphGroupFile(pmphGroupFile);
        PmphGroupMemberVO pmphGroupMemberVO = pmphGroupMemberService.getPmphGroupMemberByMemberId(pmphGroupFile.getGroupId(), userId, false);
        pmphGroupMessageService.addGroupMessage(pmphGroupMemberVO.getDisplayName() + "上传了文件" + file.getOriginalFilename(), pmphGroupFile.getGroupId(), sessionId, Const.SENDER_TYPE_0);
    }
    return "success";
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) PmphGroupFile(com.bc.pmpheep.back.po.PmphGroupFile) PmphGroupMemberVO(com.bc.pmpheep.back.vo.PmphGroupMemberVO) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException)

Example 3 with PmphGroupFile

use of com.bc.pmpheep.back.po.PmphGroupFile in project pmph by BCSquad.

the class PmphGroupServiceImpl method deletePmphGroupById.

/**
 * @param PmphGroup
 * @return 影响行数
 * @throws CheckedServiceException
 */
@Override
public String deletePmphGroupById(PmphGroup pmphGroup, String sessionId) throws CheckedServiceException {
    String result = "FAIL";
    PmphUser pmphUser = SessionUtil.getPmphUserBySessionId(sessionId);
    if (null == pmphUser || null == pmphUser.getId()) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "用户为空");
    }
    if (pmphUser.getIsAdmin() || pmphGroupMemberService.isFounder(pmphGroup.getId(), sessionId)) {
        // 超级管理员与小组创建者才有权利删除小组
        if (null == pmphGroup.getId()) {
            throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "主键为空");
        }
        PmphGroup group = pmphGroupDao.getPmphGroupById(pmphGroup.getId());
        // 获取该小组的文件
        List<PmphGroupFile> pmphGroupFiles = pmphGroupFileService.listPmphGroupFileByGroupId(pmphGroup.getId());
        Long[] ids = new Long[pmphGroupFiles.size()];
        for (int i = 0; i < pmphGroupFiles.size(); i++) {
            PmphGroupFile pmphGroupFile = pmphGroupFiles.get(i);
            ids[i] = pmphGroupFile.getId();
        }
        // 删除小组
        int num = pmphGroupDao.deletePmphGroupById(pmphGroup.getId());
        if (num > 0) {
            if (ArrayUtil.isNotEmpty(ids)) {
                // 判断该小组是否有文件
                // 删除小组文件
                pmphGroupFileService.deletePmphGroupFileById(pmphGroup.getId(), ids, sessionId);
            }
            if (!RouteUtil.DEFAULT_GROUP_IMAGE.equals(group.getGroupImage())) {
                // 解散小组时删除小组头像
                fileService.remove(group.getGroupImage());
            }
            // 删除小组消息
            pmphGroupMessageService.deletePmphGroupMessageByGroupId(pmphGroup.getId());
            // 删除小组成员
            result = pmphGroupMemberService.deletePmphGroupMemberOnGroup(pmphGroup.getId());
        }
    } else {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.ILLEGAL_PARAM, "该用户没有此操作权限");
    }
    return result;
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) PmphGroupFile(com.bc.pmpheep.back.po.PmphGroupFile) PmphGroup(com.bc.pmpheep.back.po.PmphGroup) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException)

Example 4 with PmphGroupFile

use of com.bc.pmpheep.back.po.PmphGroupFile in project pmph by BCSquad.

the class PmphGroupFileServiceImpl method deletePmphGroupFileById.

/**
 * @param id
 *            主键id
 * @return 影响行数
 * @throws CheckedServiceException
 */
@Override
public String deletePmphGroupFileById(Long groupId, Long[] ids, String sessionId) throws CheckedServiceException {
    String result = "FAIL";
    PmphUser pmphUser = SessionUtil.getPmphUserBySessionId(sessionId);
    if (ObjectUtil.isNull(pmphUser) || ObjectUtil.isNull(pmphUser.getId())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "用户为空");
    }
    Long userId = pmphUser.getId();
    PmphGroupMemberVO currentUser = new PmphGroupMemberVO();
    if (ObjectUtil.isNull(ids) || ids.length == 0) {
        throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.NULL_PARAM, "主键为空");
    } else {
        if (!pmphUser.getIsAdmin()) {
            currentUser = pmphGroupMemberService.getPmphGroupMemberByMemberId(groupId, userId, false);
        }
        for (Long id : ids) {
            Long uploaderId = pmphGroupFileDao.getPmphGroupFileById(id).getMemberId();
            if (pmphUser.getIsAdmin() || uploaderId.equals(currentUser.getId()) || currentUser.getIsFounder() || currentUser.getIsAdmin()) {
                // 超级管理员、小组创建者、小组管理者、文件上传人才可以删除文件
                PmphGroupFile pmphGroupFile = pmphGroupFileDao.getPmphGroupFileById(id);
                Integer num = pmphGroupFileDao.getPmphGroupFileTotalByFileId(pmphGroupFile.getFileId());
                if (1 == num) {
                    fileService.remove(pmphGroupFile.getFileId());
                }
                pmphGroupFileDao.deletePmphGroupFileById(id);
            } else {
                throw new CheckedServiceException(CheckedExceptionBusiness.GROUP, CheckedExceptionResult.ILLEGAL_PARAM, "该用户没有此操作权限");
            }
        }
        result = "SUCCESS";
    }
    return result;
}
Also used : PmphUser(com.bc.pmpheep.back.po.PmphUser) PmphGroupMemberVO(com.bc.pmpheep.back.vo.PmphGroupMemberVO) PmphGroupFile(com.bc.pmpheep.back.po.PmphGroupFile) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException)

Aggregations

PmphGroupFile (com.bc.pmpheep.back.po.PmphGroupFile)4 PmphUser (com.bc.pmpheep.back.po.PmphUser)3 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)3 PmphGroupMemberVO (com.bc.pmpheep.back.vo.PmphGroupMemberVO)2 PmphGroup (com.bc.pmpheep.back.po.PmphGroup)1 File (java.io.File)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1