<?php

Doo::loadModel('fileup');

class attFile
{

    private $__file;

    function __construct()
    {
        $this->__file = new fileup();
    }

    public function insertFile($pid, $stid, $mpid, $pmid, $numpname, $auditoruid, $filepath, $ziphashcode = 0, $filehashcode = 0, $times = 0)
    {
        return $this->__file->createFile($pid, $stid, $mpid, $pmid, $numpname, $auditoruid, $filepath, $ziphashcode = 0, $filehashcode, 2, $times);
    }

    public function getMaxRow()
    {
        return $this->__file->getOne(array('where' => 'aid = (select max(aid)) order by aid desc', 'asArray' => TRUE));
    }

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

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

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

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

    public function getNewLast2($pmid, $numpname)
    {
        return $this->__file->getOne(array('where' => 'stid=? and isnew=2', 'param' => array($pmid, $numpname), 'groupby' => 'numpname', 'asArray' => TRUE));
    }

    public function getFirstFile($pmid, $numpname, $times = 0)
    {
        return $this->__file->getOne(array('where' => 'pmid=? and numpname=? and times=?  and isnew=1', 'orderby' => 'aid', 'param' => array($pmid, $numpname, $times), 'asArray' => TRUE));
    }

    public function getLastData($stid)
    {
        return $this->__file->find(array('where' => 'stid=? and isnew=2', 'param' => array($stid), 'groupby' => 'pmid', 'asArray' => TRUE));
    }

    public function getFileUpdate($pmid)
    {
        return $this->__file->getOne(array('where' => 'aid=(SELECT max(aid) FROM jl_attachment where pmid=? and isnew=2)', 'desc' => 'aid', 'param' => array($pmid), 'asArray' => TRUE));
//	return $this->__file->getOne(array('where' => 'numpname=(SELECT max(numpname) FROM jl_attachment pmid=?) and isnew=2', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getLastNewProfile($pmid)
    {
        return $this->__file->getOne(array('where' => 'pmid=? and isnew=2', 'groupby' => 'numpname', 'desc' => 'numpname', 'param' => array($pmid), 'asArray' => TRUE));
    }

    public function getLastNewProfile2($pmid)
    {
        return $this->__file->getOne(array('where' => 'pmid=? and isnew=2', 'groupby' => 'pmid,numpname,times', 'desc' => 'aid', 'param' => array($pmid), 'asArray' => TRUE));
    }

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

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

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

    public function getReportFile($pmid, $numpname, $times)
    {
        return $this->__file->getOne(array('where' => 'pmid=? and numpname=? and times=?', 'asc' => 'aid', 'param' => array($pmid, $numpname, $times), 'asArray' => TRUE));
    }

// for api
    public function getAllRowPid($pid)
    {
        return $this->__file->find(array('where' => 'pid=? and isnew=2', 'groupby' => 'pmid,numpname', 'desc' => 'times', 'param' => array($pid), 'asArray' => TRUE));
    }

    //  for API 根据PID分组pmid
    public function getPmidGroup($pid)
    {
        return $this->__file->find(array('where' => 'pid=? and isnew=2', 'groupby' => 'pmid', 'param' => array($pid), 'asArray' => TRUE));
    }

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

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

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

    public function setOldfileFlag($pmid, $numpname, $times = 0)
    {
        $this->__file->isnew = 1;
        return $this->__file->update(array('where' => 'pmid=? and numpname=? and times=?', 'param' => array($pmid, $numpname, $times)));
    }

    public function updateHash($pmid, $numpname, $times = 0)
    {
        $this->__file->filehashcode = md5(mt_rand());
        return $this->__file->update(array('where' => 'pmid=? and numpname=? and times=? and isnew=2', 'param' => array($pmid, $numpname, $times)));
    }

}

?>