<?php

Doo::loadModel('measureaudit');

/**
 * Description of Users
 *
 * @author zongheng
 */
class MeasureauditAct
{

    private $__measureaudit;

    function __construct()
    {
        $this->__measureaudit = new MeasureAudit();
    }

    public function insertMeasureAudit($pid, $creatoruid, $auditoruid, $mpid, $pmid, $stid, $numpname, $status = 'uncheck', $last = 2, $times = 0)
    {
        if (!isset($pid))
            return FALSE;
        $this->__measureaudit->pid = filter_var($pid, FILTER_VALIDATE_INT);
        $this->__measureaudit->creatoruid = filter_var($creatoruid, FILTER_VALIDATE_INT);
        $this->__measureaudit->auditoruid = filter_var($auditoruid, FILTER_VALIDATE_INT);
        $this->__measureaudit->mpid = filter_var($mpid, FILTER_VALIDATE_INT);
        $this->__measureaudit->pmid = filter_var($pmid, FILTER_VALIDATE_INT);
        $this->__measureaudit->stid = filter_var($stid, FILTER_VALIDATE_INT);
        $this->__measureaudit->numpname = filter_var($numpname, FILTER_VALIDATE_INT);
        if ($times > 0)
            $this->__measureaudit->times = filter_var($times, FILTER_VALIDATE_INT);
        $this->__measureaudit->mastatus = $status;
        $this->__measureaudit->last = $last;
        $this->__measureaudit->audittime = time();
        return $this->__measureaudit->insert();
    }

