<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2013.11.19
 */
class memberIModel extends X
{
    public function getuserData($name)
    {
        $sql = "SELECT * FROM " . DB_PREFIXOA . "user WHERE name='" . $name . "'";
        return parent::$obj->fetch_first($sql);
    }
    public function getuserDataid($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIXOA . "user WHERE id='" . $id . "'";
        return parent::$obj->fetch_first($sql);
    }
    public function doAdduser($array)
    {
        $array['password'] = md5($array['password']);
        $adminid           = parent::$obj->fetch_newid('SELECT MAX(id) FROM ' . DB_PREFIXOA . 'user', 1);
        $array             = array_merge(array(
            'emp_no'      => $array['name'],
            'create_time' => time(),
            'is_del'      => 1
        ), $array);
        return parent::$obj->insert(DB_PREFIXOA . 'user', $array);
    }
    public function getusercheck($array)
    {
        $name     = $array['username'];
        $password = md5($array['password']);
        $sql = "SELECT * FROM " . DB_PREFIXOA . "user WHERE name='" . $name . "' AND password='$password'";
        return parent::$obj->fetch_first($sql);
    }
    public function savetime($id)
    {
        $array['last_login_time'] = time();
        return parent::$obj->update(DB_PREFIXOA . 'user', $array, 'id=' . $id . '');
    }
    public function saveinfo($info)
    {
        $id = $info['id'];
        unset($info['id']);
        return parent::$obj->update(DB_PREFIXOA . 'user', $info, 'id=' . $id . '');
    }

    public function getAllList($items)
    {
        $pagesize = intval($items['pagesize']);
        $where    = " WHERE" . $items['searchsql'];
        $start    = ($items['page'] - 1) * $pagesize;
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "order_info " . $where;
        $total    = parent::$obj->fetch_count($countsql);
        $sql      = "SELECT * FROM " . DB_PREFIX . "order_info " .
            $where . $items['orderby'] . " LIMIT " . $start . ", " . $pagesize . "";
        $data = parent::$obj->getall($sql);
        return array($total, $data);
    }
    public function getordertotal($where)
    {
        $where    = " WHERE" . $where;
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "order " . $where;
        $total    = parent::$obj->fetch_count($countsql);
        return  $total;
    }
    public function getAllorderList($items)
    {
        $pagesize = intval($items['pagesize']);
        $where    = " WHERE" . $items['searchsql'];
        $start    = ($items['page'] - 1) * $pagesize;
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "order" . $where;
        $total    = parent::$obj->fetch_count($countsql);
        $sql      = "SELECT * FROM " . DB_PREFIX . "order" .
            $where . $items['orderby'] . " LIMIT " . $start . ", " . $pagesize . "";
        $data = parent::$obj->getall($sql);
        return array($total, $data);
    }

