/**
|
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
* <p>
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
* <p>
|
* http://www.apache.org/licenses/LICENSE-2.0
|
* <p>
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
package org.springblade.modules.dvoule.service.impl;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.springblade.modules.dvoule.entity.Dvoule;
|
import org.springblade.modules.dvoule.mapper.DvouleMapper;
|
import org.springblade.modules.dvoule.service.DvouleService;
|
import org.springblade.modules.dvoule.vo.DvouleVO;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.InputStream;
|
import java.text.DecimalFormat;
|
import java.util.Map;
|
|
/**
|
* 服务实现类
|
*
|
* @author Blade
|
* @since 2019-11-07
|
*/
|
@Service
|
@DS("slaves")
|
public class DvouleServiceImpl extends ServiceImpl<DvouleMapper, Dvoule> implements DvouleService {
|
private final static String xls = "xls";
|
private final static String xlsx = "xlsx";
|
@Override
|
public Map selecthz(String stcd) {
|
return baseMapper.selecthz(stcd);
|
}
|
|
@Override
|
public void upLoadFile(MultipartFile file) {
|
try{
|
//获得文件名
|
String fileName = file.getOriginalFilename();
|
InputStream in = file.getInputStream();
|
Workbook workbooks = null;
|
if(fileName.endsWith(xls)) {
|
workbooks = new HSSFWorkbook(in);
|
}
|
else {
|
workbooks = new XSSFWorkbook(in);
|
}
|
|
|
int numberOfSheets = workbooks.getNumberOfSheets();
|
//第一个sheet
|
for (int j=0;j<numberOfSheets;j++) {
|
Sheet sheet1 = workbooks.getSheetAt(j);
|
|
//有效行数
|
int rowNubms = sheet1.getPhysicalNumberOfRows();
|
for (int i = 1; i < rowNubms; i++) {
|
Row rowi = sheet1.getRow(i);
|
if (rowi == null) {
|
continue;
|
}
|
//创建数据库表对应的实体类
|
Cell cell1 = rowi.getCell(0);
|
if(cell1.getCellType() == Cell.CELL_TYPE_BLANK) {
|
continue;
|
}
|
Cell cell2 = rowi.getCell(1);//站名
|
Cell cell3 = rowi.getCell(2);//站码
|
Cell cell4 = rowi.getCell(3);//县区
|
Cell cell5 = rowi.getCell(4);//东经
|
Cell cell6 = rowi.getCell(5);//北纬
|
Cell cell7 = rowi.getCell(6);//水位
|
Cell cell8 = rowi.getCell(7);//黄海高程
|
Cell cell9 = rowi.getCell(8);//5
|
Cell cell10 = rowi.getCell(9);//10
|
Cell cell11 = rowi.getCell(10);//20
|
Cell cell12 = rowi.getCell(11);//50
|
Cell cell13 = rowi.getCell(12);//备注
|
Cell cell14 = rowi.getCell(13);//站类
|
|
|
//此处判断为null,以防get值的时候 报空指针
|
String rivername = (cell1 != null) ? getCellValue(cell1) : null;//河流名称
|
String stnm = (cell2 != null) ? getCellValue(cell2) : null;//站名
|
//String stcd = (cell3 != null) ? getCellValue(cell3) : null;
|
DecimalFormat df = new DecimalFormat("0");
|
String stcd ="";//站码
|
if(cell3.getCellType() == Cell.CELL_TYPE_NUMERIC){
|
stcd=df.format(cell3.getNumericCellValue());
|
}
|
else {
|
stcd = cell3.toString();
|
}
|
int number = DvouleServiceImpl.super.baseMapper.selectInfo(stcd);
|
if (number==1){
|
DvouleServiceImpl.super.baseMapper.deleteInfo(stcd);
|
}
|
String xname = (cell4 != null) ? getCellValue(cell4) : null;//县区
|
String dj = (cell5 != null) ? getCellValue(cell5) : null;//东经
|
String bw = (cell6 != null) ? getCellValue(cell6) : null;//北纬
|
String z = (cell7 != null) ? getCellValue(cell7) : null;//水位
|
String hz = (cell8 != null) ? getCellValue(cell8) : null;//黄海高程
|
String fy = (cell9 != null) ? getCellValue(cell9) : null;//5
|
String oy = (cell10 != null) ? getCellValue(cell10) : null;//10
|
String ty = (cell11 != null) ? getCellValue(cell11) : null;//20
|
String toy = (cell12 != null) ? getCellValue(cell12) : null;//50
|
String bz = (cell13 != null) ? getCellValue(cell13) : null;//备注
|
String zl = (cell14 != null) ? getCellValue(cell14) : null;//站类
|
|
|
//填充实体类
|
Dvoule dvoule= new Dvoule();
|
dvoule.setStcd(stcd);
|
dvoule.setHz(Double.parseDouble(hz));
|
dvoule.setFy(fy);
|
dvoule.setOy(oy);
|
dvoule.setTy(ty);
|
dvoule.setToy(toy);
|
DvouleService.super.save(dvoule);
|
//使用JPA 保存到数据库中
|
in.close(); //关闭流
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public IPage<DvouleVO> selectCz(IPage page, DvouleVO dvouleVO) {
|
return baseMapper.selectCz(page, dvouleVO);
|
}
|
|
@Override
|
public int selectInfo(String pid) {
|
return baseMapper.selectInfo(pid);
|
}
|
|
@Override
|
public void deleteInfo(String pid) {
|
baseMapper.deleteInfo(pid);
|
}
|
|
private String getCellValue(Cell cell){
|
String value ="";
|
switch (cell.getCellType()){
|
case Cell.CELL_TYPE_STRING:
|
value = cell.getStringCellValue();
|
break;
|
case Cell.CELL_TYPE_NUMERIC:
|
value = String.valueOf(cell.getNumericCellValue());
|
break;
|
case Cell.CELL_TYPE_BLANK:
|
value = "";
|
break;
|
case Cell.CELL_TYPE_BOOLEAN:
|
value = String.valueOf(cell.getBooleanCellValue());
|
break;
|
case Cell.CELL_TYPE_ERROR:
|
value = String.valueOf(cell.getErrorCellValue());
|
break;
|
case Cell.CELL_TYPE_FORMULA:
|
value = String.valueOf(cell.getCellFormula());
|
break;
|
}
|
return value;
|
}
|
}
|