<?php

Doo::loadModel('numofper');

/**
 * Description of numofperact
 *
 * @author zongheng
 */
class NumofperAct {

    private $__numofperact;

    function __construct() {
        $this->__numofperact = new Numofper();
    }

    public function insert($pid, $uid, $tenderid, $stid, $phaseno, $currcontractval, $currchangeval, $stopnowtotal, $currdone, $curralltotal, $times = 0) {
        $this->__numofperact->uid = $uid;
        $this->__numofperact->pid = $pid;
        $this->__numofperact->pmid = $tenderid;
        $this->__numofperact->stid = $stid;
        $this->__numofperact->numpname = $phaseno;
        $this->__numofperact->times = $times;
        $this->__numofperact->currcontractval = $currcontractval;
        $this->__numofperact->currchangeval = $currchangeval;
        $this->__numofperact->stopnowtotal = $stopnowtotal;
        $this->__numofperact->currdone = $currdone;
        $this->__numofperact->curralltotal = $curralltotal;
        $this->__numofperact->currstatus = 'checking';
        $this->__numofperact->audittime = 0;
        $this->__numofperact->intime = time();
        return $this->__numofperact->insert();
    }

    public function redoRow($pid, $tenderid, $stid, $phaseno, $times) {
        $this->__numofperact->pid = $pid;
        $this->__numofperact->pmid = $tenderid;
        $this->__numofperact->stid = $stid;
        $this->__numofperact->numpname = $phaseno;
        if ($times > 0)
            $this->__numofperact->times = $times;
        $this->__numofperact->currcontractval = 0;
        $this->__numofperact->currchangeval = 0;
        $this->__numofperact->stopnowtotal = 0;
        $this->__numofperact->currdone = 0;
        $this->__numofperact->curralltotal = 0;
        $this->__numofperact->currstatus = 'checking';
        $this->__numofperact->audittime = time();
        return $this->__numofperact->insert();
    }

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

