<?php
class newsAction
{
	private $HEAD_CSSJS = ['css'=>['css/article.css', 'css/account.css' ], 'js'=>['js/account.js','js/article.js'], 'plugin'=>[]];

	function __construct()
	{
		LibTpl::Set('HEAD_CSSJS', $this->HEAD_CSSJS);
	}

	/**
	 * @method 查看公告/文章列表
	 * @author 廖
	 * @copyright 2018-1-3
	 */
	function index()
	{
		$B2cArticle = new B2cArticle();
		//公告 
		$cat = ['867','868'];
		//分页取数据
		$table = 'cm_article left join cm_article_category on art_id=arc_art_id';
		$res = $B2cArticle->get($table,['count(*) count','arc_cat_id'],sprintf("and arc_cat_id in(%s) and art_status='pass' group by arc_cat_id",implode(',',$cat)));
		$total = array_column($res['data'],'count','arc_cat_id');
		//获取公告部分
		$perPage=30;
		$cat = ['867'];
		$filed = ['art_id,art_title,(case when art_last_time>art_add_time then art_last_time else art_add_time end) timeseq,arc_cat_id'];
		if($total[$cat[0]]>0)
		{
			$where = sprintf("and arc_cat_id in(%s) and art_status='pass' order by art_seq asc,timeseq desc limit %d",implode(',',$cat), $perPage);
			$res = $B2cArticle->get($table, $filed, $where);
			LibTpl::Set('annouArr', $res['data']);
			$Page = LibPage::Show($total[$cat[0]], $perPage);
			LibTpl::Set('PageAnnou', $Page);
		}
		//促销
		$cat = ['868'];
		if($total[$cat[0]]>0)
		{
			$where = sprintf("and arc_cat_id in(%s) and art_status='pass' order by art_seq asc,timeseq desc limit %d ",implode(',',$cat), $perPage);
			$res = $B2cArticle->get($table, $filed, $where);
			LibTpl::Set('saleArr', $res['data']);
			$Page = LibPage::Show($total[$cat[0]], $perPage);
			LibTpl::Set('PageSale', $Page);
		}
		$headArr = array(
			'title'=>'宜配公告-跑街的',	
			'keyword'=>'',	
			'des'=>''
		);
		LibTpl::Set('headArr', $headArr);
		LibTpl::Put();
	}
	/**
	 * @method 查看公告/文章
	 * @author 廖
	 * @copyright 2018-1-3
	 */
	function notice()
	{
		//首先获取帮助中心的全部文章
		$Params = mvc::$URL_PARAMS;
		$title = '公告';
		if(LibFc::Int($Params['artId']))
		{
			$B2cBase = new B2cBase();
			$cat = ['867','868'];
			if(in_array($Params['catId'],$cat))
			{
				$res = $B2cBase->get(sprintf('(select art_id,art_status,art_title,art_txt,(case when art_last_time>art_add_time then art_last_time else art_add_time end) timeseq,arc_cat_id,lead(art_id,1) over(order by art_seq asc,(case when art_last_time>art_add_time then art_last_time else art_add_time end) desc) prev_id,lag(art_id,1) over(order by art_seq asc,(case when art_last_time>art_add_time then art_last_time else art_add_time end) desc) Next_id from cm_article left join cm_article_category on art_id=arc_art_id where arc_cat_id=%d)',$Params['catId']),['*'],"and art_status='pass' and art_id=".$Params['artId'],true);
			}
			else
			{
				//其他公告
				$cat = ['863','865'];
				$res = $B2cBase->get('cm_article left join cm_article_category on art_id=arc_art_id',['art_id','art_title','art_txt'],sprintf('and art_id=%d and arc_cat_id in(%s)',$Params['artId'],implode(',',$cat)),true);
			}
			if(!empty($res['data']['art_title'])) $title = $res['data']['art_title'];
			LibTpl::Set('art', $res['data']);
		}
		else
		{
			LibTpl::Error('没有内容');
			exit;
		}
		$headArr = array(
			'title'=>$title.'-跑街的',	
			'keyword'=>'',	
			'des'=>''
		);
		LibTpl::Set('headArr', $headArr);
		LibTpl::Put();
	}
	/**
	 * @method ajax获取公告/文章
	 * @author 廖
	 * @copyright 2018-2-26
	 */
	function ajaxGetNews()
	{
		$arr = ['annou'=>867,'sales'=>868];
		if(LibFc::Int($arr[$_POST['order']]))
		{
			$B2cBase = new B2cBase();
			$keyword = '';
			if(!empty($_POST['keyword']))
			{
				$keyword = sprintf("and art_title like '%%%s%%'",LibFc::Escape($_POST['keyword']));
				LibTpl::Set('keywordSearch',$_POST['keyword']);
			}
			$table = 'cm_article left join cm_article_category on art_id=arc_art_id';
			$res = $B2cBase->get($table,['count(*) count'],sprintf("and arc_cat_id=%d %s and art_status='pass'",$arr[$_POST['order']],$keyword),true);
			$total = $res['data']['count'];
			LibTpl::Set('order',$_POST['order']);
			if($total>0)
			{
				$nowpage = LibFc::Int($_POST['page'])?$_POST['page']:1;
				LibTpl::Set('nowpage',$nowpage);
				$perPage = 30;
				$where = sprintf("and arc_cat_id=%d %s and art_status='pass' order by art_seq asc,timeseq desc limit %d,%d",$arr[$_POST['order']],$keyword, ($nowpage-1)*$perPage, $perPage);
				$filed = ['art_id,art_title,arc_cat_id,(case when art_last_time>art_add_time then art_last_time else art_add_time end) timeseq'];
				$res = $B2cBase->get($table, $filed, $where);
				LibTpl::Set('Arr',$res['data']);
			}
			$Page = LibPage::Show($total, $perPage);
			LibTpl::Set('Page', $Page);
			$Tpl = LibTpl::Fetch('news/ajaxGetNews.php');
			$array = array('status'=>true,'data'=>$Tpl,'code'=>0);
			LibFc::ajaxJsonEncode($array);
		}
		else LibFc::ajaxJsonEncode(['status'=>false,'data'=>'参数不正确']);
	}

}