Browse Source

桩号导入方法

develop
lau572 8 months ago
parent
commit
f99021d9f9
  1. 7
      zc-business/src/main/java/com/zc/business/controller/DcStakeMarkController.java
  2. 2
      zc-business/src/main/java/com/zc/business/domain/DcStakeMark.java
  3. 9
      zc-business/src/main/java/com/zc/business/mapper/DcStakeMarkMapper.java
  4. 11
      zc-business/src/main/java/com/zc/business/service/IDcStakeMarkService.java
  5. 84
      zc-business/src/main/java/com/zc/business/service/impl/DcStakeMarkServiceImpl.java
  6. 41
      zc-business/src/main/resources/mapper/business/DcStakeMarkMapper.xml

7
zc-business/src/main/java/com/zc/business/controller/DcStakeMarkController.java

@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
/**
@ -112,4 +113,10 @@ public class DcStakeMarkController extends BaseController {
return toAjax(dcStakeMarkService.removeStakeMark(ids));
}
@ApiOperation("根据json文件路径导入桩号")
@PostMapping("importStakeMarkByJsonFilePath")
public AjaxResult importJsonStakeMark(String filePath) throws IOException {
return dcStakeMarkService.importJsonStakeMark(filePath);
}
}

2
zc-business/src/main/java/com/zc/business/domain/DcStakeMark.java

@ -29,5 +29,7 @@ public class DcStakeMark {
private Date updateTime;
@ApiModelProperty("所属辖区")
private Long sectionId;
@ApiModelProperty("桩号里程")
private Long mileage;
}

9
zc-business/src/main/java/com/zc/business/mapper/DcStakeMarkMapper.java

@ -1,14 +1,21 @@
package com.zc.business.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zc.business.domain.DcDevice;
import com.zc.business.domain.DcStakeMark;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 桩号Mapper接口
*
* @author zhaoxianglong
* @author
*/
@Mapper
public interface DcStakeMarkMapper extends BaseMapper<DcStakeMark> {
boolean batchInsert(List<DcStakeMark> dcStakeMarkList);
}

11
zc-business/src/main/java/com/zc/business/service/IDcStakeMarkService.java

@ -2,8 +2,10 @@ package com.zc.business.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.zc.business.domain.DcStakeMark;
import java.io.IOException;
import java.util.List;
/**
@ -60,4 +62,13 @@ public interface IDcStakeMarkService extends IService<DcStakeMark> {
* @return 设备信息
*/
DcStakeMark getStakeMark(String id);
/**
* 导入桩号
*
* @param filePath json文件路径
* @return
*/
AjaxResult importJsonStakeMark(String filePath) throws IOException;
}

84
zc-business/src/main/java/com/zc/business/service/impl/DcStakeMarkServiceImpl.java

