<?php
/**
*广告服务
*/
class naAction extends baseAction
{
	/**
	  * @method 通过各种条件查询商城配件 返回广告代码
	  * @http  GET
	  * @param  type  string  [选填]  item(默认)|number
	  * @param  mod3Id  int  [选填]  车型ID
	  * @param  keyword string [选填] 关键字
	  * @author soul
	  * @copyright 2017/8/16
	  * @return 
		{"status":true,"data":html,"code":0}
		{"status":false,"data":'失败原因',"code":0}
	  */
	function getB2cProByAny()
	{
		$Params = mvc::$URL_PARAMS;
		$html = '';
		$type = empty($Params['type'])? 'item': $Params['type'];
		$B2cBase = new B2cBase();
		$fileArr = ['pro_id', 'pro_name', 'pro_sale_price', 'pro_promotion_btime', 'pro_promotion_etime', 'pro_promotion_price', 'shop_id', 'shop_uid', 'shop_domain', 'shop_name', 'spi_path', 'concat(spi_name, \'.\', spi_ext) as spi_name', 'shop_logo'];
		switch($type)
		{
			case 'item':
				$whereArr = [];
				if($Params['keyword'] != '')
				{
					$keywordArr = LibFc::keyword($Params['keyword']);
					$B2cKeyword = new B2cKeyword();
					$res = $B2cKeyword->getKeywordKeyIds($keywordArr);
					if(!empty($res['data']))
					{
						$whereArr['keyword'] = sprintf("(select prk_pro_id from bc_product_keyword where prk_key_id in (%s) group BY prk_pro_id HAVING count(*) = %d)", implode(',', $res['data']), count($res['data']));
					}
				}
				$where = '';
				if(!empty($whereArr['keyword']) && $Params['mod3Id'] > 0)
				{
					$where = sprintf("(select pml3_pro_id from bc_product_model3_link where pml3_mod3_id=%d and pml3_pro_id in %s)", (int) $Params['mod3Id'], $whereArr['keyword']);
				}
				else if(!empty($whereArr['keyword']))
				{
					$where = $whereArr['keyword'];
				}
				else if($Params['mod3Id'] > 0)
				{
					$where = sprintf("(select pml3_pro_id from bc_product_model3_link where pml3_mod3_id=%d)", (int) $Params['mod3Id']);
				}
				if($where != '')
				{
					$table = 'bc_product 
					  join sh_shop on pro_shop_id=shop_id and shop_status = 1 and shop_istest != 1 and pro_show_b2c =1
					  left join bc_product_pic on pp_pro_id=pro_id and pp_ismain=1
					  left join sh_pic on pp_spi_id = spi_id
					  left join bc_number on pro_id = num_pro_id and num_ismain = 1';
					$where = sprintf(" and pro_id in %s  and pro_status =1 order BY pro_sold_count desc, rand() limit 6", $where);
					$res = $B2cBase->get($table, $fileArr, $where);
					$proArr = $res['data'];
				}
			break;
			case 'number':
				$formatNum = LibFc::FormatNum($Params['keyword']);
				if(strlen($formatNum) > 2)
				{
					$table = 'bc_product 
					  join sh_shop on pro_shop_id=shop_id and shop_status = 1 and shop_istest != 1 and pro_show_b2c =1
					  left join bc_product_pic on pp_pro_id=pro_id and pp_ismain=1
					  left join sh_pic on pp_spi_id = spi_id
					  left join bc_number on pro_id = num_pro_id and num_ismain = 1';
					
					$where = sprintf(" and pro_id in (
						select distinct num_pro_id from bc_number where (num_format like '%s%%' or num_reformat like '%s%%') 
					  ) and pro_status =1 group by pro_id order BY pro_sold_count desc, rand() limit 6", $formatNum, strrev($formatNum));
					$res = $B2cBase->get($table, $fileArr, $where);
					$proArr = $res['data'];
				}
			break;
		}
		if(!empty($proArr))
		{
			$time = time();
			foreach($proArr as $k=>$v)
			{
				$v['pro_price'] = $v['pro_promotion_btime'] > $time && $v['pro_promotion_etime'] < $time && $v['pro_promotion_price'] > 0?  $v['pro_promotion_price']:  $v['pro_sale_price'];
				$v['pro_url'] = mvc::$cfg['urls']['b2c'].'item/index?proId='.$v['pro_id'];
				$v['pro_img'] = !empty($v['spi_path'])? mvc::$cfg['FILE_CENTER']['b2c']['url_get'].'&c=product&f='.$v['spi_path'].'240/'.$v['spi_name'].mvc::$cfg['FILE_CENTER']['b2c']['get_uri']: mvc::$cfg['urls']['b2c'].'static/images/noimg.gif';
				$proArr[$k] = $v;
			}
			LibTpl::Set('proArr', $proArr);
			$html = LibTpl::Fetch('ads/getB2cProByAny.php');
		}
		LibFc::ReturnJson(true, $html);
	}



