* Created by PhpStorm.
* User: Administrator
* Date: 14-12-23
* Time: 下午4:27*/
class Sync extendsMy_Controller {/**
* @var hr伴侣数据库的实例*/
private $hrdb;private $year;private $month;private $date;private $month_31 = array(1,3,5,7,8,10,12);private $month_30 = array(4,6,9,11);private $startTimeStamp;private $recursionCount = 0;//递归计数
public function__construct() {
parent::__construct();header("Content-type:text/html;charset=utf-8");
date_default_timezone_set(‘PRC‘);$this->load->model(‘sync_mdl‘);$this->hrdb = $this->sync_mdl->getSqlServer();
}public functionaction() {$beginYear = $this->input->post(‘beginYear‘);$beginMonth = $this->input->post(‘beginMonth‘);$beginDay = $this->input->post(‘beginDay‘);$endYear = $this->input->post(‘endYear‘);$endMonth = $this->input->post(‘endMonth‘);$endDay = $this->input->post(‘endDay‘);if( !empty($beginYear) && !empty($beginMonth) && !empty($beginDay) && empty($endYear) && empty($endMonth) && empty($endDay)) {//自定义开始同步时间,默认同步1天
$this->year = $beginYear; $this->month = $beginMonth; $this->date = $beginDay;$this->startTimeStamp = $this->getTimeStamp($this->year, $this->month, $this->date);$result = $this->fetchOneDayData($this->startTimeStamp, $this->computeTime());
}else if( !empty($beginYear) && !empty($beginMonth) && !empty($beginDay) && !empty($endYear) && !empty($endMonth) && !empty($endDay) ) {//自定义开始 结束的同步时间
$endTimeStamp = $this->getTimeStamp($endYear, $endMonth, $endDay);$result = $this->fetchOneDayData( $this->startTimeStamp, $endTimeStamp);
}else{echo ‘
时间不能为空
‘;}//if(!empty( array(‘mobile‘)) ) {
//就用电话创建账号
//} else if(email 不为空) {
//就用邮箱创建账号
//} else {
//不做任何操作
//}
//如果数据不为空,判断mobile 或者 email 是否存在于小职了
//以上通过,便进行插入到小职了的数据库
// echo $this->startTimeStamp; echo ‘
‘;
//echo $this->computeTime();
//当前取值的数据;
$data[‘year‘] = $this->year;$data[‘month‘] = $this->month;$data[‘day‘] = $this->date;$this->load->view(‘sync‘, $data);echo ‘
‘;if( !empty($result) ) {print_r($result);}else if(isset($result) && $result == ‘today‘) {echo ‘已经到今天了!‘;
}else if( empty($rs) ) {echo ‘本日无数据可同步‘;
}
}/**取出一天的数据, 只要计算的当天数据为空,递归调用,直到当天有数据
* @param $startTimeStamp 开始时间戳
* @param $endTimeStamp 结束时间戳
* @return mixed 返回一天的数据*/
private function fetchOneDayData($startTimeStamp, $endTimeStamp) {if($startTimeStamp == $endTimeStamp) {return ‘today‘;
}else{$rs = $this->fetchHrData($startTimeStamp, $endTimeStamp);if(empty($rs)) {return array();
}else if( !empty($rs) ) {return $rs;
}//if(!empty($rs)) {
//
// return $rs;
//
// } else if(empty($rs)) {
// $this->recursionCount = $this->recursionCount + 1;
// $this->startTimeStamp = $endTimeStamp;
// if($this->recursionCount < 50) {
// //echo ‘‘.$this->recursionCount.‘‘;
// //如果数据不为空的时候,这里接收到本身调用自己返回的数组,并返回
// $re = $this->fetchOneDayData($this->startTimeStamp, $this->computeTime());
// return $re;
//
// } else {
// return ‘超过50天没有数据了‘;
// }
// }
}//end else
}/**根据时间取出每天数据
* @param $beginTime
* @param $endTime*/
private function fetchHrData($beginTime, $endTime) {$sql = "SELECT truename,pubdate FROM resume_cn WHERE pubdate >= $beginTime AND pubdate <= $endTime";$query = $this->hrdb->query($sql);$rs = $query->result_array();//echo ‘
‘;return $rs;//print_r($rs);
//$this->test($rs[9][‘pubdate‘])
}/**计算年份是否是闰年,如果是闰年 2月份是29天 平年是28天, 每调用一次这个函数,天数增 加1天
* @return string 时间戳,是经过计算的,前加 ‘00‘ 后加‘000‘ ,为了与hr伴侣的时间戳相匹配*/
private functioncomputeTime() {if (($this->year % 4 == 0 && $this->year % 100 != 0) || ($this->year % 400 == 0)) {$this->computeTimeDate(29);return $this->getTimeStamp($this->year, $this->month, $this->date);
}else{$this->computeTimeDate(28);return $this->getTimeStamp($this->year, $this->month, $this->date);
}
}/**根据2月份是多少天,计算日期时间,
* @param $Feb 2月的天数*/
private function computeTimeDate($Feb) {if ($this->month == 2) {if($this->date >= 1 && $this->date <= $Feb) {$this->date = $this->date + 1;
}else if($this->date > $Feb) {$this->computeDateMonth();
}else{die(‘2月份天数不在正常范围内‘);
}
}else if( in_array($this->month, $this->month_30) ) {if( $this->date >= 1 && $this->date < 30) {$this->date = $this->date + 1;
}else if($this->date >= 30){$this->computeDateMonth();
}else{die(‘30天的月份天数不在正常范围内‘);
}
}else if(in_array($this->month, $this->month_31)) {if( $this->date >= 1 && $this->date < 31) {$this->date = $this->date + 1;
}else if( $this->date >= 31){$this->computeDateMonth();
}else{die(‘31天的月份天数不在正常范围内‘);
}
}else{//echo $this->month;
die(‘年月日不正确‘);
}
}/**
* 计算一年当中的月份是否为12个月,如果超过12 就让年份 +1*/
private functioncomputeDateMonth() {if($this->month >= 1 && $this->month < 12) {$this->month = $this->month + 1;$this->date = 1;
}else if($this->month == 12) {if( $this->year == date(‘Y‘, time()) ) {return;
}else{$this->year = $this->year + 1;$this->month = 1;$this->computeTime();
}
}else{die(‘computeDateMonth函数计算错误‘);
}
}/**获取用于与HR数据库对比的时间戳,如果定义了年月日 就用定义的时间,否则就取当前时间
* @param $y 年
* @param $m 月
* @param $d 日
* @param $h 小时
* @param $i 分钟
* @param $s 秒
* @return string 时间戳*/
private function getTimeStamp($y = ‘‘, $m = ‘‘, $d = ‘‘, $h = ‘0‘, $i = ‘0‘, $s = ‘0‘) {if( $y && $m && $d) {return ‘00‘ . mktime( $h, $i, $s, $m, $d, $y ) . ‘000‘;
}else{return ‘00‘ . time() . ‘000‘;
}
}
}/*End of file Sync.php*/
/*Location: controllers/sync.php*/