    public function ordergoodslist($where){
        $sql      = "SELECT * FROM " . DB_PREFIX . "order_info WHERE" .$where;
        return parent::$obj->getall($sql);
    }
    public function getAlllogList($items)
    {
        $pagesize = intval($items['pagesize']);
        $where    = " WHERE" . $items['searchsql'];
        $start    = ($items['page'] - 1) * $pagesize;
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "account_log " . $where;
        $total    = parent::$obj->fetch_count($countsql);
        $sql      = "SELECT * FROM " . DB_PREFIX . "account_log " .
            $where . $items['orderby'] . " LIMIT " . $start . ", " . $pagesize . "";
        $data = parent::$obj->getall($sql);
        return array($total, $data);
    }
    public function getuseraddress($items)
    {
        $user_id  = $items['adminid'];
        $where    = " WHERE 1=1 AND user_id='$user_id'";
        $start    = ($items['page'] - 1) * $items['pagesize'];
        $countsql = "SELECT COUNT(*) AS my_count FROM " . DB_PREFIX . "member_address AS v" . $where;
        $total    = parent::$obj->fetch_count($countsql);
        $sql      = "SELECT v.*FROM " . DB_PREFIX . "member_address AS v" .
            $where . " ORDER BY v.time DESC LIMIT " . $start . ", " . $items['pagesize'] . "";
        $array    = parent::$obj->getall($sql);
        // foreach ($array as $key=>$value) {
        //  $array[$key]['url'] = XUrl::getContentUrl('article', $value['dirname'], $value['articleid']);
        //  }
        return array($total, $array);
    }
    public function getuseraddressinfo($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "member_address WHERE user_id='" . $id . "' AND is_mo='1'";
        return parent::$obj->fetch_first($sql);
    }
    public function getuseraddressinfo1($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "member_address WHERE id='$id'";
        return parent::$obj->fetch_first($sql);
    }
    public function savememberinfo($info)
    {
        $id   = $info['user_id'];
        $sql  = "SELECT * FROM " . DB_PREFIX . "member_info WHERE user_id='" . $id . "'";
        $data = parent::$obj->fetch_first($sql);
        if (!empty($data)) {
            unset($info['id']);
            return parent::$obj->update(DB_PREFIX . 'member_info', $info, 'user_id=' . $id . '');
        } else {
            return parent::$obj->insert(DB_PREFIX . 'member_info', $info);
        }
    }
    public function getprovince()
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "region WHERE parent_id='0' AND level='1'";
        return parent::$obj->getall($sql);
    }
    public function getcity($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "region WHERE parent_id='$id' AND level='2'";
        return parent::$obj->getall($sql);
    }
    public function getares($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "region WHERE parent_id='$id' AND level='3'";
        return parent::$obj->getall($sql);
    }
    public function getregion($id, $level)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "region WHERE parent_id='$id' AND level='$level'";
        return parent::$obj->getall($sql);
    }
    public function getregionname($id)
    {
        $sql  = "SELECT * FROM " . DB_PREFIX . "region WHERE id='$id'";
        $data = parent::$obj->fetch_first($sql);
        return $data['name'];
    }
    public function addaddress($array)
    {
        return parent::$obj->insert(DB_PREFIX . 'member_address', $array);
    }
    public function saveaddress($array)
    {
        $id = $array['id'];
        unset($array['id']);
        return parent::$obj->update(DB_PREFIX . 'member_address', $array, 'id=' . $id . '');
    }
    public function delmo()
    {
        $array['is_mo'] = 0;
        parent::$obj->update(DB_PREFIX . 'member_address', $array);
    }
    public function getaddresscount($id)
    {
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "member_address WHERE user_id='$id'";
        $total = parent::$obj->fetch_count($countsql);
        return $total;
    }
    public function deladdress($id)
    {
        $sql = "DELETE FROM " . DB_PREFIX . "member_address WHERE id='" . intval($id) . "'";
        return parent::$obj->query($sql);
    }
    public function getaddressinfo($id)
    {
        $sql = "SELECT * FROM " . DB_PREFIX . "member_address WHERE id='" . $id . "'";
        return parent::$obj->fetch_first($sql);
    }
    public function getall(){
        $sql = "SELECT * FROM " . DB_PREFIXOA . "user";
        return parent::$obj->getall($sql);
    }
    public function getorderinfo($ordernum){
        $sql = "SELECT * FROM " . DB_PREFIX . "order WHERE ordernum='" . $ordernum . "'";
        return parent::$obj->fetch_first($sql);
    }
    public function addfeedback($array){
        return parent::$obj->insert(DB_PREFIX . 'feedback', $array);
    }
    public function getsetup($name){
        $sql = "SELECT * FROM " . DB_PREFIX . "setup WHERE setname='" . $name . "'";
        return parent::$obj->fetch_first($sql);
    }
    public function addlog($log){
        return parent::$obj->insert(DB_PREFIX . 'integral_log', $log);
    }
    public function getlog($ordernum){
        $sql = "SELECT * FROM " . DB_PREFIX . "integral_log WHERE order_num='$ordernum' AND status='1'";
        return parent::$obj->getall($sql);
    }
    public function savelog($info)
    {
        $id = $info['id'];
        unset($info['id']);
        return parent::$obj->update(DB_PREFIX . 'integral_log', $info, 'id=' . $id . '');
    }
    public function getAllprofitList($items)
    {
        $pagesize = intval($items['pagesize']);
        $where    = " WHERE" . $items['searchsql'];
        $start    = ($items['page'] - 1) * $pagesize;
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIX . "integral_log" . $where;
        $total    = parent::$obj->fetch_count($countsql);
        $sql      = "SELECT * FROM " . DB_PREFIX . "integral_log" .
            $where . $items['orderby'] . " LIMIT " . $start . ", " . $pagesize . "";
        $data = parent::$obj->getall($sql);
        return array($total, $data);
    }
    public function addorder($array)
    {
        return parent::$obj->insert(DB_PREFIX . 'payorder', $array);
    }
    public function getpayorderinfo($number){
        $sql = "SELECT * FROM ".DB_PREFIX."payorder WHERE order_number='$number'";
        $data = parent::$obj->fetch_first($sql);
        return $data;
    }
    public function addlog1($arr){
        return parent::$obj->insert(DB_PREFIX.'account_log', $arr);
    }
    public function payorder($number){
        $info['status']='1';
        return parent::$obj->update(DB_PREFIX.'payorder', $info, 'order_number='.$number.'');
    }
    public function getparentlist($id){
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIXOA . "user WHERE parent_id='" . $id . "'";
        $total    = parent::$obj->fetch_count($countsql);
        $sql = "SELECT * FROM " . DB_PREFIXOA . "user WHERE parent_id='" . $id . "'";
        $data = parent::$obj->getall($sql);
        return array($total, $data);
    }
    public function getparenttotal($id){
        $countsql = "SELECT COUNT(*) FROM " . DB_PREFIXOA . "user WHERE parent_id='" . $id . "'";
        return $total    = parent::$obj->fetch_count($countsql);
    }
    public function addtiorder($array){
        return parent::$obj->insert(DB_PREFIX . 'tiorder', $array);
    }
}
?>
