<?php

Doo::loadCore('db/DooModel');

/**
 * Description of newPHPClass
 *
 * @author zongheng
 */
class Pay extends DooModel {

    public $id;
    public $trade_sn;
    public $userid;
    public $username;
    public $contactname;
    public $email;
    public $telephone;
    public $discount;
    public $money;
    public $quantity;
    public $addtime;
    public $paytime;
    public $usernote;
    public $pay_id;
    public $pay_type;
    public $payment;
    public $type;
    public $ip;
    public $status;
    public $adminnote;
    public $checker;
    public $_table = 'wk_pay_account';
    public $_primarykey = 'id';
    public $_fields = array('id', 'trade_sn', 'userid', 'username', 'contactname', 'email', 'telephone', 'discount', 'money', 'quantity', 'addtime', 'paytime', 'usernote', 'pay_id', 'pay_type', 'payment', 'type', 'ip', 'status', 'adminnote','checker');

    public function __construct() {
        parent::setupModel(__CLASS__);
    }

    public function add($array) {
        if (!is_array($array))
            return FALSE;
        foreach ($array as $key => $value) {
            $this->$key = $value;
        }
        return $this->insert();
    }

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

    public function getRowsBySN($sn) {
        if (empty($sn))
            return FALSE;
        return $this->getOne(array('select' => 'id,userid,money,status', 'where' => 'trade_sn=?', 'param' => array($sn), 'limit' => '1', 'asArray' => TRUE));
    }

    private function _ExsitTheUser($uid) {
        $arruser = $this->getOne(array('select' => 'uid', 'where' => 'uid=?', 'param' => array($uid), 'limit' => '1', 'asArray' => TRUE));
        if (empty($arruser) && is_array($arruser)) {
            $this->uid = intval($uid);
            return $this->insert();
        } else {
            return FALSE;
        }
    }

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

    public function getWidthAndLevel($uid, $level) {
        $userz = $this->getRowByUid($uid);
        $level[] = $userz['totalintegral'];
        sort($level);
        $thekey = array_search($userz['totalintegral'], $level);
        if (isset($level[$thekey - 1])) {
            $tmplevel = $userz['totalintegral'] - $level[$thekey - 1];
        } else {
            $tmplevel = $userz['totalintegral'];
        }
        $width = round($tmplevel / $level[$thekey + 1], 2) * 100 . '%';
        return array('width' => $width, 'currlevel' => $thekey + 1, 'currinte' => $tmplevel, 'levelup' => $level[$thekey + 1]);
    }

    public function updateRowbyUid($uid, $array) {
        if ($uid)
            $this->_ExsitTheUser($uid);
        if (!is_array($array))
            return FALSE;
        foreach ($array as $key => $val) {
            $this->$key = $val;
        }
        return $this->update(array('where' => 'uid=' . $uid));
    }

    public function updateRowbySN($sn, $array) {
        if (!is_array($array))
            return FALSE;
        foreach ($array as $key => $val) {
            $this->$key = $val;
        }
        return $this->update(array('where' => 'trade_sn=' . $sn));
    }

    public function get_all_posts($limit = NULL) {
        if (!empty($limit)) {
            $postArray = $this->limit($limit, null, 'id', array('asArray' => TRUE));
        } else {
            $postArray = $this->find(array('select' => 'id, trade_sn, userid, username, contactname, email, telephone, discount, money, quantity, addtime, paytime, usernote, pay_id, pay_type, payment, type, ip, status, adminnote', 'desc' => 'id', 'asArray' => true));
        }
        return $postArray;
    }

	public function get_all_posts_status($limit = NULL,$status="") {
		
        $postArray = $this->limit($limit, null, 'id', array('where' => $status , 'desc' => 'id', 'asArray' => true));
        
        return $postArray;
    }
    
    public function setToSucc($sn) {
        if (!$sn)
            return FALSE;
        $this->status = 'succ';
        return $this->update(array('where' => 'trade_sn=' . $sn));
    }

    public function get_sum_succ_posts($payment,$start,$end){
        return $this->getOne(array('select' => 'sum(money) as sum', 'where' => 'pay_type="'. $payment .'" and status="succ" and paytime between '. $start .' and '. $end, 'asArray' => TRUE));
    }

}

?>
