lxw
2023-10-30 be9a45d423c442c5daf7b1919aaf598f97a85083
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;
 
    @Autowired
    private SubTablePageInfoService subService;
 
    //查询LD9单体测试数据
    public Response serchByCondition(int recordNum,  int testRecordCount, int battGroupId,int testMonNum) {
        int number=recordNum;//总数
        int roteN=0;
        int endN= BattTestData.RC_NUM_PARAM;//总笔数
        if(number<=endN){
            roteN=1;
        }else{
            if(number%endN==0){
                roteN=number/endN;
            }else{
                roteN=number/endN+1;
            }
        }
        //List<Ld9testdata> list=mapper.serchByCondition(roteN,testRecordCount,battGroupId,testMonNum);
        List<Ld9testdata> list=subService.serchByCondition(roteN,testRecordCount,battGroupId,testMonNum);
        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);
        List<Ld9testdata> list=subService.getLD9AllGroupByTestRecordCount(ld9inf);
        return list;
    }
    //根据电池组id和test_record_count算出总时间
    public Map getLD9AllGroupTimeLong(Ld9testdataInf ld9inf) {
        //Map numMap=mapper.getLD9AllGroupTimeLong(ld9inf);
        Map numMap=subService.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)->{
            //过滤特殊字符,避免路径遍历攻击
            key = ActionUtil.filterFileName(key);
            OutputStream out = null;
            try {
                String imgFilePath=root+File.separator+key;
                out = new FileOutputStream(imgFilePath);
                // Base64解码
                //byte[] b = decoder.decodeBuffer(URLDecoder.decode(value, "utf-8"));
                byte[] b = decoder.decodeBuffer(value);
                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;
    }
}