<?php
namespace app\service;

use think\facade\Db;
use app\service\SystemService;
use app\service\ResourcesService;
use app\service\MultilingualService;

/**
 * 神邸服务层
 */
class LotteryService {
    /**
     * 星座神列表
     * @param   [array]           $params [输入参数]
     */
    public static function Zodiaca($params = []) {
        $lang = MultilingualService::GetUserMultilingualValue();
        // 从缓存获取
        $key = SystemService::CacheKey('shopxo.cache_home_lottery_list_key').APPLICATION_CLIENT_TYPE;
        $data = MyCache($key);
        if($data === null || MyEnv('app_debug') || MyC('common_data_is_use_cache') != 1) {
            // 文章
            $params = [
                'where'  => ['data_flag'=>1],
                'field'  => 'lottery_id,lottery_date,lotteryissue,number_a,number_b,number_c,million,jackpot,is_finish',
                'm'      => 0,
                'n'      => 20,
            ];
            $ret = self::ArticleList($params);
            $data = empty($ret['data']) ? [] : $ret['data'];

            // 存储缓存
            MyCache($key, $data, 180);
        }
        return $data;
    }

    /**
     * 星座神列表
     * @param   [array]          $params [输入参数]
     */
    public static function Zodiac($params) {
        $where = empty($params['where']) ? [] : $params['where'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $order_by = empty($params['order_by']) ? self::LotteryByOrder($params) : trim($params['order_by']);
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;

        // 列表读取前钩子
        $hook_name = 'plugins_service_lottery_list_begin';
        MyEventTrigger($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'params'        => &$params,
            'where'         => &$where,
            'field'         => &$field,
            'order_by'      => &$order_by,
            'm'             => &$m,
            'n'             => &$n,
        ]);

        $data = Db::name('aadeitycard')->field($field)->where($where)->order($order_by)->limit($m, $n)->select()->toArray();
        return DataReturn(MyLang('handle_success'), 0, self::LotteryListHandle($data, $params));
    }

    /**
     * 数据处理
     * @param   [array]          $data   [数据列表]
     * @param   [array]          $params [输入参数]
     */
    public static function LotteryListHandle($data, $params = []) {
        if(!empty($data)) {
            // 字段列表
            $keys = ArrayKeys($data);
            foreach($data as $k=>&$v) {
                // 增加索引
                $v['data_index'] = $k+1;
                // 是否完结
                if($v['is_finish'] ==1) {
                    $v['is_finish_name'] = MyLang('finish_title');
                }else{
                    $v['is_finish_name'] = MyLang('wait_title');
                }
            }
        }
        return $data;
    }

    /**
     * 总数
     * @param    [array]          $where [条件]
     */
    public static function LotteryTotal($where)
    {
        return (int) Db::name('aadeitycard')->where($where)->count();
    }

    /**
     * 条件
     * @param   [array]           $params [输入参数]
     */
    public static function LotteryWhere($params = [])
    {
        // 默认条件
        $where = [
            ['data_flag', '=', 1],
        ];
        return $where;
    }

    /**
     * 排序
     * @param   [array]           $params [输入参数]
     */
    public static function LotteryByOrder($params = []) {
        $order_by = 'lottery_id desc';
        return $order_by;
    }


}
?>