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

class Track_model extends CI_Model
{
    protected $_table = 'tracks';
    protected $_table_user = 'users';
    protected $_table_track_user = 'track_user';
    protected $_table_track_artists = 'track_artists';

    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($dataInsert)
    {
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    /**
     * Xử lý thông tin ca sĩ
     *
     * @param $user_id
     * @param $track_id
     */
    public function add_singer($user_id, $track_id)
    {
        // Kiểm tra tồn tại singer
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query_user = $this->db->get($this->_table_user)->result_array();
        if ($query_user) {
            // Kiểm tra tồn tại track
            $this->db->select('id');
            $this->db->where('id', $track_id);
            $query_track = $this->db->get($this->_table)->result_array();
            if ($query_track) {
                // Kiểm tra tồn tại track-singer
                $this->db->select('*');
                $this->db->where('track_id', $track_id);
                $this->db->where('user_id', $user_id);
                $this->db->limit(1);
                $query_singer = $this->db->get($this->_table_track_user)->result_array();
                // Nếu chưa có thì insert, có rồi thì thôi
                if (empty($query_singer)) {
                    $this->db->set('track_id', $track_id);
                    $this->db->set('user_id', $user_id);
                    $this->db->set('name', $query_user[0]['full_name']);
                    $this->db->insert($this->_table_track_user);
                }
            }
        }
    }

    /**
     * Xử lý thông tin nhạc sĩ
     *
     * @param $user_id
     * @param $track_id
     */
    public function add_artist($user_id, $track_id)
    {
        // Kiểm tra tồn tại user (composer)
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query_user = $this->db->get($this->_table_user)->result_array();

        if ($query_user) {
            // Kiểm tra tồn tại track
            $this->db->select('id');
            $this->db->where('id', $track_id);
            $query_track = $this->db->get($this->_table)->result_array();

            if ($query_track) {
                $this->db->select('*');
                $this->db->where('track_id', $track_id);
                $this->db->limit(1);
                $query_composer = $this->db->get($this->_table_track_artists)->result_array();

                // Tạo dữ liệu mới
                $this->db->set('track_id', $track_id);
                $this->db->set('user_id', $user_id);
                $this->db->set('name', $query_user[0]['full_name']);

                // Tồn tại thông tin nhạc sĩ thì update, trái lại thì insert
                if ($query_composer) {
                    $this->db->where('id', $query_composer[0]['id']);
                    $this->db->update($this->_table_track_artists);
                } else {
                    $this->db->insert($this->_table_track_artists);
                }
            }
        }
    }

    public function update($id, $dataUpdate, $art_path)
    {
        $this->db->set('title', $dataUpdate);
        $this->db->set('art', $art_path);
        $this->db->where('id', $id);
        $this->db->update($this->_table);
    }

    /**
     * Chỉ cập nhật ảnh cho bài hát
     *
     * @param $id
     * @param $art_path
     */
    public function update_art($id, $art_path, $art_type='same')
    {
        $this->db->set('art', $art_path);
        $this->db->set('art_type', $art_type);
        $this->db->where('id', $id);
        $this->db->update($this->_table);
    }

    public function update_track_artists($id_track, $user_id, $id, $active)
    {
        // die($dataUpdate);
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        if ($active == 'insert') {
            $this->db->set('track_id', $id_track);
            $this->db->insert($this->_table_track_artists);
        } else {
            $this->db->where('track_id', $id_track);
            $this->db->where('id', $id);
            $this->db->update($this->_table_track_artists);
        }
    }

    public function update_track_user($id_track, $user_id, $id, $active)
    {
        $this->db->select('full_name');
        $this->db->where('id', $user_id);
        $query = $this->db->get('users')->result_array();
        $this->db->set('user_id', $user_id);
        if ($query) {
            $this->db->set('name', $query[0]['full_name']);
        }
        if ($active == 'insert') {
            $this->db->set('track_id', $id_track);
            $this->db->insert($this->_table_track_user);
        } else {
            $this->db->where('track_id', $id_track);
            $this->db->where('id', $id);
            $this->db->update($this->_table_track_user);
        }
    }

    public function getById($id)
    {
        $this->db->select($this->_table . '.*, track_user.name AS name_singer, 
            track_user.id AS id_singer,
            track_artists.name AS artist, 
            track_artists.id AS id_track_artist');
        $this->db->from($this->_table);
        $this->db->join($this->_table_track_user, 'track_user.track_id = tracks.id', 'left');
        $this->db->join($this->_table_track_artists, 'track_artists.track_id = tracks.id', 'left');
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Lấy ds ca sĩ theo ID của bài hát từ bảng track_user
     *
     * @param $id
     * @return mixed
     */
    public function getSingerLítByTrackId($id)
    {
        $this->db->select('track_user.name AS name_singer, track_user.user_id AS id_singer');
        $this->db->from($this->_table_track_user);
        $this->db->join('tracks', 'tracks.id = track_user.track_id', 'left');
        $this->db->where($this->_table_track_user . '.track_id', $id);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm xóa ca sĩ khỏi bài hát
     *
     * @param $track_id
     * @param $singer_id
     * @return boolean
     */
    public function removeSinger($track_id, $singer_id)
    {
        $this->db->where('track_id', $track_id);
        $this->db->where('user_id', $singer_id);
        $this->db->delete($this->_table_track_user);
        return true;
    }
     
    public function getByIdUser($table, $id) {
        if ($id != 0) {
            $this->db->select("*");
            $this->db->where('id', $id);
            return $this->db->get($table)->result_array();
        } else
            return false;
        
    }
    /**
     * Lay cuoc thi hien hanh
     * @return mixed
     */
    public function getCurrentContest()
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where('is_active', 1);
        $this->db->order_by('created_at', 'desc');
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function cmsCountAll($name = null, $fBySinger = null, $fByComposer = null, $fBySource = null, $fByBlacklist = null, $fByRemoveRanking = null)
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('title', $name, 'both');
            $this->db->or_like('description', $name, 'both');
            $this->db->or_like('slug', $name, 'both');
            $this->db->or_like('slug', MyHelper::genSlug($name), 'both');
            $this->db->or_like('tag', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        if (!empty($fBySource)) {
            switch ($fBySource) {
                case 'ZING':
                    $this->db->where('id_zing is NOT NULL', null, false);
                    $this->db->where('id_zing !=', '');
                    break;
                case 'NCT':
                    $this->db->where('id_nct is NOT NULL', null, false);
                    $this->db->where('id_nct !=', '');
                    break;
                case 'KEENG':
                    $this->db->where('id_keeng is NOT NULL', null, false);
                    $this->db->where('id_keeng !=', '');
                    break;
            }
        }
        if ($fBySinger == 1) {
            $this->db->where('singer_list is NOT NULL', null, false);
        } else if ($fBySinger == 2) {
            $this->db->where('singer_list', null);
        }
        if ($fByComposer == 1) {
            $this->db->where('composer_list is NOT NULL', null, false);
        } else if ($fByComposer == 2) {
            $this->db->where('composer_list', null);
        }
        if ($fByBlacklist == 1) {
            $this->db->where('blacklist_listen is NOT NULL', null, false);
        }
        if ($fByRemoveRanking == 1) {
            $this->db->where('is_remove_from_ranking', $fByRemoveRanking);
        }
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    public function cmsGetPagination($record, $start, $name = null, $fBySinger = null, $fByComposer = null, $fBySource = null, $fByBlacklist = null, $fByRemoveRanking = null)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        if ($name != null) {
            $this->db->like('tracks.title', $name, 'both');
            $this->db->or_like('tracks.description', $name, 'both');
            $this->db->or_like('tracks.slug', $name, 'both');
            $this->db->or_like('tracks.slug', MyHelper::genSlug($name), 'both');
            $this->db->or_like('tracks.tag', $name, 'both');
            $this->db->or_where('id', intval($name));
        }
        if (!empty($fBySource)) {
            switch ($fBySource) {
                case 'ZING':
                    $this->db->where('id_zing is NOT NULL', null, false);
                    $this->db->where('id_zing !=', '');
                    break;
                case 'NCT':
                    $this->db->where('id_nct is NOT NULL', null, false);
                    $this->db->where('id_nct !=', '');
                    break;
                case 'KEENG':
                    $this->db->where('id_keeng is NOT NULL', null, false);
                    $this->db->where('id_keeng !=', '');
                    break;
            }
        }
        if ($fBySinger == 1) {
            $this->db->where('singer_list is NOT NULL', null, false);
        } else if ($fBySinger == 2) {
            $this->db->where('singer_list', null);
        }
        if ($fByComposer == 1) {
            $this->db->where('composer_list is NOT NULL', null, false);
        } else if ($fByComposer == 2) {
            $this->db->where('composer_list', null);
        }
        if ($fByBlacklist == 1) {
            $this->db->where('blacklist_listen is NOT NULL', null, false);
        }
        if ($fByRemoveRanking == 1) {
            $this->db->where('is_remove_from_ranking', $fByRemoveRanking);
        }
        $this->db->order_by($this->_table . '.id', 'desc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        //
        return $query->result_array();
    }

    public function getListForSelectBox()
    {
        $this->db->select('id, fullname, username, sid');
        $this->db->from($this->_table);
        $this->db->where('sid !=', '');
        $this->db->order_by($this->_table . '.username', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListUsers()
    {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->order_by('users.full_name', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH ngày trong bảng report_views_daily
     * @param $daily_at
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportDaily($daily_at, $limit = 100, $offset = 0)
    {
        $this->db->from('report_views_daily');
        $this->db->where('date =', $daily_at);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH tuần trong bảng report_views_weekly (cũ) và bảng report_views_weekly_new (từ 29/3-4/4)
     *
     * @param $week
     * @param $year
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportWeekly($week, $year, $limit = 100, $offset = 0)
    {
        //$sql = "SELECT * FROM report_views_weekly WHERE `week` = ? AND `year` = ? ORDER BY total_point DESC LIMIT {$offset}, {$limit} ";
        //$query  = $this->db->query($sql, $week, $year);

        if ($week >= 14 && $year >= 2020) {
            $this->db->from('report_views_weekly_new');
        } else {
            $this->db->from('report_views_weekly');
        }

        $this->db->where('week =', $week);
        $this->db->where('year =', $year);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy dữ liệu BXH tháng
     *
     * @param $month
     * @param $year
     * @param int $limit
     * @param int $offset
     *
     * @return mixed
     */
    public function getReportMonthly($month, $year, $limit = 100, $offset = 0)
    {
        //$sql = "SELECT * FROM reports_views_monthly WHERE `month` = ? AND `year` = ? ORDER BY total_point DESC LIMIT {$offset}, {$limit} ";
        //$query  = $this->db->query($sql, $month, $year);

        if ($month >= 4 && $year >= 2020) {
            $this->db->from('report_views_monthly_new');
        } else {
            $this->db->from('reports_views_monthly');
        }

        $this->db->where('month =', $month);
        $this->db->where('year =', $year);
        $this->db->limit($limit, $offset);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getUserByTrack($track_id) {
        //$query = $this->db->query("SELECT * FROM track_user WHERE track_id=?", $track_id);
        //if ($query) $users = $query->result_array();
        //return $users;

        $this->db->from($this->_table_track_user);
        $this->db->where('track_id =', $track_id);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getArtistsByTrack($track_id) {
        //$query = $this->db->query("SELECT * FROM track_artists WHERE track_id=?", $track_id);
        //if ($query) $users = $query->result_array();
        //return $users;

        $this->db->from($this->_table_track_artists);
        $this->db->where('track_id =', $track_id);
        $query = $this->db->get();

        return $query->result_array();
    }

    public function getTrackById($id)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getByTitle($title, $id)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where($this->_table . '.title', $title);
        if ($id) {
            $this->db->where_not_in('id', $id);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Hàm lấy ds bài hát chưa cập nhật thông tin ca sĩ
     *
     * @return mixed
     */
    public function getTrackUpdateSinger($limit=50)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where('singer_list is NULL', null, false);
        $this->db->or_where('singer_list', '');
        $this->db->limit($limit);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy tổng bài hát cần cập nhật ca sĩ
     *
     * @return int
     */
    public function getTotalTrackUpdateSinger()
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        $this->db->where('singer_list is NULL', null, false);
        $this->db->or_where('singer_list', '');
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }

    /**
     * Hàm lấy ds bài hát chưa cập nhật thông tin nhạc sĩ
     *
     * @return mixed
     */
    public function getTrackUpdateComposer($limit=50)
    {
        $this->db->select($this->_table . '.*');
        $this->db->from($this->_table);
        $this->db->where('composer_list is NULL', null, false);
        $this->db->or_where('composer_list', '');
        $this->db->limit($limit);
        $query = $this->db->get();

        return $query->result_array();
    }

    /**
     * Hàm lấy tổng bài hát cần cập nhật nhạc sĩ
     *
     * @return int
     */
    public function getTotalTrackUpdateComposer()
    {
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        $this->db->where('composer_list is NULL', null, false);
        $this->db->or_where('composer_list', '');
        $query = $this->db->get();
        $result = $query->result_array();
        if ($result) {
            return $result[0]['totalResults'];
        } else {
            return 0;
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */