package cn.tisson.icsp.task;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import cn.tisson.icsp.ftp.common.FileUtil;
import cn.tisson.icsp.service.ObstacleSvc;
import cn.tisson.icsp.service.TimeTaskService;
import cn.tisson.icsp.utils.FtpApche;
import cn.tisson.icsp.utils.GetFileCharsetUtils;
/**
* 严障信息
* @author Administrator
*
*/
@Service("obstacleTask")
public class ObstacleTask {
public static ResourceBundle RESOURCE_BUNDLE =
ResourceBundle.getBundle("cn.tisson.icsp.commonConfig");
protected String ftp_server_ip=RESOURCE_BUNDLE.getString("ftp_server_ip");
protected int ftp_server_port=new Integer(RESOURCE_BUNDLE.getString("ftp_server_port"));
protected String ftp_server_username=RESOURCE_BUNDLE.getString("ftp_server_username");
protected String ftp_server_password=RESOURCE_BUNDLE.getString("ftp_server_password");
@Autowired
TimeTaskService timeTaskService;
@Autowired
ObstacleSvc obstacleSvc;
@Scheduled(cron="0 0/1 * * * ?")
//@Scheduled(cron="0 30 8 * * ?")
public void run() {
System.out.println("=====ObstacleTask======");
try {
int lockCount=this.timeTaskService.findUnLockTimeLockJob("Obstacle_LOCK");
if(lockCount>0){//判断锁是否在解锁状态
timeTaskService.updateLockTimeLockJob("Obstacle_LOCK");//锁定
String date = null;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
date = "" + cal.get(Calendar.YEAR);
date += cal.get(Calendar.MONTH) >= 9 ? (cal.get(Calendar.MONTH) + 1) : ("0" + (cal.get(Calendar.MONTH) + 1));
date += cal.get(Calendar.DAY_OF_MONTH) >= 10 ? cal.get(Calendar.DAY_OF_MONTH) : ("0" + cal.get(Calendar.DAY_OF_MONTH));
date="20151115";
String filename = "gz_obstacle_"+date+".txt";//要down的文件
//FTP服务器hostname 端口 登录名 密码
FtpApche.downFile(ftp_server_ip, ftp_server_port, ftp_server_username, ftp_server_password,
//FTP服务器上的相对路径 文件名 下载后保存到本地的路径
"/predeal", filename, System.getProperty("user.dir"));
// String code = GetFileCharsetUtils.getFilecharset((System.getProperty("user.dir") + File.separator + filename));
//ANSI文件用GBK打开
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + File.separator + filename), "GBK"));
String line = null;
int i = 1;
while ((line = br.readLine()) != null) {
String[] cols = line.split("※");
if(i!=1){
Long l=Long.parseLong(cols[8]);
this.obstacleSvc.saveObstacle(cols[0], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7], l);
System.out.println("插入严障信息");
}
i++;
}
br.close();
FileUtil.fileDelete(System.getProperty("user.dir") + File.separator + filename);
timeTaskService.updateUnLockTimeLockJob("Obstacle_LOCK");//解锁
}
}catch (Exception e) {
e.printStackTrace();
timeTaskService.updateUnLockTimeLockJob("Obstacle_LOCK");//解锁
}
}
}