Commit 47fcac0f authored by shiyu's avatar shiyu

导入类目数据

parent 063b1b86
package com.wwdz.ch.admin.job;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.wwdz.ch.db.dao.CategoryDao;
import com.wwdz.ch.db.domain.Category;
import com.wwdz.ch.db.mapper.CategoryMapper;
......@@ -7,7 +11,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Component
......@@ -34,4 +41,34 @@ public class SynCategoryInfo {
public void importCategory() {
String FILE_NAME = "wanwu_category.xlsx";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
StringArrayExcelReadListener listener = new StringArrayExcelReadListener();
ExcelReader reader = EasyExcelFactory.read(inputStream, listener).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
reader.read(readSheet);
reader.finish();
List<String> head = listener.getHead();
logger.info("excel title = {}", head);
List<List<String>> dataList = listener.getData();
logger.info(">>>>>>>>>> excel 读取数据 : {} 条", dataList.size());
dataList.parallelStream().forEach(array -> {
Category category = new Category();
int categoryId = Integer.parseInt(array.get(0));
category.setId(categoryId);
category.setName(array.get(1));
category.setDesc(array.get(2));
category.setPid(Integer.parseInt(array.get(3)));
category.setLevel(Integer.parseInt(array.get(5)));
category.setCreated(new Date());
category.setUpdated(new Date());
category.setIsDeleted(false);
category.setIsLeaf(array.get(11).equals("0")?false:true);
category.setIcon("https://cdn.wanwudezhi.com"+ array.get(13));
category.setSort(Integer.parseInt(array.get(6)));
categoryDao.add(category);
});
}
}
package com.wwdz.ch.admin.impl;
import com.wwdz.ch.admin.job.SynCategoryInfo;
import com.wwdz.ch.admin.service.CategoryService;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -16,8 +17,20 @@ public class CategoryServiceImplTest {
@Autowired
CategoryService categoryService;
@Autowired
SynCategoryInfo synCategoryInfo;
@Test
public void getSubCategory() {
categoryService.getSubCategory(1001025);
}
@Test
public void importCategory(){
synCategoryInfo.importCategory();
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment