<?php

Doo::loadModel('contract');

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

    private $__mcontract;

    function __construct()
    {
        $this->__mcontract = new Contract();
    }

    public function insertContract($pid, $uid, $stname)
    {
        if (!isset($pid))
            return FALSE;
        $this->__mcontract->pid = filter_var($pid, FILTER_VALIDATE_INT);
        $this->__mcontract->uid = filter_var($uid, FILTER_VALIDATE_INT);
        $this->__mcontract->stname = filter_var($stname, FILTER_SANITIZE_STRING);
        $this->__mcontract->stkey = $this->create_randomstr(10);
        $this->__mcontract->intime = time();
        return $this->__mcontract->insert();
    }

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

    public function getPidWithKey($stkey)
    {
        return $this->__mcontract->getOne(array('where' => 'stkey=?', 'param' => array($stkey), 'asArray' => TRUE));
    }

    public function getRowByPid($pid)
    {
        return $this->__mcontract->find(array('where' => 'pid=?', 'param' => array($pid), 'asArray' => TRUE));
    }

    public function getRowByStid($stid)
    {
        return $this->__mcontract->getOne(array('where' => 'stid=?', 'param' => array($stid), 'asArray' => TRUE));
    }

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

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

    public function updateStName($stid, $stname)
    {
        $this->__mcontract->stname = filter_var($stname, FILTER_SANITIZE_STRING);
        return $this->__mcontract->update(array('where' => 'stid=?', 'param' => array($stid)));
    }

    /**
     * 随机字符串函数
     * @param $password 密码
     * @param $random 随机数
     */
    function random($length, $chars = '0123456789')
    {
        $hash = '';
        $max = strlen($chars) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

    /**
     * 生成随机字符串
     * @param string $lenth 长度
     * @return string 字符串
     */
    function create_randomstr($lenth = 6)
    {
        return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
    }

}
