whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/service/Ld9testdataService.java
@@ -1,20 +1,46 @@
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.Battinf;
import com.whyc.pojo.Ld9testdata;
import com.whyc.pojo.Ld9testdataInf;
import com.whyc.pojo.UserInf;
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 javax.servlet.http.HttpServletRequest;
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;
import java.util.stream.Collectors;
@Service
public class Ld9testdataService {
    @Resource
    private Ld9testdataMapper mapper;
    @Autowired
    private Environment environment;
    @Autowired
    private SubTablePageInfoService subService;
    @Autowired
    private BattGroupStationUserService battGroupStationUserService;
    //查询LD9单体测试数据
    public Response serchByCondition(int recordNum,  int testRecordCount, int battGroupId,int testMonNum) {
@@ -30,8 +56,120 @@
                roteN=number/endN+1;
            }
        }
        List<Ld9testdata> list=mapper.serchByCondition(roteN,testRecordCount,battGroupId,testMonNum);
        //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, HttpServletRequest request) {
        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();
        }
        //校验传入的linf.getBattGroupId()是否为当前用户管理的
        UserInf user = (UserInf) request.getSession().getAttribute("user");
        if(user == null){
            user = new UserInf();
            user.setUName("未登录的用户账号");
            user.setUId(0L);
            user.setURole(0);
        }
        Integer battGroupId = linf.getBattGroupId();
        List<Battinf> stationInfoList = battGroupStationUserService.getStationInfoList(user.getUId());
        List<Integer> ownedBattGroupIdList = stationInfoList.stream().map(Battinf::getBattGroupId).collect(Collectors.toList());
        if(!ownedBattGroupIdList.contains(battGroupId)){
            return new Response().set(1,false,"导入失败,当前用户无权限导入传参电池组的文件");
        }
        String picroot=fileDirName+File.separator+"ld9"+File.separator+ battGroupId +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;
                    }
                }
                if(out!=null){
                    out.write(b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(out!=null){
                    try {
                        out.flush();
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return true;
    }
}