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

/**
 * Contents model class
 */

class Contents_model extends MY_Model
{

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

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

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


    public function get_admin_list($limit = '', $offset = '', $where = '', $like = '', $findex = '', $forder = '', $sfield = '', $skeyword = '', $sop = 'OR')
    {
        $select = 'contents.*, user.user_id, user.user_username, user.user_is_admin, user.user_icon, crawl_list.crawl_title, crawl_list.site_type, crawl_list.site_key';
		$join[] = array('table' => 'user', 'on' => 'contents.user_id = user.user_id', 'type' => 'inner');
		$join[] = array('table' => 'crawl_list', 'on' => 'contents.crawl_id = crawl_list.crawl_id', 'type' => 'left');
		if (isset($where['contents_tag.tag_name']) && $where['contents_tag.tag_name']) {
			$join[] = array('table' => 'contents_tag', 'on' => 'contents.con_id = contents_tag.con_id', 'type' => 'inner');
		}
		$result = $this->_get_list_common($select, $join, $limit, $offset, $where, $like, $findex, $forder, $sfield, $skeyword, $sop);
		return $result;
    }

    public function get_api_list($app_id = '', $is_bookmark = '', $user_id = '', $tag = '', $cat1_id = '', $cat2_id = '', $for_push = '')
    {
		$this->db->where('contents.app_id', $app_id);
		$this->db->where('contents.is_open', '1');
		$this->db->where('contents.is_del', '0');
		$this->db->where('con_published_at <=', cdate('Y-m-d H:i:s'));
		$this->db->where('con_published_at is not null');
		if ($for_push) {
			$this->db->where('contents.is_push', '1');
			$this->db->where('con_published_at >=', cdate('Y-m-d H:i:s', time()-86400));
		}

		if ($cat1_id) {
			$this->db->where('cat1_id', $cat1_id);
		}
		if ($cat2_id) {
			$this->db->where('cat2_id', $cat2_id);
		}
		//$this->db->order_by('con_sort', 'desc');
		$this->db->order_by('con_published_at', 'desc');
		if ($is_bookmark && $user_id) {
			$this->db->join('contents_bookmark', 'contents.con_id = contents_bookmark.con_id', 'inner');
			$this->db->where('contents_bookmark.user_id', $user_id);
		}
		if ($tag) {
			$this->db->join('contents_tag', 'contents.con_id = contents_tag.con_id', 'inner');
			$this->db->where('contents_tag.tag_name', $tag);
		}
		$qry = $this->db->get($this->_table);
		$result = $qry->result_array();
		return $result;
    }

    public function get_api_popular_list($app_id = '', $is_bookmark = '', $user_id = '', $tag = '', $cat1_id = '', $cat2_id = '')
    {
		//$this->db->select('contents.*, count(like_id) cnt');
		$this->db->where('contents.app_id', $app_id);
		$this->db->where('contents.is_open', '1');
		$this->db->where('contents.is_del', '0');
        $this->db->where('contents.con_like >', '0');
		$this->db->where('con_published_at <=', cdate('Y-m-d H:i:s'));
		$this->db->where('con_published_at is not null');

		if ($cat1_id) {
			$this->db->where('cat1_id', $cat1_id);
		}
		if ($cat2_id) {
			$this->db->where('cat2_id', $cat2_id);
		}
		if ($is_bookmark && $user_id) {
			$this->db->join('contents_bookmark', 'contents.con_id = contents_bookmark.con_id', 'inner');
			$this->db->where('contents_bookmark.user_id', $user_id);
		}
		if ($tag) {
			$this->db->join('contents_tag', 'contents.con_id = contents_tag.con_id', 'inner');
			$this->db->where('contents_tag.tag_name', $tag);
		}
//		$this->db->join('contents_like', 'contents.con_id = contents_like.con_id', 'left');
//        $this->db->where('contents.con_like >', '0');
//        $this->db->where('contents_like.like_at >', cdate('Y-m-d H:i:s', time()-7*86400));
        $this->db->group_by('con_id');
        //$this->db->order_by('cnt', 'desc');
        $this->db->order_by('con_like', 'desc');
		$qry = $this->db->get($this->_table);
		$result = $qry->result_array();
		return $result;
    }

	public function select_max_sort($app_id = '')
	{
		$this->db->where('app_id', $app_id);
		$this->db->select_max('con_sort');
		$qry = $this->db->get($this->_table);
		$result = $qry->row_array();
		return $result['con_sort'];
	}

	public function update_share_count($con_id = '')
	{
		$this->db->query("UPDATE ci_contents SET con_share_count = con_share_count + 1 WHERE con_id='".$con_id."'");
		return true;
	}

	public function plus_like_count($con_id = '')
	{
		$this->db->query("UPDATE ci_contents SET con_like = con_like + 1 WHERE con_id='".$con_id."'");
		return true;
	}

	public function minus_like_count($con_id = '')
	{
		$this->db->query("UPDATE ci_contents SET con_like = con_like - 1 WHERE con_id='".$con_id."'");
		return true;
	}
}

