<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-6-5
 * Time: 上午11:42
 * To change this template use File | Settings | File Templates.
 */
class Table_Account
{
    const ACCOUNT_FETCH_ALL = 0;
    const ACCOUNT_FETCH_ROW = 1;
    const ACCOUNT_FETCH_ONE = 2;

    protected $_table = NULL;

    public function __construct() {
        $this->_table = new DbTable_Account();
    }

    /**
     * 账号列表数据插入
     *
     * @param array $data 字段 => 值
     * @return mixed 所插入行的主键值
     */
    public function insert($data) {
        return $this->_table->insert($data);
    }

    public function getByField($value, $field = 'account_id', $colums = "*"){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    /**
     * 账号列表数据更新
     *
     * @param array $data 字段 => 值
     * @param mixed $value 需要检索的字段值
     * @param string $key 需要检索的字段，默认为主键
     * @return integer 所更新的行数
     */
    public function update($data, $value, $key = 'account_id') {
        $where = $this->_table->getAdapter()->quoteInto("{$key}=?", $value);

        return $this->_table->update($data, $where);
    }

    /**
     * 账号列表据删除
     *
     * @param mixed $value 需要检索的字段值
     * @param string $key 需要检索的字段，默认为主键
     * @return integer 所删除的行数
     */
    public function delete($value, $key = 'account_id') {
        $where = $this->_table->getAdapter()->quoteInto("{$key}=?", $value);

        return $this->_table->delete($where);
    }

    /**
     * 根据指定条件获取相关数据
     *
     * @param array $condition 需要检索的条件
     * @param boolean $count 只获取统计数量，默认为FALSE
     * @return mixed
     */
    public function getByCondition($condition, $count = FALSE) {
        $type = self::ACCOUNT_FETCH_ALL;
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');

        if ($count) {
            $select->from($table, 'count(*)');
            $type = self::ACCOUNT_FETCH_ONE;
        }
        elseif (isset($condition['field']) && is_array($condition['field']) && !empty($condition['field'])) {
            $select->from($table, join($condition['field'], ','));
        }
        elseif (isset($condition['field']) && is_string($condition['field']) && !empty($condition['field'])) {
            $select->from($table, $condition['field']);
        }
        else {
            $select->from($table, '*');
        }

        if (isset($condition['account_id']) && is_numeric($condition['account_id'])) {
            $select->where('account_id=?', (int) $condition['account_id']);
            $count ? NULL : $type = self::ACCOUNT_FETCH_ROW;
        }

        if (isset($condition['account_code']) && !empty($condition['account_code'])) {
            $select->where('account_code=?', $condition['account_code']);
            $count ? NULL : $type = self::ACCOUNT_FETCH_ROW;
        }

        if (isset($condition['account_email']) && !empty($condition['account_email'])) {
            $select->where('account_email=?', $condition['account_email']);
            $count ? NULL : $type = self::ACCOUNT_FETCH_ROW;
        }

        if (isset($condition['customer_id']) && is_numeric($condition['customer_id'])) {
            $select->where('customer_id=?', (int) $condition['customer_id']);
        }

        if (isset($condition['customer_code']) && !empty($condition['customer_code'])) {
            $select->where('customer_code=?', $condition['customer_code']);
        }

        if (isset($condition['account_status']) && !empty($condition['account_status'])) {
            $select->where('account_status=?', $condition['account_status']);
        }

        if (isset($condition['account_name']) && !empty($condition['account_name'])) {
            $select->where('account_name=?', $condition['account_name']);
        }

        if (isset($condition['account_password']) && !empty($condition['account_password'])) {
            $select->where('account_password=?', $condition['account_password']);
        }

        if (isset($condition['forgotten_auth_key']) && !empty($condition['forgotten_auth_key'])) {
            $select->where('forgotten_auth_key=?', $condition['forgotten_auth_key']);
        }

        if (isset($condition['page']) && is_numeric($condition['page'])
            && isset($condition['size']) && is_numeric($condition['size'])) {
            $page = (int) $condition['page'];
            $size = (int) $condition['size'];

            if ($page > 0 && $size > 0) {
                $select->limit($size, ($page - 1) * $size);
            }
        }

        switch ($type) {
            case self::ACCOUNT_FETCH_ALL:
                return $this->_table->getAdapter()->fetchAll($select);
                break;

            case self::ACCOUNT_FETCH_ROW:
                return $this->_table->getAdapter()->fetchRow($select);
                break;

            case self::ACCOUNT_FETCH_ONE:
                return $this->_table->getAdapter()->fetchOne($select);
                break;

            default:
                return NULL;
                break;
        }
    }

    /**
     * 判断用户名是否唯一（客户子账号）
     *
     * @param string $name 用户名
     * @param integer|string $customer 客户ID或代码
     * @return boolean
     */
    public function isUniqueName($name, $customer) {
        $condition['account_name'] = $name;

        if (is_numeric($customer)) {
            $condition['customer_id'] = (int) $customer;
        }
        else {
            $condition['customer_code'] = $customer;
        }

        if ($this->getByCondition($condition, TRUE)) {
            return FALSE;
        }

        return TRUE;
    }

    /**
     * 判断邮箱是否唯一（全局）
     *
     * @param string $email 邮箱
     * @return boolean
     */
    public function isUniqueEmail($email) {
        if (Service_Customer::getByField($email, 'customer_email')) {
            return FALSE;
        }
        elseif ($this->getByCondition(array('account_email' => $email), TRUE)) {
            return FALSE;
        }

        return TRUE;
    }
}
