Browse Source

Merge branch 'feature-wangyc-uploadCmp-v2.0' into dev-mysql

wangyc 8 years ago
parent
commit
8a9e18a389

+ 29 - 1
pom.xml

@@ -316,7 +316,18 @@
 		<dependency>
 			<groupId>org.apache.poi</groupId>
 			<artifactId>poi</artifactId>
-			<version>3.9</version>
+			<version>3.17</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.poi</groupId>
+			<artifactId>poi-ooxml</artifactId>
+			<version>3.17</version>
+			<exclusions>
+				<exclusion>
+					<groupId>org.apache.poi</groupId>
+					<artifactId>poi</artifactId>
+				</exclusion>
+			</exclusions>
 		</dependency>
 		<!-- BeeCloud支付集成SDK -->
 		<dependency>
@@ -436,6 +447,23 @@
 			<groupId>javax.servlet</groupId>
 			<artifactId>servlet-api</artifactId>
 		</dependency>
+		<!--Hutool POI工具类(对MS Office操作)-->
+		<!--<dependency>
+			<groupId>org.apache.poi</groupId>
+			<artifactId>poi-ooxml</artifactId>
+			<version>3.17</version>
+		</dependency>-->
+		<dependency>
+			<groupId>com.xiaoleilu</groupId>
+			<artifactId>hutool-poi</artifactId>
+			<version>3.2.2</version>
+			<exclusions>
+				<exclusion>
+					<groupId>org.apache.poi</groupId>
+					<artifactId>poi-ooxml</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
 	</dependencies>
 	<build>
 		<finalName>platform-b2c</finalName>

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

@@ -9,6 +9,7 @@ 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;
@@ -28,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;
 
 /**
@@ -428,63 +428,29 @@ public class ComponentSubmitController {
 
 	/**
 	 * 上传器件数据到爬虫库(没有参数对应关系)
-	 * 
+	 *
 	 * @param uploadItem 文件
 	 * @return 爬取器件信息
 	 */
 	@Transactional
 	@RequestMapping(value = "upload/componentCrawls", method = RequestMethod.POST)
-	public List<ComponentCrawl> uploadComponentCrawl(FileUpload uploadItem) {
-		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.uploadComponentCrawl(workbook, fileName);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		return componentCrawls;
+	public ResultMap uploadComponentCrawlV2(FileUpload uploadItem) {
+		logger.log("器件上传", "器件上传");
+		return componentSubmitService.uploadComponentCrawl(uploadItem);
 	}
-	
+
 	/**
 	 * 上传器件数据到爬虫库(已有参数对应关系)
-	 * 
+	 *
 	 * @param uploadItem 文件
 	 * @param kindContrastId 对应关系id
 	 * @return 爬取器件信息
 	 */
 	@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

+ 8 - 0
src/main/java/com/uas/platform/b2c/prod/product/component/modal/Component.java

@@ -607,6 +607,14 @@ public class Component implements Serializable {
 		this.company = company;
 	}
 
+	public Integer getLifecycle() {
+		return lifecycle;
+	}
+
+	public void setLifecycle(Integer lifecycle) {
+		this.lifecycle = lifecycle;
+	}
+
 	public String getCompanyUrl() {
 		return companyUrl;
 	}

+ 9 - 8
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;
@@ -116,19 +118,18 @@ public interface ComponentSubmitService {
 	public ComponentCrawlSubmit saveBatch(List<ComponentCrawlSubDetail> details);
 
 	/**
-	 * 上传器件爬取数据(没有参数对应关系)
-	 * 
-	 * @param workbook 表格
+	 * 上传器件爬取数据
+	 * @param uploadItem 上传文件
 	 * @return 爬取器件
 	 */
-	public List<ComponentCrawl> uploadComponentCrawl(Workbook workbook, 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);
+
 }

+ 329 - 301
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,7 +31,10 @@ 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;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -48,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;
@@ -532,211 +537,339 @@ 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);
+	public ResultMap uploadComponentCrawl(FileUpload uploadItem) {
+		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("表格或文件名错误,请核对后重新上传");
+		}
 
