package com.reptileclient.jlifeapp.web;

import com.reptileclient.jlifeapp.service.HmSalesOrderService;
import com.reptileclient.jlifeapp.util.DateUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "银联商交易及销售数据抓取", description = "银联商交易及销售数据抓取")
public class HmSalesOrderController {

	private static final Logger logger = LoggerFactory.getLogger(HmSalesOrderController.class);

	@Autowired
	HmSalesOrderService hmSalesOrderService;

	/**
	 * 测抓数据
	 * 
	 * @return
	 */
	@ApiOperation(value = "银联商交易及销售数据抓取\"2020-11-28\", \"2020-12-01\"", notes = "\"2020-11-28\", \"2020-12-01\"")
	@GetMapping("runTes")
	public String runTes(String startDate, String endDate) {
		//不能多日抓。。。。。。
		new Thread() {
			public void run() {
				List<String> dateByTwoDateList = getDateByTwoDateList(startDate, endDate);
				if(dateByTwoDateList!=null&&dateByTwoDateList.size()>0) {
					dateByTwoDateList.forEach(dt->{
						System.out.println("日期："+dt);
						hmSalesOrderService.fetchingData(dt, dt);
					});
				}


//				hmSalesOrderService.fetchingDataType(startDate, endDate);
			};
		}.start();

		return "ok";
	}

	// 获取两个日期间的所有日期List
	public static List<String> getDateByTwoDateList(String startTime, String endTime) {
		try {
			List<String> list = new ArrayList<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date start = sdf.parse(startTime);
			Date end = sdf.parse(endTime);
			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start);
			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				list.add(sdf.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;

	}

}