GetSchedulePlanThread.java 2.34 KB
package com.bsth.service.realcontrol.buffer;

import java.text.SimpleDateFormat;
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.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.schedule.SchedulePlanInfo;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.schedule.SchedulePlanInfoRepository;
import com.bsth.util.BatchSaveUtils;

/**
 * 
 * @ClassName: GetSchedulePlanThread 
 * @Description: TODO(从计划排班表抓取当天实际排班) 
 * @author PanZhao
 * @date 2016年6月6日 下午7:42:43 
 *
 */
@Component
public class GetSchedulePlanThread  extends Thread{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	SchedulePlanInfoRepository schedulePlanInfoRepository;
	
	@Autowired
	ScheduleRealInfoRepository scheduleRealInfoRepository;
	
	SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
	
	@Override
	public void run() {
		try{
			List<ScheduleRealInfo>  realList = null;
			String dateStr = /*sdf.format(new Date())*/"2016-06-01";
			Date cDate = sdfyyyyMMdd.parse(dateStr);
			//查询数据库是否有今日排班
			int size = scheduleRealInfoRepository.countByDate(cDate);
			if(size > 0){
				//从数据库恢复当日排班
				realList = scheduleRealInfoRepository.findByDate(cDate);
			}
			else{
				List<SchedulePlanInfo> list = schedulePlanInfoRepository.findByDate(dateStr);
				
				//实际排班计划
				realList = JSONArray.parseArray(JSON.toJSONString(list), ScheduleRealInfo.class);
				//查询数据库最大ID
				Long id = scheduleRealInfoRepository.getMaxId();
				if(null == id)
					id = 0L;
				id ++;
				
				for(ScheduleRealInfo item : realList){
					item.setSpId(item.getId());
					item.setId(id ++);//设置ID
				}
				//入库
				new BatchSaveUtils<ScheduleRealInfo>().saveList(realList, ScheduleRealInfo.class);
			}
			
			//写入缓存
			ScheduleBuffer.init(realList);
			
			logger.info("获取当天实际排班计划数量:" + realList.size());
		}catch(Exception e){
			logger.error("",e);
		}
	}
}