附件代码整理

首先设计附件表结构,用于存储附件

附加结构 

 

controller层代码

package com.sinosoft.springbootplus.files.controller;


import com.sinosoft.springbootplus.common.api.ApiResult;
import com.sinosoft.springbootplus.common.controller.BaseController;
import com.sinosoft.springbootplus.files.application.service.impl.JgjgFileServiceImpl;
import com.sinosoft.springbootplus.files.domain.entity.JgjgFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 * 监管机构 附件API
 * </pre>
 *
 * @author wp
 * @since 2022-05-23
 */
@Slf4j
@RestController
@RequestMapping("/jgjgFile")
@Api(tags="监管机构系统-附件API",description = "提供监管机构系统-附件相关的 Rest API")
public class JgjgFileController extends BaseController {
    private JgjgFileServiceImpl jgjgFileServiceImpl;

    public JgjgFileController(JgjgFileServiceImpl jgjgFileServiceImpl) {
        this.jgjgFileServiceImpl = jgjgFileServiceImpl;
    }

    /**
     * 上传附件
     * @author wp
     * @date 2022/5/23
     * @param file MultipartFile
     * @return ApiResult<JgjgFile>
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传附件", notes = "上传附件", response = ApiResult.class)
    public ApiResult<JgjgFile> uploadFile(@RequestParam("file") MultipartFile file) {
        return jgjgFileServiceImpl.uploadFile(file);
    }

    /**
     * 下载附件
     * @author wp
     * @date 2022/5/23
     * @param id 附件ID
     * @param response 响应
     */
    @GetMapping("/download/{id}")
    @ApiOperation(value = "下载附件", notes = "下载附件")
    public void downloadFile(@PathVariable("id") Long id, HttpServletResponse response) {
        jgjgFileServiceImpl.downloadFile(id, response);
    }

    /**
     * 删除附件
     * @author wp
     * @date 2022/5/23
     * @param id 附件ID
     */
    @GetMapping("deleted/{id}")
    @ApiOperation(value = "删除附件", notes = "删除附件")
    public void deletedFile(@PathVariable("id") Long id) {
        jgjgFileServiceImpl.deletedFile(id);
    }
}

 service层代码

package com.sinosoft.springbootplus.files.application.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sinosoft.springbootplus.common.api.ApiResult;
import com.sinosoft.springbootplus.files.domain.entity.JgjgFile;
import com.sinosoft.springbootplus.files.domain.mapper.JgjgFileMapper;
import com.sinosoft.springbootplus.mybatis.service.impl.BaseServiceImpl;
import com.sinosoft.springbootplus.oss.OssClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * <pre>
 * 监管机构系统-附件表 服务实现类
 * </pre>
 *
 * @author wp
 * @since 2022-05-10
 */
@Slf4j
@Service
public class JgjgFileServiceImpl extends BaseServiceImpl<JgjgFileMapper, JgjgFile> {
    // 文件操作
    private OssClient ossClient;

