whycxzp
2022-09-13 1e387103b534a69e0944bfef35cb8153ea0bfef2
src/main/java/com/whyc/service/SoftwareService.java
@@ -1,6 +1,8 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.UserOperation;
@@ -20,6 +22,7 @@
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
@@ -158,14 +161,18 @@
        if(!software.getFileName().equals(file1Name.substring(0,file1Name.lastIndexOf(".")))){
            return new Response().set(1,false,"附件的文件名与软件发布记录excel内的文件名称不一致");
        }
        Software softExists = getByFilename(software.getFileName());
        if(softExists != null){ //文件名:规格型号_软件版本,已存在,不能重复上传;这种情况属于追加机型,别处更新
            return new Response().set(1,false,"文件名称("+softExists.getFileName()+")已存在,不能重复上传");
        }
        Date date = new Date();
        String dateUnion = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(date);
        //文件重命名
        file1Name = file1Name.substring(0,file1Name.lastIndexOf(".")) + "_" + dateUnion +file1Name.substring(file1Name.lastIndexOf("."));
        file2Name = file2Name.substring(0,file2Name.lastIndexOf(".")) + "_" + dateUnion +file2Name.substring(file2Name.lastIndexOf("."));
        //文件重命名(暂不需要,严格遵守名称规则的情况下不会重名)
        //file1Name = file1Name.substring(0,file1Name.lastIndexOf(".")) + "_" + dateUnion +file1Name.substring(file1Name.lastIndexOf("."));
        //file2Name = file2Name.substring(0,file2Name.lastIndexOf(".")) + "_" + dateUnion +file2Name.substring(file2Name.lastIndexOf("."));
        //存储路径
        String rootFile = CommonUtil.getRootFile();
        String softwareDir = rootFile + "software" + File.separator + software.getOwner();
        String softwareDir = rootFile + "software" + File.separator + software.getOwner()+ File.separator + software.getFileName();
        String softwareHttpUrl = softwareDir.substring(softwareDir.lastIndexOf("doc_file"+ File.separator + "software"));
        File softwareDirFile = new File(softwareDir);
        if(!softwareDirFile.exists()){
@@ -184,7 +191,43 @@
        return new Response().set(1,true,"上传完成");
    }
    private Software getByFilename(String fileName) {
        QueryWrapper<Software> query = Wrappers.query();
        query.eq("file_name",fileName).last(" limit 1");
        return mapper.selectOne(query);
    }
    private void insertBatch(List<Software> softwareList){
        mapper.insertBatchSomeColumn(softwareList);
    }
    @Transactional
    public Response updateApplyModel(MultipartFile multipartFile, List<Software> softwareList) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        Software software = softwareList.get(0);
        QueryWrapper<Software> query = Wrappers.query();
        query.eq("file_name", software.getFileName()).last(" limit 1");
        Software softwareDB = mapper.selectOne(query);
        if(softwareDB == null){
            return new Response().set(1,false,"对应的软件并未上传过,无法更新适用机型");
        }else{
            //写入新增的软件发布记录excel
            String rootFile = CommonUtil.getRootFile();
            String softwareDir = rootFile + "software" + File.separator + software.getOwner()+ File.separator + software.getFileName();
            String softwareHttpUrl = softwareDir.substring(softwareDir.lastIndexOf("doc_file"+ File.separator + "software"));
            multipartFile.transferTo(new File(softwareDir + File.separator + originalFilename));
            //先删除对应的适用机型,再新增适用机型记录
            UpdateWrapper<Software> update = Wrappers.update();
            update.eq("file_name",softwareDB.getFileName());
            mapper.delete(update);
            softwareList.forEach(software2 -> {
                software2.setFileUrl(softwareDB.getFileUrl());
                software2.setExcelUrl(softwareHttpUrl + File.separator + originalFilename);
                software2.setCreateTime(new Date());
            });
            mapper.insertBatchSomeColumn(softwareList);
            return new Response().set(1,true,"更新完成");
        }
    }
}