<?php

Doo::loadModel('sign');
Doo::loadModel('signatt');
Doo::loadModel('signaudit');
Doo::loadModel('signauditatt');

class Signn
{

    private $_sign,$_signatt,$_signaudit,$_signauditatt;

    function __construct()
    {
        $this->_sign = new Sign();
        $this->_signatt = new SignAtt();
        $this->_signaudit = new SignAudit();
        $this->_signauditatt = new SignAuditAtt();
    }

    public function getNeedSignNumbyUid($uid){
        return $this->_signaudit->count(array('where' => 'audituid=? and status="checking"', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getMySignNumbyUid($uid,$sql =' 1'){
        return $this->_sign->count(array('where' => $sql.' and audituid=?', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getUnSignNum($sql,$uid){
        return $this->_sign->count(array('where' => $sql.' and status="uncheck" AND (`isinter`=0 OR `isinter`=3) AND (`ownuid`='.$uid.' OR tender IN (SELECT pmid FROM jl_measure_audit WHERE auditoruid='.$uid.' GROUP BY auditoruid) OR project IN (SELECT pid FROM jl_project_measure WHERE uid='.$uid.'))', 'asArray' => TRUE));
    }
    public function getDoingSignNumbyUid($uid, $sql = ' 1'){
        return $this->_sign->count(array('where' => $sql.' and (status="checking" or status="back") and sid in(select `jl_sign_audit`.sid from `jl_sign_audit` where `jl_sign_audit`.audituid=?)', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getDoneSignNumbyUid($uid, $sql = ' 1'){
        return $this->_sign->count(array('where' => $sql.' and status="checked" and sid in(select `jl_sign_audit`.sid from `jl_sign_audit` where `jl_sign_audit`.audituid=?)', 'param' => array($uid), 'asArray' => TRUE));
    }

//    public function getSignNum($sql,$uid){
//        return $this->_sign->count(array('where' => $sql.' and (`audituid`='.$uid.' OR sid IN (SELECT sid FROM jl_sign_audit WHERE jl_sign_audit.audituid='.$uid.'))','asArray' => TRUE));
//    }

    public function getSignNum($sql){
        return $this->_sign->count(array('where' => $sql,'asArray' => TRUE));
    }

    public function getNeedSignbyUid($uid){
        return $this->_signaudit->find(array('where' => 'audituid=? and status="checking"', 'param' => array($uid), 'desc' => 'satid', 'asArray' => TRUE));
    }

    public function getOneSignbysid($sid){
        return $this->_sign->getOne(array('where' => 'sid=?', 'param' => array($sid), 'asArray' => TRUE));
    }

    public function getSignAuditList($sid){
        return $this->_signaudit->find(array('where' => 'sid=?', 'param' => array($sid), 'asArray' => TRUE));
    }

    public function getisSignbySidUid($sid,$uid){
        return $this->_signaudit->getOne(array('where' => 'sid=? and audituid=? and status="checking"', 'param' => array($sid,$uid), 'asArray' => TRUE));
    }

    public function getSignAttList($sid){
        return $this->_signatt->find(array('where' => 'sid=?', 'param' => array($sid), 'asArray' => TRUE));
    }

    public function getSignAuditAttList($said){
        return $this->_signauditatt->find(array('where' => 'said=?', 'param' => array($said), 'asArray' => TRUE));
    }

    public function getOneSignAudit($sid,$uid){
        return $this->_signaudit->getOne(array('where' => 'sid=? and audituid=?', 'param' => array($sid,$uid), 'asArray' => TRUE));
    }

    public function updateSignAudit($satid,$status,$time = 0){
        $this->_signaudit->satid = $satid;
        $this->_signaudit->status = $status;
        if($status == 'checked'){
            $this->_signaudit->intime = time();
        }else{
            $this->_signaudit->intime = $time;
        }
        return $this->_signaudit->update();
    }

    public function insertSignAuditAtt($uid,$said,$path,$position){
        $this->_signauditatt->audituid = filter_var($uid, FILTER_VALIDATE_INT);
        $this->_signauditatt->said = filter_var($said, FILTER_VALIDATE_INT);
        $this->_signauditatt->picpath = filter_var($path, FILTER_SANITIZE_STRING);
        $this->_signauditatt->position = filter_var($position, FILTER_SANITIZE_STRING);
        return $this->_signauditatt->insert();
    }

    public function updateSignStatus($sid){
        $this->_sign->sid = $sid;
        $this->_sign->status = 'checked';
        $this->_sign->sintime = time();
        return $this->_sign->update();
    }

    public function updateSignMsg($sid,$uid){
        $this->_sign->sid = $sid;
        $this->_sign->status = 'checking';
        $this->_sign->audituid = $uid;
        $this->_sign->aintime = time();
        return $this->_sign->update();
    }

    public function getDoneSignListbyUid($uid, $sql = ' 1', $limit){
        return $this->_sign->find(array('where' => $sql.' and status="checked" and sid in(select `jl_sign_audit`.sid from `jl_sign_audit` where `jl_sign_audit`.audituid=?)', 'param' => array($uid), 'desc' => 'sid', 'limit' => $limit, 'asArray' => TRUE));
    }

    public function getDoingSignListbyUid($uid,$sql = ' 1', $limit){
        return $this->_sign->find(array('where' => $sql.' and (status="checking" or status="back") and sid in(select `jl_sign_audit`.sid from `jl_sign_audit` where `jl_sign_audit`.audituid=?)', 'param' => array($uid), 'desc' => 'sid', 'limit' => $limit, 'asArray' => TRUE));
    }

    public function getMySignListbyUid($uid,$sql = ' 1',$limit){
        return $this->_sign->find(array('where' => $sql.' and audituid=?', 'param' => array($uid), 'desc' => 'sid', 'limit' => $limit, 'asArray' => TRUE));
    }

    public function updateSignShareStatus($status,$sid){
        $this->_sign->sid = $sid;
        $this->_sign->ishare = $status;
        return $this->_sign->update();
    }

    public function getUnSignList($sql, $uid, $limit){
        return $this->_sign->find(array('where' => $sql.' and status="uncheck" AND (`isinter`=0 OR `isinter`=3) AND (`ownuid`='.$uid.' OR tender IN (SELECT pmid FROM jl_measure_audit WHERE auditoruid='.$uid.' GROUP BY auditoruid) OR project IN (SELECT pid FROM jl_project_measure WHERE uid='.$uid.'))', 'desc' => 'sid', 'limit' => $limit, 'asArray' => TRUE));
    }

    public function getSignList($sql, $limit){
        return $this->_sign->find(array('where' => $sql, 'desc' => 'sid', 'limit' => $limit, 'asArray' => TRUE));
    }

    public function insertSign($pid, $tid, $phaseno, $name, $ownuid,$intime,$widhei,$isinter = 0, $internum = 0){
        $this->_sign->project = $pid;
        $this->_sign->tender = $tid;
        $this->_sign->phaseno = $phaseno;
        $this->_sign->name = $name;
        $this->_sign->ownuid = $ownuid;
        $this->_sign->intime = $intime;
        $this->_sign->widhei = $widhei;
        $this->_sign->isinter = $isinter;
        $this->_sign->internum = $internum;
        return $this->_sign->insert();
    }

    public function insertSignAtt($sid, $num, $path){
        $this->_signatt->sid = $sid;
        $this->_signatt->curnum = $num;
        $this->_signatt->filepath = $path;
        return $this->_signatt->insert();
    }

    public function updateSignPageNum($sid, $pagenum){
        $this->_sign->sid = $sid;
        $this->_sign->pagenum = $pagenum;
        return $this->_sign->update();
    }

    public function insertSignAudit($sid,$status,$uid,$name){
        $this->_signaudit->sid = $sid;
        $this->_signaudit->status = $status;
        $this->_signaudit->audituid = $uid;
        $this->_signaudit->aname = $name;
        return $this->_signaudit->insert();
    }

    public function getSignListbyClient($sql){
        return $this->_sign->find(array('where' => $sql, 'asArray' => TRUE));
    }

    public function getSignbyName($tender,$phaseno,$name){
        return $this->_sign->getOne(array('where' => 'tender=? and phaseno=? and name=?', 'param' => array($tender,$phaseno,$name), 'asArray' => TRUE));
    }

    public function getSignGroupByProject($sql = '1'){
        return $this->_sign->find(array('select' => 'project', 'where' => $sql, 'groupby' => 'project', 'asArray' => TRUE));
    }

    public function getSignGroupByTender($pid,$sql = ' 1'){
        return $this->_sign->find(array('select' => 'tender', 'where' => $sql.' and project=?', 'param' => array($pid), 'groupby' => 'tender', 'asArray' => TRUE));
    }

    public function getSignGroupByPhaseno($pmid,$sql = ' 1'){
        return $this->_sign->find(array('select' => 'phaseno', 'where' => $sql.' and tender=?', 'param' => array($pmid), 'groupby' => 'phaseno', 'asArray' => TRUE));
    }

    public function delSignbysid($sid,$status){
        if($status == 'checking'){

            $this->_signaudit->sid = $sid;
            $this->_signaudit->delete();

            $attlist = $this->getSignAttList($sid);
            foreach ($attlist as $key => $value) {
                $this->_signauditatt->said = $value['said'];
                $this->_signauditatt->delete();
            }
        }

        $this->_sign->sid = $sid;
        $this->_sign->delete();

        $this->_signatt->sid = $sid;
        $this->_signatt->delete();
    }

    public function updateInterMediateStatus($sid,$status){
        $this->_sign->sid= $sid;
        $this->_sign->isinter = $status;
        return $this->_sign->update();
    }

    public function getOneSignAtt($sid,$num){
        return  $this->_signatt->getOne(array('where' => 'sid=? and curnum=?', 'param' => array($sid,$num), 'asArray' => TRUE));
    }

    public function updateSignAtt($sid,$curnum,$path){
        $signattmsg = $this->_signatt->getOne(array('where' => 'sid=? and curnum=?', 'param' => array($sid,$curnum), 'asArray' => TRUE));
        if(!empty($signattmsg)){
            $this->_signatt->said = $signattmsg['said'];
            $this->_signatt->filepath = $path;
            return $this->_signatt->update();
        }else{
            return false;
        }
    }

    public function updateSignStatusAndTime($sid,$status,$time = ''){
        $this->_sign->sid = $sid;
        $this->_sign->status = $status;
        $this->_sign->sintime = $time;
        return $this->_sign->update();
    }

}
