<?php
namespace app\index\controller;
use think\facade\Db;
use app\index\controller\Common;
use app\service\SystemService;
use app\service\ArticleService;
use app\service\MultilingualService;
use app\service\LinkService;

/**
 * 财富神
 */
class Ploutos extends Common {
	/**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
    }
	/**
     * 详情
     */
	public function Index() {

        $params = $this->data_request;

        // 模板数据
        $assign = [
            // 主题文章
            'ploutos_article_list'          => ArticleService::RecommendedArticleList(115,1),
            'ploutos_article_bottom_list'   => ArticleService::RecommendedArticleList(116,2),
            'article_footer_list'      => ArticleService::RecommendedArticleList(111,1),
            // 友情链接
            'link_list'   => LinkService::HomeLinkList(),
        ];
        $assign['goods_floor_list'] = self::PloutosHomeList();

        $assign['home_seo_site_title'] = $assign['ploutos_article_list'][0]['seo_title'];
        $assign['home_seo_site_keywords'] = $assign['ploutos_article_list'][0]['seo_keywords'];
        $assign['home_seo_site_description'] = $assign['ploutos_article_list'][0]['seo_desc'];

        // 获取历史列表
        $ret = self::PloutosList($params);

        // 分页
        $page_params = [
            'number'    => $ret['data']['page_size'],
            'total'     => $ret['data']['total'],
            'where'     => $this->data_request,
            'page'      => $ret['data']['page'],
            'url'       => MyUrl('index/ploutos/index'),
            'bt_number' => IsMobile() ? 2 : 4,
        ];
        $page = new \base\Page($page_params);
        $page_html = $page->GetPageHtml();

        $assign['data_total'] = $ret['data']['total'];
        $assign['data_list'] = $ret['data']['data'];
        $assign['page_html'] = $page_html;

		// 数据赋值
        MyViewAssign($assign);
		return MyView();
	}    
    /**
     * 获取楼层数据
     * @param   [array]          $params [输入参数]
     */
    public static function PloutosHomeList($params = []) {
        $lang = MultilingualService::GetUserMultilingualValue();
        // 缓存
        $key = '';//SystemService::CacheKey('shopxo.cache_ploutos_floor_list_key');
        $data = null;//MyCache($key);
        if($data === null || MyEnv('app_debug') || MyC('common_data_is_use_cache') != 1) {
            // 获取数据
            $data = [];
            $where = [
                ['is_finish', '=', 1],
                ['data_flag', '=', 1],
            ];
            $data['ploutos'] = Db::name('aaploutos')->where($where)->order('lottery_id desc')->limit(1)->find();
            $data['jackpot'] = Db::name('aaploutos')->where([['is_finish','=',0],['data_flag','=',1]])->order('lottery_id desc')->limit(1)->find();
            $areaid = $data['ploutos']['areaid'];
            switch($lang){
                case 'zh':
                    $data['ploutos']['area_name'] = Db::name('areas')->where(['areaid'=>$areaid])->value('area_name');                    
                    break;
                case 'hi':
                    $data['ploutos']['area_name'] = Db::name('areas')->where(['areaid'=>$areaid])->value('area_namehi');                    
                    break;
                case 'es':
                    $data['ploutos']['area_name'] = Db::name('areas')->where(['areaid'=>$areaid])->value('area_namees');                 
                    break;
                default:
                    $data['ploutos']['area_name'] = Db::name('areas')->where(['areaid'=>$areaid])->value('area_nameen');
                    break;
            }
            
            // 存储缓存
            MyCache($key, $data, 180);
        }
        return $data;
    }    
    /**
     * 获取楼层数据
     */
    public static function PloutosList($params = []) {
        $lang = MultilingualService::GetUserMultilingualValue();
        // 返回格式
        $result = [
            'page_start'    => 0,
            'page_size'     => 0,
            'page'          => 1,
            'page_total'    => 0,
            'total'         => 0,
            'data'          => [],
        ];
        $where = [
            ['a.is_finish', '=', 1],
            ['a.data_flag', '=', 1],
        ];
        $order_by = 'lottery_id desc';
        $field = 'a.*,r.area_name,r.area_nameen,r.area_namehi,r.area_namees';
        $result['page'] = max(1, isset($params['page']) ? intval($params['page']) : 1);
        $result['page_size'] = empty($params['page_size']) ? MyC('home_search_limit_number', 20, true) : intval($params['page_size']);
        // 数量不能超过500
        if($result['page_size'] > 500) {
            $result['page_size'] = 500;
        }
        $result['page_start'] = intval(($result['page']-1)*$result['page_size']);

        $ploutos_query = Db::name('aaploutos')->alias('a')->join('areas r', 'a.areaid=r.areaid');

        $result['total'] = (int) $ploutos_query->where($where)->count();
        
        $result['data'] = $ploutos_query->where($where)->field($field)->order($order_by)->limit($result['page_start'], $result['page_size'])->select()->toArray();
        foreach($result['data'] as $k=>&$v) {
            switch($lang){
                case 'zh': $result['data'][$k]['areaid'] = $v['area_name']; break;
                case 'hi': $result['data'][$k]['areaid'] = $v['area_namehi']; break;
                case 'es': $result['data'][$k]['areaid'] = $v['area_namees']; break;
                default: $result['data'][$k]['areaid'] = $v['area_nameen']; break;
            }
        }
        $result['page_total'] = ceil($result['total']/$result['page_size']);

        return DataReturn(MyLang('handle_success'), 0, $result);

    }

}
?>