<?php
defined('IN_WEB') or die('Include Error!');

/**
 * 廣告相关
 */
class Advertisement
{
    public function listFunction($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)) {
            return oo::response(-1,[],'参数错误');
        }
        $data = oo::commonOprModel('advertisement')->getRemainCount($uid);
		$ret = oo::commonOprModel('readconfig')->getSysCon('Advertisement',"cache");
        return json_encode([
            'code' => 1, 'msg'  => '', 'data' => $data,
            'cache'=> json_decode($ret),
        ],JSON_NUMERIC_CHECK);
    }
  
  	public function reward($param){
        $uid = oo::functions()->uint($param['uid']);
        $id  = oo::functions()->uint($param['id']);
        $time  = oo::functions()->uint($param['time']);
        $sign= oo::functions()->escape($param['sign']);

        if(empty($uid) || empty($id)|| empty($sign)){
            return oo::response(-1,[],'参数错误');
        }
        $redisSign = oo::commonOprRedis('common')->get(okeys::lastRedisSign($uid));
        $serverSign = "{$uid}#2019$"."SUPERANT{$time}&^%";
        if($sign != md5($serverSign) || $sign == $redisSign){
            return oo::response(-2,[],$sign.'签名异常'.$redisSign.'签名异常'.$serverSign);
        }
        $ret = oo::commonOprModel('advertisement')->getRemainCount($uid,$id);
        if(empty($ret)){
            return oo::response(-3,[],'广告未开启');
        }
        if($ret['remain']<=0){
            return oo::response(-4,[],'次数不足');
        }

        if(!empty($ret['reward']) && $ret['reward']['specialReward'] == 0){
            oo::commonOprModel('member')->optProperty($uid,$ret['reward']['reward'],'+',108,"广告获得:视频Id-{$id}");
            oo::commonOprModel('advertisement')->addCount($uid,$id);
            oo::commonOprRedis('common')->setex(okeys::lastRedisSign($uid),$sign,3600);
            $userAssets  = oo::commonOprModel('member')->getUserAssetsInfo($uid);
            return oo::response(1,['money'=>$userAssets['money'],'spins'=>$userAssets['spins']],'奖励发放成功');
        }else{
            switch ($id){
                case 11:
                    $chatNum = intval(oo::commonOprRedis('common')->hGet(okeys::chatMaxNum(),$uid));
                    oo::commonOprRedis('common')->hSet(okeys::chatMaxNum(),$uid,$chatNum-5);
                break;
            }
            oo::commonOprModel('advertisement')->addCount($uid,$id);
            return oo::response(1);
        }
    }
}