-			// 目标类目
-			fourthRow.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
-			String kindName = fourthRow.getCell(2).getStringCellValue().trim();
+		List<ComponentCrawl> components = new ArrayList<ComponentCrawl>();
 
-			// 商城品牌
-			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);
+		// 提取公共数据
+		List<Object> firstRow = rows.get(1);
+		Map<String, Object> commonData = validateCommonData(firstRow);
+		// 商城类目
+		Kind b2cKind = (Kind)commonData.get("b2cKind");
+		// 目标类目
+		String kindName = commonData.get("kindName").toString();
+		// 商城品牌
+		Brand b2cBrand = (Brand)commonData.get("b2cBrand");
+		// url
+		String url = commonData.get("url").toString();
+
+		// 如果参数参数对应关系出现重复返回前台提示是否新增参数对应关系
+		List<KindCrawl> existKindCrawls = kindContrastDao.findByKindNameAndB2cKiIdAndResourceAndB2cBrIdAndUrl(kindName, b2cKind.getId(), b2cBrand.getNameEn(), b2cBrand.getId(), url);
+		if (CollectionUtils.isNotEmpty(existKindCrawls)) {
+			ComponentCrawl isRepeated = new ComponentCrawl();
+			isRepeated.setB2cKiId(existKindCrawls.get(0).getId());
+			isRepeated.setVersion((short) -1);
+			components.add(isRepeated);
+
+			resultMap.setCode(405);
+			resultMap.setData(existKindCrawls.get(0).getId());
+			return resultMap;
+		}
+
+		// 生成参数对应关系
+		KindCrawl kindCrawl = initKindCrawl(b2cKind.getId(), b2cBrand, kindName, url, fileName);
+
+		// 生成数据解析任务
+		ComponentCrawlTask task = initComponentCrawlTask(kindCrawl, fileName);
+
+		// 写入器件数据
+		components = writeComponentCrawls(rows, b2cBrand, b2cKind, kindName, task);
+		componentCrawlDao.save(components);
+
+		resultMap.setCode(200);
+		resultMap.setData(components.size());
+		return resultMap;
+	}
 
-			// 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++;
-				}
+	/**
+	 * 写入器件数据
+	 * @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>();
 
-				// 取标准参数
-				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());
+		// 取表头信息
+		List<String> propertyNames = (List<String>)(List)rows.get(0);
+		int maxSize = propertyNames.size();
+
+		for (int i = 1; i < rows.size(); i++) {
+			List<Object> row = rows.get(i);
+            if (row != null && row.size() > 0) {
+                ComponentCrawl component = new ComponentCrawl();
+
+                // 型号
+                Object code = row.get(0);
+                if (code != null && StringUtils.hasText(code.toString())) {
+                    component.setCode(code.toString().trim());
+                } else {
+                    throw new IllegalOperatorException(String.format("第%s行型号为空", (i + 1)));
+                }
+
+                // 规格书
+                Object attach = row.get(5);
+                if (attach != null && StringUtils.hasText(attach.toString())) {
+                    component.setAttach(attach.toString().trim());
+                }
+
+                // 图片
+                Object img = row.get(6);
+                if (img != null && StringUtils.hasText(img.toString())) {
+                    component.setImg(img.toString().trim());
+                }
+
+				/**
+				 * 生命周期 (器件所处生命周期)
+				 * ACTIVE 815 正常
+				 * DISCONTINUED 816 即将停产
+				 * OBSOLETE 817 停产
+				 * PRELIMINARY 818 新品
+				 */
+                Object lifecycle = row.get(7);
+				if (lifecycle != null && StringUtils.hasText(String.valueOf(lifecycle))) {
+					try {
+						component.setLifecycle(Integer.valueOf(String.valueOf(lifecycle)));
+					} catch (NumberFormatException e) {
 					}
+				}
 
