<?php

namespace Hura8\Components\Article\Model;

class UArticleModel extends ArticleModel
{

    public function getSameCategoryArticle($main_id, $category_id){

        $query = $this->db->runQuery("
                                (
                                    SELECT `item_id`
                                    FROM ".$this->tb_article_per_category."
                                    WHERE `category_id` = ? AND `status`=1 AND `item_id` > ?
                                    ORDER BY `item_id` DESC
                                    LIMIT 10
    
                                ) UNION ALL (
                                    SELECT `item_id`
                                    FROM ".$this->tb_article_per_category."
                                    WHERE `category_id` = ? AND `status`=1 AND `item_id` < ?
                                    ORDER BY `item_id` DESC
                                    LIMIT 10
                                )
							",
            ['d', 'd', 'd', 'd'],
            [$category_id, $main_id, $category_id, $main_id]
        );

        $article_list_id = [];
        $article_item_info = array();
        $article_item = [];

        foreach ( $this->db->fetchAll($query) as $rs ) {
            if(!isset($article_item_info[$rs["item_id"]])) $article_item_info[$rs["item_id"]] = array();
            if(!in_array($rs["item_id"], $article_list_id)) $article_list_id[] = $rs["item_id"];

            if($rs["item_id"] > $main_id) {
                $article_item['new'][$rs["item_id"]] = &$article_item_info[$rs["item_id"]];
            }
            else {
                $article_item['old'][$rs["item_id"]] = &$article_item_info[$rs["item_id"]];
            }
        }

        $list_article_info = $this->getListByIds($article_list_id);
        foreach ($article_list_id as $_id) {
            if(isset($list_article_info[$_id])) $article_item_info[$_id] = $list_article_info[$_id];
        }


        return $article_item;
    }

}
