package cn.gov.customs.data.timer;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

import cn.gov.customs.data.repository.app.AppRepository;
import cn.gov.customs.data.repository.wxjy.common.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.gov.customs.data.entity.wxjy.common.*;

@Component
@PropertySource("classpath:data-acquisition.properties")
public class AppBaseDataTask {

	private Logger log = Logger.getLogger(AppBaseDataTask.class);
	@Autowired
	private AppRepository appDao;
	@Autowired
	private CountryRepository countryRepository;
	@Autowired
	private CustomsRepository customsRepository;
	@Autowired
	private TradeRepository tradeRepository;
	@Autowired
	private TransfRepository transfRepository;
	@Autowired
	private UnitRepository unitRepository;
	@Autowired
	private TrnVeTypeRelRepository trnVeTypeRelRepository;
	@Autowired
	private WrapRepository wrapRepository;
	@Autowired
	private BusiTypeRepository busiTypeRepository;
	@Autowired
	private StepCodeRepository stepCodeRepository;
	@Autowired
	private PortRepository portRepository;

	/**
	 * 基础表同步
	 * @throws ParseException
	 */
	@Scheduled(cron = "${schedule.cron.month.baseData}")
	public void baseInfoTask() throws ParseException{
		smCountrySynchro();
		smCustomsSynchro();
		smTransfSynchro();
		smTradeSynchro();
		smUnitData();
		smTrnVeTypeRelSynchro();
		smWrapSynchro();
		smPortSynchro();
		smBusiTypeSynchro();
		smStepCodeSynchro();
	}

