lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
src/main/java/com/whyc/service/Ld9testdataService.java
@@ -1,20 +1,36 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.BattTestData;
import com.whyc.dto.Response;
import com.whyc.mapper.Ld9testdataMapper;
import com.whyc.pojo.Ld9testdata;
import com.whyc.pojo.Ld9testdataInf;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.BASE64Decoder;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
@Service
public class Ld9testdataService {
    @Resource
    private Ld9testdataMapper mapper;
    @Autowired
    private Environment environment;
    //查询LD9单体测试数据
    public Response serchByCondition(int recordNum,  int testRecordCount, int battGroupId,int testMonNum) {
@@ -34,4 +50,92 @@
        PageInfo pageInfo=new PageInfo(list);
        return new Response().set(1,pageInfo);
    }
    //根据电池组id和test_record_count查询出整组的放电记录
    public List<Ld9testdata> getLD9AllGroupByTestRecordCount(Ld9testdataInf ld9inf) {
        List<Ld9testdata> list=mapper.getLD9AllGroupByTestRecordCount(ld9inf);
        return list;
    }
    //根据电池组id和test_record_count算出总时间
    public Map getLD9AllGroupTimeLong(Ld9testdataInf ld9inf) {
        Map numMap=mapper.getLD9AllGroupTimeLong(ld9inf);
        return numMap;
    }
    //ld9导入整组放电图片
    public Response filePicUpload(Ld9testdataInf linf) {
        String fileDirName = "";
        int configType = Integer.parseInt(environment.getProperty("configFile.type"));
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //测试版
        if(configType==1){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        String picroot=fileDirName+File.separator+"ld9"+File.separator+linf.getBattGroupId()+File.separator+linf.getTestRecordCount()+File.separator+linf.getMonNum()+File.separator;
        boolean b=createFile(picroot);//创建文件路径
        boolean bl=false;
        String msg="";
        if(b){
           msg="图片存在,图片不用上传!";
        }else {
            bl=GenerateImage(linf.getMapPic(),picroot);
            if(bl){
                msg="图片不存在,图片重新上传成功!";
            }else{
                msg="导入失败!";
            }
        }
        return new Response().set(1,bl,msg);
    }
    //判断路径是否存在,不存在则创建。存在就判断是否有四张图
    public boolean createFile(String pathName) {
        File f = new File(pathName);
        boolean bl=false;
        if(!f.exists()){
            f.mkdirs();
        }/*else{
         File[] files=f.listFiles();
         if(files.length>=4){
            bl=true;
         }
      }*/
        return bl;
    }
    // 对字节数组字符串进行Base64解码并生成图片
    public  boolean GenerateImage(Map<String,String> map_pic, String root)  {
        // 图像数据为空
        if (map_pic == null|| map_pic.size() == 0){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        map_pic.forEach((key,value)->{
            OutputStream out = null;
            try {
                String imgFilePath=root+File.separator+key;
                out = new FileOutputStream(imgFilePath);
                // Base64解码
                byte[] b = decoder.decodeBuffer(URLDecoder.decode(value, "utf-8"));
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                out.write(b);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        return true;
    }
}