	/**
	  * @method 获取跑街令广告信息
	  * @http   GET
	  * @param  paId	 int    [必填]  VIN解码:84
	  * @param  Ip     string   [选填]  IP地址   IP|pctIds二选一
	  * @param  pctIds string   [选填]  省市ID  19,6544
	  * @param     word	     string [选填]  关键字  VIN解码、车型查询传品牌ID
	  * @param     modtype	 string [VIN解码、车型查询必填]  VIN解码:解码接口的modtype   车型查询: 根据车型mod2_pc=1时传pc, mod2_cv=1时传cv
	  * @param  make_id int     [选填]  汽车生产商ID，有则传
	  * @param  all int         [选填]  默认0：只返回一条  1：返回所有
	  * @author soul
	  * @copyright 2019/10/15
	  * @return 
	  		展示渠道 ads_showby（ads:自有广告系统 union:第三方广告联盟）
			ads_union位广告联盟的广告代码信息（type:  baidu百度联盟, csj:穿山甲联盟）
	  		
			显示类型ads_show_style
			网址跳转:'' 对应  ads_url
			IM:im  对应 ads_ims
			资讯:zixun 对应 ads_art_id

		all=0
		{"status":true,"data":{"ads_id":"4","ads_name":"兴弛辉汽配跑街令开机","ads_url":"http:\/\/pjl.paojd.cn\/nas\/XCH\/XCH.html","ads_img":"http:\/\/192.168.1.42\/file_center\/center\/read.php?site=bna&c=bna&f=9\/ad626f53ce34d4a2.jpg&i=42","ads_title":"广州市兴弛辉汽配有限公司","ads_price":"0.00","ads_org_price":"0.00","ads_des":"","ads_proid":"0","ads_css":"","ads_show_style":"im","ads_ims":{"uid":"172999","u_name":"yipartsb2c"}},"code":0}

		all=1 
		{"status":true,"data":[{"ads_id":"16","ads_name":"昊阳减震器","ads_url":"#","ads_img":"http:\/\/192.168.1.42\/file_center\/center\/read.php?site=bna&c=bna&f=311\/aacbc2bb13122037.jpg&i=42","ads_title":"昊阳减震器","ads_price":"0.00","ads_org_price":"0.00","ads_des":"","ads_proid":"0","ads_css":"","ads_show_style":"im","ads_ims":{"uid":"172999","u_name":"yipartsb2c"}}....],"code":0,"encry":0,"runtime":107}

		{"status":true,"data":{"ads_id":"3","ads_showby":"union","ads_name":"开机默认广告广告招租","ads_url":"https:\/\/www.paojd.cn\/company\/info?ads_id=29","ads_art_id":"0","ads_img":"http:\/\/192.168.1.42\/file_center\/center\/read.php?site=bna&c=bna&f=181\/31503ff2b7be34ea.jpg&i=42","ads_title":"广州宜配信息科技有限公司","ads_price":"0.00","ads_org_price":"0.00","ads_des":"","ads_proid":"0","ads_css":"","ads_show_style":"","ads_ims":{},"ads_app_act":"openShop","ads_union":{"and":{"apid":"15667399","appsid":"e4d15880"},"ios":{"apid":"15667402","appsid":"e5c80b8c"},"type":"baidu","remark":"百度联盟开机广告"}},"code":"","runtime":60}
	  */
	public function pjlNa()
	{
		$Params = mvc::$URL_PARAMS;
		$paId = (int) $Params['paId'];
		if ($paId < 1) LibFc::ReturnJson(false, '广告位ID不正确');

		$arr = [];
		$AdsBase = new AdsBase();
		//广告位信息
		$res = $AdsBase->get('pj_place', ['pa_id, pa_is_area, pa_is_keyword, pa_keyword_type, pa_ads_id, pa_max'], sprintf(" and pa_id=%d", $paId), true);
		if (empty($res['data'])) LibFc::ReturnJson(false, '广告位ID不正确');
		$paArr = $res['data'];

		//区域
		$filed = ['ads_id', 'ads_showby', 'ads_name', 'ads_url', 'ads_art_id', 'ads_img', 'ads_title', 'ads_price', 'ads_org_price', 'ads_des', 'ads_proid', 'ads_css', 'ads_show_style', 'ads_ims', 'ads_app_act', 'ads_union'];
		$where = '';
		$pctArr = [];
		if($paArr['pa_is_area'])
		{ 
			if(!empty($Params['pctIds']))
			{
				$pctArr = [];
				foreach(explode(',', $Params['pctIds']) as $k=>$v)
				{
					$pctArr[$k] = (int) $v;
				}
			}

			if(empty($pctArr) && !empty($Params['Ip']))
			{
				$OthPct = new OthPct();
				$res = $OthPct->getIpPCT($Params['Ip']);
				$pctArr = $res['data']['pctids'];
			}

			if(!empty($pctArr)){
				$where .= sprintf(" and pat_pc_id=%d", $pctArr[0]);
			}else{
				$where .= ' and 1=0';
			}
		}else if($paArr['pa_is_keyword']){
			if(!empty($Params['word'])){
				$where .= sprintf(" and pat_keyword='%s'", LibFc::Escape($Params['word']));
			}else{
				$where .= ' and 1=0';
			}
		}

		if($paArr['pa_is_keyword'] && empty($Params['make_id']) && !empty($Params['mod3_id'])){
			$Ypc2Model = new Ypc2Model();
			$res = $Ypc2Model->get('bc_model3', ['mod3_make_id make_id'], sprintf(" and mod3_id=%d", $Params['mod3_id']), true);
			if($res['data']['make_id']) $Params['make_id'] = $res['data']['make_id'];
		}
		$where .= sprintf(" and pla_status = 1 and pla_pa_id=%d and pat_btime <= %d and pat_etime > %d and ads_id != %d", $paId, time(), time()-86400, $paArr['pa_ads_id']);
		if(!empty($Params['modtype'])) $where .= $Params['modtype'] == 'cv'? ' and pat_modcv=1':' and pat_modpc=1';
		$table = 'pj_place_ads JOIN pj_place_ads_time ON pat_pla_id = pla_id JOIN pj_user_ads AS c ON ads_id=pla_ads_id';
		$res = $AdsBase->get($table, array_merge($filed, ['pal_shoufei', 'pat_make_id']), $where);
		$arr = $res['data'];
		//2026/5/18 临时加入汽车生产商过滤
		if(!empty($arr) && !empty($Params['make_id'])){
			foreach($arr as $k=>$v){
				if(!empty($v['pat_make_id']) && $v['pat_make_id'] != $Params['make_id']){
					unset($arr[$k]);
				}
			}
			$arr = array_values($arr);
		}

		if(empty($arr) || ($Params['version_num'] >= 6002000 && count($arr) < $paArr['pa_max']) && $arr[0]['pal_shoufei'] == -1){
			$table = 'pj_place JOIN pj_user_ads AS c ON ads_id=pa_ads_id';
			$where = sprintf(" and pa_id=%d", $paId);
			$res = $AdsBase->get($table, $filed, $where, true);
			if(!empty($res['data'])) $arr[] = $res['data'];
		}
		
		if(!empty($arr)){
			foreach($arr as $k=>$v){
				if(!empty($v['ads_img'])){
					$imgs = explode(',', $v['ads_img']);
					shuffle($imgs);
					$v['ads_img'] = mvc::$cfg['FILE_CENTER']['bna']['url_get'].'&c=bna&f='.$imgs[0].mvc::$cfg['FILE_CENTER']['bna']['get_uri'];
				}
				if(isset($v['ads_ims'])) $v['ads_ims'] = json_decode($v['ads_ims']);
				if($v['ads_showby'] == 'union' && !empty($v['ads_union'])){
					$v['ads_union'] = json_decode($v['ads_union'], true);
				}else{
					unset($v['ads_union']);
				}
				unset($v['pal_shoufei']);
				$arr[$k] = $v;
			}

			if(empty($Params['all'])){
				shuffle($arr);
				$arr = $arr[0];
			}
		}
		LibFc::ReturnJson(true, $arr);
	}


