package com.reptileclient.jlifeapp.service.impl;

import com.google.gson.Gson;
import com.reptileclient.jlifeapp.constant.HemaConstant;
import com.reptileclient.jlifeapp.dao.hm.HmSalesOrderMapper;
import com.reptileclient.jlifeapp.dao.hm.HmTokenMapper;
import com.reptileclient.jlifeapp.entity.hm.HmSalesOrder;
import com.reptileclient.jlifeapp.entity.hm.HmToken;
import com.reptileclient.jlifeapp.service.HmDownHistoryService;
import com.reptileclient.jlifeapp.service.HmSalesOrderService;
import com.reptileclient.jlifeapp.util.HttpUtil;
import com.reptileclient.jlifeapp.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @author 陆，
 * 	银联商交易及销售数据抓取
 *
 */
@Service
public class HmSalesOrderServiceImpl implements HmSalesOrderService {

	private static final Logger logger = LoggerFactory.getLogger(HmSalesOrderServiceImpl.class);
	public static Gson gson = new Gson();

	@Autowired
	HmSalesOrderMapper hmSalesOrderMapper;

	@Autowired
	HmDownHistoryService hmDownHistoryService;

	@Autowired
	HmTokenMapper hmtk;

	public static Map<String, String> supplierNoMap = new HashMap<String, String>();
	static {
		supplierNoMap.put("282136170", "北京");
		supplierNoMap.put("282160323", "南京");
		supplierNoMap.put("282139010", "深圳");
		supplierNoMap.put("282121208", "上海");
	}

	@SuppressWarnings("unchecked")
	public void fetchingData(String startDate, String endDate) {
		HmToken hm = hmtk.selectAll();
		// 分4个城市
		
		try {
			for (Entry<String, String> s : supplierNoMap.entrySet()) {
				postHm(startDate, endDate, hm.getToken(), s.getKey());
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("银联商交易及销售数据抓取异常："+e.getMessage());
		}
		

	}

	@SuppressWarnings("unchecked")
	private void postHm(String startDate, String endDate, String token, String supplierNo) {
		Integer total = 0;
		Integer pageIndex = 1;
		Integer pageSize = 500;

		while (true) {
			String parmJson = HemaConstant.HEMADATA_LYSJYJXS_PARM.replace("parm_startDate", startDate)
					.replace("parm_endDate", endDate).replace("parm_supplierNo", supplierNo)
					.replace("parm_pageIndex", pageIndex + "").replace("parm_pageSize", pageSize + "");

			logger.info(supplierNoMap.get(supplierNo)+"联营商交易及销售单请求参数：" + parmJson);

			Map<String, Object> request = HttpUtil.request(token, parmJson, HemaConstant.HEMADATA_LYSJYJXS_URL);
			if (request.get("success").toString().equals("true")) {
				Map<String, Object> data = (Map<String, Object>) request.get("data");
				String tt = data.get("total").toString();
				total = (int) Double.parseDouble(tt);
				logger.info(supplierNoMap.get(supplierNo)+"联营商交易及销售单总条数："+ + total);

				if(total>0) {
					List<HmSalesOrder> fromJson = JsonUtil.jsonToList(JsonUtil.getJson(data.get("data")),
							HmSalesOrder.class);
					
					// 插入到数据库
					hmSalesOrderMapper.batcInsert(fromJson);
				}
				hmDownHistoryService.insertDownLog(HemaConstant.HEMADATA_LYSJYJXS_NAME+supplierNoMap.get(supplierNo),
						HemaConstant.HEMADATA_LYSJYJXS_URL, parmJson, startDate+" 00:00:00", endDate+" 00:00:00", 0);
			} else {
				//失败
				hmDownHistoryService.insertDownLog(HemaConstant.HEMADATA_LYSJYJXS_NAME+supplierNoMap.get(supplierNo),
						HemaConstant.HEMADATA_LYSJYJXS_URL, parmJson, startDate+" 00:00:00", endDate+" 00:00:00",1);
			}

			if (pageIndex * pageSize >= total) {
				break;
			}
			pageIndex = pageIndex + 1;

		}
	}
	 

	public static void main(String[] args) {

	}
}
