16 changed files with 352 additions and 58 deletions
@ -0,0 +1,165 @@
|
||||
package com.air.facilities.listener; |
||||
|
||||
import cn.hutool.core.util.ObjectUtil; |
||||
|
||||
import com.air.baseRegion.dto.BaseRegionDTO; |
||||
import com.air.baseRegion.mapper.BaseRegionMapper; |
||||
import com.air.baseRegion.vo.BaseRegionVO; |
||||
import com.air.dictitem.entity.DictItem; |
||||
import com.air.dictitem.mapper.DictItemMapper; |
||||
import com.air.facilities.dto.FacilitiesFileDTO; |
||||
import com.air.facilities.entity.Facilities; |
||||
import com.air.facilities.mapper.FacilitiesMapper; |
||||
import com.air.facilities.service.FacilitiesService; |
||||
import com.air.facilities.service.impl.FacilitiesServiceImpl; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
||||
import com.cinderella.framework.common.core.exception.BusinessException; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Lazy; |
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.math.BigDecimal; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @program: back-end |
||||
* @description: 监听 |
||||
* @author: cyz |
||||
* @create: 2022-07-14 09:05 |
||||
**/ |
||||
public class FacilitiesListener extends AnalysisEventListener<FacilitiesFileDTO> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FacilitiesListener.class); |
||||
|
||||
@Autowired |
||||
@Lazy |
||||
private FacilitiesMapper facilitiesMapper; |
||||
|
||||
@Autowired |
||||
@Lazy |
||||
private BaseRegionMapper baseRegionMapper; |
||||
|
||||
@Autowired |
||||
@Lazy |
||||
private DictItemMapper dictItemMapper; |
||||
|
||||
@Autowired |
||||
private ThreadPoolTaskExecutor executor; |
||||
|
||||
/** |
||||
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来 |
||||
* |
||||
* @param |
||||
*/ |
||||
public FacilitiesListener(DictItemMapper dictItemMapper, BaseRegionMapper baseRegionMapper,FacilitiesMapper facilitiesMapper,ThreadPoolTaskExecutor executor) { |
||||
this.dictItemMapper = dictItemMapper; |
||||
this.baseRegionMapper = baseRegionMapper; |
||||
this.facilitiesMapper = facilitiesMapper; |
||||
this.executor = executor; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 10; |
||||
List<FacilitiesFileDTO> list = new ArrayList<FacilitiesFileDTO>(); |
||||
|
||||
|
||||
@Override |
||||
public void invoke(FacilitiesFileDTO bzhdSystemConstructionImport, AnalysisContext analysisContext) { |
||||
|
||||
LOGGER.info("解析到一条数据:{}", bzhdSystemConstructionImport); |
||||
list.add(bzhdSystemConstructionImport); |
||||
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
LOGGER.info("大于了"); |
||||
|
||||
// //开启多线程处理
|
||||
// executor.execute(()->{
|
||||
// String threadNamePrefix = executor.getThreadNamePrefix();
|
||||
// LOGGER.info(threadNamePrefix);
|
||||
// saveData();
|
||||
// // 存储完成清理 list
|
||||
// list.clear();
|
||||
// LOGGER.info("大于了");
|
||||
// });
|
||||
|
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 所有数据解析完成了 都会来调用 |
||||
* |
||||
* @param context |
||||
*/ |
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
|
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
@Transactional |
||||
public void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
//开启多线程处理
|
||||
executor.execute(()->{ |
||||
String threadNamePrefix = executor.getThreadNamePrefix(); |
||||
LOGGER.info(threadNamePrefix); |
||||
for (FacilitiesFileDTO demoData : list) { |
||||
LOGGER.info(list.size() + "======================"); |
||||
Facilities facilities = new Facilities(); |
||||
facilities.setName(demoData.getName()); |
||||
String[] split = demoData.getNautica().split(","); |
||||
String srt = "POINT(" + split[0] + " " + split[1] + ")"; |
||||
String s = split[0]; |
||||
BigDecimal bd = new BigDecimal(s); |
||||
facilities.setLon(bd);//经度
|
||||
String se = split[1]; |
||||
BigDecimal bed = new BigDecimal(se); |
||||
facilities.setLat(bed);//纬度
|
||||
facilities.setGeom(srt); |
||||
//查询周边类型
|
||||
DictItem rim = dictItemMapper.selectOne(Wrappers.<DictItem>query().lambda() |
||||
.like(StringUtils.isNotBlank(demoData.getClassify()), DictItem::getLabel, demoData.getClassifyTwo()) |
||||
.eq(DictItem::getType, "rim") |
||||
); |
||||
if (ObjectUtil.isNotNull(rim)) { |
||||
facilities.setType(rim.getValue()); |
||||
facilities.setTypecn(rim.getLabel()); |
||||
} else { |
||||
throw new BusinessException("没有该分类"); |
||||
} |
||||
//查询地区ID
|
||||
BaseRegionDTO baseRegionDTO = new BaseRegionDTO(); |
||||
baseRegionDTO.setProvince(demoData.getProvince()); |
||||
baseRegionDTO.setCity(demoData.getCity()); |
||||
baseRegionDTO.setArea(demoData.getArea()); |
||||
BaseRegionVO baseRegionVO = baseRegionMapper.selectBaseRegion(baseRegionDTO); |
||||
// facilities.setSite(baseRegionVO.getXianId());
|
||||
if (StringUtils.isNotBlank(demoData.getAddress())) { |
||||
facilities.setAddress(demoData.getAddress()); |
||||
} |
||||
facilitiesMapper.insertFacilities(facilities); |
||||
} |
||||
// 存储完成清理 list
|
||||
list.clear(); |
||||
}); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue