westdc-zf1/application/models/Heihe.php

721 lines
18 KiB
PHP

<?php
/**
* Heihe 黑河项目通用类
*/
//namespace Heihe;
class Heihe
{
private $db;//传入PDO对象
//使用到的公共变量
public $tbl_heiheproject = "heiheproject" ; //黑河项目表
public $tbl_heiheuser = "heiheuser"; //黑河用户表
public $project_status; //汇交项目计划状态
function __construct($db)
{
$this->db = $db;
$this->project_status = array(
0 => "计划未提交",
1 => "计划未审核",
2 => "跟踪专家审核",
3 => "跟踪专家通过",
4 => "数据委员会通过"
);
}
//获取所有计划
public function fetch()
{
$sql = "SELECT * FROM {$this->tbl_heiheproject}
ORDER BY id ASC";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
return $rows;
}
//计划内的yuan数据
public function metadata($keyword = "")
{
if(empty($keyword))
$wheresql = "";
else{
$wheresql = [];
$keyword = trim($keyword);
$keyword = str_replace("'","''",$keyword);
$wheresql[] = " md.title LIKE '%$keyword%' ";
$wheresql[] = " md.title_en LIKE '%$keyword%'";
$wheresql[] = " md.description LIKE '%$keyword%' ";
$wheresql = " WHERE " . join(" OR ",$wheresql);
}
$sql = "SELECT md.*,s.viewed,g.id as gid,gen.id as genid,st.status as mdstatus,ds.id as datasetid,fund.title as heihe_fund_title FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
LEFT JOIN mdstat s ON md.uuid=s.uuid
LEFT JOIN geonetworkmetadata g ON g.uuid=md.uuid
LEFT JOIN mdstatus st ON md.uuid=st.uuid
LEFT JOIN dataset ds ON md.uuid=ds.uuid
LEFT JOIN en.geonetworkmetadata gen on gen.uuid=md.uuid
$wheresql
ORDER BY md.id";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
//获取汇交状态
public function getStatus($code){
$status = $this->project_status;
if(isset($status[$code]))
{
return $status[$code];
}
}
//改变状态
public function ChangeStatus($p,$s){
$sql = "UPDATE ".$this->tbl_heiheproject." SET status=? WHERE id=?";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($s,$p));
return $rs;
}
//专家激活
public function expertActive($pid,$code,$email)
{
if(empty($pid))
{
return 1;
}
if(!is_numeric($pid))
{
return 1;
}
if(!preg_match("/[A-Za-z0-9]/",$code))
{
return 1;
}
if(strlen($code) != 12)
{
return 1;
}
$validation = $this->makeValidation($email);
if($code != $validation)
{
return 5;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
return 10;
}
$matched = false;
$match_expert = "";
foreach($experts as $k=>$v)
{
if($v['validation'] == $code)
{
$matched = true;
$match_expert = $v;
unset($experts[$k]);
break;
}
}
if($matched != true)
{
return 15;
}
if(isset($match_expert['activated']))
{
if(strtotime($match_expert['activated']) < time())
{
return 20;
}
}
$match_expert['activated'] = date("Y-m-d H:i:s",time());
$experts[] = $match_expert;
$experts = json_encode($experts);
$this->expertUpdate($pid,$experts);
return 200;
}
public function expertActiveError($level){
$errors = array(
1 => "参数错误",
5 => "您的验证码不正确",
10 => "此项目尚未邀请任何专家",
15 => "您没有被邀请,请联系数据中心进行核实",
20 => "您的邀请已经激活",
50 => "服务器错误,请刷新页面重试",
200 => "激活成功",
);
if(isset($errors[$level]))
return $errors[$level];
else
return "发生意外错误,请重试";
}
//邀请一个专家
/*
$data = array(
"name"=>'', //专家姓名
"email"=>'', //专家email
"created"=>'', //邀请时间
"validation"=>'', //验证码
"activated"=>'' //激活时间
)
*/
public function expertAdd($pid,$data){
if(!is_array($data))
{
return 0;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
$experts = array($data);
}else{
foreach($experts as $k=>$v)
{
if($v['email'] == $data['email'])
{
return -1;
}
}
$experts[] = $data;
}
$experts = json_encode($experts,JSON_NUMERIC_CHECK);
return $this->expertUpdate($pid,$experts);
}
//移除一个专家
public function expertRemove($pid,$email){
if(empty($pid) || empty($email))
{
return 0;
}
$experts = $this->expertRead($pid);
if(!is_array($experts))
{
return -1;
}else{
foreach($experts as $k=>$v)
{
if(isset($v['email']) && $v['email'] == $email)
{
unset($experts[$k]);
}
}
}
$experts = json_encode($experts);
return $this->expertUpdate($pid,$experts);
}
//更新专家
public function expertUpdate($pid,$obj){
$sql = "UPDATE ".$this->tbl_heiheproject." SET expert='$obj' WHERE id=$pid";
$rs = $this->db->exec($sql);
return $rs;
}
//读取已有的专家
public function expertRead($pid){
if(!is_numeric($pid))
{
return NULL;
}
$sql = "SELECT expert FROM ".$this->tbl_heiheproject." WHERE id=$pid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
$expert = $row['expert'];
if(empty($expert))
{
return NULL;
}
$expert = json_decode($expert,true);
if(count($expert)<1)
{
return 0;
}else{
return $expert;
}
}
//邀请码
public function makeValidation($email){
return substr(md5($email),5,12);
}
//生成邀请连接
public function makeInviteLink($pid,$code)
{
$http_base = "http://".$_SERVER ['HTTP_HOST'];
$url = $http_base."/heihe/projects/invite/".$code.'/pid/'.$pid;
return $url;
}//生成邀请连接
//从pgsql读取数组并拆分为php数组
public function getArray($str){
if(strlen($str)>3)
{
return explode(",",substr($str,1,-1));
}else{
return NULL;
}
}
//将php数组组装成pgsql中的数组
public function mkArray($array){
if(!is_array($array))
{
return "{".$array."}";
}
if(count($array)==1)
{
$key = max(array_keys($array));
return "{".$array[$key]."}";
}
if(count($array)>1)
{
return "{".join(",",$array)."}";
}
}
//从数组中获得键名
public function getKey($array,$needle)
{
foreach($array as $k=>$v)
{
if($v == $needle)
{
return $k;
}
}
}
}
class Status{
public $fund_id;
function __construct()
{
$this->db = \Zend_Registry::get('db');
}
public function makeConditionFound($field)
{
if(empty($this->fund_id))
{
return null;
}
$str = join("','",$this->fund_id);
return "$field IN ('". $str . "')";
}
public function makeWhereSql($array)
{
if(is_array($array) && count($array) > 0)
return " WHERE " . join(" AND ",$array);
else
return "";
}
//项目总数
public function projectsCount()
{
$fund = $this->makeConditionFound("hh.code");
if(!empty( $fund ))
{
$wheresql = $this->makeWhereSql(array($fund));
}else{
$wheresql = "";
}
$sql = "SELECT count(hh.code) as c FROM heiheproject hh $wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//数据条数
public function dataCount()
{
$arr = [
"md.uuid IS NOT NULL",
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT count(md.uuid) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//数据量
public function dataSize()
{
$arr = [
"md.uuid IS NOT NULL"
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT SUM(md.filesize) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//文件数
public function fileCount()
{
$arr = [
"md.uuid IS NOT NULL",
"df.filename !~ E'[/|\]+$'"
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT count(df.id) FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataset ds ON ds.uuid = md.uuid
RIGHT JOIN datafile df ON ds.id = df.dsid
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//数据申请数量
public function dataApplyTimes()
{
$arr = [
"md.uuid IS NOT NULL",
"(o.offlineappid <> -1 OR o.onlineappid <> -1)"
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT count(distinct o.id) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataorder o ON o.uuid = md.uuid
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//服务人次
public function applyTimes()
{
$arr = [
"md.uuid IS NOT NULL",
"(o.offlineappid <> -1 OR o.onlineappid <> -1)"
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT count(distinct o.offlineappid) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataorder o ON o.uuid = md.uuid
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//服务人次(用户唯一)
public function applyTimesDistinct()
{
$arr = [
"md.uuid IS NOT NULL",
"(o.offlineappid <> -1 OR o.onlineappid <> -1)"
];
$fund = $this->makeConditionFound("fund.fund_id");
if(!empty($fund))
$arr[] = $fund;
$wheresql = $this->makeWhereSql($arr);
$sql = "SELECT count(distinct o.userid) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataorder o ON o.uuid = md.uuid
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//每个用户数据申请量
public function downloadTimesByData()
{
$sql = "SELECT o.userid,count(o.id) FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataorder o ON o.uuid = md.uuid
WHERE md.uuid IS NOT NULL AND (o.offlineappid <> -1 OR o.onlineappid <> -1)
GROUP BY o.userid";
$rs = $this->db->query($sql);
return $rs->fetchAll;
}
//详细的用户申请次数统计
public function downloadTimesByUser()
{
$sql = "SELECT u.id,u.username, u.realname ,count(o.id) as c FROM metadata md
LEFT JOIN mdfund mf ON mf.uuid = md.uuid
RIGHT JOIN fund ON fund.id = mf.fid
RIGHT JOIN heiheproject hh ON hh.code = fund.fund_id
RIGHT JOIN dataorder o ON o.uuid = md.uuid
LEFT JOIN users u ON u.id = o.userid
WHERE md.uuid IS NOT NULL AND (o.offlineappid <> -1 OR o.onlineappid <> -1)
GROUP BY u.id,u.username, u.realname";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//为黑河计划项目服务的情况
public function serviceTimes($type = "offline")
{
if($type == 'offline')
{
$arr = ['ts_approved IS NOT NULL'];
}else{
$arr = [];
}
$fund = $this->makeConditionFound("project_id");
if(!empty($fund))
{
$arr[] = $fund;
}else{
$select = "SELECT DISTINCT code FROM heiheproject";
$rs = $this->db->query($select);
$code_statck = [];
while($row = $rs->fetch())
{
$code_statck[] = $row['code'];
}
$arr[] = " project_id IN ('".join("','" , $code_statck)."')";
}
$wheresql = $this->makeWhereSql($arr);
if($type == 'offline')
$sql = "SELECT count(id) FROM offlineapp $wheresql";
else
$sql = "SELECT count(id) FROM onlineapp $wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//服务数据大小
public function serviceFilesize($type = "offline")
{
if($type == 'offline')
{
$arr = ['off.ts_approved IS NOT NULL'];
}else{
$arr = [];
}
if($type == 'offline')
$fund = $this->makeConditionFound("off.project_id");
else
$fund = $this->makeConditionFound("ol.project_id");
if(!empty($fund))
{
$arr[] = $fund;
}else{
$select = "SELECT DISTINCT code FROM heiheproject";
$rs = $this->db->query($select);
$code_statck = [];
while($row = $rs->fetch())
{
$code_statck[] = $row['code'];
}
if($type == 'offline')
$arr[] = " off.project_id IN ('".join("','" , $code_statck)."')";
else
$arr[] = " ol.project_id IN ('".join("','" , $code_statck)."')";
}
$wheresql = $this->makeWhereSql($arr);
if($type == 'offline')
$sql = "SELECT SUM(md.filesize) /1024 as size FROM metadata md
LEFT JOIN dataorder o ON o.uuid=md.uuid
LEFT JOIN offlineapp off ON off.id = o.offlineappid
$wheresql";
else
$sql = "SELECT SUM(md.filesize) /1024 as size FROM metadata md
LEFT JOIN dataorder o ON o.uuid=md.uuid
LEFT JOIN onlineapp as ol ON ol.id = o.onlineappid
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
//服务计划内的项目个数
public function serviceForProjects($type = "offline")
{
if($type == 'offline')
{
$arr = ['ts_approved IS NOT NULL'];
}else{
$arr = [];
}
$fund = $this->makeConditionFound("project_id");
if(!empty($fund))
{
$arr[] = $fund;
}else{
$select = "SELECT DISTINCT code FROM heiheproject";
$rs = $this->db->query($select);
$code_statck = [];
while($row = $rs->fetch())
{
$code_statck[] = $row['code'];
}
$arr[] = " project_id IN ('".join("','" , $code_statck)."')";
}
$wheresql = $this->makeWhereSql($arr);
if($type == 'offline')
$sql = "SELECT count(DISTINCT project_id) as c FROM offlineapp
$wheresql";
elseif($type == "online")
$sql = "SELECT count(DISTINCT project_id) as c FROM onlineapp
$wheresql";
else
$sql = "SELECT count(DISTINCT project_id) FROM
(SELECT DISTINCT off.project_id FROM offlineapp off UNION ALL SELECT DISTINCT ol.project_id FROM onlineapp ol) apply
$wheresql";
$rs = $this->db->query($sql);
return $rs->fetchColumn(0);
}
}