+                // 描述
+                Object description = row.get(8);
+                if (description != null && StringUtils.hasText(description.toString())) {
+                    component.setDescription(description.toString().trim());
+                } else {
+					throw new IllegalOperatorException(String.format("第%s行描述为空,请补充完全", i + 1));
 				}
 
-				// 取标准参数单位
-				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());
+                // 参数值
+                Set<PropertyValueCrawl> propertyValues = new HashSet<PropertyValueCrawl>();
+                for (int j = 9; j < maxSize && j < row.size(); j++) {
+                    Object property = row.get(j);
+                    if (property != null && StringUtils.hasText(property.toString())) {
+                        PropertyValueCrawl propertyValue = new PropertyValueCrawl();
+                        propertyValue.setValue(property.toString().trim());
+                        propertyValue.setPropertyName(String.valueOf(propertyNames.get(j)).trim());
+                        propertyValues.add(propertyValue);
+                    }
+                }
+
+                component.setProperties(propertyValues);
+                // 设置基本参数
+                component.setB2cBrId(b2cBrand.getId());
+                component.setB2cKiId(b2cKind.getId());
+                component.setKindName(kindName);
+                component.setTask(task.getId());
+
+                components.add(component);
+            }
+		}
+		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, 9);
+				List<Object> exceptHeader = new ArrayList<Object>();
+				exceptHeader.add("型号");
+				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) {
+							// 列数超过34的文件不得超过2000行
+							if (rowSize > 34 && size > 2000) {
+								throw new IllegalOperatorException("属性超过25个时,文件只允许上传2000行");
+							}
+							map.put("rows", rows);
+							map.put("fileName", fileName);
+							return map;
+							// 整体列数不得超过50
 						} else {
-							units.add("");
+							throw new IllegalOperatorException("属性值不得超过41个");
 						}
+						// 文件不得超过10000行
+					} else {
+						throw new IllegalOperatorException("上传文件必须大于0行小于10000行");
 					}
+				} else {
+					throw new IllegalOperatorException("表头格式错误,前8列应为 \'型号\',\'商城类目\',\'目标类目\',\'商城品牌\',\'来源网址\',\'规格书\',\'图片\',\'描述\'");
 				}
+			} else {
+				throw new IllegalOperatorException("表头信息错误,请核对后再上传");
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return map;
+	}
 
-				// 去具体器件参数
-				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());
-						}
+	/**
+	 * 初始化数据解析任务
+	 * @param kindCrawl 参数对应关系
+	 * @param fileName 上传文件名
+	 * @return
+	 */
+	private ComponentCrawlTask initComponentCrawlTask(KindCrawl kindCrawl, String fileName) {
+		// 生成数据解析任务
+		ComponentCrawlTask task = new ComponentCrawlTask();
+		task.setContrastId(kindCrawl.getId());
+		task.setContrast(FastjsonUtils.toJson(kindCrawl));
+		task.setCreateDate(new 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)));
+
+		kindCrawl.setTaskid(task.getTaskId());// 将数据解析任务号回写到参数对应关系中
+		kindContrastDao.save(kindCrawl);
+
+		return componentCrawlTaskDao.save(task);
+	}
 
-						// 图片
-						if (row.getCell(6) != null) {
-							row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
-							component.setImg(row.getCell(6).getStringCellValue().trim());
-						}
+	/**
+	 * 初始化标准参数对应关系
+	 * @param b2cKindId 商城类目id
+	 * @param b2cBrand 商城品牌
+	 * @param kindName 目标类目
+	 * @param url 官网地址
+	 * @param fileName 上传文件名
+	 * @return
+	 */
+	private KindCrawl initKindCrawl(Long b2cKindId, Brand b2cBrand, String kindName, String url, String fileName) {
+		Date date = new Date();
+		KindCrawl kindCrawl = new KindCrawl();
+		kindCrawl.setResource(b2cBrand.getNameEn());
+		kindCrawl.setB2cBrId(b2cBrand.getId());
+		kindCrawl.setKindName(kindName);
+		kindCrawl.setB2cKiId(b2cKindId);
+		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);
+		return kindContrastDao.findOne(kindCrawl.getId());// 因为save返回的对象没有关联的实体信息只有id,所以需要重新查询一次,为后续生成json存入任务做准备
+	}
 
