BillTask.java 2.4 KB
package com.agent.billcheck;

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

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.agent.entity.agent.BillCheckEntity;
import com.agent.service.agent.BillCheckFeeService;
import com.agent.service.agent.BillCheckService;
import com.framework.util.JsonKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;

/**
 * Depiction:
 * <p>
 * Modify:
 * <p>
 * Author: William Lynn
 * <p>
 * Create Date:2018年9月6日 下午12:09:07
 * 
 */
@Component
public class BillTask {
	public static boolean isRunning = false;

	private final static String BILL_API = "http://10.50.3.64:8080/tj/cost/cost";

	@Scheduled(cron = "10 * * * * ?")
	public void perform() {
		if (BillTask.isRunning) {
			return;
		}

		BillTask.isRunning = true;
		System.err.println();
		System.err.println("BillTask start,will get json data......");

		BillCheckService billCheckService = (BillCheckService) SpringContextKit.getBean("billCheckService");
		BillCheckEntity last = billCheckService.last();
		int fid = last != null ? last.getFid() : 0;
		System.err.println("BillTask start,last fid is: " + fid);

		Map<String, String> queryParas = new HashMap<>();
		queryParas.put("fid", String.valueOf(fid));
		String json = "";
		try {
			json = HttpKit.get(BILL_API, queryParas);
		} catch (Exception e) {
			System.err.println("BillTask get json data error -->" + e.toString());
		}
		List<BillCheckBean> list = JsonKit.parseArray(json, BillCheckBean.class);

		if (list != null && !list.isEmpty()) {
			BillCheckFeeService billCheckFeeService = (BillCheckFeeService) SpringContextKit
					.getBean("billCheckFeeService");

			for (BillCheckBean bean : list) {
				String mawbNo = bean.getCode();
				String start = mawbNo.substring(0, 3);
				String end = mawbNo.substring(3, mawbNo.length());
				mawbNo = start + "-" + end;
				bean.setCode(mawbNo);
				
				if (!billCheckService.exist(mawbNo)) {
					bean.setStation(getStationCode(bean.getChgseq()));

					billCheckService.save(bean);
				}

				if (!billCheckFeeService.exist(mawbNo, bean.getFeenm())) {
					billCheckFeeService.save(bean);
				}
			}

		}

		System.err.println("BillTask end, get json data finish......");
		BillTask.isRunning = false;
	}

	private String getStationCode(String str) {
		if (StrKit.isBlank(str))
			return "";
		String code = str.replaceAll("[^(A-Za-z)]", "");
		return code;
	}

}