	/**
	 * @throws ParseException
	 * @Description:国家表定时任务
	 */
	public void smCountrySynchro() {
		log.info("===国家基础表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "SELECT COUNTRY_CODE,COUN_C_NAME,ISO_E,COUN_E_NAME,ABBR_C,EXAM_MARK,HIGH_LOW FROM COUNTRY ";
		List<Object[]> countryList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != countryList && countryList.size() > 0) {
			// 直接删除
			countryRepository.deleteAll();
			for (int i = 0; i < countryList.size(); i++) {
				Country smCountry = new Country();
				smCountry.setCountryCode(countryList.get(i)[0]==null?"":countryList.get(i)[0]+"");
				smCountry.setCounCName(countryList.get(i)[1]==null?"":countryList.get(i)[1]+"");
				smCountry.setIsoE(countryList.get(i)[2]==null?"":countryList.get(i)[2]+"");
				smCountry.setCounEName(countryList.get(i)[3]==null?"":countryList.get(i)[3]+"");
				smCountry.setAbbrC(countryList.get(i)[4]==null?"":countryList.get(i)[4]+"");
				smCountry.setExamMark(countryList.get(i)[5]==null?"":countryList.get(i)[5]+"");
				smCountry.setHighLow(countryList.get(i)[6]==null?"":countryList.get(i)[6]+"");
				countryRepository.save(smCountry);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===国家基础表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smCustomsSynchro() {
		// TODO Auto-generated method stub
		log.info("===海关基础表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select CUSTOMS_CODE,CUSTOMS_NAME,ABBR_CUST from CUSTOMS order by CUSTOMS_CODE;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			customsRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				Customs sc = new Customs();
				sc.setCustomsCode(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setCustomsName(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				sc.setAbbrCust(customsList.get(i)[2] != null ? customsList.get(i)[2].toString() : null);
				customsRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===海关基础表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	/**
	 * @throws ParseException
	 * @Description:运输方式表同步
	 */
	public void smTransfSynchro() throws ParseException{
		log.info("===运输方式表基础表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "SELECT TRAF_CODE,TRAF_SPEC FROM TRANSF ";
		List<Object[]> transfList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != transfList && transfList.size() > 0) {
			// 直接删除
			transfRepository.deleteAll();
			// 减免税份数有则插入，没有则往下继续判断减免税货值
			for (int i = 0; i < transfList.size(); i++) {
				Transf trans = new Transf();
				trans.setTrafCode(transfList.get(i)[0]==null?"":transfList.get(i)[0]+"");
				trans.setTrafSpec(transfList.get(i)[1]==null?"":transfList.get(i)[1]+"");
				transfRepository.save(trans);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===运输方式表基础表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	/**
	 * @throws ParseException
	 * @Description:监管方式表同步
	 */
	public void smTradeSynchro() {
		log.info("===监管方式表基础表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "SELECT TRADE_MODE,ABBR_TRADE,FULL_TRADE FROM TRADE ";
		List<Object[]> tradeList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != tradeList && tradeList.size() > 0) {
			// 直接删除SM_TRANSF
			tradeRepository.deleteAll();
			for (int i = 0; i < tradeList.size(); i++) {
				Trade trans = new Trade();
				trans.setTradeMode(tradeList.get(i)[0]==null?"":tradeList.get(i)[0]+"");
				trans.setAbbrTrade(tradeList.get(i)[1]==null?"":tradeList.get(i)[1]+"");
				trans.setFullTrade(tradeList.get(i)[2]==null?"":tradeList.get(i)[2]+"");
				tradeRepository.save(trans);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===监管方式表基础表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smUnitData(){
		log.info("===计量单位基础表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select UNIT_CODE,UNIT_NAME,CONV_CODE,CONV_RATIO from UNIT";
		List<Object[]> unitList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != unitList && unitList.size() > 0) {
			// 直接删除
			unitRepository.deleteAll();
			// 减免税份数有则插入，没有则往下继续判断减免税货值
			for (int i = 0; i < unitList.size(); i++) {
				Unit sc = new Unit();
				sc.setUnitCode(unitList.get(i)[0] != null ? unitList.get(i)[0].toString() : null);
				sc.setUnitName(unitList.get(i)[1] != null ? unitList.get(i)[1].toString() : null);
				sc.setConvCode(unitList.get(i)[2] != null ? unitList.get(i)[2].toString() : null);
				sc.setConvRatio(unitList.get(i)[3] != null ? (BigDecimal)unitList.get(i)[3] : new BigDecimal(0));
				unitRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===计量单位基础表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smTrnVeTypeRelSynchro() {
		// TODO Auto-generated method stub
		log.info("===运输工具类型代码表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select VE_TYPE,VE_TYPE_NOTE,VE_MODE,VE_UNUSE from TRN_VE_TYPE_REL;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			trnVeTypeRelRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				TrnVeTypeRel sc = new TrnVeTypeRel();
				sc.setVeType(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setVeTypeNote(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				sc.setVeMode(customsList.get(i)[2] != null ? customsList.get(i)[2].toString() : null);
				sc.setVeUnuse(customsList.get(i)[3] != null ? customsList.get(i)[3].toString() : null);
				trnVeTypeRelRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===运输工具类型代码表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smWrapSynchro() {
		// TODO Auto-generated method stub
		log.info("===包装种类代码表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select WRAP_CODE,WRAP_NAME from WRAP ;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			wrapRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				Wrap sc = new Wrap();
				sc.setWrapCode(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setWrapName(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				wrapRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===包装种类代码表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smPortSynchro() {
		// TODO Auto-generated method stub
		log.info("===包装种类代码表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select PORT_CODE,COUNTRY_CODE,PORT_C_NAME,PORT_E_NAME from WRAP ;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			portRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				Port sc = new Port();
				sc.setPortCode(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setCountryCode(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				sc.setPortCName(customsList.get(i)[2] != null ? customsList.get(i)[2].toString() : null);
				sc.setPortEName(customsList.get(i)[3] != null ? customsList.get(i)[3].toString() : null);
				portRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===包装种类代码表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smBusiTypeSynchro() {
		// TODO Auto-generated method stub
		log.info("===行业种类代码表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select BUSI_CODE,BUSI_NAME from BUSI_TYPE;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			busiTypeRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				BusiType sc = new BusiType();
				sc.setBusiCode(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setBusiName(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				busiTypeRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===行业种类代码表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}

	public void smStepCodeSynchro() {
		// TODO Auto-generated method stub
		log.info("===环节代码表同步，并存储到本地库中，开始执行===");
		long b = System.currentTimeMillis();
		String sql = "select STEP_CODE,STEP_MESS from STEP_CODE;";
		List<Object[]> customsList = (List<Object[]>) appDao.getQueryBySql(sql);
		if (null != customsList && customsList.size() > 0) {
			stepCodeRepository.deleteAll();
			for (int i = 0; i < customsList.size(); i++) {
				StepCode sc = new StepCode();
				sc.setStepCode(customsList.get(i)[0] != null ? customsList.get(i)[0].toString() : null);
				sc.setStepMess(customsList.get(i)[1] != null ? customsList.get(i)[1].toString() : null);
				stepCodeRepository.save(sc);
			}
		}
		long e = System.currentTimeMillis();
		log.info("===环节代码表同步，并存储到本地库中,执行结束，执行==="+(e-b)+" ms===");
	}
}
