Browse Source

科技需求批量导入

Antiloveg 8 years ago
parent
commit
d250fd4a38

+ 7 - 16
src/main/java/com/goafanti/admin/controller/AdminAchievementApiController.java

@@ -12,7 +12,6 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 
-import org.junit.Test;
 import org.springframework.beans.BeanUtils;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -60,7 +59,12 @@ public class AdminAchievementApiController extends CertifyApiController {
 		Result res = new Result();
 		List<AchievementImportBo> data = JSON.parseArray(d, AchievementImportBo.class);
 		if (data == null || data.isEmpty()) {
-			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "导入数据"));
+			return res;
+		}
+		
+		if (data.size() > AFTConstants.IMPORTMAXLENTH) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "导入数据量"));
 			return res;
 		}
 
@@ -476,9 +480,8 @@ public class AdminAchievementApiController extends CertifyApiController {
 				f.setAccessible(true);
 				try {
 					if (!f.getName().equals("keywords") && (f.get(bo) == null || "".equals(f.get(bo)))) {
-						AchievementImportFields.getFieldDesc(f.toString().substring(f.toString().lastIndexOf(".") + 1));
 						res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", AchievementImportFields
-								.getFieldDesc(f.toString().substring(f.toString().lastIndexOf(".") + 1))));
+								.getFieldDesc(f.getName())));
 						return res;
 					}
 				} catch (IllegalArgumentException | IllegalAccessException e) {
@@ -570,16 +573,4 @@ public class AdminAchievementApiController extends CertifyApiController {
 		return res;
 	}
 
-	@Test
-	public void test() {
-		String[] s = new String(" , a").trim().split(",|,");
-		System.out.println(StringUtils.isBlank(" "));
-		System.out.println(s.length);
-		for (String ss : s) {
-			if (!StringUtils.isBlank(ss)) {
-				System.out.println("a" + ss.trim() + "b");
-			}
-		}
-	}
-
 }

+ 60 - 10
src/main/java/com/goafanti/admin/controller/AdminDemandApiController.java

@@ -1,6 +1,8 @@
 package com.goafanti.admin.controller;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.UUID;
@@ -18,11 +20,13 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import com.alibaba.fastjson.JSON;
 import com.goafanti.admin.service.AftFileService;
 import com.goafanti.common.bo.Result;
 import com.goafanti.common.constant.AFTConstants;
 import com.goafanti.common.constant.ErrorConstants;
 import com.goafanti.common.controller.CertifyApiController;
+import com.goafanti.common.enums.AchievementImportFields;
 import com.goafanti.common.enums.AttachmentType;
 import com.goafanti.common.enums.DeleteStatus;
 import com.goafanti.common.enums.DemandAuditStatus;
@@ -46,30 +50,43 @@ public class AdminDemandApiController extends CertifyApiController {
 	private UserService		userService;
 	@Resource
 	private AftFileService	aftFileService;
-	
+
 	/**
 	 * 批量导入科技需求
 	 */
+	@SuppressWarnings("unchecked")
 	@RequestMapping(value = "/importDemand", method = RequestMethod.POST)
-	public Result importDemand(@RequestParam(name = "data[]", required = false) DemandImportBo[] data){
+	public Result importDemand(@RequestParam(name = "data[]", required = false) String d) {
 		Result res = new Result();
-		
+		List<DemandImportBo> data = JSON.parseArray(d, DemandImportBo.class);
+		if (data == null || data.isEmpty()) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "导入数据"));
+			return res;
+		}
+
+		if (data.size() > AFTConstants.IMPORTMAXLENTH) {
+			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "导入数据量"));
+			return res;
+		}
+		res = disposeImportDemand(res, data);
+		if (!res.getError().isEmpty()) {
+			return res;
+		}
+		data = (List<DemandImportBo>) res.getData();
+		demandService.insertImport(data);
 		return res;
-		
+
 	}
-	
-	
+
 	/**
 	 * 成果需求匹配列表
 	 */
 	@RequestMapping(value = "/achievementDemand", method = RequestMethod.GET)