-						// 参数值
-						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);
-									}
-								}
-							}
-						}
+	/**
+	 * 验证公共数据
+	 * @param firstRow
+	 * @return
+	 */
+	private Map<String, Object> validateCommonData(List<Object> firstRow) {
+		if (CollectionUtils.isNotEmpty(firstRow)) {
+			Map<String, Object> commonData = new HashedMap();
 
-						component.setProperties(propertyValues);
+			// 商城类目
+			Object b2cKindName = firstRow.get(1);
+			if (b2cKindName != null && StringUtils.hasText(b2cKindName.toString())) {
+				List<Kind> kinds = kindDao.findByNameCn(b2cKindName.toString().trim());
+				if (CollectionUtils.isEmpty(kinds)) {
+					throw new IllegalOperatorException("商城类目不存在");
+				}
+				commonData.put("b2cKind", kinds.get(0));
+			} else {
+				throw new IllegalOperatorException("商城类目为空");
+			}
 
-						// 设置基本参数
-						component.setB2cBrId(brand.getId());
-						component.setB2cKiId(kind.getId());
-						component.setKindName(kindName);
-						component.setTask(task.getId());
+			// 目标类目
+			Object kindName = firstRow.get(2);
+			if (kindName != null &&  StringUtils.hasText(kindName.toString())) {
+				commonData.put("kindName", kindName.toString().trim());
+			} else {
+				throw new IllegalOperatorException("目标类目为空");
+			}
 
-						components.add(component);
-					}
+			// 商城品牌
+			Object brandName = firstRow.get(3);
+			if (brandName != null && StringUtils.hasText(brandName.toString())) {
+				List<Brand> brands = brandDao.findByUpperNameEn(brandName.toString().trim());
+				if (CollectionUtils.isEmpty(brands)) {
+					throw new IllegalOperatorException("商城品牌不存在");
 				}
-				components = componentCrawlDao.save(components);
-			} else {
-				throw new IllegalOperatorException("目标参数不完善或格式错误");
+				commonData.put("b2cBrand", brands.get(0));
+			}
+
+			// url
+			Object url = firstRow.get(4);
+			if (url != null && StringUtils.hasText(url.toString())) {
+				commonData.put("url", url.toString().trim());
 			}
+
+			return commonData;
 		} else {
-			throw new IllegalOperatorException("基本数据不完善");
+			throw new IllegalOperatorException("第二列数据不存在");
 		}
-		return components;
 	}
 
 	@Override
-	public List<ComponentCrawl> uploadComponentCrawlWithSame(Workbook workbook, String fileName, Long kindContrastId) {
+	public ResultMap uploadComponentCrawlWithSame(FileUpload uploadItem, Long kindContrastId) {
+		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());
@@ -747,135 +880,30 @@ 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);
-		}
-		
-		// 取目标参数
-		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);
-								}
-							}
-						}
-					}
+			// 生成数据解析任务
+			task = initComponentCrawlTask(kindCrawl, fileName);
+		}
 
-					component.setProperties(propertyValues);
+		// 对应商城品牌
+		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.setB2cBrId(kindCrawl.getB2cBrId());
-					component.setB2cKiId(kindCrawl.getB2cKiId());
-					component.setKindName(kindCrawl.getKindName());
-					component.setTask(task.getId());
+		// 写入器件数据
+		components = writeComponentCrawls(rows, brand, kind, kindCrawl.getKindName(), task);
+		componentCrawlDao.save(components);
 
-					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);
 			});