    public function getUncheckRow($uid, $status)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=? and mastatus=?', 'param' => array($uid, $status), 'asArray' => TRUE));
    }

    public function getAllRow($uid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getAuditProject($uid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=? and last=2', 'groupby' => 'pid', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getAuditProject2($uid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'groupby' => 'pid', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getProject($uid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'groupby' => 'pmid,numpname', 'param' => array($uid), 'asArray' => TRUE));
    }

    public function getUncheckMaxMpid($uid, $pid, $status)
    {
        return $this->__measureaudit->getOne(array('select' => 'max(mpid) as mpid', 'where' => 'auditoruid=? and pid=? and mastatus=?', 'param' => array($uid, $pid, $status), 'asArray' => TRUE));
    }

    public function setStatusTrue($mpid)
    {
        $this->__measureaudit->mastatus = 'checked';
        return $this->__measureaudit->update(array('where' => 'mpid=?', 'param' => array($mpid)));
    }

    public function setStatusFalse($mpid)
    {
        $this->__measureaudit->mastatus = 'checkno';
        return $this->__measureaudit->update(array('where' => 'mpid=?', 'param' => array($mpid)));
    }

    public function getStatusByMpid($mpid)
    {
        return $this->__measureaudit->getOne(array('select' => 'mastatus', 'where' => 'mpid=?', 'param' => array($mpid), 'asArray' => TRUE));
    }

    public function getRowByMpid($mpid)
    {
        return $this->__measureaudit->find(array('where' => 'mpid=?', 'param' => array($mpid), 'asArray' => TRUE));
    }

    public function getRowByMpid2($mpid)
    {
        return $this->__measureaudit->find(array('where' => 'mpid=? order by last desc,maid asc', 'param' => array($mpid), 'asArray' => TRUE));
    }

    public function getRowByMpidTimes($mpid,$numpname,$times)
    {
        return $this->__measureaudit->find(array('where' => 'mpid=? and numpname=? and times=? order by last desc,maid asc', 'param' => array($mpid,$numpname,$times), 'asArray' => TRUE));
    }

    public function getRowGroupByTimes($mpid)
    {
        return $this->__measureaudit->getOne(array('where' => 'mpid=?', 'groupby' => 'times', 'desc' => 'maid', 'param' => array($mpid), 'asArray' => TRUE));
    }

// for api
    public function getAlluserMeasureAudit($bid, $num)
    {
        $maxtimesArray = $this->getMaxTimes($bid, $num);
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? and times=? order by last desc,maid asc', 'param' => array($bid, $num, $maxtimesArray['maxtimes']), 'asArray' => TRUE));
    }

// for api
    public function getAllAudit($bid, $num, $times)
    {
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? and times=? order by last desc,maid asc', 'param' => array($bid, $num, $times), 'asArray' => TRUE));
    }

    public function getUserAudit($bid, $num, $times = 0)
    {
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? and times=? order by maid asc', 'param' => array($bid, $num, $times), 'asArray' => TRUE));
    }

    public function getUserAuditLast($bid, $num)
    {
        $maxtimesArray = $this->getMaxTimes($bid, $num);
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? and times=? order by maid asc', 'param' => array($bid, $num, $maxtimesArray['maxtimes']), 'asArray' => TRUE));
    }

    public function getRowbyUID($audituid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'param' => array($audituid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID($audituid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=? and ((mastatus=\'uncheck\' or mastatus=\'checking\') or (mastatus=\'checked\' and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= audittime))', 'param' => array($audituid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID2($audituid)
    {
        return $this->__measureaudit->find(array('where' => '(mastatus=\'checking\') and auditoruid=?', 'groupby' => 'pmid,numpname', 'param' => array($audituid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID3($audituid)
    {
        return $this->__measureaudit->find(array('where' => '(mastatus=\'checked\' or mastatus=\'checkno\') and auditoruid=?', 'groupby' => 'pid', 'desc' => 'audittime','param' => array($audituid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID4($audituid,$pid)
    {
        return $this->__measureaudit->find(array('where' => '(mastatus=\'checked\' or mastatus=\'checkno\') and auditoruid=? and pid=?', 'groupby' => 'stid', 'desc' => 'audittime','param' => array($audituid,$pid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID5($audituid,$pid,$stid)
    {
        return $this->__measureaudit->find(array('where' => '(mastatus=\'checked\' or mastatus=\'checkno\') and auditoruid=? and pid=? and stid=?', 'groupby' => 'pmid', 'desc' => 'audittime','param' => array($audituid,$pid,$stid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID6($audituid)
    {
        return $this->__measureaudit->find(array('select' => 'pid', 'where' => '(mastatus=\'checking\') and auditoruid=?', 'groupby' => 'pid', 'param' => array($audituid), 'asArray' => TRUE));
    }

    public function getCheckedRowbyUID7($audituid,$pid)
    {
        return $this->__measureaudit->find(array('where' => '(mastatus=\'checking\') and auditoruid=? and pid=?', 'groupby' => 'pmid,numpname', 'param' => array($audituid,$pid), 'asArray' => TRUE));
    }

    public function getMyRecord($audituid)
    {
        Doo::db()->connect();
        $reArray = Doo::db()->fetchAll('select a.* from jl_measure_audit a inner join (select pmid ,numpname, max(times) times from jl_measure_audit group by pmid,numpname) b on a.pmid = b.pmid and a.numpname = b.numpname and a.times = b.times   and (mastatus=\'uncheck\' or mastatus=\'checking\') and auditoruid=? order by a.pmid', array($audituid));
        Doo::db()->disconnect();
        return $reArray;
    }

    public function getMyRecord2($audituid)
    {
        $resArray = Doo::db()->fetchAll('select a.* from jl_measure_audit a inner join (select pmid ,numpname, max(times) times from jl_measure_audit group by pmid,numpname) b on a.pmid = b.pmid and a.numpname = b.numpname and a.times = b.times  and auditoruid=? order by a.pmid', array($audituid));
        Doo::db()->disconnect();
        return $resArray;
    }

    public function getMyAllRecord($audituid)
    {
        return Doo::db()->query('select a.* from jl_measure_audit a inner join (select pmid ,numpname, max(times) times from jl_measure_audit group by pmid,numpname) b on a.pmid = b.pmid and a.numpname = b.numpname and a.times = b.times   and auditoruid=? order by a.pmid', array($audituid));
    }

    public function getUpUIDStatus($pmid, $numpname, $creatoruid)
    {
        return $this->__measureaudit->getOne(array('where' => 'pmid=? AND numpname=? AND auditoruid=?', 'param' => array($pmid, $numpname, $creatoruid), 'asArray' => TRUE));
    }

    public function getMyAuditStatus($pmid, $numpname, $times, $uid)
    {
        return $this->__measureaudit->getOne(array('where' => 'pmid=? AND numpname=? AND times=? AND auditoruid=?', 'param' => array($pmid, $numpname, $times, $uid), 'asArray' => TRUE));
    }

    public function getMyAuditStatus2($pmid, $numpname, $times, $uid)
    {
        return $this->__measureaudit->getOne(array('where' => 'pmid=? AND numpname=? AND times=? AND auditoruid=?', 'param' => array($pmid, $numpname, $times, $uid), 'asc' => 'maid', 'asArray' => TRUE));
    }


    public function getFirstAuditor($pmid, $numpname, $times = 0)
    {
        return $this->__measureaudit->getOne(array('where' => 'maid =(select min(maid) from jl_measure_audit where `pmid`=? and `numpname` =? and times=? and last=2)', 'param' => array($pmid, $numpname, $times), 'asArray' => TRUE));
    }

    public function updateMastatus($maid)
    {
        $this->__measureaudit->mastatus = 'checking';
//        $this->__measureaudit->auditcontent = '';
//        $this->__measureaudit->audittime = '';
        return $this->__measureaudit->update(array('where' => 'maid=?', 'param' => array($maid)));
    }

    // for api
    public function updateAuditorMPID($mpid, $tenderid, $phaseno, $times = 0)
    {
        $this->__measureaudit->mpid = $mpid;
        return $this->__measureaudit->update(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($tenderid, $phaseno, $times)));
    }

    public function setCheckno($maid, $auditcontent = NULL, $isonline = 0)
    {
        $this->__measureaudit->mastatus = 'checkno';
        $this->__measureaudit->audittime = time();
        $this->__measureaudit->onlineaudit = $isonline;
        if (isset($auditcontent) && $auditcontent)
            $this->__measureaudit->auditcontent = $auditcontent;
        return $this->__measureaudit->update(array('where' => 'maid=?', 'param' => array($maid)));
    }

    public function setStatusTo($maid, $status = 3, $auditcontent = null, $isonline = 0)
    {
        $ma1 = new MeasureAudit();
        if ($status == 3) {
            $ma1->mastatus = 'checked';
        }
        if ($status == 4) {
            $ma1->mastatus = 'checkno';
        }
        if (isset($auditcontent) && $auditcontent) {
            $ma1->auditcontent = $auditcontent;
        }
        $ma1->audittime = gettimeofday()['sec'];
        $ma1->onlineaudit = $isonline;
        return $ma1->update(array('where' => 'maid=?', 'param' => array($maid)));
    }

    public function setMyAuditPass($maid, $auditcontent = null, $isonline = 0, $uid = 0)
    {
        $ma = new MeasureAudit();
        $ma->mastatus = 'checked';
        $ma->audittime = gettimeofday()['sec'];
        if (isset($auditcontent) && $auditcontent) {
            $ma->auditcontent = $auditcontent;
        }
        $ma->onlineaudit = $isonline;
        return $ma->update(array('where' => 'maid=? and auditoruid=?', 'param' => array($maid, $uid)));
    }

    public function getRowInfo($pmid, $numpname, $auditoruid)
    {
        return $this->__measureaudit->getOne(array('where' => 'pmid=? and numpname=? and auditoruid=?', 'desc' => 'times', 'param' => array($pmid, $numpname, $auditoruid), 'asArray' => TRUE));
    }

// for api
    public function getLastNewRowInfo($pmid, $numpname, $auditoruid)
    {
        $maxtimesArray = $this->getMaxTimes($pmid, $numpname);
        return $this->__measureaudit->getOne(array('where' => 'pmid=? and numpname=? and auditoruid=? and times=?', 'param' => array($pmid, $numpname, $auditoruid, $maxtimesArray['maxtimes']), 'asArray' => TRUE));
    }

    public function getStatusTender($pmid, $numpname)
    {
        return $this->__measureaudit->getOne(array('where' => 'pmid=? and numpname=? and last=1', 'param' => array($pmid, $numpname), 'asArray' => TRUE));
    }

    public function getStatusTen($pmid, $numpname)
    {
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=?', 'param' => array($pmid, $numpname), 'asArray' => TRUE));
    }

    public function getAuditUser($bid, $num)
    {
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? order by last desc,maid asc', 'param' => array($bid, $num), 'asArray' => TRUE));
    }

    // 去重复
    public function getAuditUserRedo($bid, $num)
    {
        $maxtimesArray = $this->getMaxTimes($bid, $num);
        return $this->__measureaudit->find(array('where' => 'pmid=? and numpname=? and times=? order by last desc,maid asc', 'param' => array($bid, $num, $maxtimesArray['maxtimes']), 'asArray' => TRUE));
    }

    // 去重复
    public function getMaxTimes($bid, $num)
    {
        return $this->__measureaudit->getOne(array('select' => 'max(times) as maxtimes', 'where' => 'pmid=? and numpname=?', 'param' => array($bid, $num), 'asArray' => TRUE));
    }

    public function getMaxTimesInt($bid, $num)
    {
        return $this->__measureaudit->getOne(array('select' => 'max(times) as maxtimes', 'where' => 'pmid=? and numpname=?', 'param' => array($bid, $num), 'asArray' => TRUE))['maxtimes'];
    }

    public function delAuditUser($tenderid, $phaseno, $uid)
    {
        return $this->__measureaudit->delete(array('where' => 'pmid=? and numpname=? and auditoruid=?', 'param' => array($tenderid, $phaseno, $uid), 'asArray' => TRUE));
    }

    public function delAuditUserTimes($tenderid, $phaseno, $uid, $times = 0)
    {
        return $this->__measureaudit->delete(array('where' => 'pmid=? and numpname=? and auditoruid=? and times=?', 'param' => array($tenderid, $phaseno, $uid, $times), 'asArray' => TRUE));
    }

    public function getCurrUserAllProject($audituid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'param' => array($audituid), 'desc' => 'numpname,times', 'groupby' => 'pmid', 'asArray' => TRUE));
    }

    // for API 获取用户参与PMID组
    public function getPmidGroup($audituid)
    {
        return $this->__measureaudit->find(array('where' => 'auditoruid=?', 'param' => array($audituid), 'groupby' => 'pmid', 'asArray' => TRUE));
    }

    // for API 根据pmid,审核人id 获取最新一期最新一次记录
    public function getLastNumTimes($pmid, $audituid)
    {
        return $this->__measureaudit->getOne(array('where' => 'auditoruid=? and pmid=?', 'desc' => 'maid', 'param' => array($audituid, $pmid), 'asArray' => TRUE));
    }

    public function getUserPmid($audituid)
    {
        return $this->__measureaudit->find(array('select' => 'pmid', 'where' => 'auditoruid=?', 'param' => array($audituid), 'groupby' => 'pmid', 'asArray' => TRUE));
    }

    /**
     *
     * @param type $tenderid
     * @param type $phaseno
     * @return type
     *  最新一次的
     */
    public function getAuditOpinion($tenderid, $phaseno)
    {
        $maxtimesArray = $this->getMaxTimes($tenderid, $phaseno);
        return $this->__measureaudit->find(array('select' => 'auditoruid,auditcontent', 'where' => 'pmid=? and numpname=? and times=?', 'param' => array($tenderid, $phaseno, $maxtimesArray['maxtimes']), 'asArray' => TRUE));
    }

    public function getMyChecking($pid, $auditoruid, $all = false)
    {
        if ($all) {
            return $this->__measureaudit->find(array('where' => 'pid=? and auditoruid=? and mastatus=\'checking\'', 'param' => array($pid, $auditoruid), 'asArray' => TRUE));
        } else {
            return $this->__measureaudit->getOne(array('where' => 'pid=? and auditoruid=? and mastatus=\'checking\'', 'param' => array($pid, $auditoruid), 'asArray' => TRUE));
        }

    }

    public function getCountMyChecking($pid, $auditoruid)
    {

        return $this->__measureaudit->count(array('where' => 'pid=? and auditoruid=? and mastatus=\'checking\'', 'param' => array($pid, $auditoruid)));

    }

}