	/**
	  * @method 广告点击记录收集
	  * @http   GET
	  * @param  adsId	 int    [必填]  广告ID
	  * @param  paId	 int    [必填]  所属广告位ID
	  * @param  Ip     string   [选填]  IP地址   IP|pctIds
	  * @param  pctIds string   [选填]  省市ID  19,6544
	  * @param     word	     string [选填]  关键字  VIN解码、车型查询传品牌ID
	  * @param     modtype	 string [VIN解码、车型查询必填]  VIN解码:解码接口的modtype   车型查询: 根据车型mod2_pc=1时传pc, mod2_cv=1时传cv
	  * @author soul
	  * @copyright 2019/10/15
	  * @return 
		{"status":true,"data": true,"code":0}
	  */
	public function pjlNaClick()
	{
		$Params = mvc::$URL_PARAMS;
		$adsId = (int) $Params['adsId'];
		$paId = (int) $Params['paId'];

		if ($adsId > 0 && $paId > 0)
		{
			$pctArr = [];
			if(!empty($Params['pctIds']))
			{
				$pctArr = [];
				foreach(explode(',', $Params['pctIds']) as $k=>$v)
				{
					$pctArr[$k] = (int) $v;
				}
			}
			$AdsBase = new AdsBase();
			$res = $AdsBase->get('pj_place_ads', ['pla_id'], sprintf(" and pla_ads_id=%d", $adsId), true);
			if(!empty($res['data']))
			{
				if(empty($pctArr) && !empty($Params['Ip']))
				{
					$OthPct = new OthPct();
					$res = $OthPct->getIpPCT($Params['Ip']);
					$pctArr = $res['data']['pctids'];
				}
				$allTjArr = [];
				$temp = [
					'DTYPE'=>'ADS_CLICK',	
					'ac_date'=>date('Ymd'),	
					'ac_ads_id'=>(int) $adsId,
					'ac_pa_id'=>(int) $paId,
					'ac_view_uid'=>(int) $Params['uid'],
					'ac_ip'=>$Params['Ip'],
					'ac_atime'=>time()
				];
				if(!empty($Params['word'])) $temp['ac_word'] = $Params['word'];
				if(!empty($Params['modtype'])) $temp['ac_modtype'] = $Params['modtype'];
				if(!empty($Params['ac_from'])) $temp['ac_from'] = $Params['ac_from'];
				if(!empty($pctArr)) $temp['ac_pcts'] = json_encode(['pid'=>(int) $pctArr[0], 'cid'=>(int) $pctArr[1]]);
				$allTjArr[] = json_encode($temp);
				$MainRabbit = new MainRabbit('TJ_OTH');
				$MainRabbit->send($allTjArr);
				LibFc::ReturnJson(true, 'ok');
			}
		}
		LibFc::ReturnJson(false, 'pjlNaClick参数无效');
	}


