package com.goafanti.admin.controller;

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;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.BaseApiController;
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;

	@Autowired
	private FragmentService	fragmentService;

	@RequestMapping(value = "/vacationcrawler", method = RequestMethod.GET)
	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<>();
			cb.setResult(json);
			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);
			if (json.containsKey(year)) {
				fragmentService.save("vacations" + year, json.get(year));
			}
			res.setData(json);
		} catch (Exception e) {
			res.getError().add(buildError("", e.getMessage()));
		}
		return res;
	}
}
