<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class adminSModel extends X {

    public function getList($items) {
        $where      = " WHERE 1=1".$items['searchsql'];
        $start      = ($items['page']-1)*$items['pagesize'];
        $countsql   = "SELECT COUNT(v.adminid) FROM ".DB_PREFIXSA."admin AS v".$where;
        $total      = parent::$obj->fetch_count($countsql);
        $sql        = "SELECT v.*,g.groupname".
                      " FROM ".DB_PREFIXSA."admin AS v".
                      " LEFT JOIN ".DB_PREFIX."authgroup AS g ON v.groupid=g.groupid".
                      $where." ORDER BY v.adminid DESC LIMIT ".$start.", ".$items['pagesize']."";
        $array = parent::$obj->getall($sql);
        return array($total, $array);
    }

    public function getroleList($items) {
        $where      = " WHERE 1=1".$items['searchsql'];
        $start      = ($items['page']-1)*$items['pagesize'];
        $countsql   = "SELECT COUNT(v.groupid) FROM ".DB_PREFIX."authgroup AS v".$where;
        $total      = parent::$obj->fetch_count($countsql);
        $sql        = "SELECT *".
                      " FROM ".DB_PREFIX."authgroup".
                      $where." ORDER BY groupid DESC LIMIT ".$start.", ".$items['pagesize']."";
        $array = parent::$obj->getall($sql);
        return array($total, $array);
    }

    public function getData($id) {
        $sql = "SELECT * FROM ".DB_PREFIXSA."admin WHERE adminid='".intval($id)."'";
        return parent::$obj->fetch_first($sql);
    }

    public function doEdit($id, $array) {
        if (isset($array['password']) && !empty($array['password'])) {
            $array['password'] = md5($array['password']);
        }
        return parent::$obj->update(DB_PREFIXSA.'admin', $array, 'adminid='.$id.'');
    }

    public function doCheckUserName($username) {
        $sql = "SELECT adminid FROM ".DB_PREFIXSA."admin WHERE lower(adminname)='".strtolower($username)."'";
        return parent::$obj->check_data($sql);
    }

    public function doAdd($array) {
        $array['password'] = md5($array['password']);
        $adminid = parent::$obj->fetch_newid('SELECT MAX(adminid) FROM '.DB_PREFIXSA.'admin', 1);
        $array = array_merge(array(
                'adminid'=>$adminid,
                'timeline'=>time(),
            ), $array);
        return parent::$obj->insert(DB_PREFIXSA.'admin', $array); 
    }

    public function doCheckUid($id) {
        if ($id == parent::$wrap_admin['adminid']) {
            return true;
        }
        else {
            return false;
        }
    }

    public function doDel($id) {
        $sql = "DELETE FROM ".DB_PREFIXSA."admin WHERE adminid='".intval($id)."'";
        var_dump($sql);die;
        return parent::$obj->query($sql);
    }

}

