示例#1
0
  function match($match_desc) {
    switch($match_desc[0]) {
      case "or":
	for($i=1; $i<sizeof($match_desc); $i++)
	  if($this->match($match_desc[$i]))
	    return true;
        return false;
      case "and":
	for($i=1; $i<sizeof($match_desc); $i++)
	  if(!$this->match($match_desc[$i]))
	    return false;
	return true;
      case "~fuzzy is":
      case "~is":
	$values=$this->get_multi($match_desc[1]);
	for($i=2; $i<sizeof($match_desc); $i++)
	  for($j=0; $j<sizeof($values); $j++)
	    if($values[$j]==$match_desc[$i])
	      return true;
        return false;
      case "fuzzy is":
      case "is":
	$value=$this->get($match_desc[1]);
	for($i=2; $i<sizeof($match_desc); $i++)
	  if($value==$match_desc[$i])
	    return true;
        return false;
      case "~is not":
	$values=$this->get_multi($match_desc[1]);
	for($i=2; $i<sizeof($match_desc); $i++)
	  for($j=0; $j<sizeof($values); $j++)
	    if($values[$j]!=$match_desc[$i])
	      return false;
        return true;
      case "is not":
	$value=$this->get($match_desc[1]);
	for($i=2; $i<sizeof($match_desc); $i++)
	  if($value!=$match_desc[$i])
	    return false;
        return true;
      case "~exist":
      case "exist":
	if($this->get($match_desc[1]))
	  return true;
        return false;
      case "~exist not":
      case "exist not":
	if($this->get($match_desc[1]))
	  return false;
        return true;
      case "~>":
      case "~<":
      case "~>=":
      case "~<=":
	$values=$this->get_multi($match_desc[1]);
	for($j=0; $j<sizeof($values); $j++) {

	  $comp1=parse_number($values[$j]);
	  $comp2=parse_number($match_desc[2]);

	  switch(substr($match_desc[0], 1)) {
	    case ">":
	      if($comp1>$comp2)
		return true;
	      break;
	    case "<":
	      if($comp1<$comp2)
		return true;
	      break;
	    case ">=":
	      if($comp1>=$comp2)
		return true;
	      break;
	    case "<=":
	      if($comp1<=$comp2)
		return true;
	      break;
	  }
	}
	return false;
      case ">":
      case "<":
      case ">=":
      case "<=":
	$value=$this->get($match_desc[1]);

	$comp1=parse_number($value);
	$comp2=parse_number($match_desc[2]);

	switch($match_desc[0]) {
	  case ">":
	    if($comp1>$comp2)
	      return true;
	    break;
	  case "<":
	    if($comp1<$comp2)
	      return true;
	    break;
	  case ">=":
	    if($comp1>=$comp2)
	      return true;
	    break;
	  case "<=":
	    if($comp1<=$comp2)
	      return true;
	    break;
	}

	return false;
      case "not":
        return !$this->match($match_desc[1]);
      case "true":
	return true;
      case "false":
	return false;
      default:
        print "Invalid match desc '$match_desc[0]'\n";
    }

    return false;
  }
