<?php

Doo::loadModel('itemmnum');

class ItemMeasureNumpofper
{

    private $__itemMeasureNum;

    function __construct()
    {
        $this->__itemMeasureNum = new ItemMeasureNumofper();
    }

    public function insert($postArray)
    {
        $this->__itemMeasureNum->iaid = $postArray['iaid'];
        $this->__itemMeasureNum->ownerid = $postArray['ownerid'];
        $this->__itemMeasureNum->softwareitemid = $postArray['itemid'];
        $this->__itemMeasureNum->pid = $postArray['pid'];
        $this->__itemMeasureNum->pmid = $postArray['pmid'];
        $this->__itemMeasureNum->isdel = 0;
        $this->__itemMeasureNum->deltime = 0;
        $this->__itemMeasureNum->categoryid = $postArray['categoryid'];
        $this->__itemMeasureNum->tips = $postArray['tips'];
        $this->__itemMeasureNum->numpname = $postArray['numpname'];
        $this->__itemMeasureNum->intime = time();
        return $this->__itemMeasureNum->insert();
    }

    public function getItemMeasureNum($tenderid, $limit, $numpname, $userid)
    {
        if (!empty($limit)) {
            if (isset($numpname) && isset($userid)) {
                return $this->__itemMeasureNum->limit($limit, null, 'imnid', array('where' => "pmid=? AND isdel=0 AND numpname=? AND ownerid=?", 'param' => array($tenderid, $numpname, $userid), 'asArray' => TRUE));
            } elseif (isset($numpname)) {
                return $this->__itemMeasureNum->limit($limit, null, 'imnid', array('where' => "pmid=? AND isdel=0 AND numpname=?", 'param' => array($tenderid, $numpname), 'asArray' => TRUE));
            } elseif (isset($userid)) {
                return $this->__itemMeasureNum->limit($limit, null, 'imnid', array('where' => "pmid=? AND isdel=0 AND ownerid=?", 'param' => array($tenderid, $userid), 'asArray' => TRUE));
            } else {
                return $this->__itemMeasureNum->limit($limit, null, 'imnid', array('where' => "pmid=? AND isdel=0", 'param' => array($tenderid), 'asArray' => TRUE));
            }
        } else {
            return $this->__itemMeasureNum->find(array('where' => 'pmid=? AND isdel=0', 'param' => array($tenderid), 'asArray' => TRUE));
        }
    }

    public function getItemMeasureNumDel($limit, $tenderid)
    {
        if (!empty($limit)) {
            return $this->__itemMeasureNum->limit($limit, null, 'imnid', array('where' => "pmid=? AND isdel=1", 'param' => array($tenderid), 'asArray' => TRUE));
        } else {
            return $this->__itemMeasureNum->find(array('where' => 'pmid=? AND isdel=1', 'param' => array($tenderid), 'asArray' => TRUE));
        }

    }

    public function getCount($tenderid, $deltag = 1, $numpname, $userid)
    {
        if (isset($numpname) && isset($userid)) {
            return $this->__itemMeasureNum->count(array('where' => 'pmid=? AND isdel=? AND numpname=? AND ownerid=?', 'param' => array($tenderid, $deltag, $numpname, $userid), 'asArray' => TRUE));
        }
        if (isset($numpname)) {
            return $this->__itemMeasureNum->count(array('where' => 'pmid=? AND isdel=? AND numpname=? ', 'param' => array($tenderid, $deltag, $numpname), 'asArray' => TRUE));
        }
        if (isset($userid)) {
            return $this->__itemMeasureNum->count(array('where' => 'pmid=? AND isdel=? AND ownerid=?', 'param' => array($tenderid, $deltag, $userid), 'asArray' => TRUE));
        }
        return $this->__itemMeasureNum->count(array('where' => 'pmid=? AND isdel=?', 'param' => array($tenderid, $deltag), 'asArray' => TRUE));

    }

    public function getRowData($imnid)
    {
        return $this->__itemMeasureNum->getOne(array('where' => 'imnid=?', 'param' => array($imnid), 'asArray' => TRUE));
    }

    public function updateItemFields($id, $tips)
    {
        $this->__itemMeasureNum->tips = iconv('GBK', 'UTF-8', $tips);
        return $this->__itemMeasureNum->update(array('where' => 'imnid=?', 'param' => array($id)));
    }

    public function deleteItem($id)
    {
        return $this->__itemMeasureNum->delete(array('where' => 'imnid=?', 'param' => array($id)));
    }

    public function updateDeltag($id, $deluid)
    {
        $this->__itemMeasureNum->isdel = 1;
        $this->__itemMeasureNum->deltime = time();
        $this->__itemMeasureNum->deluid = $deluid;
        return $this->__itemMeasureNum->update(array('where' => 'imnid=?', 'param' => array($id)));
    }

    public function recoveryDel($id)
    {
        $this->__itemMeasureNum->isdel = 0;
        return $this->__itemMeasureNum->update(array('where' => 'imnid=?', 'param' => array($id)));
    }

    public function updateIAID($oldiaid, $newiaid)
    {
        $this->__itemMeasureNum->iaid = $newiaid;
        return $this->__itemMeasureNum->update(array('where' => 'iaid=?', 'param' => array($oldiaid)));
    }


    public function delItemFields($imnid)
    {
        return $this->__itemMeasureNum->delete(array('where' => 'imnid=?', 'param' => array($imnid)));
    }

    public function delItemIAID($iaid)
    {
        return $this->__itemMeasureNum->delete(array('where' => 'iaid=?', 'param' => array($iaid)));
    }
}
