<?php

namespace Act\Lib;

use Dever;

class Num
{
    # 获取我在这个小刊中的本数
    public function get($id, $uid)
    {
        $where['uid'] = $uid;
        $where['journal_id'] = $id;
        $where['state'] = 1;
        $data = Dever::db('act/journal_num')->one($where);

        if ($data) {
            return $data['num'];
        } else {
            return 0;
        }
    }
    
    # 获取小刊的积分排行
    public function getList($id, $uid = false)
    {
        $key = 'journal_num_' . $id;
        $data = Dever::cache($key);
        if (!$data) {
            $where['journal_id'] = $id;
            if ($uid > 0) {
                $where['uid'] = $uid;
            }
            $data = Dever::db('act/journal_num')->getAll($where);

            if ($data) {
                foreach ($data as $k => $v) {
                    $user = Dever::load('passport/api')->info($v['uid']);
                    $data[$k]['username'] = $user['username'];
                    $data[$k]['avatar'] = $user['avatar'];
                    $data[$k]['mobile'] = $user['mobile'];
                }

                Dever::cache($key, $data, 600);
            }
        }
        
        return $data;
    }

    public function submit($id, $uid = false, $num = false, $update = true)
    {
        $where['uid'] = $uid;
        $where['journal_id'] = $id;
        $table = 'act/journal_num';
        $db = Dever::db($table);
        $info = $db->one($where);
        if (!$info) {
            $where['num'] = $num;
            $db->insert($where);
        } else {
            if ($update) {
                $where['where_id'] = $info['id'];
                if (!$info['num'] || $info['num'] < 0) {
                    $info['num'] = 0;
                }
                $where['num'] = $info['num'] + $num;
                $db->update($where);
            }
        }

        return true;
    }

    # 矫正数据用的，放到计划任务中
    public function cron()
    {
        

        return true;
    }

    # 订阅数计数器
    public function setCounter($id, $num = 0)
    {
        $key = 'journal_buy_num3_' . $id;
        /*
        $callback = function() use($id) {
            $journal = Dever::db('journal/info')->one($id);
            return $journal['num_ding'];
        };
        Dever::counter($key, $num, $callback);
        */
        Dever::cache($key, $num);
        return true;
    }

    # 订阅数计数器
    public function getCounter($id)
    {
        $key = 'journal_buy_num3_' . $id;
        //return Dever::counter($key);
        return Dever::cache($key);
    }

    /**
     * 导出成excel
     *
     * @return mixed
     */
    public function out_api()
    {
        $id = Dever::input('id');
        $journal = Dever::db('journal/info')->one($id);

        if (!$journal) {
            return;
        }

        $score = $this->getList($id);
        $header = $data = array();
        $name = $journal['name'];
        if ($score) {
            $i = 0;
            foreach ($score as $k => $v) {
                $data[$i] = array
                (
                    $i+1,
                    $v['username'],
                    $v['mobile'],
                    $v['num'],
                );
                $i++;
            }
        }

        $header = array
        (
            '排序', '用户名', '手机号', '本数'
        );

        $file = $name . '_排行榜';
        if (Dever::input('test')) {
            print_r($header);
            print_r($data);die;
        }
        
        Dever::excelExport($data, $header, $file, $sheet = 0, $sheetName = $name);
        return;
    }
}