<?php
namespace wstmart\app\model;
use think\Model;
use think\Db;
/**
 * ============================================================================
 *  文章类
 */
class AppPort extends Model{
	/*
	 * APP首页轮播图
	 * */
	public function appBanner(){
		$result=listAds('app_new_top_banner',8,0);
		return $result;
	}
	/**
	 * app新版桔子头条
	 */
	public function appNews(){
		$catId=8;
		$where = array();
		$where['isShow'] = 1;
		$where['dataFlag'] = 1;
		$catIds=db('article_cats')->where(['parentId'=>$catId,'dataFlag'=>1])->column('catId');
		$where['catId']=array('in',$catIds);
		$result = Db::name('articles')->where($where)->field('articleId,articleTitle,catId')->select();
		return $result;
	}
	/**
	 * app新版桔子头条分类接口
	 */
	public function appNewsClass(){
		$catId=(int)input('catId/d');
		$where = array();
		$where['isShow'] = 1;
		$where['dataFlag'] = 1;
		$id=db('article_cats')->where(['catId'=>$catId,'dataFlag'=>1])->value('parentId');
		$where['parentId']=$id;
		$result = Db::name('article_cats')->where($where)->select();
		return $result;
	}
	/**
	 * app新版桔子头条
	 */
	public function appNewsLists(){
		$catId=(int)input('catId/d');
		$articleId=(int)input('articleId');
		$where = array();
		$where['isShow'] = 1;
		$where['dataFlag'] = 1;
		$id=db('article_cats')->where(['catId'=>$catId,'dataFlag'=>1])->value('parentId');
		$where['parentId']=$id;
		$result = Db::name('article_cats')->where($where)->select();
		return $result;
	}
	/**
	 * app新版桔子头条详情页借口
	 */
	public function appNewsDetail(){
		$articleId=(int)input('articleId');
		$where = array();
		$where['isShow'] = 1;
		$where['dataFlag'] = 1;
		$where['articleId']=$articleId;
		$result = Db::name('articles')->where($where)->select();
		return $result;
	}
	/*
	 * app新版秒杀接口
	 * */
	public function appSecKill(){
		$now=time();
		$where=[];
		$where['startTime']=array('elt',$now);
		$where['endTime']=array('egt',$now);
		$result=Db::name('hyhsale_goods')->alias('gu')
				->join('__GOODS__ g','gu.goodsId=g.goodsId','inner')
				->where('g.dataFlag=1 and g.isSale=1 and g.goodsStatus=1 and gu.dataFlag=1 and gu.hyhsaleStatus=1 and gu.isSelf=0')
				->where($where)
				->field('gu.goodsId,g.marketPrice,gu.goodsPrice,gu.totalNum,gu.startTime,gu.endTime,g.goodsName,g.goodsImg,gu.specsId')
				->select();
		foreach($result as &$v){
			$v['goodsImg']=WSTImg($v['goodsImg'],3);
		}		
		return $result;
	}
	/*
	 * app新版ect专区/活动层接口
	 * */
	public function appEctAct(){
		$result['ect']=listAds('app_new_ect_left',1,0);
		$result['act']=listAds('app_new_act_photo',1,0);
		return $result;
	}
	/*
	 * app新版品牌热卖接口
	 * */
	public function appBrands(){
		$result['brands']=listAds('app_new_page_brand',1,0);
		$result['brandsList']=db('brands b')
				->join('hyhbrands_rec hr','b.brandId=hr.brandId')
				->whereNotIn('hr.isSelf',1)
				->field('b.brandId,brandName,brandImg,shopId,findOrder')
				->where(['dataFlag'=>1])->limit(6)->order('findOrder desc')->select();
		return $result;
	}
	/*
	 * app新版居家好帮手广告位接口
	 * */
	public function appJadvertis(){
		//居家好帮手左上位置
		$row['left']=listAds('app_new_jujia_lt',2,0);
		$row['right']=listAds('app_new_jujia_rt',2,0);
		$result['zxz']=listAds('app_new_jujia_lul',1,0);
		$result['zxy']=listAds('app_new_jujia_lur',1,0);
		$result['yxz']=listAds('app_new_jujia_rul',1,0);
		$result['yxy']=listAds('app_new_jujia_rur',1,0);
		$row['under']= array_merge($result['zxz'],$result['zxy'],$result['yxz'],$result['yxy']);
		return $row;
	}
	/*
	 * app新版潮流先锋广告位接口
	 * */
	public function appCadvertis(){
		//潮流先锋左上位置
		$row['left']=listAds('app_new_chao_lt',2,0);
		//潮流先锋左下左位置
		$result['zxz']=listAds('app_new_chao_lul',1,0);
		//潮流先锋左下右位置
		$result['zxy']=listAds('app_new_chao_lur',1,0);
		//潮流先锋右上位置
		$row['right']=listAds('app_new_chao_rt',2,0);
		//潮流先锋右下左位置
		$result['yxz']=listAds('app_new_chao_rul',1,0);
		//潮流先锋右下右位置
		$result['yxy']=listAds('app_new_chao_rur',1,0);
		$row['under']= array_merge($result['zxz'],$result['zxy'],$result['yxz'],$result['yxy']);
		return $row;
	}
	/*
	 * app新版食品超市广告位接口
	 * */
	public function appSadvertis(){
		//食品超市左上位置
		$row['left']=listAds('app_new_shi_lt',2,0);
		//食品超市左下左位置
		$result['zxz']=listAds('app_new_shi_lul',1,0);
		//食品超市左下右位置
		$result['zxy']=listAds('app_new_shi_lur',1,0);
		//食品超市右上位置
		$row['right']=listAds('app_new_shi_rt',2,0);
		//食品超市右下左位置
		$result['yxz']=listAds('app_new_shi_rul',1,0);
		//食品超市右下右位置
		$result['yxy']=listAds('app_new_shi_rur',1,0);
		$row['under']= array_merge($result['zxz'],$result['zxy'],$result['yxz'],$result['yxy']);
		return $row;
	}
	/*
	 * app新版家电馆广告位接口
	 * */
	public function appDadvertis(){
		//家电馆左上位置
		$row['left']=listAds('app_new_dian_lt',2,0);
		//家电馆左下左位置
		$result['zxz']=listAds('app_new_dian_lul',1,0);
		//家电馆左下右位置
		$result['zxy']=listAds('app_new_dian_lur',1,0);
		//家电馆右上位置
		$row['right']=listAds('app_new_dian_rt',2,0);
		//家电馆右下左位置
		$result['yxz']=listAds('app_new_dian_rul',1,0);
		//家电馆右下右位置
		$result['yxy']=listAds('app_new_dian_rur',1,0);
		$row['under']= array_merge($result['zxz'],$result['zxy'],$result['yxz'],$result['yxy']);
		return $row;
	}
	/*
	 * app为你推荐接口
	 * */
	public function recommend(){
		$userId=session('WST_USER.userId');
		$page = (int)input('post.page/d');
		$cacheData = cache('APP_CATS_RECOMMEND_'.$page.'_'.$userId);
		if($cacheData)return $cacheData;
		$childId=[];
		$catId=[];
		$goodsCat=[];
		$goods_ids=db('page_view')->where('userId',$userId)->field('count(goodsId)num,goodsId')->group('goodsId')->order('num desc')->limit('10')->select();
		foreach($goods_ids as $key=>$value){
			$childId[]=$value['goodsId'];
		}
		if(!$userId ||count($childId)<4){
			$goods_ids=db('page_view')->field('count(goodsId)num,goodsId')->group('goodsId')->order('num desc')->limit('10')->select();
			foreach($goods_ids as $key=>$value){
				$childId[]=$value['goodsId'];
			}
		}

		$catIds=db('goods')->whereIn('goodsId',$childId)->field('goodsCatIdPath')->select();
		foreach($catIds as $key=>$value){
			$cat=explode("_",$value['goodsCatIdPath']);
			//$goodsCat=current($cat);
			$goodsCatId=next($cat);
			$cat=db('goods_cats')->where(['catId'=>$goodsCatId,'dataFlag'=>1])->value('catId');
			$catId[]=$cat;
		}
		$catId=array_unique(array_filter($catId));
		//dump($catId);
		$childId=db('goods_cats')->whereIn('parentId',$catId)->field('catId')->select();
		//dump($childId);
		foreach($childId as $k=>$v){
			foreach($v as $kk=>$vv){
				$goodsCat[]=$vv;
			}
		}
		$goods=db('goods')->whereIn('goodsCatId',$goodsCat)->where('dataFlag=1 AND isSale=1 AND goodsStatus=1')
				->field('goodsId,goodsName,goodsImg,shopId,marketPrice,shopPrice')
				->paginate(input('pagesize/d'))->toArray();
		cache('APP_CATS_RECOMMEND_'.$page.'_'.$userId,$goods,3600);
		return $goods;
	}
}