<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * User model class
 */

class User_model extends MY_Model
{

    /**
     * 테이블명
     */
    public $_table = 'user';

    /**
     * 사용되는 테이블의 프라이머리키
     */
    public $primary_key = 'user_id'; // 사용되는 테이블의 프라이머리키

    public $search_sfield = '';

    function __construct()
    {
        parent::__construct();
    }


    public function get_by_id($id = 0, $select = '')
    {
        $id = (int) $id;
        if (empty($id) OR $id < 1) {
            return false;
        }
        $where = array('user_id' => $id);
        return $this->get_one('', $select, $where);
    }


    public function get_by_email($email = '', $select = '')
    {
        if (empty($email)) {
            return false;
        }
        $where = array('user_email' => $email);
        return $this->get_one('', $select, $where);
    }

    public function get_by_api_email($app_id = '', $email = '', $select = '')
    {
        if (empty($app_id)) {
            return false;
        }
        if (empty($email)) {
            return false;
        }
        $where = array('app_id' => $app_id, 'user_email' => $email);
        return $this->get_one('', $select, $where);
    }


	public function get_by_token($app_id = '', $token = '', $select = '')
    {
        if (empty($token)) {
            return false;
        }
        $where = array('app_id' => $app_id, 'user_token' => $token);
        return $this->get_one('', $select, $where);
    }


    public function get_superadmin_list($select='')
    {
        $where = array(
            'user_is_admin' => 1,
            'user_denied' => 0,
        );
        $result = $this->get('', $select, $where);

        return $result;
    }


    public function get_admin_list($limit = '', $offset = '', $where = '', $like = '', $findex = '', $forder = '', $sfield = '', $skeyword = '', $sop = 'OR')
    {
        $result = $this->_get_list_common($select = '', $join = '', $limit, $offset, $where, $like, $findex, $forder, $sfield, $skeyword, $sop);

        return $result;
    }


    public function get_register_count($type = 'd', $start_date = '', $end_date = '', $orderby = 'asc')
    {
        if (empty($start_date) OR empty($end_date)) {
            return false;
        }
        $left = ($type === 'y') ? 4 : ($type === 'm' ? 7 : 10);
        if (strtolower($orderby) !== 'desc') $orderby = 'asc';

        $this->db->select('count(*) as cnt, left(user_register_datetime, ' . $left . ') as day ', false);
        $this->db->where('left(user_register_datetime, 10) >=', $start_date);
        $this->db->where('left(user_register_datetime, 10) <=', $end_date);
        $this->db->where('user_denied', 0);
        $this->db->group_by('day');
        $this->db->order_by('user_register_datetime', $orderby);
        $qry = $this->db->get($this->_table);
        $result = $qry->result_array();

        return $result;
    }
}
