直接有用的程序

72 阅读1分钟
public Boolean ImportSQLText(MultipartFile file, HttpServletResponse response){
    try {
        // 创建相应流及【格式】【名称】
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode("sql.txt","UTF-8"));
        // 将导入进来的sql记事本封装到List集合理
        InputStreamReader is=new InputStreamReader(file.getInputStream());
        BufferedReader br=new BufferedReader(is);
        String line=br.readLine();
        List<String> list=new ArrayList<>();
        while (line != null){
            list.add(line);
            line=br.readLine();
        }
        // 封装sql逻辑【替换】已换换行符拼接然后转成字节流
        List<String> endList = getEndList(list);
        // 换行符
        String str= System.getProperty("line.separator");
        byte[] bytes = endList.stream().collect(Collectors.joining(str)).getBytes();
        // 写入相应流
        outputStream.write(bytes);
        //释放资源
        br.close();
        is.close();
        outputStream.close();
        return Boolean.TRUE;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Boolean.FALSE;
}

public List<String> getEndList(List<String> list){
    List<CustomerMergeApplyDO> customerMergeApplyDOS = customerMergeApplyManager.lambdaQuery()
            .select(CustomerMergeApplyDO::getTargetCustomerCode,CustomerMergeApplyDO::getTargetCustomerUserId,
                    CustomerMergeApplyDO::getSourceCustomerCode,CustomerMergeApplyDO::getSourceCustomerUserId,
                    CustomerMergeApplyDO::getCode,CustomerMergeApplyDO::getTargetCustomerName, CustomerMergeApplyDO::getSourceCustomerName)
            .eq(CustomerMergeApplyDO::getBizDataStatus, DICT_NO)
            .list();
    if(CollectionUtil.isEmpty(customerMergeApplyDOS)){
       Collections.emptyList();
    }
    List<CustomerMergeApplysDTO> customerMergeApplysDTOS = convertTo(customerMergeApplyDOS, CustomerMergeApplysDTO.class);
    getTargetCustomerCodeInfo(customerMergeApplysDTOS);
    List<String> endList=new ArrayList<>();
    for(CustomerMergeApplysDTO customerMergeApplys: customerMergeApplysDTOS){
        // 以~连接集合成字符串,再全局替换
        String sqlStr=replaceAll(list.stream().collect(Collectors.joining("~")),customerMergeApplys);
        // 以~分割成数组再转集合
        List<String> stringList = Arrays.asList(sqlStr.split("~"));
        endList.addAll(stringList);
    }
    return endList;
}


public String replaceAll(String str,CustomerMergeApplysDTO customerMergeApplys){
    return
            str.replace("#{object.targetCustomerUserId}", StringUtils.toString(customerMergeApplys.getTargetCustomerUserId()))
            .replace("#{object.targetCustomerCode}",StringUtils.toString(customerMergeApplys.getTargetCustomerCode()))
            .replace("#{object.targetCustomerId}",StringUtils.toString(customerMergeApplys.getTargetCustomerId()))
            .replace("#{object.targetMobile}",StringUtils.toString(customerMergeApplys.getTargetMobile()))
            .replace("#{object.targetCustomerName}",StringUtils.toString(customerMergeApplys.getTargetCustomerName()))
            .replace("#{object.targetCustomerUserCode}",StringUtils.toString(customerMergeApplys.getTargetCustomerUserCode()))
            .replace("#{object.targetUserCode}",StringUtils.toString(customerMergeApplys.getTargetUserCode()))
            .replace("#{object.targetUserId}",StringUtils.toString(customerMergeApplys.getTargetUserId()))
            .replace("#{object.targetUsername}",StringUtils.toString(customerMergeApplys.getTargetUsername()))
            .replace("#{object.sourceCustomerCode}",StringUtils.toString(customerMergeApplys.getSourceCustomerCode()))
            .replace("#{object.sourceCustomerUserId}",StringUtils.toString(customerMergeApplys.getSourceCustomerUserId()))
            .replace("#{object.sourceCustomerName}",StringUtils.toString(customerMergeApplys.getSourceCustomerName()))
            .replace("#{object.sourceUserCode}",StringUtils.toString(customerMergeApplys.getSourceUserCode()))
            .replace("#{object.sourceUserId}",StringUtils.toString(customerMergeApplys.getSourceUserId()));
}