Explorar o código

【器件上传】-- 补充重复上传接口

wangyc %!s(int64=8) %!d(string=hai) anos
pai
achega
42a329636b

+ 6 - 78
src/main/java/com/uas/platform/b2c/prod/product/component/controller/ComponentSubmitController.java

@@ -9,14 +9,13 @@ import com.uas.platform.b2c.core.utils.FastjsonUtils;
 import com.uas.platform.b2c.external.erp.product.service.OperationInfoService;
 import com.uas.platform.b2c.prod.product.component.modal.*;
 import com.uas.platform.b2c.prod.product.component.service.ComponentSubmitService;
+import com.uas.platform.b2c.trade.support.ResultMap;
 import com.uas.platform.core.exception.IllegalOperatorException;
 import com.uas.platform.core.logging.BufferedLoggerManager;
 import com.uas.platform.core.model.PageInfo;
 import com.uas.platform.core.model.PageParams;
 import com.uas.platform.core.model.ServiceCode;
 import com.uas.platform.core.model.Status;
-import com.xiaoleilu.hutool.poi.excel.ExcelReader;
-import com.xiaoleilu.hutool.poi.excel.ExcelUtil;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -30,7 +29,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -436,62 +434,9 @@ public class ComponentSubmitController {
 	 */
 	@Transactional
 	@RequestMapping(value = "upload/componentCrawls", method = RequestMethod.POST)
-	public List<ComponentCrawl> uploadComponentCrawlV2(FileUpload uploadItem) {
-        String fileName = uploadItem.getFile().getOriginalFilename();
-		List<ComponentCrawl> componentCrawls = new ArrayList<ComponentCrawl>();
-
-		try {
-			InputStream is = uploadItem.getFile().getInputStream();
-			ExcelReader reader = ExcelUtil.getReader(is);
-
-            // 获取表头数据
-            List<List<Object>> headers = reader.read(0, 0);
-            if (headers != null && headers.size() > 0) {
-
-                // 验证表头信息
-                List<Object> header = headers.get(0).subList(0, 8);
-                List<Object> exceptHeader = new ArrayList<Object>();
-                exceptHeader.add("型号");
-                exceptHeader.add("商城类目");
-                exceptHeader.add("目标类目");
-                exceptHeader.add("商城品牌");
-                exceptHeader.add("来源网址");
-                exceptHeader.add("规格书");
-                exceptHeader.add("图片");
-                exceptHeader.add("描述");
-
-                if (header.equals(exceptHeader)) {
-                    // 获取全表数据
-                    List<List<Object>> rows = reader.read();
-                    int size = rows.size();
-
-                    if (size > 0 && size < 10000) {
-                        int rowSize = headers.get(0).size();
-                        if (rowSize < 50) {
-                            // 列数超过30的文件不得超过2000行
-                            if (rowSize > 30 && size > 2000) {
-                                throw new IllegalOperatorException("属性超过22个时,文件只允许上传2000行");
-                            }
-                            componentCrawls = componentSubmitService.uploadComponentCrawl(rows, fileName);
-                        // 整体列数不得超过50
-                        } else {
-                            throw new IllegalOperatorException("属性值不得超过42个");
-                        }
-                    // 文件不得超过10000行
-                    } else {
-                        throw new IllegalOperatorException("上传文件必须大于0行小于10000行");
-                    }
-                } else {
-                    throw new IllegalOperatorException("表头格式错误,前8列应为 \'型号\',\'商城类目\',\'目标类目\',\'商城品牌\',\'来源网址\',\'规格书\',\'图片\',\'描述\'");
-                }
-            } else {
-                throw new IllegalOperatorException("表头信息错误,请核对后再上传");
-            }
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
+	public ResultMap uploadComponentCrawlV2(FileUpload uploadItem) {
 		logger.log("器件上传", "器件上传");
-		return componentCrawls;
+		return componentSubmitService.uploadComponentCrawl(uploadItem);
 	}
 
 	/**
@@ -503,26 +448,9 @@ public class ComponentSubmitController {
 	 */
 	@Transactional
 	@RequestMapping(value = "upload/componentCrawlsWithSame/{kindContrastId}", method = RequestMethod.POST)
-	public List<ComponentCrawl> uploadComponentCrawlWithSame(FileUpload uploadItem, @PathVariable("kindContrastId") Long kindContrastId) {
-		String fileName = uploadItem.getFile().getOriginalFilename();
-		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
-		InputStream is = null;
-		Workbook workbook = null;
-		List<ComponentCrawl> componentCrawls = new ArrayList<ComponentCrawl>();
-		try {
-			is = uploadItem.getFile().getInputStream();
-			if ("xls".equals(suffix)) {
-				workbook = new HSSFWorkbook(is);
-			} else if ("xlsx".equals(suffix)) {
-				workbook = new XSSFWorkbook(is);
-			} else {
-				throw new IllegalOperatorException("文件格不正确,请上传.xls或.xlsx的文件");
-			}
-			componentCrawls = componentSubmitService.uploadComponentCrawlWithSame(workbook, fileName, kindContrastId);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		return componentCrawls;
+	public ResultMap uploadComponentCrawlWithSame(FileUpload uploadItem, @PathVariable("kindContrastId") Long kindContrastId) {
+		logger.log("器件上传", "器件重新上传");
+		return componentSubmitService.uploadComponentCrawlWithSame(uploadItem, kindContrastId);
 	}
 	
 	

+ 6 - 0
src/main/java/com/uas/platform/b2c/prod/product/component/dao/ComponentCrawlDao.java

@@ -30,6 +30,12 @@ public interface ComponentCrawlDao extends JpaSpecificationExecutor<ComponentCra
 	 */
 	public List<ComponentCrawl> findByTask(Long task);
 
+	/**
+	 * 通过任务但id删除数据
+	 * @param task
+	 */
+	public void deleteByTask(Long task);
+
 	/**
 	 * 通过任务id获取去重后的爬取图片路径
 	 * @param task

+ 7 - 14
src/main/java/com/uas/platform/b2c/prod/product/component/service/ComponentSubmitService.java

@@ -1,6 +1,8 @@
 package com.uas.platform.b2c.prod.product.component.service;
 
+import com.uas.platform.b2c.common.base.model.FileUpload;
 import com.uas.platform.b2c.prod.product.component.modal.*;
+import com.uas.platform.b2c.trade.support.ResultMap;
 import com.uas.platform.core.model.PageInfo;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.data.domain.Page;
@@ -115,28 +117,19 @@ public interface ComponentSubmitService {
 	 */
 	public ComponentCrawlSubmit saveBatch(List<ComponentCrawlSubDetail> details);
 
-	/**
-	 * 上传器件爬取数据(没有参数对应关系)
-	 * 
-	 * @param workbook 表格
-	 * @return 爬取器件
-	 */
-	public List<ComponentCrawl> uploadComponentCrawl(Workbook workbook, String fileName);
-
 	/**
 	 * 上传器件爬取数据
-	 *
+	 * @param uploadItem 上传文件
 	 * @return 爬取器件
 	 */
-	public List<ComponentCrawl> uploadComponentCrawl(List<List<Object>> rows, String fileName);
+	public ResultMap uploadComponentCrawl(FileUpload uploadItem);
 
 	/**
 	 * 上传器件爬取数据(已有参数对应关系)
-	 * @param workbook 表格
-	 * @param fileName 文件名
-	 * @param kindContrastId 参数关系id
+	 * @param uploadItem 上传文件
+	 * @param kindContrastId 对应参数对应id
 	 * @return 爬取器件
 	 */
-	public List<ComponentCrawl> uploadComponentCrawlWithSame(Workbook workbook, String fileName, Long kindContrastId);
+	public ResultMap uploadComponentCrawlWithSame(FileUpload uploadItem, Long kindContrastId);
 
 }

+ 149 - 324
src/main/java/com/uas/platform/b2c/prod/product/component/service/impl/ComponentSubmitServiceImpl.java

@@ -1,6 +1,7 @@
 package com.uas.platform.b2c.prod.product.component.service.impl;
 
 import com.uas.platform.b2c.common.account.dao.UserBaseInfoDao;
+import com.uas.platform.b2c.common.base.model.FileUpload;
 import com.uas.platform.b2c.common.base.service.SendMessageService;
 import com.uas.platform.b2c.core.support.SystemSession;
 import com.uas.platform.b2c.core.utils.FastjsonUtils;
@@ -17,11 +18,10 @@ import com.uas.platform.b2c.prod.product.kind.dao.KindDao;
 import com.uas.platform.b2c.prod.product.kind.dao.KindPropertyDao;
 import com.uas.platform.b2c.prod.product.kind.model.Kind;
 import com.uas.platform.b2c.prod.product.kind.model.KindCrawl;
-import com.uas.platform.b2c.prod.product.kind.model.KindProperty;
 import com.uas.platform.b2c.prod.product.property.dao.PropertyDao;
-import com.uas.platform.b2c.prod.product.property.model.Property;
 import com.uas.platform.b2c.prod.product.property.model.PropertyValueCrawl;
 import com.uas.platform.b2c.prod.product.property.model.PropertyValueSubmit;
+import com.uas.platform.b2c.trade.support.ResultMap;
 import com.uas.platform.core.exception.IllegalOperatorException;
 import com.uas.platform.core.model.EncodingRulesConstant;
 import com.uas.platform.core.model.PageInfo;
@@ -31,6 +31,8 @@ import com.uas.platform.core.persistence.criteria.CriterionExpression.Operator;
 import com.uas.platform.core.persistence.criteria.LogicalExpression;
 import com.uas.platform.core.persistence.criteria.PredicateUtils;
 import com.uas.platform.core.persistence.criteria.SimpleExpression;
+import com.xiaoleilu.hutool.poi.excel.ExcelReader;
+import com.xiaoleilu.hutool.poi.excel.ExcelUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.map.HashedMap;
 import org.apache.poi.ss.usermodel.Cell;
@@ -49,6 +51,8 @@ import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -533,197 +537,18 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 	}
 
 	@Override
-	public List<ComponentCrawl> uploadComponentCrawl(Workbook workbook, String fileName) {
-		List<ComponentCrawl> components = new ArrayList<ComponentCrawl>();
-		Sheet sheet = workbook.getSheetAt(0);
-		int rowNum = sheet.getLastRowNum();
-		if (rowNum > 10000)
-			throw new IllegalOperatorException("上传文件不可超过10000行");
-		Row fourthRow = sheet.getRow(3);
-		if (fourthRow.getCell(1) != null && fourthRow.getCell(2) != null && fourthRow.getCell(3) != null && fourthRow.getCell(4) != null) {
-			// 商城类目
-			fourthRow.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
-			List<Kind> kinds = kindDao.findByNameCn(fourthRow.getCell(1).getStringCellValue().trim());
-			if (CollectionUtils.isEmpty(kinds)) {
-				throw new IllegalOperatorException("商城类目不存在");
-			}
-			Kind kind = kinds.get(0);
-
-			// 目标类目
-			fourthRow.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
-			String kindName = fourthRow.getCell(2).getStringCellValue().trim();
-
-			// 商城品牌
-			fourthRow.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
-			List<Brand> brands = brandDao.findByNameEn(fourthRow.getCell(3).getStringCellValue().trim());
-			if (CollectionUtils.isEmpty(brands)) {
-				throw new IllegalOperatorException("商城品牌不存在");
-			}
-			Brand brand = brands.get(0);
-
-			// url
-			fourthRow.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
-			String url = fourthRow.getCell(4).getStringCellValue().trim();
-
-			// 如果参数参数对应关系出现重复返回前台提示是否新增参数对应关系
-			List<KindCrawl> existKindCrawls = kindContrastDao.findByKindNameAndB2cKiIdAndResourceAndB2cBrIdAndUrl(kindName, kind.getId(), brand.getNameEn(), brand.getId(), url);
-			if (CollectionUtils.isNotEmpty(existKindCrawls)) {
-				ComponentCrawl isRepeated = new ComponentCrawl();
-				isRepeated.setB2cKiId(existKindCrawls.get(0).getId());
-				isRepeated.setVersion((short) -1);
-				components.add(isRepeated);
-				return components;
-			}
-			
-			Date date = new Date();
-			// 生成参数对应关系
-			KindCrawl kindCrawl = new KindCrawl();
-			kindCrawl.setResource(brand.getNameEn());
-			kindCrawl.setB2cBrId(brand.getId());
-			kindCrawl.setKindName(kindName);
-			kindCrawl.setB2cKiId(kind.getId());
-			kindCrawl.setUrl(url);
-			kindCrawl.setCreateDate(date);
-			kindCrawl.setCreaterUu(SystemSession.getUser().getUserUU());
-			kindCrawl.setInTask((short) 1);// 参数对应关系直接设置为已提交
-			kindCrawl.setUpdateDate(date);
-			kindCrawl.setUpdaterUu(SystemSession.getUser().getUserUU());
-			kindCrawl.setFile(fileName);// 设置上传文件名
-			
-			kindCrawl = kindContrastDao.save(kindCrawl);
-			kindCrawl = kindContrastDao.findOne(kindCrawl.getId());// 因为save返回的对象没有关联的实体信息只有id,所以需要重新查询一次,为后续生成json存入任务做准备
-			// 生成数据爬取任务
-			ComponentCrawlTask task = new ComponentCrawlTask();
-			task.setContrastId(kindCrawl.getId());
-			task.setContrast(FastjsonUtils.toJson(kindCrawl));
-			task.setCreateDate(date);
-			task.setCreaterUu(SystemSession.getUser().getUserUU());
-			task.setStatus(Status.TO_CROWL.value());
-			task.setFile(fileName);
-			task.setTaskId(EncodingRulesConstant.COMPONENT_CRAWL_TASK.replaceFirst("_TIMESTAP_NUMBER", createNumberService.getTimeNumber("product$component_crawl_task", 8)));
-			
-			task = componentCrawlTaskDao.save(task);
-			
-			kindCrawl.setTaskid(task.getTaskId());// 将数据爬取任务号回写到参数对应关系中
-			kindContrastDao.save(kindCrawl);
-			
-//			// 任务
-//			fourthRow.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
-//			ComponentCrawlTask task = componentCrawlTaskDao.findByTaskId(fourthRow.getCell(5).getStringCellValue().trim());
-
-			// 取目标参数
-			Row thirdRow = sheet.getRow(2);
-			if (thirdRow.getCell(7) != null) {
-				int i = 7;
-				List<String> proeprtyNames = new ArrayList<String>();
-				while (thirdRow.getCell(i) != null) {
-					thirdRow.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
-					proeprtyNames.add(thirdRow.getCell(i).getStringCellValue().trim());
-					i++;
-				}
-
-				// 取标准参数
-				Row firstRow = sheet.getRow(0);
-				List<Property> properties = new ArrayList<Property>();
-				for (int j = 7; j <= i; j++) {
-					if (firstRow.getCell(j) != null && !firstRow.getCell(j).getStringCellValue().trim().equals("") && !firstRow.getCell(j).getStringCellValue().trim().equals(" ")) {
-						firstRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-						List<Property> propertyList = propertyDao.findByLabelCn(firstRow.getCell(j).getStringCellValue().trim());
-						if (CollectionUtils.isEmpty(propertyList)) {
-							throw new IllegalOperatorException(firstRow.getCell(j).getStringCellValue().trim() + ",此标准参数不存在");
-						}
-						properties.add(propertyDao.findByLabelCn(firstRow.getCell(j).getStringCellValue().trim()).get(0));
-					} else {
-						properties.add(new Property());
-					}
-
-				}
-
-				// 取标准参数单位
-				Row secondRow = sheet.getRow(1);
-				List<String> units = new ArrayList<String>();
-				if (secondRow != null) {
-					for (int j = 7; j <= i; j++) {
-						if (secondRow.getCell(j) != null) {
-							secondRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-							units.add(secondRow.getCell(j).getStringCellValue().trim());
-						} else {
-							units.add("");
-						}
-					}
-				}
-
-				// 去具体器件参数
-				for (int r = 3; r <= rowNum; r++) {
-					Row row = sheet.getRow(r);
-					if (row != null && row.getCell(0) != null) {
-						ComponentCrawl component = new ComponentCrawl();
-						// 原厂型号
-						row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
-						component.setCode(row.getCell(0).getStringCellValue().trim());
-
-						// 规格书
-						if (row.getCell(5) != null) {
-							row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
-							component.setAttach(row.getCell(5).getStringCellValue().trim());
-						}
+	public ResultMap uploadComponentCrawl(FileUpload uploadItem) {
+		long start = System.currentTimeMillis();
 
-						// 图片
-						if (row.getCell(6) != null) {
-							row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
-							component.setImg(row.getCell(6).getStringCellValue().trim());
-						}
-
-						// 参数值
-						Set<PropertyValueCrawl> propertyValues = new HashSet<PropertyValueCrawl>();
-						for (int j = 7; j <= i; j++) {
-							if (row.getCell(j) != null) {
-								row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-								if (!row.getCell(j).getStringCellValue().trim().equals("") && !row.getCell(j).getStringCellValue().trim().equals(" ")) {
-									PropertyValueCrawl propertyValue = new PropertyValueCrawl();
-									row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-									propertyValue.setValue(row.getCell(j).getStringCellValue().trim());
-									if (secondRow != null) {
-										propertyValue.setUnit(units.get(j - 7));
-									}
-									propertyValue.setPropertyName(proeprtyNames.get(j - 7));
-									propertyValue.setPropertyId(properties.get(j - 7).getId());
-									if (properties.get(j - 7).getId() != null) {
-										KindProperty kindProperty = kindPropertyDao.findByKindIdAndPropertyId(kind.getId(), properties.get(j - 7).getId());
-										if (kindProperty == null) {
-											throw new IllegalOperatorException("标准参数不存在");
-										}
-										propertyValue.setDetno(kindProperty.getDetno());
-										propertyValue.setType(kindProperty.getType());
-										propertyValues.add(propertyValue);
-									}
-								}
-							}
-						}
-
-						component.setProperties(propertyValues);
-
-						// 设置基本参数
-						component.setB2cBrId(brand.getId());
-						component.setB2cKiId(kind.getId());
-						component.setKindName(kindName);
-						component.setTask(task.getId());
-
-						components.add(component);
-					}
-				}
-				components = componentCrawlDao.save(components);
-			} else {
-				throw new IllegalOperatorException("目标参数不完善或格式错误");
-			}
-		} else {
-			throw new IllegalOperatorException("基本数据不完善");
+		ResultMap resultMap = new ResultMap();
+		// 验证表格格式
+		Map<String, Object> map = validateExcel(uploadItem);
+		List<List<Object>> rows = (List<List<Object>>)(List)map.get("rows");
+		String fileName = map.get("fileName").toString();
+		if (rows == null || StringUtils.isEmpty(fileName)) {
+			throw new IllegalOperatorException("表格或文件名错误,请核对后重新上传");
 		}
-		return components;
-	}
 
-	@Override
-	public List<ComponentCrawl> uploadComponentCrawl(List<List<Object>> rows, String fileName) {
 		List<ComponentCrawl> components = new ArrayList<ComponentCrawl>();
 
 		// 提取公共数据
@@ -745,7 +570,10 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 			isRepeated.setB2cKiId(existKindCrawls.get(0).getId());
 			isRepeated.setVersion((short) -1);
 			components.add(isRepeated);
-			return components;
+
+			resultMap.setCode(405);
+			resultMap.setData(existKindCrawls.get(0).getId());
+			return resultMap;
 		}
 
 		// 生成参数对应关系
@@ -754,6 +582,30 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 		// 生成数据解析任务
 		ComponentCrawlTask task = initComponentCrawlTask(kindCrawl, fileName);
 
+		// 写入器件数据
+		components = writeComponentCrawls(rows, b2cBrand, b2cKind, kindName, task);
+		componentCrawlDao.save(components);
+
+		long end = System.currentTimeMillis();
+		System.err.println("第一次历时:" + (end - start));
+
+		resultMap.setCode(200);
+		resultMap.setData(components.size());
+		return resultMap;
+	}
+
+	/**
+	 * 写入器件数据
+	 * @param rows excel信息
+	 * @param b2cBrand 商城品牌
+	 * @param b2cKind 商城类目
+	 * @param kindName 目标类目
+	 * @param task 解析任务
+	 * @return
+	 */
+	private List<ComponentCrawl> writeComponentCrawls(List<List<Object>> rows, Brand b2cBrand, Kind b2cKind, String kindName, ComponentCrawlTask task) {
+		List<ComponentCrawl> components = new ArrayList<ComponentCrawl>();
+
 		// 取表头信息
 		List<String> propertyNames = (List<String>)(List)rows.get(0);
 		int maxSize = propertyNames.size();
@@ -809,7 +661,72 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 
 			components.add(component);
 		}
-		return componentCrawlDao.save(components);
+		return components;
+	}
+
+	/**
+	 * 验证表格格式
+	 * @param uploadItem
+	 * @return
+	 */
+	private Map<String, Object> validateExcel(FileUpload uploadItem) {
+		Map<String, Object> map = new HashedMap();
+		String fileName = uploadItem.getFile().getOriginalFilename();
+		List<ComponentCrawl> componentCrawls = new ArrayList<ComponentCrawl>();
+
+		try {
+			InputStream is = uploadItem.getFile().getInputStream();
+			ExcelReader reader = ExcelUtil.getReader(is);
+
+			// 获取表头数据
+			List<List<Object>> headers = reader.read(0, 0);
+			if (headers != null && headers.size() > 0) {
+
+				// 验证表头信息
+				List<Object> header = headers.get(0).subList(0, 8);
+				List<Object> exceptHeader = new ArrayList<Object>();
+				exceptHeader.add("型号");
+				exceptHeader.add("商城类目");
+				exceptHeader.add("目标类目");
+				exceptHeader.add("商城品牌");
+				exceptHeader.add("来源网址");
+				exceptHeader.add("规格书");
+				exceptHeader.add("图片");
+				exceptHeader.add("描述");
+
+				if (header.equals(exceptHeader)) {
+					// 获取全表数据
+					List<List<Object>> rows = reader.read();
+					int size = rows.size();
+
+					if (size > 0 && size < 10000) {
+						int rowSize = headers.get(0).size();
+						if (rowSize < 50) {
+							// 列数超过30的文件不得超过2000行
+							if (rowSize > 30 && size > 2000) {
+								throw new IllegalOperatorException("属性超过22个时,文件只允许上传2000行");
+							}
+							map.put("rows", rows);
+							map.put("fileName", fileName);
+							return map;
+							// 整体列数不得超过50
+						} else {
+							throw new IllegalOperatorException("属性值不得超过42个");
+						}
+						// 文件不得超过10000行
+					} else {
+						throw new IllegalOperatorException("上传文件必须大于0行小于10000行");
+					}
+				} else {
+					throw new IllegalOperatorException("表头格式错误,前8列应为 \'型号\',\'商城类目\',\'目标类目\',\'商城品牌\',\'来源网址\',\'规格书\',\'图片\',\'描述\'");
+				}
+			} else {
+				throw new IllegalOperatorException("表头信息错误,请核对后再上传");
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return map;
 	}
 
 	/**
@@ -915,21 +832,31 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 	}
 
 	@Override
-	public List<ComponentCrawl> uploadComponentCrawlWithSame(Workbook workbook, String fileName, Long kindContrastId) {
+	public ResultMap uploadComponentCrawlWithSame(FileUpload uploadItem, Long kindContrastId) {
+		long start = System.currentTimeMillis();
+
+		ResultMap resultMap = new ResultMap();
+		// 验证表格格式
+		Map<String, Object> map = validateExcel(uploadItem);
+		List<List<Object>> rows = (List<List<Object>>)(List)map.get("rows");
+		String fileName = map.get("fileName").toString();
+		if (rows == null || StringUtils.isEmpty(fileName)) {
+			throw new IllegalOperatorException("表格或文件名错误,请核对后重新上传");
+		}
+
 		List<ComponentCrawl> components = new ArrayList<ComponentCrawl>();
-		Sheet sheet = workbook.getSheetAt(0);
-		int rowNum = sheet.getLastRowNum();
-		if (rowNum > 10000)
-			throw new IllegalOperatorException("上传文件不可超过10000行");
-		Date date = new Date();
-		
+
+		// 获取对应的参数对应关系
 		KindCrawl kindCrawl = kindContrastDao.findOne(kindContrastId);
-		ComponentCrawlTask task = new ComponentCrawlTask();
-		
+		if (kindCrawl == null) {
+			throw new IllegalOperatorException("对应参数关系不存在,请重新核对信息");
+		}
 		// 如果参数对应关系为进行中状态,则不可上传数据
 		if (kindCrawl.getInTask().shortValue() == 2) {
 			throw new IllegalOperatorException("参数对应关系所关联任务正在进行中,如需重新上传数据,请联系相关人员");
 		}
+
+		ComponentCrawlTask task = new ComponentCrawlTask();
 		// 如果参数对应关系为已提交状态,将任务号延用;否则生成新任务
 		if (kindCrawl.getInTask().shortValue() == 1) {
 			task = componentCrawlTaskDao.findByTaskId(kindCrawl.getTaskid());
@@ -940,135 +867,33 @@ public class ComponentSubmitServiceImpl implements ComponentSubmitService {
 				task.setFile(fileName);
 				componentCrawlTaskDao.save(task);
 				
-				List<ComponentCrawl> componentCrawls = componentCrawlDao.findByTask(task.getId());// 删除之前任务上传的数据
-				if (CollectionUtils.isNotEmpty(componentCrawls)) {
-					componentCrawlDao.delete(componentCrawls);
-				}
+				componentCrawlDao.deleteByTask(task.getId());// 删除之前任务上传的数据
 			}
 		} else {
-			// 生成数据爬取任务
-			task.setContrastId(kindContrastId);
-			task.setContrast(FastjsonUtils.toJson(kindCrawl));
-			task.setCreateDate(date);
-			task.setCreaterUu(SystemSession.getUser().getUserUU());
-			task.setFile(fileName);
-			task.setStatus(Status.TO_CROWL.value());
-			task.setTaskId(EncodingRulesConstant.COMPONENT_CRAWL_TASK.replaceFirst("_TIMESTAP_NUMBER", createNumberService.getTimeNumber("product$component_crawl_task", 8)));
-			
-			task = componentCrawlTaskDao.save(task);
-			
-			// 将数据爬取任务号回写到参数对应关系中
-			kindCrawl.setTaskid(task.getTaskId());
-			kindCrawl.setInTask((short) 1);// 参数对应关系直接设置为已提交
-			kindCrawl.setFile(fileName);// 设置上传文件名
-			kindContrastDao.save(kindCrawl);
+			// 生成数据解析任务
+			task = initComponentCrawlTask(kindCrawl, fileName);
 		}
-		
-		// 取目标参数
-		Row thirdRow = sheet.getRow(2);
-		if (thirdRow.getCell(7) != null) {
-			int i = 7;
-			List<String> proeprtyNames = new ArrayList<String>();
-			while (thirdRow.getCell(i) != null) {
-				thirdRow.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
-				proeprtyNames.add(thirdRow.getCell(i).getStringCellValue().trim());
-				i++;
-			}
-
-			// 取标准参数
-			Row firstRow = sheet.getRow(0);
-			List<Property> properties = new ArrayList<Property>();
-			for (int j = 7; j <= i; j++) {
-				if (firstRow.getCell(j) != null && !firstRow.getCell(j).getStringCellValue().trim().equals("") && !firstRow.getCell(j).getStringCellValue().trim().equals(" ")) {
-					firstRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-					List<Property> propertyList = propertyDao.findByLabelCn(firstRow.getCell(j).getStringCellValue().trim());
-					if (CollectionUtils.isEmpty(propertyList)) {
-						throw new IllegalOperatorException(firstRow.getCell(j).getStringCellValue().trim() + ",此标准参数不存在");
-					}
-					properties.add(propertyDao.findByLabelCn(firstRow.getCell(j).getStringCellValue().trim()).get(0));
-				} else {
-					properties.add(new Property());
-				}
-
-			}
-
-			// 取标准参数单位
-			Row secondRow = sheet.getRow(1);
-			List<String> units = new ArrayList<String>();
-			if (secondRow != null) {
-				for (int j = 7; j <= i; j++) {
-					if (secondRow.getCell(j) != null) {
-						secondRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-						units.add(secondRow.getCell(j).getStringCellValue().trim());
-					} else {
-						units.add("");
-					}
-				}
-			}
-
-			// 取具体器件参数
-			for (int r = 3; r <= rowNum; r++) {
-				Row row = sheet.getRow(r);
-				if (row != null && row.getCell(0) != null) {
-					ComponentCrawl component = new ComponentCrawl();
-					// 原厂型号
-					row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
-					component.setCode(row.getCell(0).getStringCellValue().trim());
-
-					// 规格书
-					if (row.getCell(5) != null) {
-						row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
-						component.setAttach(row.getCell(5).getStringCellValue().trim());
-					}
 
-					// 图片
-					if (row.getCell(6) != null) {
-						row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
-						component.setImg(row.getCell(6).getStringCellValue().trim());
-					}
-
-					// 参数值
-					Set<PropertyValueCrawl> propertyValues = new HashSet<PropertyValueCrawl>();
-					for (int j = 7; j <= i; j++) {
-						if (row.getCell(j) != null) {
-							row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-							if (!row.getCell(j).getStringCellValue().trim().equals("") && !row.getCell(j).getStringCellValue().trim().equals(" ")) {
-								PropertyValueCrawl propertyValue = new PropertyValueCrawl();
-								row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
-								propertyValue.setValue(row.getCell(j).getStringCellValue().trim());
-								if (secondRow != null) {
-									propertyValue.setUnit(units.get(j - 7));
-								}
-								propertyValue.setPropertyName(proeprtyNames.get(j - 7));
-								propertyValue.setPropertyId(properties.get(j - 7).getId());
-								if (properties.get(j - 7).getId() != null) {
-									KindProperty kindProperty = kindPropertyDao.findByKindIdAndPropertyId(kindCrawl.getB2cKiId(), properties.get(j - 7).getId());
-									if (kindProperty == null) {
-										throw new IllegalOperatorException("标准参数不存在");
-									}
-									propertyValue.setDetno(kindProperty.getDetno());
-									propertyValue.setType(kindProperty.getType());
-									propertyValues.add(propertyValue);
-								}
-							}
-						}
-					}
+		// 对应商城品牌
+		Brand brand = brandDao.findOne(kindCrawl.getB2cBrId());
+		if (brand == null) {
+			throw new IllegalOperatorException("对应参数关系商城品牌不存在");
+		}
+		// 对应商城类目
+		Kind kind = kindDao.findOne(kindCrawl.getB2cKiId());
+		if (kind == null) {
+			throw new IllegalOperatorException("对应参数关系商城类目不存在");
+		}
 
-					component.setProperties(propertyValues);
+		// 写入器件数据
+		components = writeComponentCrawls(rows, brand, kind, kindCrawl.getKindName(), task);
+		componentCrawlDao.save(components);
 
-					// 设置基本参数
-					component.setB2cBrId(kindCrawl.getB2cBrId());
-					component.setB2cKiId(kindCrawl.getB2cKiId());
-					component.setKindName(kindCrawl.getKindName());
-					component.setTask(task.getId());
+		long end = System.currentTimeMillis();
+		System.err.println("重复历时:" + (end - start));
 
-					components.add(component);
-				}
-			}
-			components = componentCrawlDao.save(components);
-		} else {
-			throw new IllegalOperatorException("目标参数不完善或格式错误");
-		}
-		return components;
+		resultMap.setCode(200);
+		resultMap.setData(components.size());
+		return resultMap;
 	}
 }

+ 6 - 6
src/main/webapp/resources/js/admin/controllers/product/UploadComponentCrawlCtrl.js

@@ -95,8 +95,8 @@ define(['app/app'], function(app) {
 				file: file,
 				method: 'POST'
 			}).success(function(data) {
-				if(data[0].version == -1) {
-					var kindContrastId = data[0].b2cKiId;
+				if(data.code == 405) {
+					var kindContrastId = data.data;
 					$modal.open({
 						animation: true,
 						size: 'md',
@@ -115,8 +115,8 @@ define(['app/app'], function(app) {
 						return;
 					});
 				} else {
-					toaster.pop('success', '提示', '提交成功');
-					window.location.reload();
+					toaster.pop('success', '提示', '成功上传' + data.data + '条');
+					// window.location.reload();
 				}
 			}).error(function(response) {
 				toaster.pop('error', '提示', response);
@@ -130,8 +130,8 @@ define(['app/app'], function(app) {
 				file: myFile,
 				method: 'POST'
 			}).success(function(data) {
-				toaster.pop('success', '提示', '提交成功');
-				window.location.reload();
+				toaster.pop('success', '提示', '成功上传' + data.data + '条');
+				// window.location.reload();
 			}).error(function(response) {
 				toaster.pop('error', '提示', response);
 			});