<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\advertisement\public_class;
use \temmoku\db;
use \plugin\advertisement\init;
class cache{
	
	public static function run($data){
		self::setting_cache();
		self::ad_cache();
	}
	
	//核心缓存
	public static function setting_cache(){
		$data=db::select('*')->from(init::$table."set")->query();
		$list=[];
		foreach($data AS $key=>$row){
			$list[$row['u_key']]=$row['u_value'];
		}
		$array=['setting'=>$list];
		createconf($array,'setting',init::$dir,true);
	}
	//广告缓存
	public static function ad_cache(){
		$data=db::select('*')->from(init::$table."list")->query();
		$datadb=[];
		foreach((array)$data AS $key=>$row){
			$row['setting']=\unserialize($row['setting']);
			//print_r($row);
			if($row['ad_type']=='images' || $row['ad_type']=='text'){
				$buy_list=db::select('*')->from(init::$table."buy_list")->where("bid= '{$row['id']}' AND endtime > '".NOWTIME."'")->query();
				
				$row['adlist']=[];
				foreach($buy_list as $_row){
					$row['adlist'][]=\unserialize($_row['content']);
				}
				
				$buy_list_num=count($buy_list);
				$balance=$row['sellnum'] - $buy_list_num;
				
				if($balance){
					for($i=0;$i<$balance;$i++){
						if($row['ad_type']=='images'){
							$row['adlist'][]=[
								"imgurl"=>$row['setting']['images']['imgurl'][$buy_list_num+$i],
								"weburl"=>$row['setting']['images']['weburl'][$buy_list_num+$i],
							];
						}else{
							$row['adlist'][]=[
								"title"=>$row['setting']['text']['title'][$buy_list_num+$i],
								"weburl"=>$row['setting']['text']['weburl'][$buy_list_num+$i],
							];
						}
					}
				}
			}
			$datadb[$row['tag']]=serialize($row);
		}
		$array=['ad_list'=>$datadb];
		
		createconf($array,'ad',init::$dir,true);
	}
}
?>