albertshaw 8 lat temu
rodzic
commit
e8c1a136db

+ 19 - 29
src/main/java/com/goafanti/admin/controller/AdminTriggerController.java

@@ -1,11 +1,9 @@
 package com.goafanti.admin.controller;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -13,21 +11,28 @@ import org.springframework.web.bind.annotation.RestController;
 
 import com.goafanti.common.bo.Result;
 import com.goafanti.common.controller.BaseApiController;
-import com.goafanti.common.utils.FileUtils;
-import com.goafanti.common.utils.LoggerUtils;
+import com.goafanti.common.model.HtmlFragment;
+import com.goafanti.common.utils.StringUtils;
 import com.goafanti.crawler.RequestUtils;
 import com.goafanti.crawler.callback.TransitFutureCallback;
+import com.goafanti.fragment.service.FragmentService;
 
 @RestController
 @RequestMapping(value = "/api/admin/trigger")
 public class AdminTriggerController extends BaseApiController {
 
 	@Value(value = "${upload.path}")
-	private String uploadPath = null;
+	private String			uploadPath	= null;
+
+	@Autowired
+	private FragmentService	fragmentService;
 
 	@RequestMapping(value = "/vacationcrawler", method = RequestMethod.GET)
-	public Result vacationCrawlerStart(String id) {
+	public Result vacationCrawlerStart(String year) {
 		Result res = new Result();
+		if (StringUtils.isBlank(year)) {
+			return res.error(buildError("", "需要年份信息。"));
+		}
 		try {
 			TransitFutureCallback cb = new TransitFutureCallback("<a.*?href=\"(.*?)\".*?节假日安排.*?</a>");
 			Map<String, String> json = new HashMap<>();
@@ -35,32 +40,17 @@ public class AdminTriggerController extends BaseApiController {
 			RequestUtils.startRequest(
 					"http://sousuo.gov.cn/list.htm?n=40&p=0&t=paper&sort=pubtime&subchildtype=gc189&location=%E7%BB%BC%E5%90%88%E6%94%BF%E5%8A%A1%E5%85%B6%E4%BB%96&timetype=timeqb",
 					cb);
-			for (String j : json.keySet()) {
-				saveToFile("/json/vacations/" + j + ".json", json.get(j));
+			if (json.containsKey(year)) {
+				String id = "vacations" + year;
+				HtmlFragment hf = new HtmlFragment();
+				hf.setContent(json.get(year));
+				hf.setId(id);
+				fragmentService.save(id, hf);
 			}
+			res.setData(json);
 		} catch (Exception e) {
 			res.getError().add(buildError("", e.getMessage()));
 		}
 		return res;
 	}
-
-	private void saveToFile(String filePath, String s) {
-		File file = new File(uploadPath + filePath);
-		file.getParentFile().mkdirs();
-		FileWriter fw = null;
-		try {
-			fw = new FileWriter(file);
-			fw.write(s);
-		} catch (IOException e) {
-			LoggerUtils.error(FileUtils.class, e.getMessage(), e);
-		} finally {
-			try {
-				if (fw != null) {
-					fw.close();
-				}
-			} catch (IOException e) {
-				fw = null;
-			}
-		}
-	}
 }

+ 2 - 0
src/main/java/com/goafanti/common/mapper/HtmlFragmentMapper.xml

@@ -28,6 +28,8 @@
   <insert id="insert" parameterType="com.goafanti.common.model.HtmlFragment">
     insert into html_fragment (id, content)
     values (#{id,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR})
+    ON DUPLICATE KEY UPDATE 
+    content=values(content)
   </insert>
   <insert id="insertSelective" parameterType="com.goafanti.common.model.HtmlFragment">
     insert into html_fragment

+ 11 - 1
src/main/java/com/goafanti/fragment/controller/FragmentController.java

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 import com.goafanti.common.bo.Result;
 import com.goafanti.common.controller.BaseApiController;
+import com.goafanti.common.model.HtmlFragment;
 import com.goafanti.fragment.service.FragmentService;
 
 @RestController
@@ -19,7 +20,16 @@ public class FragmentController extends BaseApiController {
 
 	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
 	public Result get(@PathVariable String id) {
-		return new Result(fragmentService.selectById("evaluation_announcement"));
+		return new Result(fragmentService.selectById(id));
+	}
+
+	@RequestMapping(value = "/json/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
+	public String json(@PathVariable String id) {
+		HtmlFragment hf = fragmentService.selectById(id);
+		if (hf != null) {
+			return hf.getContent();
+		}
+		return "{}";
 	}
 
 }

+ 8 - 0
src/main/java/com/goafanti/fragment/service/FragmentService.java

@@ -1,6 +1,7 @@
 package com.goafanti.fragment.service;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -19,4 +20,11 @@ public class FragmentService extends BaseMybatisDao<HtmlFragmentMapper> {
 	public HtmlFragment selectById(String id) {
 		return htmlFragmentMapper.selectByPrimaryKey(id);
 	}
+
+	@CachePut(value = "HtmlFragmentCache", key = "'HtmlFragment:'+#id")
+	public HtmlFragment save(String id, HtmlFragment record) {
+		htmlFragmentMapper.insert(record);
+		return record;
+	}
+
 }