	/**
	  * @method 广告显示记录收集
	  * @http   GET
	  * @param  adsIds	 string [必填] 广告ID, 多个用逗号分隔
	  * @param  paId	 int    [必填]  所属广告位ID
	  * @param  Ip     string   [选填]  IP地址   IP|pctIds
	  * @param  pctIds string   [选填]  省市ID  19,6544
	  * @param     word	     string [选填]  关键字  VIN解码、车型查询传品牌ID
	  * @param     modtype	 string [VIN解码、车型查询必填]  VIN解码:解码接口的modtype   车型查询: 根据车型mod2_pc=1时传pc, mod2_cv=1时传cv
	  * @author soul
	  * @copyright 2019/10/15
	  * @return 
		{"status":true,"data": true,"code":0}
	  */
	public function pjlNaShow()
	{
		$Params = mvc::$URL_PARAMS;
		$adsIdArr = [];
		foreach(explode(',', $Params['adsIds']) as $v)
		{
			$adsIdArr[] = (int) $v;
		}
		$paId = (int) $Params['paId'];
		if (!empty($adsIdArr) && $paId > 0)
		{
			$pctArr = [];
			if(!empty($Params['pctIds']))
			{
				$pctArr = [];
				foreach(explode(',', $Params['pctIds']) as $k=>$v)
				{
					$pctArr[$k] = (int) $v;
				}
			}
			$AdsBase = new AdsBase();
			$res = $AdsBase->get('pj_place_ads', ['pla_ads_id'], sprintf(" and pla_ads_id in(%s)", implode(',', $adsIdArr)));
			if(!empty($res['data']))
			{
				$adsIdArr = array_column($res['data'], 'pla_ads_id');
				
				if(empty($pctArr) && !empty($Params['Ip']))
				{
					$OthPct = new OthPct();
					$res = $OthPct->getIpPCT($Params['Ip']);
					$pctArr = $res['data']['pctids'];
				}
				$allTjArr = [];
				foreach($adsIdArr as $adsId)
				{
					//访问统计
					$temp = [
						'DTYPE'=>'ADS_SHOW',	
						'as_date'=>date('Ymd'),	
						'as_ads_id'=>(int) $adsId,
						'as_pa_id'=>(int) $paId,
						'as_view_uid'=>(int) $Params['uid'],
						'as_ip'=>$Params['Ip'],
						'as_atime'=>time()
					];
					if(!empty($Params['word'])) $temp['as_word'] = $Params['word'];
					if(!empty($Params['modtype'])) $temp['as_modtype'] = $Params['modtype'];
					if(!empty($Params['as_from'])) $temp['as_from'] = $Params['as_from'];
					if(!empty($pctArr)) $temp['as_pcts'] = json_encode(['pid'=>(int) $pctArr[0], 'cid'=>(int) $pctArr[1]]);
					$allTjArr[] = json_encode($temp);
				}
				$MainRabbit = new MainRabbit('TJ_OTH');
				$MainRabbit->send($allTjArr);
				LibFc::ReturnJson(true, 'ok');
			}
		}
		LibFc::ReturnJson(false, 'pjlNaShow参数无效');
	}


