|
|
|
|
@ -2,9 +2,15 @@ package com.zhehekeji.web.service;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
|
|
import com.zhehekeji.core.util.Assert;
|
|
|
|
|
import com.zhehekeji.web.entity.Category;
|
|
|
|
|
import com.zhehekeji.web.mapper.CategoryMapper;
|
|
|
|
|
import com.zhehekeji.web.pojo.category.CategoryExcel;
|
|
|
|
|
import com.zhehekeji.web.pojo.category.CategoryModel;
|
|
|
|
|
import com.zhehekeji.web.pojo.category.PageSearch;
|
|
|
|
|
import com.zhehekeji.web.pojo.stock.StockExcel;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
|
|
@ -16,15 +22,21 @@ import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
@ -33,54 +45,88 @@ public class CategoryService {
|
|
|
|
|
@Resource
|
|
|
|
|
private CategoryMapper categoryMapper;
|
|
|
|
|
|
|
|
|
|
// public void importExcel(MultipartFile file) throws IOException {
|
|
|
|
|
// EasyExcel.read(file.getInputStream(), HashMap.class, new CategoryImport()).sheet().doRead();
|
|
|
|
|
// }
|
|
|
|
|
public PageInfo<Category> list(PageSearch pageSearch){
|
|
|
|
|
PageHelper.startPage(pageSearch.getPageNum(),pageSearch.getPageSize());
|
|
|
|
|
List<Category> all = categoryMapper.selectByMap(new HashMap<>(0));
|
|
|
|
|
return new PageInfo<>(all);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Category> list(String name){
|
|
|
|
|
List<Category> list = categoryMapper.selectList(new QueryWrapper<Category>().like("name",name));
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void edit(Category category){
|
|
|
|
|
Assert.notNull(category.getId(),"ID不能为空");
|
|
|
|
|
category.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
categoryMapper.updateById(category);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void del(Integer id){
|
|
|
|
|
categoryMapper.deleteById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Integer add(Category category){
|
|
|
|
|
category.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
try {
|
|
|
|
|
categoryMapper.insert(category);
|
|
|
|
|
}catch (DuplicateKeyException e){
|
|
|
|
|
Assert.isTrue(false,"品规已存在");
|
|
|
|
|
}
|
|
|
|
|
return category.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void importExcel(MultipartFile file) throws IOException {
|
|
|
|
|
public Integer importExcel(MultipartFile file) throws IOException {
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
|
|
|
Thread thread = new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
runExcel(wb);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
thread.start();
|
|
|
|
|
return rows-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void runExcel (XSSFWorkbook workbook) {
|
|
|
|
|
XSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
|
|
|
Row firstRow = sheet.getRow(0);
|
|
|
|
|
int columns = firstRow.getLastCellNum();
|
|
|
|
|
List<Category> categories = new ArrayList<>();
|
|
|
|
|
for (int i = 1; i < rows; i++) {
|
|
|
|
|
XSSFRow row = sheet.getRow(i);
|
|
|
|
|
String parentName = "";
|
|
|
|
|
StringBuffer category = new StringBuffer();
|
|
|
|
|
for(int j = 0;j<columns;j++){
|
|
|
|
|
|
|
|
|
|
XSSFCell cell = row.getCell(j);
|
|
|
|
|
Category category = new Category();
|
|
|
|
|
category.setLevel(j+1);
|
|
|
|
|
String name = cell.getStringCellValue();
|
|
|
|
|
parentName = name;
|
|
|
|
|
category.setName(name);
|
|
|
|
|
categories.add(category);
|
|
|
|
|
if(cell != null){
|
|
|
|
|
String name = cell.getStringCellValue();
|
|
|
|
|
if(!StringUtils.isEmpty(name)) {
|
|
|
|
|
category.append(name).append("-");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(category.length() > 0){
|
|
|
|
|
category.deleteCharAt(category.length()-1);
|
|
|
|
|
}
|
|
|
|
|
insert(category.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void insert(Category category,String parentName){
|
|
|
|
|
// 父类id
|
|
|
|
|
Integer parentLevel = 0;
|
|
|
|
|
Integer parentId = 0;
|
|
|
|
|
if(category.getLevel() == 1){
|
|
|
|
|
//本身就是最顶级
|
|
|
|
|
parentLevel = 1;
|
|
|
|
|
parentId = 0;
|
|
|
|
|
}else if (category.getLevel() > 1) {
|
|
|
|
|
parentLevel = category.getLevel() - 1;
|
|
|
|
|
Category parent = categoryMapper.getByNameAndLevelAndParent(parentName,parentLevel,0);
|
|
|
|
|
if(parent != null){
|
|
|
|
|
parentId = parent.getId();
|
|
|
|
|
}else {
|
|
|
|
|
log.error("category 不存在:name:{},level:{}",parentName,parentLevel);
|
|
|
|
|
public Integer insert(String name){
|
|
|
|
|
Category category = categoryMapper.getByName(name);
|
|
|
|
|
if(category == null){
|
|
|
|
|
category = new Category();
|
|
|
|
|
category.setName(name);
|
|
|
|
|
category.setUpdateTime(LocalDateTime.now());
|
|
|
|
|
try {
|
|
|
|
|
categoryMapper.insert(category);
|
|
|
|
|
}catch (DuplicateKeyException e){
|
|
|
|
|
log.warn("品规已存在:{}",name);
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Category newCate = categoryMapper.getByNameAndLevelAndParent(category.getName(),category.getLevel(),parentId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return category.getId();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|