    public function getCountTotalnum($pmid) {
        return $this->__numofperact->count(array('where' => 'pmid=? and currstatus != \'checkno\' ', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getCountTotalnum2($pmid) {
        return $this->__numofperact->getOne(array('select' => 'numpname', 'where' => 'pmid=?', 'param' => array($pmid), 'groupby' => 'numpname', 'desc' => 'numpname', 'asArray' => TRUE));
    }

    public function getMaxStatusTimes($pmid, $numpname) {
        return $this->__numofperact->getOne(array('select' => 'currstatus', 'where' => 'pmid=? and numpname=?', 'param' => array($pmid, $numpname), 'desc' => 'times', 'asArray' => TRUE));
    }

    // for API
    public function getMaxTimes($pmid, $numpname) {
        return $this->__numofperact->getOne(array('select' => 'times,currstatus', 'where' => 'pmid=? and numpname=?', 'param' => array($pmid, $numpname), 'desc' => 'times', 'asArray' => TRUE));
    }

    public function getCountTotalplus($pmid) {
        return $this->__numofperact->getOne(array('select' => 'sum(curralltotal) as totalplus', 'where' => 'pmid=?', 'groupby' => 'numpname', 'desc' => 'times', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getStopNow($pmid) {
        return $this->__numofperact->getOne(array('select' => 'sum(stopnowtotal) as sntotal', 'where' => 'pmid=?', 'groupby' => 'numpname', 'desc' => 'times', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getStopNowTotal($pmid) {
        return $this->__numofperact->getOne(array('select' => 'stopnowtotal as sntotal', 'where' => 'pmid=?', 'groupby' => 'pmid,numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getStopNowSt($stid) {
        return $this->__numofperact->getOne(array('select' => 'sum(stopnowtotal) as sntotal', 'where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getStopNowT($pmid) {
        return $this->__numofperact->getOne(array('select' => 'sum(stopnowtotal) as sntotal', 'where' => 'pmid=?', 'param' => array($pmid), 'DESC' => 'audittime', 'asArray' => TRUE));
    }

    public function getCurrDoneSt($stid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currdone) as currdone', 'where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getCurrDonePmid($pmid) {
        return $this->__numofperact->getOne(array('select' => 'currdone as currdone', 'where' => 'pmid=?', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getCurrTotal($pmid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currcontractval) as currtotal', 'where' => 'pmid=?', 'groupby' => 'numpname', 'desc' => 'times', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getCurrAlltotal($pmid) {
        return $this->__numofperact->getOne(array('select' => 'currdone as currtotal', 'where' => 'pmid=?', 'param' => array($pmid), 'groupby' => 'pmid,numpname', 'desc' => 'numpname', 'asArray' => TRUE));
    }

    public function getCurrdoneNew($pmid) {
        return $this->__numofperact->getOne(array('select' => 'currdone as currtotal', 'where' => 'pmid=?', 'param' => array($pmid), 'desc' => 'mpid', 'asArray' => TRUE));
    }

    public function getCountTotalSTID($stid) {
        return $this->__numofperact->getOne(array('select' => 'sum(curralltotal) as totalplus', 'where' => 'stid=? and currstatus != \'checkno\'', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getCountTotalSTID2($stid) {
        $pdArray = NULL;
        $pdArray = $this->getPMID($stid);
        $totalPlus = 0;
        if ($pdArray)
            foreach ($pdArray as $value) {
                $t = $this->getMaxNumORTimes($value['pmid']);
                $totalPlus = $totalPlus + $t;
            }
        return array('totalplus' => $totalPlus);
    }

// 根据条件STID获取分组后的PMID
    public function getPMID($stid) {
        return $this->__numofperact->find(array('select' => 'pmid', 'where' => 'stid=? and currstatus != \'checkno\'', 'groupby' => 'pmid', 'param' => array($stid), 'asArray' => TRUE));
    }

// 根据条件STID获取分组后的PMID
    public function getMaxNumORTimes($pmid) {
        $pmidArray = $this->__numofperact->getOne(array('where' => 'pmid=? and currstatus != \'checkno\'', 'param' => array($pmid), 'desc' => 'mpid', 'asArray' => TRUE));
        return $pmidArray['curralltotal'];
    }

    public function getNumByStid($stid) {
        return $this->__numofperact->find(array('where' => 'stid=?', 'param' => array($stid), 'desc' => 'mpid', 'asArray' => TRUE));
    }

    public function getRowByPmid($pmid) {
        return $this->__numofperact->find(array('where' => 'pmid=? and currstatus != \'checkno\'', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getGroupByLastOne($pmid) {
        return $this->__numofperact->find(array('where' => 'pmid=? and currstatus!=\'checkno\'', 'groupby' => 'numpname', 'desc' => 'numpname,times', 'param' => array($pmid), 'asArray' => TRUE));
    }

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

    public function getRowByPmid2($pmid) {
        return $this->__numofperact->find(array('where' => 'pmid=?', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getRowTender($pmid) {
        return $this->__numofperact->find(array('where' => 'pmid=?', 'orderby' => 'numpname', 'param' => array($pmid), 'desc' => 'mpid', 'asArray' => TRUE));
    }

    public function getRowTender2($pmid) {
        return $this->__numofperact->getOne(array('where' => 'pmid=?', 'orderby' => 'numpname', 'param' => array($pmid), 'desc' => 'mpid', 'asArray' => TRUE));
    }

    public function getRowTenderCurr($pmid) {
        return $this->__numofperact->find(array('where' => 'numpname=(SELECT max(numpname) FROM jl_measure_numofper where pmid=?) and pmid=?', 'orderby' => 'numpname', 'param' => array($pmid, $pmid), 'desc' => 'mpid', 'asArray' => TRUE));
    }

    public function getAlluserMeasureAudit($bid, $num) {
        return $this->__numofperact->getOne(array('select' => 'mpid', 'where' => 'pmid=? and numpname=?', 'param' => array($bid, $num), 'asArray' => TRUE));
    }

    public function getNumOfStatus($bid, $num) {
        return $this->__numofperact->getOne(array('where' => 'pmid=? and numpname=?', 'param' => array($bid, $num), 'asArray' => TRUE));
    }

    public function getCurrStatus($mpid) {
        return $this->__numofperact->getOne(array('where' => 'mpid=?', 'param' => array($mpid), 'asArray' => TRUE));
    }

    public function getRow($pmid, $numpname, $times = 0) {
        return $this->__numofperact->getOne(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($pmid, $numpname, $times), 'asArray' => TRUE));
    }

    public function hasTheTimesRow($pmid, $numpname, $times = 0) {
        return $this->__numofperact->count(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($pmid, $numpname, $times), 'asArray' => TRUE));
    }

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

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

//    public function getMaxCurrStatus($pid, $pmid, $stid) {
//	return $this->__numofperact->getOne(array('where' => 'numpname=(SELECT max(numpname) FROM jl_measure_numofper where pid=? and  pmid=? and stid=?)', 'param' => array($pid, $pmid, $stid), 'asArray' => TRUE));
//    }

    public function getLastStatus($pid, $pmid) {
        return $this->__numofperact->getOne(array('where' => 'pid=? and pmid=? and currstatus != \'checkno\'', 'desc' => 'numpname', 'param' => array($pid, $pmid), 'asArray' => TRUE));
    }

    public function getLastStatus2($pid, $pmid) {
        return $this->__numofperact->getOne(array('where' => 'pid=? and pmid=?', 'groupby' => 'pmid,numpname,times', 'desc' => 'times', 'param' => array($pid, $pmid), 'asArray' => TRUE));
    }

    public function getLastStatus3($pid, $pmid) {
        return $this->__numofperact->getOne(array('where' => 'pid=? and pmid=?', 'desc' => 'numpname', 'param' => array($pid, $pmid), 'asArray' => TRUE));
    }

    public function getLastRow($pid, $pmid) {
        return $this->__numofperact->getOne(array('where' => 'pid=? and pmid=?', 'groupby' => 'numpname,times', 'desc' => 'mpid', 'param' => array($pid, $pmid), 'asArray' => TRUE));
    }

    public function getChangTotal($pid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currchangeval) as changtotal', 'where' => 'pid=? and currstatus != \'checkno\'', 'param' => array($pid), 'asArray' => TRUE))['changtotal'];
    }

    public function getCurrDone($pid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currdone) as currdone', 'where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE))['currdone'];
    }

    public function getCurrDoneNow($pid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currdone) as currdone', 'where' => 'pid=? and currstatus!=\'checkno\'', 'param' => array($pid), 'asArray' => TRUE))['currdone'];
    }

    public function getCheckedCurrdone($stid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currdone) as currdone', 'where' => 'stid = ?', 'param' => array($stid), 'asArray' => TRUE))['currdone'];
    }

    public function getMaxCurrStatus($pid, $pmid, $stid) {
        return $this->__numofperact->getOne(array('where' => 'numpname = (SELECT max(numpname) FROM jl_measure_numofper where pid = ? and pmid = ? and stid = ?)', 'param' => array($pid, $pmid, $stid), 'asArray' => TRUE));
    }

    //标段下最新一期
    public function getLastNew($pmid) {
        return $this->__numofperact->getOne(array('select' => 'stopnowtotal, currdone', 'where' => 'pmid = ?', 'groupby' => 'pmid, numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
//	return $this->__numofperact->getOne(array('select' => 'stopnowtotal, currdone', 'where' => 'pmid = ? and currstatus!=\'checkno\'', 'orderby' => 'numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
    }

    //标段下最新一期
    public function getLastNew2($pmid) {
        return $this->__numofperact->getOne(array('where' => 'pmid = ?', 'groupby' => 'pmid, numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
//	return $this->__numofperact->getOne(array('select' => 'stopnowtotal, currdone', 'where' => 'pmid = ? and currstatus!=\'checkno\'', 'orderby' => 'numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
    }

    //更新数据
    public function updateNumofper($tenderid, $phaseno, $itemArray, $times = 0) {
        if (!is_array($itemArray)) {
            return FALSE;
        }
        $this->__numofperact->currcontractval = $itemArray['BQHTJL'];
        $this->__numofperact->currchangeval = $itemArray['BQSLBGJL'];
        $this->__numofperact->stopnowtotal = $itemArray['JZSQLJWC'];
        $this->__numofperact->currdone = $itemArray['BQWCJL'];
        $this->__numofperact->curralltotal = $itemArray['LJWCJL'];
        return $this->__numofperact->update(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($tenderid, $phaseno, $times)));
    }

    //更新数据
    public function updateAuditStatus($tenderid, $phaseno, $statuno = 'checking', $times = 0) {
        $this->__numofperact->currstatus = $statuno;
        $this->__numofperact->audittime = time();
        return $this->__numofperact->update(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($tenderid, $phaseno, $times)));
    }

//    public function getCurrdoneSTID($stid) {
//	return $this->__numofperact->getOne(array('select' => 'sum(currdone) as totalplus', 'where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
//    }
    public function getCurrdoneSTID($stid) {
        return $this->__numofperact->getOne(array('select' => 'sum(currdone) as totalplus', 'where' => 'stid=?  and currstatus!=\'checkno\'', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getCurrDoneSMontht($stid) {
        return $this->__numofperact->find(array('select' => 'sum(currdone) as currdone,FROM_UNIXTIME(audittime,"%Y-%m") as month', 'where' => 'stid=?'
                    , 'param' => array($stid), 'groupby' => 'FROM_UNIXTIME(audittime,"%Y-%m")', 'asc' => 'audittime', 'asArray' => TRUE));
    }

    public function getStopNowStMonth($stid) {
        return $this->__numofperact->find(array('select' => 'sum(stopnowtotal) as sntotal,FROM_UNIXTIME(audittime,"%Y-%m") as month', 'where' => 'stid=? '
                    , 'param' => array($stid), 'groupby' => 'FROM_UNIXTIME(audittime,"%Y-%m")', 'asc' => 'audittime', 'asArray' => TRUE));
    }

    public function getCurrTotalStMonth($stid) {
        return $this->__numofperact->find(array('select' => 'sum(curralltotal) as currtotal,FROM_UNIXTIME(audittime,"%Y-%m") as month', 'where' => 'stid=? '
        , 'param' => array($stid), 'groupby' => 'FROM_UNIXTIME(audittime,"%Y-%m")', 'asc' => 'audittime', 'asArray' => TRUE));
    }

    public function getCountTimes($pmid, $numpname, $times) {
        return $this->__numofperact->count(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($pmid, $numpname, $times)));
    }

    public function getCountNumpname($pmid, $numpname) {
        return $this->__numofperact->count(array('where' => 'pmid=? and numpname=?', 'param' => array($pmid, $numpname)));
    }


    public function getCurrDonePmMonth($pmid) {
        return $this->__numofperact->find(array('select' => 'sum(currdone) as currdone,FROM_UNIXTIME(intime,"%Y-%m") as month', 'where' => 'pmid=? and currstatus!=\'checkno\''
        , 'param' => array($pmid), 'groupby' => 'FROM_UNIXTIME(intime,"%Y-%m")', 'asc' => 'intime', 'asArray' => TRUE));
    }

    public function getCurrTotalPmMonth($pmid) {
        return $this->__numofperact->find(array('select' => 'sum(curralltotal) as currtotal,FROM_UNIXTIME(intime,"%Y-%m") as month', 'where' => 'pmid=? and currstatus!=\'checkno\''
        , 'param' => array($pmid), 'groupby' => 'FROM_UNIXTIME(intime,"%Y-%m")', 'asc' => 'intime', 'asArray' => TRUE));
    }

    public function getCurrDoneStMonthNoCheckno($stid) {
        return $this->__numofperact->find(array('select' => 'sum(currdone) as currdone,FROM_UNIXTIME(intime,"%Y-%m") as month', 'where' => 'stid=? and currstatus!=\'checkno\''
        , 'param' => array($stid), 'groupby' => 'FROM_UNIXTIME(intime,"%Y-%m")', 'asc' => 'intime', 'asArray' => TRUE));
    }


    public function getCurrTotalStMonthNoCheckno($stid) {
        $sql = 'SELECT sum(curralltotal) as currtotal,FROM_UNIXTIME(intime,"%Y-%m") as month FROM (SELECT max(mpid) as mpid,max(cast(`curralltotal` as DECIMAL)) as curralltotal,max(intime) as intime FROM `jl_measure_numofper` WHERE stid='.$stid.' AND currstatus!=\'checkno\' GROUP BY pmid) as tmptable GROUP BY FROM_UNIXTIME(intime,"%Y-%m") ORDER BY intime ASC';
        $query = Doo::db ()->query ( $sql );

        $result = $query->fetchAll ();

        return $result;
//        return $this->__numofperact->find(array('select' => 'sum(curralltotal) as currtotal,FROM_UNIXTIME(intime,"%Y-%m") as month', 'where' => 'stid=? and currstatus!=\'checkno\''
//        , 'param' => array($stid), 'groupby' => 'FROM_UNIXTIME(intime,"%Y-%m")', 'asc' => 'intime', 'asArray' => TRUE));
    }
}