	/**
	 * @method 获取广告可售的情况
	 * @http  get
	 * @param sby string  [选填] 展示模式 默认place  place:按广告位展示  group:按省|品牌展示
	 * @param status string [选填] 状态 默认all  all:全部  wsw:未售完  zjks:最近可售  zjdq:最近到期  sw:售罄
	 * @author soul 2021/4/6
	 * @return
	 	sby = place 按广告位展示
		{"status":true,"data":[{"pa_id":"83","pa_name":"开机广告","pa_is_area":"1","pa_is_keyword":"0","pa_keyword_type":"","pa_max":"3","adsuse":[{"id":"1","name":"北京","has":2,"can":1,"cantime":"2021-04-24","candate":0},{"id":"20","name":"广西省","has":3,"can":0,"cantime":"2021-06-06","candate":42},{"id":"21","name":"江西省","has":2,"can":1,"cantime":"2021-04-24","candate":0},{"id":"22","name":"四川省","has":2,"can":1,"cantime":"2021-04-24","candate":0},{"id":"23","name":"海南省","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"24","name":"贵州省","has":3,"can":0,"cantime":"2021-06-06","candate":42},{"id":"25","name":"云南省","has":2,"can":1,"cantime":"2021-04-24","candate":0},{"id":"26","name":"西藏","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"27","name":"陕西省","has":2,"can":1,"cantime":"2021-04-24","candate":0},{"id":"28","name":"甘肃省","has":3,"can":0,"cantime":"2021-06-06","candate":42},{"id":"29","name":"青海省","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"30","name":"宁夏省","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"31","name":"新疆","has":3,"can":0,"cantime":"2021-05-12","candate":17},{"id":"32","name":"台湾","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"42","name":"香港","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"43","name":"澳门","has":1,"can":2,"cantime":"2021-04-24","candate":0},{"id":"84","name":"钓鱼岛","has":1,"can":2,"cantime":"2021-04-24","candate":0}]},{"pa_id":"84","pa_name":"VIN解码","pa_is_area":"0","pa_is_keyword":"1","pa_keyword_type":"brand","pa_max":"1","adsuse":[{"id":"6","name":"奥迪","has":1,"can":0,"cantime":"2021-12-20","candate":239},{"id":"49","name":"哈弗","has":0,"can":"1","cantime":"2021-04-24","candate":0},{"id":"66","name":"哈飞","has":1,"can":0,"cantime":"2021-07-20","candate":86},{"id":"82","name":"海格","has":0,"can":"1","cantime":"2021-04-24","candate":0},{"id":"75","name":"黄海","has":0,"can":"1","cantime":"2021-04-24","candate":0},{"id":"48","name":"海马","has":1,"can":0,"cantime":"2021-05-20","candate":25},{"id":"139","name":"悍马","has":1,"can":0,"cantime":"2021-07-08","candate":74},{"id":"84","name":"众泰","has":0,"can":"1","cantime":"2021-04-24","candate":0},{"id":"83","name":"中兴","has":0,"can":"1","cantime":"2021-04-24","candate":0}]}],"code":"","encry":0,"runtime":64}

		sby=group 按省|品牌展示
		{"status":true,"data":{"province":[{"id":"1","name":"北京","plas":[{"id":83,"name":"开机广告","has":2,"can":1,"cantime":"2021-04-24"},{"id":85,"name":"首页轮播","has":1,"can":2,"cantime":"2021-04-24"}]},{"id":"84","name":"钓鱼岛","plas":[{"id":83,"name":"开机广告","has":1,"can":2,"cantime":"2021-04-24"},{"id":85,"name":"首页轮播","has":1,"can":2,"cantime":"2021-04-24"}]}],"brand":[{"id":"6","name":"奥迪","plas":[{"id":84,"name":"VIN解码","has":1,"can":0,"cantime":"2021-12-20"},{"id":86,"name":"车型查询","has":1,"can":0,"cantime":"2021-07-28"}]},{"id":"83","name":"中兴","plas":[{"id":84,"name":"VIN解码","has":0,"can":"1","cantime":"2021-04-24"},{"id":86,"name":"车型查询","has":0,"can":"1","cantime":"2021-04-24"}]}]},"code":"","encry":0,"runtime":54}

		"has":已售广告数,"can":可售广告数,"cantime":"可售广告日期","candate":离最近可售广告天数
	 */
	public function getAdsPlaces()
	{
		$AdsTj = new AdsTj();
		$res = $AdsTj->getAdsPlaces(mvc::$URL_PARAMS);
		LibFc::EchoJson($res);
	}

	
	//宜配车架号广告
	public function getYpVinAdsBrand()
	{
		$Ypc2Model = new Ypc2Model();
		$res = $Ypc2Model->get('bc_brand', ['brand_id id, brand_name name, brand_name name_cn'], 'order by left(brand_initial, 1), brand_name');
		$arr = [];
		foreach($res['data'] as $v){
			$v['id'] += 1000;
			$arr[] = $v;
		}
		LibFc::ReturnJson(true, $arr);
	}

}