示例#2
0
 public function index()
 {
     $this->meta_title = '数据统计';
     // 初始化城市信息
     $where_base = [];
     list($city, $is_city_id) = $this->_initRegion();
     // 读取学校选择菜单数据
     $university_where = ['status' => 1];
     if ($city > 0) {
         // 判断是省还是区
         $university_where['id'] = ['exp', ' in (select university_id from zj_university_campus where ' . ($is_city_id ? 'city_id' : 'province_id') . '=' . $city . ')'];
     }
     $university = M('university')->where($university_where)->field('id,name')->order('name asc')->select();
     $university = array_merge([['id' => '-1', 'name' => '全部学校']], $university);
     $university_id = I('get.university_id', -1, 'intval');
     $university_name = '未知校区';
     foreach ($university as $vo) {
         if ($vo['id'] == $university_id) {
             $university_name = $vo['name'];
             break;
         }
     }
     $this->assign('_university', $university);
     $this->assign('university_name', $university_name);
     // 读取周期选择菜单数据
     $_period = ['day' => '按日查看', 'month' => '按月查看', 'total' => '查看至今'];
     $period = I('period', 'day');
     $this->assign('_period', $_period);
     $this->assign('period_name', $_period[$period]);
     // 查询时间段
     $orderModel = M('loan_order');
     $day_min = date('Y-m-d', $orderModel->min('created'));
     $day_max = date('Y-m-d');
     // 根据查看类型,确定时间范围
     $start_date = I('start', date('Y-m-d', strtotime('-7 day')));
     $end_date = I('end', $day_max);
     if ($period == 'day') {
         $peroid_code = 'day';
         $start_date = max($start_date, $day_min);
         $end_date = min($end_date, $day_max);
     } elseif ($period == 'month') {
         $peroid_code = 'month';
         $start_date = date('Y-m-01', strtotime($start_date));
         $end_date = date('Y-m-d', strtotime(date('Y-m-01', strtotime($end_date)) . ' +1 month -1 day'));
     } elseif ($period == 'total') {
         $peroid_code = 'month';
         $start_date = $day_min;
         $end_date = $day_max;
     } else {
         $this->error('未知的查看时间类型!');
     }
     // 初始化空白数据
     $empty_data = [];
     $xtitle = [];
     for ($date = $start_date; $date <= $end_date;) {
         $date = $period == 'month' ? substr($date, 0, 7) : $date;
         $xtitle[] = $date;
         $empty_data[$date] = 0;
         $date = date('Y-m-d', strtotime('+ 1 ' . $peroid_code, strtotime($date)));
     }
     // 最终数据
     $category = [];
     $type_config = [1 => '提交签约人数', 2 => '签约成功人数', 3 => '申请贷款金额', 4 => '通过贷款金额', 6 => '实际放款金额', 5 => '服务费收入'];
     $where_stat = ['target_date' => [['egt', $start_date], ['elt', $end_date]]];
     if ($city > 0) {
         // 判断是省还是区
         $where_stat['university_id'] = ['exp', ' in (select university_id from zj_university_campus where ' . ($is_city_id ? 'city_id' : 'province_id') . '=' . $city . ')'];
     }
     if ($university_id >= 0) {
         $where_stat['university_id'] = $university_id;
     }
     $model = M('loan_stat');
     $list = [];
     function parse_number($type, $number)
     {
         return in_array($type, [3, 4, 5, 6]) ? $number : intval($number);
     }
     $statModel = new \Loan\Model\StatModel();
     if ($period == 'day') {
         foreach ($type_config as $type => $type_title) {
             $where_stat['type'] = $type;
             $data = $empty_data;
             $data_stat = $model->where($where_stat)->group('target_date')->getField('target_date,sum(number) number');
             foreach ($data_stat as $day => $number) {
                 $data[$day] = parse_number($type, $number);
             }
             $list[$type_title] = array_values($data);
         }
     } elseif ($period == 'month') {
         foreach ($type_config as $type => $type_title) {
             $where_stat['type'] = $type;
             $data = $empty_data;
             $stat_data = $model->where($where_stat)->group('target_date')->getField('target_date,sum(number) number');
             foreach ($stat_data as $day => $number) {
                 $month = substr($day, 0, 7);
                 $data[$month] += parse_number($type, $number);
             }
             $list[$type_title] = array_values($data);
         }
     } elseif ($period == 'total') {
         $xtitle = [$start_date];
         foreach ($type_config as $type => $type_title) {
             $where_stat['type'] = $type;
             $list[$type_title] = [parse_number($type, $model->where($where_stat)->sum('number'))];
         }
     }
     $this->assign('start', $start_date);
     $this->assign('end', $end_date);
     $this->assign('legend', array_keys($list));
     $this->assign('xtitle', $xtitle);
     $this->assign('list', $list);
     $this->display();
 }
 /**
  * Format currency.
  *
  * Formats a number into a US currency string.
  *
  * Note: This function make no attempt at internationalization.
  *
  * @param mixed $value The value to format.
  *
  * @return string [string]           The formatted currency.
  */
 function format_currency($value)
 {
     // Note: there is a money_format() function. I like this brute-force method better.
     return '$' . number_format(parse_number($value), 2, '.', ',');
 }