-	public Result achievementDemand(String id){
+	public Result achievementDemand(String id) {
 		Result res = new Result();
 		res.setData(demandService.selectAchievementDemandListByDemandId(id));
 		return res;
 	}
-	
-	
 
 	/**
 	 * 上传技术需求批量导入Excel模板
@@ -108,7 +125,7 @@ public class AdminDemandApiController extends CertifyApiController {
 		}
 		return res;
 	}
-	
+
 	/**
 	 * 下载技术需求批量导入Excel模板
 	 * 
@@ -475,4 +492,37 @@ public class AdminDemandApiController extends CertifyApiController {
 		return fileName;
 	}
 
+	private Result disposeImportDemand(Result res, List<DemandImportBo> data) {
+		Field[] field = DemandImportBo.class.getDeclaredFields();
+		for (DemandImportBo bo : data) {
+			for (Field f : field) {
+				f.setAccessible(true);
+				try {
+					if (!f.getName().equals("keywords") && (f.get(bo) == null || "".equals(f.get(bo)))) {
+						res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "",
+								AchievementImportFields.getFieldDesc(f.getName())));
+						return res;
+					}
+				} catch (IllegalArgumentException | IllegalAccessException e) {
+				}
+			}
+			String[] keywords = bo.getKeyword().trim().split(",|,");
+			if (null == keywords || keywords.length < 1) {
+				res.getError()
+						.add(buildError(ErrorConstants.PARAM_ERROR, "", AchievementImportFields.KEYWORD.getDesc()));
+				return res;
+			}
+			List<String> list = new ArrayList<>();
+			for (String s : keywords) {
+				if (!StringUtils.isBlank(s)) {
+					list.add(s.trim());
+				}
+			}
+
+			bo.setKeywords(list);
+		}
+		res.setData(data);
+		return res;
+	}
+
 }

+ 2 - 0
src/main/java/com/goafanti/common/constant/AFTConstants.java

@@ -29,4 +29,6 @@ public class AFTConstants {
 
 	public static final int		KEYWORDLENTH		= 16;
 
+	public static final int		IMPORTMAXLENTH		= 1000;					// 科技需求&科技成果批量导入最大数据量
+
 }

+ 2 - 0
src/main/java/com/goafanti/common/dao/DemandMapper.java

@@ -25,4 +25,6 @@ public interface DemandMapper {
 	DemandManageDetailBo selectOrgDemandDetail(String id);
 
 	int updateReleaseDate(String id);
+
+	void insertBatch(List<Demand> demandList);
 }

+ 1 - 1
src/main/java/com/goafanti/common/mapper/DemandKeywordMapper.xml

@@ -69,7 +69,7 @@
     where id = #{id,jdbcType=VARCHAR}
   </update>
   
-   <insert id="insertBatch" parameterType="com.goafanti.common.model.DemandKeyword">
+  <insert id="insertBatch" parameterType="com.goafanti.common.model.DemandKeyword">
     insert into demand_keyword (id, demand_id, keyword)
     values 
     <foreach item="item" index="index" collection="list" separator=",">

+ 19 - 0
src/main/java/com/goafanti/common/mapper/DemandMapper.xml

@@ -877,4 +877,23 @@
 	</if>
   </select>
   
+  <insert id="insertBatch" parameterType="com.goafanti.common.model.Demand">
+    insert into demand 
+    	(id, deleted_sign, audit_status, 
+    	status, release_status, create_time, 
+    	keyword, data_category, name,
+    	demand_type, problem_des, employer_name,
+    	employer_contacts, employer_contacts_mobile, employer_contacts_mailbox)
+    values 
+    <foreach item="item" index="index" collection="list" separator=",">
+      (
+      	#{item.id,jdbcType=VARCHAR}, #{item.deletedSign,jdbcType=INTEGER}, #{item.auditStatus,jdbcType=INTEGER},
+      	#{item.status,jdbcType=INTEGER}, #{item.releaseStatus,jdbcType=INTEGER}, #{item.createTime,jdbcType=TIMESTAMP},
+      	#{item.keyword,jdbcType=VARCHAR}, #{item.dataCategory,jdbcType=INTEGER}, #{item.name,jdbcType=VARCHAR},
+      	#{item.demandType,jdbcType=INTEGER}, #{item.problemDes,jdbcType=VARCHAR}, #{item.employerName,jdbcType=VARCHAR},
+      	#{item.employerContacts,jdbcType=VARCHAR}, #{item.employerContactsMobile,jdbcType=VARCHAR}, #{item.employerContactsMailbox,jdbcType=VARCHAR}
+      )
+    </foreach>
+  </insert>
+  
 </mapper>

+ 127 - 7
src/main/java/com/goafanti/demand/bo/DemandImportBo.java

@@ -1,18 +1,138 @@
 package com.goafanti.demand.bo;
 
+import java.util.List;
+
 public class DemandImportBo {
-	private Integer dataCategory;
 	
-	private String name;
+	/**
+	 * 数据类别(0-个人需求,1-单位需求)
+	 */
+	private Integer	dataCategory;
+	
+	/**
+	 * 名称
+	 */
+	private String	name;
+	
+	/**
+	 * 关键词
+	 */
+	private String	keyword;
+	
+	/**
+	 * 需求类型
+	 */
+	private Integer	demandType;
 	
-	private String keyword;
+	/**
+	 * 问题说明
+	 */
+	private String	problemDes;
 	
-	private Integer demandType;
 	
-	private String problemDes;
+	/**
+	 * 雇主名称
+	 */
+	private String	employerName;
 	
-	private String employerName;
+
+	/**
+	 * 雇主联系人名称
+	 */
+	private String	employerContacts;
+	
+
+	/**
+	 * 雇主联系人电话
+	 */
+	private String	employerContactsMobile;
 	
+
+	/**
+	 * 雇主联系人邮箱
+	 */
+	private String	employerContactsMailbox;
 	
-   	
+	private List<String> keywords;
+	
+	public List<String> getKeywords() {
+		return keywords;
+	}
+
+	public void setKeywords(List<String> keywords) {
+		this.keywords = keywords;
+	}
+
+	public Integer getDataCategory() {
+		return dataCategory;
+	}
+
+	public void setDataCategory(Integer dataCategory) {
+		this.dataCategory = dataCategory;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getKeyword() {
+		return keyword;
+	}
+
+	public void setKeyword(String keyword) {
+		this.keyword = keyword;
+	}
+
+	public Integer getDemandType() {
+		return demandType;
+	}
+
+	public void setDemandType(Integer demandType) {
+		this.demandType = demandType;
+	}
+
+	public String getProblemDes() {
+		return problemDes;
+	}
+
+	public void setProblemDes(String problemDes) {
+		this.problemDes = problemDes;
+	}
+
+	public String getEmployerName() {
+		return employerName;
+	}
+
+	public void setEmployerName(String employerName) {
+		this.employerName = employerName;
+	}
+
+	public String getEmployerContacts() {
+		return employerContacts;
+	}
+
+	public void setEmployerContacts(String employerContacts) {
+		this.employerContacts = employerContacts;
+	}
+
+	public String getEmployerContactsMobile() {
+		return employerContactsMobile;
+	}
+
+	public void setEmployerContactsMobile(String employerContactsMobile) {
+		this.employerContactsMobile = employerContactsMobile;
+	}
+
+	public String getEmployerContactsMailbox() {
+		return employerContactsMailbox;
+	}
+
+	public void setEmployerContactsMailbox(String employerContactsMailbox) {
+		this.employerContactsMailbox = employerContactsMailbox;
+	}
+
 }

+ 3 - 0
src/main/java/com/goafanti/demand/service/DemandService.java

@@ -5,6 +5,7 @@ import java.util.List;
 import com.goafanti.achievement.bo.AchievementDemandListBo;
 import com.goafanti.common.model.Demand;
 import com.goafanti.core.mybatis.page.Pagination;
+import com.goafanti.demand.bo.DemandImportBo;
 import com.goafanti.demand.bo.DemandListBo;
 import com.goafanti.demand.bo.DemandManageDetailBo;
 import com.goafanti.demand.bo.DemandManageListBo;
@@ -46,4 +47,6 @@ public interface DemandService {
 
 	List<AchievementDemandListBo> selectAchievementDemandListByDemandId(String id);
 
+	void insertImport(List<DemandImportBo> data);
+
 }

+ 40 - 0
src/main/java/com/goafanti/demand/service/impl/DemandServiceImpl.java

@@ -38,6 +38,7 @@ import com.goafanti.common.utils.DateUtils;
 import com.goafanti.core.mybatis.BaseMybatisDao;
 import com.goafanti.core.mybatis.page.Pagination;
 import com.goafanti.core.shiro.token.TokenManager;
+import com.goafanti.demand.bo.DemandImportBo;
 import com.goafanti.demand.bo.DemandListBo;
 import com.goafanti.demand.bo.DemandManageDetailBo;
 import com.goafanti.demand.bo.DemandManageListBo;
@@ -297,6 +298,43 @@ public class DemandServiceImpl extends BaseMybatisDao<DemandMapper> implements D
 	public List<AchievementDemandListBo> selectAchievementDemandListByDemandId(String id) {
 		return achievementDemandMapper.selectAchievementDemandListByDemandId(id);
 	}
+	
+	@Override
+	public void insertImport(List<DemandImportBo> data) {
+		if (null == data || data.isEmpty()) {
+			return;
+		}
+		Demand d = null;
+		DemandKeyword dk = null;
+		List<Demand> demandList = new ArrayList<>();
+		List<DemandKeyword> demandKeywordList = new ArrayList<>();
+		Calendar now = Calendar.getInstance();
+		now.set(Calendar.MILLISECOND, 0);
+		for (DemandImportBo bo : data){
+			d = new Demand();
+			d.setId(UUID.randomUUID().toString());
+			d.setReleaseStatus(DemandReleaseStatus.UNRELEASE.getCode());
+			d.setCreateTime(now.getTime());
+			d.setDeletedSign(DeleteStatus.UNDELETE.getCode());
+			d.setAuditStatus(DemandAuditStatus.INAUDIT.getCode());
+			d.setStatus(DemandStatus.UNRESOLVED.getCode());
+			List<String> keywordsList = bo.getKeywords();
+			if (null != keywordsList && keywordsList.size() > 0) {
+				for (String s : keywordsList) {
+					if (!StringUtils.isBlank(s)) {
+						dk = new DemandKeyword();
+						dk.setDemandId(d.getId());
+						dk.setKeyword(s.trim());
+						dk.setId(UUID.randomUUID().toString());
+						demandKeywordList.add(dk);
+					}
+				}
+			}
+			demandList.add(d);
+		}
+		demandMapper.insertBatch(demandList);
+		demandKeywordMapper.insertBatch(demandKeywordList);
+	}
 
 	private Map<String, Object> disposeParams(Integer auditStatus, String province, Integer serialNumber, String name,
 			String keyword, Integer infoSources, Integer demandType, String validityPeriodStartDate,
@@ -469,4 +507,6 @@ public class DemandServiceImpl extends BaseMybatisDao<DemandMapper> implements D
 		}
 	}
 
+	
+
 }