@ -1,19 +1,24 @@
package com.zc.business.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StakeMarkUtils;
import com.zc.business.domain.DcStakeMark;
import com.zc.business.mapper.DcStakeMarkMapper;
import com.zc.business.service.IDcStakeMarkService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
/**
* 桩号Service业务层处理
@ -23,6 +28,8 @@ import java.util.Objects;
@Service
public class DcStakeMarkServiceImpl extends ServiceImpl<DcStakeMarkMapper, DcStakeMark> implements IDcStakeMarkService {
@Resource
private DcStakeMarkMapper dcStakeMarkMapper;
public LambdaQueryWrapper<DcStakeMark> stakeMarkQueryWrapper(DcStakeMark dcStakeMark) {
@ -125,5 +132,76 @@ public class DcStakeMarkServiceImpl extends ServiceImpl<DcStakeMarkMapper, DcSta
return getById(id);
}
/**
* @Description 导入桩号
*
* @author liuwenge
* @date 2024/3/22 11:16
* @param filePath json文件路径
* @return com.ruoyi.common.core.domain.AjaxResult
*/
@Override
public AjaxResult importJsonStakeMark(String filePath) throws IOException {
Date methodStart = new Date();
File file = new File(filePath);
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
String jsonStr = sb.toString();
Map<String, Map<String,Object>> map = JSON.parseObject(jsonStr, new TypeReference<HashMap<String, Map<String,Object>>>() {});
List<DcStakeMark> stakeMarkList = new ArrayList<>();
//桩号格式校验
List<String> errorKey = new ArrayList<>();
map.keySet().forEach(key ->{
if (!checkStakeMark(key)){
errorKey.add(key);
}
DcStakeMark dcStakeMark = new DcStakeMark();
dcStakeMark.setStakeMark(key);
dcStakeMark.setLongitude(map.get(key).containsKey("lng") ? map.get(key).get("lng").toString() : "");
dcStakeMark.setLatitude(map.get(key).containsKey("lat") ? map.get(key).get("lat").toString() : "");
dcStakeMark.setDirection("1");
dcStakeMark.setSectionId(1L);
dcStakeMark.setMileage(Long.valueOf(StakeMarkUtils.formatMetre(key)));
stakeMarkList.add(dcStakeMark);
});
if (errorKey.size() > 0){
return AjaxResult.error("桩号格式错误",errorKey);
}
Date databaseStart = new Date();
Long time = databaseStart.getTime() - methodStart.getTime();
System.out.println("处理数据用时---------------------------------->" + time);
dcStakeMarkMapper.batchInsert(stakeMarkList);
Long endTime = new Date().getTime() - databaseStart.getTime();
System.out.println("导入数据库用时--------------------------------->" + endTime);
return AjaxResult.success();
}
/**
* @Description 桩号格式校验
*
* 正确格式: K060+010
* @author liuwenge
* @date 2024/3/22 20:05
* @param stakeMark
* @return boolean
*/
private boolean checkStakeMark(String stakeMark){
Pattern pattern = Pattern.compile("^K\\d{3}\\+\\d{3}$");
return pattern.matcher(stakeMark).matches();
}
}

41
zc-business/src/main/resources/mapper/business/DcStakeMarkMapper.xml

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zc.business.mapper.DcStakeMarkMapper">
<insert id="batchInsert" parameterType="List">
<foreach collection ="list" item="dcStakeMark" index= "index" separator =";">
insert into dc_stake_mark
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="dcStakeMark.stakeMark != null">stake_mark,</if>
<if test="dcStakeMark.longitude != null">longitude,</if>
<if test="dcStakeMark.latitude != null">latitude,</if>
<if test="dcStakeMark.direction != null">direction,</if>
<if test="dcStakeMark.sectionId != null">section_id,</if>
<if test="dcStakeMark.mileage != null">mileage,</if>
create_time,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dcStakeMark.stakeMark != null">#{dcStakeMark.stakeMark},</if>
<if test="dcStakeMark.longitude != null">#{dcStakeMark.longitude},</if>
<if test="dcStakeMark.latitude != null">#{dcStakeMark.latitude},</if>
<if test="dcStakeMark.direction != null">#{dcStakeMark.direction},</if>
<if test="dcStakeMark.sectionId != null">#{dcStakeMark.sectionId},</if>
<if test="dcStakeMark.mileage != null">#{dcStakeMark.mileage},</if>
now(),
</trim>
ON DUPLICATE KEY UPDATE
<trim suffixOverrides=",">
<if test="dcStakeMark.stakeMark != null">stake_mark = #{dcStakeMark.stakeMark},</if>
<if test="dcStakeMark.longitude != null">longitude = #{dcStakeMark.longitude},</if>
<if test="dcStakeMark.latitude != null">latitude = #{dcStakeMark.latitude},</if>
<if test="dcStakeMark.direction != null">direction = #{dcStakeMark.direction},</if>
<if test="dcStakeMark.sectionId != null">section_id = #{dcStakeMark.sectionId},</if>
<if test="dcStakeMark.mileage != null">mileage = #{dcStakeMark.mileage},</if>
update_time = now(),
</trim>
</foreach>
</insert>
</mapper>
Loading…
Cancel
Save