<?php

Doo::loadModel('measure');

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

    private $__mmeasure;

    function __construct()
    {
        $this->__mmeasure = new Measure();
    }

    public function insertMeasure($uid, $pid, $stid, $pmname)
    {
        if (!isset($uid))
            return FALSE;
        $this->__mmeasure->uid = filter_var($uid, FILTER_VALIDATE_INT);
        $this->__mmeasure->pid = filter_var($pid, FILTER_VALIDATE_INT);
        $this->__mmeasure->stid = filter_var($stid, FILTER_VALIDATE_INT);
        $this->__mmeasure->pmname = filter_var($pmname, FILTER_SANITIZE_STRING);
        $this->__mmeasure->intime = time();
        return $this->__mmeasure->insert();
    }

    public function getAllbyStid($stid)
    {
        if (!$stid)
            return FALSE;
        return $this->__mmeasure->find(array('where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

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

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

    public function getRowUserStid($uid)
    {
        return $this->__mmeasure->find(array('where' => 'uid=?', 'param' => array($uid), 'groupby' => 'stid', 'asArray' => TRUE));
    }

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

    public function getRowBy($stid)
    {
        if (!$stid)
            return FALSE;
        return $this->__mmeasure->find(array('where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getRowByPmid($pmid)
    {
        if (!$pmid)
            return FALSE;
        return $this->__mmeasure->getOne(array('where' => 'pmid=?', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getRowByPids($pid)
    {
        if (!$pid)
            return FALSE;
        return $this->__mmeasure->find(array('where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE));
    }

    public function getRowByPid($pid)
    {
        if (!$pid)
            return FALSE;
        return $this->__mmeasure->getOne(array('where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE));
    }

    public function getRowsByPid($pid)
    {
        if (!$pid)
            return FALSE;
        return $this->__mmeasure->find(array('where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE));
    }

    public function getCountByUid($uid, $pid)
    {
        if (!$uid)
            return FALSE;
        return $this->__mmeasure->count(array('where' => 'uid=? and pid=?', 'param' => array($uid, $pid), 'asArray' => TRUE));
    }

    public function getMearsureCount($pid)
    {
        return $this->__mmeasure->count(array('where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE));
    }

    public function getTotalWithPid($pid)
    {
        if (!$pid)
            return FALSE;
        return $this->__mmeasure->getOne(array('select' => 'sum(contracttotal) as total', 'where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE))['total'];
    }

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

    public function updateCon($pmid, $con)
    {
        $this->__mmeasure->contracttotal = $con;
        return $this->__mmeasure->update(array('where' => 'pmid=?', 'param' => array($pmid)));
    }

    public function getTotal($stid)
    {
        if (!$stid)
            return FALSE;
        return $this->__mmeasure->getOne(array('select' => 'sum(contracttotal) as total', 'where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function getCountMeasure($stid)
    {
        return $this->__mmeasure->count(array('where' => 'stid=?', 'param' => array($stid)));
    }

    public function getAll()
    {
        return $this->__mmeasure->find(array('asArray' => TRUE));
    }

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

    public function getAllbyStidSum($stid)
    {
        if (!$stid)
            return FALSE;
        return $this->__mmeasure->find(array('select' => 'sum(contracttotal) as contracttotal', 'where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

    public function updateName($pmid, $pmname)
    {
        $this->__mmeasure->pmname = $pmname;
        return $this->__mmeasure->update(array('where' => 'pmid=?', 'param' => array($pmid)));
    }

    public function getCountPmid($pmid)
    {
        return $this->__mmeasure->count(array('where' => 'pmid=?', 'param' => array($pmid)));
    }
}