    public JgjgFileServiceImpl(OssClient ossClient) {
        this.ossClient = ossClient;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveJgjgFile(JgjgFile jgjgFile) {
        return super.save(jgjgFile);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean deleteJgjgFile(Long id) {
        return super.removeById(id);
    }

    public JgjgFile getJgjgFileById(Long id) {
        return super.getById(id);
    }

    /**
     * 上传附件
     * @author wp
     * @date 2022/5/23
     * @param file MultipartFile
     * @return ApiResult<JgjgFile>
     */
    public ApiResult<JgjgFile> uploadFile(MultipartFile file) {
        if (log.isDebugEnabled()) {
            log.debug("content-type={}", file.getContentType());
            log.debug("name={}", file.getName());
            log.debug("original-filename={}", file.getOriginalFilename());
        }
        String fileName = ossClient.uploadFile(file);
        if (StringUtils.isBlank(fileName)) {
            log.error("文件上传失败!文件名为空!");
            return ApiResult.fail("文件上传失败!文件名为空!");
        }
        try {
            // 保存附件信息
            JgjgFile saveFile = new JgjgFile().setFileName(fileName)
                    .setOriginalName(file.getOriginalFilename());
            boolean isSuccess = saveJgjgFile(saveFile);
            if (!isSuccess) {
                ossClient.delFile(fileName);
                log.error("文件上传失败!保存文件信息失败!文件信息:[{}]", saveFile);
                return ApiResult.fail("文件上传失败!保存文件信息失败!");
            }
            return ApiResult.ok(saveFile);
        } catch (Exception e) {
            try {
                ossClient.delFile(fileName);
            } catch (Exception ex) {
                log.error("文件删除失败,失败原因:[{}]", ex.getMessage());
                return ApiResult.fail("文件删除失败,失败原因:" + e.getMessage());
            }
            log.error("文件上传失败,失败原因:[{}]", e.getMessage());
            return ApiResult.fail("文件上传失败,失败原因:" + e.getMessage());
        }
    }

    /**
     * 下载附件
     * @author wp
     * @date 2022/5/23
     * @param id 附件ID
     * @param response 响应
     */
    public void downloadFile(Long id, HttpServletResponse response) {
        JgjgFile jgjgFile = getJgjgFileById(id);
        if (ObjectUtil.isNull(jgjgFile)) {
            log.error("ID:[{}]未查询到附件,请检查数据是否正确", id);
        }
        try {
            ossClient.downloadFile(jgjgFile.getFileName(), jgjgFile.getOriginalName(), response);
        } catch (Exception e) {
            log.error("文件下载失败,失败原因:[{}]", e.getMessage());
        }
    }

    /**
     * 删除附件
     * @author wp
     * @date 2022/5/23
     * @param id 附件ID
     */
    public void deletedFile(Long id) {
        JgjgFile jgjgFile = getJgjgFileById(id);
        if (ObjectUtil.isNull(jgjgFile)) {
            log.error("ID:[{}]未查询到附件,请检查数据是否正确", id);
        }
        try {
            boolean isSuccess = deleteJgjgFile(id);
            if (!isSuccess) {
                log.error("文件删除失败");
            }
            ossClient.delFile(jgjgFile.getFileName());
        } catch (Exception e) {
            log.error("文件删除失败,失败原因:[{}]", e.getMessage());
        }
    }
}

mapper层代码

package com.sinosoft.springbootplus.files.domain.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sinosoft.springbootplus.files.domain.entity.JgjgFile;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
/**
 * <pre>
 * 监管机构系统-附件表 Mapper 接口
 * </pre>
 *
 * @author wp
 * @since 2022-05-10
 */
@Repository
public interface JgjgFileMapper extends BaseMapper<JgjgFile> {

    /**
     * 获取分页对象
     *
     * @param page 分页信息
     * @param wrapper 监管机构系统-附件表查询参数
     * @return
     */
    @Select("select id, owner_id, file_id, file_name, owner_type, revision, created_by, created_time, tenant_id FROM jgjg_file  ${ew.customSqlSegment}")
    IPage<JgjgFile> getJgjgFilePageList(Page page, @Param(Constants.WRAPPER) Wrapper<JgjgFile> wrapper);

}

repository代码 

package com.sinosoft.springbootplus.files.domain.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinosoft.springbootplus.files.domain.entity.JgjgFile;
import com.sinosoft.springbootplus.files.domain.mapper.JgjgFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <pre>
 * 监管机构系统-附件 仓储服务
 * </pre>
 *
 * @author wp
 * @since 2022-05-23
 */
@Slf4j
@Repository
public class JgjgFileRepository extends ServiceImpl<JgjgFileMapper, JgjgFile> {
    private JgjgFileMapper jgjgFileMapper;

    public JgjgFileRepository(JgjgFileMapper jgjgFileMapper) {
        this.jgjgFileMapper = jgjgFileMapper;
    }

    public List<JgjgFile> getJgjgFileList(QueryWrapper<JgjgFile> queryWrapper) {
        return jgjgFileMapper.selectList(queryWrapper);
    }
}

domain层代码

package com.sinosoft.springbootplus.files.domain.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sinosoft.springbootplus.files.domain.entity.JgjgFile;
import com.sinosoft.springbootplus.files.domain.repository.JgjgFileRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <pre>
 * 监管机构系统-附件 领域服务
 * </pre>
 *
 * @author wp
 * @since 2022-05-23
 */
@Slf4j
@Service
public class JgjgFileDomain {
    private JgjgFileRepository jgjgFileRepository;

    public JgjgFileDomain(JgjgFileRepository jgjgFileRepository) {
        this.jgjgFileRepository = jgjgFileRepository;
    }

    /**
     * 批量修改附件信息
     * @author wp
     * @date 2022/5/23
     * @param jgjgFileList 附件信息集合
     * @return boolean
     */
    public boolean updateBatch(List<JgjgFile> jgjgFileList) {
        return jgjgFileRepository.updateBatchById(jgjgFileList);
    }

    /**
     * 获取附件信息集合
     * @author wp
     * @date 2022/5/23
     * @param jgjgFile 附件查询参数
     * @return List<JgjgFile>
     */
    public List<JgjgFile> getJgjgFileList(JgjgFile jgjgFile) {
        if (CollUtil.isEmpty(jgjgFile.getOwnerIdList())) {
            return null;
        }
        QueryWrapper<JgjgFile> queryWrapper = Wrappers.<JgjgFile>query()
                .eq(StringUtils.isNotBlank(jgjgFile.getOwnerType()), "owner_type", jgjgFile.getOwnerType())
                .in(CollUtil.isNotEmpty(jgjgFile.getOwnerIdList()), "owner_id", jgjgFile.getOwnerIdList());
        return jgjgFileRepository.getJgjgFileList(queryWrapper);
    }

    /**
     * 补全附件信息
     * @author wp
     * @date 2022/5/23
     * @param jgjgFileList 附件信息集合
     * @param ownerId 所属信息ID
     * @param ownerType 所属表类型
     * @return List<JgjgFile>
     */
    public List<JgjgFile> completeJgjgFiles(List<JgjgFile> jgjgFileList, Long ownerId, String ownerType) {
        if (CollUtil.isEmpty(jgjgFileList)) {
            return null;
        }
        for (JgjgFile jgjgFile : jgjgFileList) {
            jgjgFile.setOwnerId(ownerId).setOwnerType(ownerType);
        }
        return jgjgFileList;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 自动发送邮件 以下是一个Python代码,可以用于自动发送邮件,帮助用户快速发送邮件,提高办公效率: ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from email.header import Header def send_email(): """自动发送邮件""" smtp_server = 'smtp.163.com' smtp_port = 25 sender = 'your_email@163.com' receiver = 'recipient_email@163.com' password = 'your_email_password' subject = '邮件主题' content = '邮件正文' msg = MIMEMultipart() msg['From'] = sender msg['To'] = receiver msg['Subject'] = Header(subject, 'utf-8') text = MIMEText(content, 'plain', 'utf-8') msg.attach(text) with open('image.png', 'rb') as f: image = MIMEImage(f.read()) image.add_header('Content-ID', '<image>') msg.attach(image) server = smtplib.SMTP(smtp_server, smtp_port) server.login(sender, password) server.sendmail(sender, receiver, msg.as_string()) server.quit() print("邮件发送成功!") if __name__ == '__main__': send_email() ``` 这个代码可以自动发送邮件,用户只需要输入发件人邮箱、收件人邮箱、发件人邮箱密码、邮件主题、邮件正文和附件路径,即可快速发送邮件。这个代码可以帮助用户快速发送邮件,提高办公效率。 2. 自动备份文件 以下是一个Python代码,可以用于自动备份指定文件夹中的所有文件,帮助用户快速备份文件,保障数据安全: ```python import shutil def file_backup(): """自动备份文件""" source_folder = input("请输入需要备份的文件夹路径:") target_folder = input("请输入备份文件夹路径:") shutil.copytree(source_folder, target_folder) print("文件备份完成!") if __name__ == '__main__': file_backup() ``` 这个代码可以自动备份指定文件夹中的所有文件,用户只需要输入原文件夹路径和备份文件夹路径,即可快速备份文件。这个代码可以帮助用户快速备份文件,保障数据安全。 3. 自动整理文件 以下是一个Python代码,可以用于自动整理指定文件夹中的所有文件,帮助用户快速整理文件,提高办公效率: ```python import os import shutil def file_organize(): """自动整理文件""" source_folder = input("请输入需要整理的文件夹路径:") target_folder = input("请输入整理后的文件夹路径:") if not os.path.exists(target_folder): os.mkdir(target_folder) for filename in os.listdir(source_folder): if filename.endswith('.docx'): shutil.copy(os.path.join(source_folder, filename), os.path.join(target_folder, 'Word文档')) elif filename.endswith('.xlsx'): shutil.copy(os.path.join(source_folder, filename), os.path.join(target_folder, 'Excel文档')) elif filename.endswith('.pdf'): shutil.copy(os.path.join(source_folder, filename), os.path.join(target_folder, 'PDF文档')) elif filename.endswith('.jpg') or filename.endswith('.png'): shutil.copy(os.path.join(source_folder, filename), os.path.join(target_folder, '图片')) else: shutil.copy(os.path.join(source_folder, filename), os.path.join(target_folder, '其他文件')) print("文件整理完成!") if __name__ == '__main__': file_organize() ``` 这个代码可以自动整理指定文件夹中的所有文件,用户只需要输入原文件夹路径和整理后的文件夹路径,即可快速整理文件。这个代码可以帮助用户快速整理文件,提高办公效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值