|
|
@ -27,6 +27,7 @@ import com.elink.esua.epdc.commons.tools.exception.ErrorCode; |
|
|
|
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|
|
|
import com.elink.esua.epdc.commons.tools.redis.RedisKeys; |
|
|
|
import com.elink.esua.epdc.commons.tools.redis.RedisUtils; |
|
|
|
import com.elink.esua.epdc.commons.tools.security.user.SecurityUser; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.ConvertUtils; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.Result; |
|
|
|
import com.elink.esua.epdc.commons.tools.utils.TreeUtils; |
|
|
@ -41,6 +42,13 @@ import com.elink.esua.epdc.modules.category.redis.CategoryRedis; |
|
|
|
import com.elink.esua.epdc.modules.category.service.CategoryService; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell; |
|
|
|
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.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -48,6 +56,8 @@ import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
@ -64,6 +74,14 @@ public class CategoryServiceImpl extends BaseServiceImpl<CategoryDao, CategoryEn |
|
|
|
@Autowired |
|
|
|
private RedisUtils redisUtils; |
|
|
|
|
|
|
|
private int totalRows = 0; // 总行数
|
|
|
|
|
|
|
|
private int totalCells = 0;// 总条数
|
|
|
|
|
|
|
|
public static int Guid=100;//id自增值
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
@Override |
|
|
@ -230,8 +248,279 @@ public class CategoryServiceImpl extends BaseServiceImpl<CategoryDao, CategoryEn |
|
|
|
|
|
|
|
@Override |
|
|
|
public Result insertPartyList(MultipartFile file) { |
|
|
|
//获取excle版本
|
|
|
|
String isExcel2003 = getExcelInfo(file); |
|
|
|
Result result = new Result(); |
|
|
|
//需存储的实体
|
|
|
|
List<CategoryEntity> list = new ArrayList<CategoryEntity>(); |
|
|
|
try{ |
|
|
|
Workbook wb = null; |
|
|
|
InputStream is = file.getInputStream(); |
|
|
|
if ("true".equals(isExcel2003)) { |
|
|
|
// 当excel是2003时,创建excel2003
|
|
|
|
wb = new HSSFWorkbook(is); |
|
|
|
}else if("false".equals(isExcel2003)){ |
|
|
|
// 当excel是2007时,创建excel2007
|
|
|
|
wb = new XSSFWorkbook(is); |
|
|
|
} else { |
|
|
|
result.setMsg("defeat"); |
|
|
|
result.setCode(1); |
|
|
|
result.setData("excle格式错误!"); |
|
|
|
return result; |
|
|
|
} |
|
|
|
// 得到第一个shell
|
|
|
|
Sheet sheet = wb.getSheetAt(0); |
|
|
|
// 得到Excel的行数
|
|
|
|
this.totalRows = sheet.getPhysicalNumberOfRows(); |
|
|
|
// 得到Excel的列数(前提是有行数)
|
|
|
|
if (totalRows > 1 && sheet.getRow(0) != null) { |
|
|
|
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); |
|
|
|
} |
|
|
|
//查询根分类开始序号
|
|
|
|
CategoryEntity categoryEntity = new CategoryEntity(); |
|
|
|
categoryEntity.setPid(0L); |
|
|
|
QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<CategoryEntity>(categoryEntity); |
|
|
|
List<CategoryEntity> categoryEntityList = baseDao.selectList(wrapper); |
|
|
|
int numm = categoryEntityList.size(); |
|
|
|
if(numm >= 0){ |
|
|
|
numm -= 1; |
|
|
|
} |
|
|
|
//一级分类
|
|
|
|
String firstClassName = ""; |
|
|
|
Long firstClassId = 0L; |
|
|
|
Integer firstClassSort = Integer.parseInt(numm+""); |
|
|
|
//二级分类
|
|
|
|
String secondClassName = ""; |
|
|
|
Long secondClassId = 0L; |
|
|
|
Integer secondClassSort = -1; |
|
|
|
//三级分类
|
|
|
|
String thirdClassName = ""; |
|
|
|
Long thirdClassId = 0L; |
|
|
|
Integer thirdClassSort = -1; |
|
|
|
//四级分类
|
|
|
|
String fourthClassName = ""; |
|
|
|
Long fourthClassId = 0L; |
|
|
|
Integer fourthClassSort = -1; |
|
|
|
// 循环Excel行数(不要表头,从2开始) ;0是excle表头、1是列名
|
|
|
|
for (int r = 2; r < totalRows; r++) { |
|
|
|
Row row = sheet.getRow(r); |
|
|
|
if (row == null) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 循环Excel的列 只需要读取2-5列
|
|
|
|
String firstClassNameCurrent = "";//本行一级分类
|
|
|
|
String secondClassNameCurrent = "";//本行二级分类
|
|
|
|
String thirdClassNameCurrent = "";//本行三级分类
|
|
|
|
String fourthClassNameCurrent = "";//本行四级分类
|
|
|
|
for (int c = 1; c < 5; c++) { |
|
|
|
Cell cell = row.getCell(c); |
|
|
|
if (null != cell) { |
|
|
|
if (c == 1) { |
|
|
|
//领域列
|
|
|
|
firstClassNameCurrent = ""; |
|
|
|
String value = getCellContent(cell); |
|
|
|
if(!"".equals(value)){ |
|
|
|
firstClassName = value; |
|
|
|
firstClassNameCurrent = value; |
|
|
|
firstClassId = getLongRandom(); |
|
|
|
firstClassSort++; |
|
|
|
secondClassSort = -1; |
|
|
|
thirdClassSort = -1; |
|
|
|
fourthClassSort = -1; |
|
|
|
} |
|
|
|
} else if (c == 2) { |
|
|
|
//大类列
|
|
|
|
secondClassNameCurrent = ""; |
|
|
|
String value = getCellContent(cell); |
|
|
|
if(!"".equals(value)){ |
|
|
|
secondClassName = value; |
|
|
|
secondClassNameCurrent = value; |
|
|
|
secondClassId = getLongRandom(); |
|
|
|
secondClassSort++; |
|
|
|
thirdClassSort = -1; |
|
|
|
fourthClassSort = -1; |
|
|
|
} |
|
|
|
} else if (c == 3) { |
|
|
|
//小类列
|
|
|
|
thirdClassNameCurrent = ""; |
|
|
|
String value = getCellContent(cell); |
|
|
|
if(!"".equals(value)){ |
|
|
|
thirdClassName = value; |
|
|
|
thirdClassNameCurrent = value; |
|
|
|
thirdClassId = getLongRandom(); |
|
|
|
thirdClassSort++; |
|
|
|
fourthClassSort = -1; |
|
|
|
} |
|
|
|
} else if (c == 4) { |
|
|
|
//细类名称列
|
|
|
|
fourthClassNameCurrent = ""; |
|
|
|
String value = getCellContent(cell); |
|
|
|
if(!"".equals(value)){ |
|
|
|
fourthClassName = value; |
|
|
|
fourthClassNameCurrent = value; |
|
|
|
fourthClassId = getLongRandom(); |
|
|
|
fourthClassSort++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(!"".equals(firstClassNameCurrent)){ |
|
|
|
//领域
|
|
|
|
CategoryEntity insertEntity = getCategoryEntity(firstClassId,0L,"0",firstClassName, |
|
|
|
firstClassId+"","0",firstClassSort,0,""); |
|
|
|
list.add(insertEntity); |
|
|
|
} |
|
|
|
if(!"".equals(secondClassNameCurrent)){ |
|
|
|
//大类
|
|
|
|
CategoryEntity insertEntity = getCategoryEntity(secondClassId,firstClassId,firstClassId+"",secondClassName, |
|
|
|
secondClassId+"","1",secondClassSort,0,firstClassName); |
|
|
|
list.add(insertEntity); |
|
|
|
} |
|
|
|
if(!"".equals(thirdClassNameCurrent)){ |
|
|
|
//小类
|
|
|
|
String pids = firstClassId + "," + secondClassId + ""; |
|
|
|
CategoryEntity insertEntity = getCategoryEntity(thirdClassId,secondClassId,pids,thirdClassName, |
|
|
|
thirdClassId+"","2",thirdClassSort,0,secondClassName); |
|
|
|
list.add(insertEntity); |
|
|
|
} |
|
|
|
if(!"".equals(fourthClassNameCurrent)){ |
|
|
|
//细分类
|
|
|
|
String pids = firstClassId + "," + secondClassId + "," + thirdClassId + ""; |
|
|
|
CategoryEntity insertEntity = getCategoryEntity(fourthClassId,thirdClassId,pids,fourthClassName, |
|
|
|
fourthClassId+"","3",fourthClassSort,0,thirdClassName); |
|
|
|
list.add(insertEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
if(list.size() > 0){ |
|
|
|
CategoryEntity[] categoryEntities = new CategoryEntity[list.size()]; |
|
|
|
for(int k = 0 ; k < list.size() ; k++){ |
|
|
|
categoryEntities[k] = list.get(k); |
|
|
|
} |
|
|
|
baseDao.insertList(categoryEntities); |
|
|
|
} |
|
|
|
result.setMsg("success"); |
|
|
|
result.setCode(0); |
|
|
|
result.setData("数据导入成功!"); |
|
|
|
return result; |
|
|
|
}catch(Exception e){ |
|
|
|
|
|
|
|
} |
|
|
|
result.setMsg("defeat"); |
|
|
|
result.setCode(1); |
|
|
|
result.setData("数据导入失败!"); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取单元格内容 |
|
|
|
* @param cell |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String getCellContent(Cell cell){ |
|
|
|
String value = ""; |
|
|
|
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { |
|
|
|
value = String.valueOf(cell.getNumericCellValue()); |
|
|
|
} else { |
|
|
|
value = cell.getStringCellValue(); |
|
|
|
} |
|
|
|
//内容超过25个字进行长度限制,否则数据库会超限
|
|
|
|
if(value.length() > 25){ |
|
|
|
value = value.substring(0,25) + "..."; |
|
|
|
} |
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取19位随机数 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public Long getLongRandom(){ |
|
|
|
Guid+=1; |
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
SimpleDateFormat dateFormat=new SimpleDateFormat("MMdd"); |
|
|
|
String time=dateFormat.format(now); |
|
|
|
String currentTimeMillis=now+""; |
|
|
|
int ran=0; |
|
|
|
if(Guid>999){ |
|
|
|
Guid=100; |
|
|
|
} |
|
|
|
ran=Guid; |
|
|
|
String str = time+currentTimeMillis.substring(1, currentTimeMillis.length())+ran; |
|
|
|
return Long.parseLong(str); |
|
|
|
} |
|
|
|
|
|
|
|
return new Result(); |
|
|
|
/** |
|
|
|
* 拼接entity |
|
|
|
* @param id ID |
|
|
|
* @param pid 上级分类ID |
|
|
|
* @param categoryName 分类名称 |
|
|
|
* @param categoryCode 分类编码 |
|
|
|
* @param categoryType 分类类别 |
|
|
|
* @param sort 排序 |
|
|
|
* @param delFlag 删除标识 0:未删除 1:删除 |
|
|
|
* @param parentName 上级部门名称 |
|
|
|
*/ |
|
|
|
public CategoryEntity getCategoryEntity(Long id,Long pid,String pids,String categoryName,String categoryCode, |
|
|
|
String categoryType,Integer sort,Integer delFlag,String parentName) { |
|
|
|
CategoryEntity entity = new CategoryEntity(); |
|
|
|
Long userId = SecurityUser.getUserId(); |
|
|
|
entity.setId(id); |
|
|
|
entity.setPid(pid); |
|
|
|
entity.setPids(pids); |
|
|
|
entity.setCategoryName(categoryName);//分类名称
|
|
|
|
entity.setCategoryCode(categoryCode);//分类编码(暂时使用ID)
|
|
|
|
entity.setCategoryType(categoryType);//分类类别(默认1)
|
|
|
|
entity.setSort(sort);//排序
|
|
|
|
entity.setDelFlag(delFlag);//删除标识 0:未删除 1:删除
|
|
|
|
entity.setParentName(parentName);//上级部门名称
|
|
|
|
entity.setCreator(userId); |
|
|
|
entity.setUpdater(userId); |
|
|
|
entity.setCreateDate(new Date()); |
|
|
|
entity.setUpdateDate(new Date()); |
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取excle版本 |
|
|
|
* @param mFile |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String getExcelInfo(MultipartFile mFile) { |
|
|
|
String fileName = mFile.getOriginalFilename();// 获取文件名
|
|
|
|
String isExcel2003 = "true";// 根据文件名判断文件是2003版本还是2007版本
|
|
|
|
try { |
|
|
|
if (!validateExcel(fileName)) {// 验证文件名是否合格
|
|
|
|
return ""; |
|
|
|
} |
|
|
|
if (isExcel2007(fileName)) { |
|
|
|
isExcel2003 = "false"; |
|
|
|
} |
|
|
|
return isExcel2003; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 验证EXCEL文件 |
|
|
|
* @param filePath |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public boolean validateExcel(String filePath) { |
|
|
|
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// @描述:是否是2003的excel,返回true是2003
|
|
|
|
public static boolean isExcel2003(String filePath) { |
|
|
|
return filePath.matches("^.+\\.(?i)(xls)$"); |
|
|
|
} |
|
|
|
// @描述:是否是2007的excel,返回true是2007
|
|
|
|
public static boolean isExcel2007(String filePath) { |
|
|
|
return filePath.matches("^.+\\.(?i)(xlsx)$"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|