function match_to_sql($match, $table_def, $type="exact") {
  $not="";
  $same="false";

  switch($match[0]) {
    case "or":
      if(sizeof($match)==1)
	return "true";

      $ret=array();
      for($i=1; $i<sizeof($match); $i++) {
	$ret[]=match_to_sql($match[$i], $table_def, $type);
      }

      return "(".implode(") or (", $ret).")";
    case "and":
      if(sizeof($match)==1)
	return "true";

      $ret=array();
      for($i=1; $i<sizeof($match); $i++) {
	$ret[]=match_to_sql($match[$i], $table_def, $type);
      }

      return "(".implode(") and (", $ret).")";
    case "not":
      return "not ".match_to_sql($match[1], $table_def, $type);
    case "is not":
      $not="not";
    case "is":
      switch($type) {
	case "index":
	  $ret=array();
	  for($i=2; $i<sizeof($match); $i++) {
	    $ret[]="osm_tags @> ".array_to_hstore(array($match[1]=>$match[$i]));
	  }

	  if($not)
	    $ret[]="osm_tags ? ".postgre_escape($match[1]);

	  return "$not (".implode(") or (", $ret).")";
	default:
	  $ret=array();
	  for($i=2; $i<sizeof($match); $i++) {
	    $ret[]=postgre_escape($match[$i]);
	  }

	  if($not)
	    $not="not osm_tags ? ".postgre_escape($match[1])." or not";

	  return "($not coalesce(osm_tags->".postgre_escape($match[1]).", '') in (".implode(", ", $ret)."))";
	}
    case "~is not":
      $not="not";
    case "~is":
      switch($type) {
	case "index":
	  $ret=array();
	  for($i=2; $i<sizeof($match); $i++) {
	    $ret[]=postgre_escape($match[$i]);
	  }

	  register_index($table_def['table'], $match[1], "tsvector", 
	                 $table_def['id']);
	  return "$not to_tsvector('simple', ".match_to_sql_colname($match[1], $table_def, $type).") @@ to_tsquery('simple', ".implode("||' | '||", $ret).")";
	default:
	  $ret=array();
	  for($i=2; $i<sizeof($match); $i++) {
	    $ret[]=postgre_escape($match[$i]);
	  }

	  return "$not oneof_in(".match_to_sql_colname($match[1], $table_def, $type).", ARRAY[".implode(", ", $ret)."])";
	}
    case "exist":
      return "osm_tags ? ".postgre_escape($match[1]);
    case "exist not":
      return "not osm_tags ? ".postgre_escape($match[1]);
    case ">=":
      $same="true";
    case ">":
      $number=parse_number($match[2]);

      if($type=="index") {
	// for index-search we make an index every 100 
	// units and change the select-statement accordingly
	$same="true";
	$number=pow(100, floor(log($number, 100)+0.000001));
	register_index($table_def['table'], $match[1], "gteq", 
		       $table_def['id'], $number);
	$var="split_semicolon(".match_to_sql_colname($match[1], $table_def, $type).")";
      }
      else {
	$var=match_to_sql_colname($match[1], $table_def, $type);
      }

      return "oneof_between($var, $number, $same, null, null)";
    case "<=":
      $same="true";
    case "<":
      $number=parse_number($match[2]);

      if($type=="index") {
	$same="true";
	$number=pow(100, ceil(log($number, 100)));
	register_index($table_def['table'], $match[1], "lteq", 
		       $table_def['id'], $number);
	$var="split_semicolon(".match_to_sql_colname($match[1], $table_def, $type).")";
      }
      else {
	$var=match_to_sql_colname($match[1], $table_def, $type);
      }

      return "oneof_between($var, null, null, $number, $same)";
    case "true":
      return "true";
    case "false":
      return "false";
    default:
      print "invalid match! "; print_r($match);
      return "true";
  }
}