<?php

namespace Act\Lib;

use Dever;

class Score
{
    # 获取我在这个小刊中的积分
    public function get($id, $uid, $type = 4)
    {
        $where['uid'] = $uid;
        $where['type'] = $type;
        $where['data_id'] = $id;
        $where['state'] = 1;
        $data = Dever::db('act/score')->one($where);

        if ($data) {
            return $data['score'];
        } else {
            return 0;
        }
    }
    
    # 获取小刊的积分排行
    public function getList($id, $type = 4, $uid = false)
    {
        $where['type'] = $type;
        $where['data_id'] = $id;
        if ($uid > 0) {
            $where['uid'] = $uid;
        }
        $data = Dever::db('act/score')->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'];
            }
        }
        return $data;
    }

    public function submit()
    {
        $user_log_id = Dever::input('user_log_id');
        $type = Dever::input('type', 4);
        $id = Dever::input('id');
        $method = Dever::input('method');

        $user_log = Dever::db('score/user_log')->one($user_log_id);

        if ($user_log) {
            if ($method == 'share' || $method == 'pay' || $method == 'code') {
                # 活动结束后，无法增加当前小刊积分
                if ($type == 4) {
                    $journal = Dever::db('journal/active')->one(array('info_id' => $id, 'state' => 1));
                    if (!$journal) {
                        return;
                    }
                    if ($journal && $journal['status'] == 2) {
                        return;
                    }
                    if ($journal && time() > $journal['end']) {
                        return;
                    }
                }
            }
            //$where['user_log_id'] = $user_log_id;
            $where['uid'] = $user_log['uid'];
            $where['data_id'] = $id;
            $where['type'] = $type;
            $table = 'act/score';
            $info = Dever::db($table)->one($where);
            if (!$info) {
                $where['score'] = $user_log['num'];
                Dever::db($table)->insert($where);
            } else {
                $where['where_id'] = $info['id'];
                $where['score'] = $info['score'] + $user_log['num'];
                Dever::db($table)->update($where);
            }
        }

        return true;
    }

    /**
     * 导出成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['score'],
                );
                $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;
    }
}