<?php

/**
 * 活动配置相关
 */
defined('IN_WEB') or die('Include Error!');
include_once dirname(__FILE__) . "/Base.php";
class Advertisement extends Base
{
    public function listFunction($param){
        $tb = otable::advertisement_config();
        if(isset($param['id'])){
            $sql = "SELECT * FROM {$tb} WHERE id={$param['id']}";
            $ret = oo::commonOprDb('config')->getOne($sql,MYSQLI_ASSOC);
        }else{
            $sql = "SELECT * FROM {$tb} WHERE 1";
            $ret = oo::commonOprDb('config')->getAll($sql,MYSQLI_ASSOC);
        }

        die(json_encode($ret));
    }

    public function save($param){
        $tb = otable::advertisement_config();
        $param = json_decode($param['data'],true);
        $id = $param['id'];

        $set = "";
        foreach ($param['data'] as $key=>$row){
            if(is_array($row)){
                $temp = json_encode($row);
            }else{
                $temp = oo::functions()->escape($row);
            }
            $set .= "`{$key}`='{$temp}',";
        }

        if(!empty($set)){
            $set = trim($set,',');
            $sql = "UPDATE {$tb} SET {$set} WHERE id = {$id} LIMIT 1 ";
            oo::commonOprDb('config')->query($sql);
        }

        switch ($param['data']['status']){
            case 0:
                oo::commonOprRedis('common')->hDel(okeys::advertisement_config(1),$id);
                oo::commonOprRedis('common')->hDel(okeys::advertisement_config(2),$id);
            break;
            case 1:
                $temp = $param['data'];
                unset($temp['trueId_ios']);
                $temp['status'] = 1;
                $max     = $temp['max'];
                $tempMax = $temp['max'];
                $weights = array_sum($temp['weights']);
                foreach ($temp['trueId'] as $key=>$value){
                    $times = ceil($temp['weights'][$key]/$weights*$max);
                    if($tempMax>$times){
                        $tempMax = $tempMax - $times;
                    }else{
                        $times   = $tempMax;
                        $tempMax = $tempMax - $times;
                    }
                    $temp['channel'][$key] = [
                        'times'=> $times,
                        'adId'   => $value,
                    ];
                }
                $temp['trueId'] = $temp['trueId'][$temp['defChannel']];
                unset($temp['weights']);
                oo::commonOprRedis('common')->hSet(okeys::advertisement_config(1),$id,json_encode($temp));
                oo::commonOprRedis('common')->hDel(okeys::advertisement_config(2),$id);
            break;
            case 2:
                $temp = $param['data'];
                $temp['trueId'] = $temp['trueId_ios'];
                $temp['status'] = 1;
                $max     = $temp['max'];
                $tempMax = $temp['max'];
                $weights = array_sum($temp['weights']);
                foreach ($temp['trueId'] as $key=>$value){
                    $times = ceil($temp['weights'][$key]/$weights*$max);
                    if($tempMax>$times){
                        $tempMax = $tempMax - $times;
                    }else{
                        $times   = $tempMax;
                        $tempMax = $tempMax - $times;
                    }
                    $temp['channel'][$key] = [
                        'times'=> $times,
                        'adId'   => $value,
                    ];
                }
                $temp['trueId'] = $temp['trueId'][$temp['defChannel']];
                unset($temp['trueId_ios']);
                unset($temp['weights']);
                oo::commonOprRedis('common')->hDel(okeys::advertisement_config(1),$id);
                oo::commonOprRedis('common')->hSet(okeys::advertisement_config(2),$id,json_encode($temp));
            break;
            case 3:
                $temp = $param['data'];
                unset($temp['trueId_ios']);
                $temp['status'] = 1;
                $max     = $temp['max'];
                $tempMax = $temp['max'];
                $weights = array_sum($temp['weights']);
                foreach ($temp['trueId'] as $key=>$value){
                    $times = ceil($temp['weights'][$key]/$weights*$max);
                    if($tempMax>$times){
                        $tempMax = $tempMax - $times;
                    }else{
                        $times   = $tempMax;
                        $tempMax = $tempMax - $times;
                    }
                    $temp['channel'][$key] = [
                        'times'=> $times,
                        'adId'   => $value,
                    ];
                }
                $temp['trueId'] = $temp['trueId'][$temp['defChannel']];
                unset($temp['weights']);
                oo::commonOprRedis('common')->hSet(okeys::advertisement_config(1),$id,json_encode($temp));
                $temp = $param['data'];
                $temp['trueId'] = $temp['trueId_ios'];
                $temp['status'] = 1;
                $max     = $temp['max'];
                $tempMax = $temp['max'];
                $weights = array_sum($temp['weights']);
                foreach ($temp['trueId'] as $key=>$value){
                    $times = ceil($temp['weights'][$key]/$weights*$max);
                    if($tempMax>$times){
                        $tempMax = $tempMax - $times;
                    }else{
                        $times   = $tempMax;
                        $tempMax = $tempMax - $times;
                    }
                    $temp['channel'][$key] = [
                        'times'=> $times,
                        'adId'   => $value,
                    ];
                }
                $temp['trueId'] = $temp['trueId'][$temp['defChannel']];
                unset($temp['trueId_ios']);
                unset($temp['weights']);
                oo::commonOprRedis('common')->hSet(okeys::advertisement_config(2),$id,json_encode($temp));
            break;
        }
        return 1;
    }
  
  	public function getSta($param){
        $date = $param['date'];
        $table = otable::advertisementSta();
        $sql = "SELECT * FROM {$table} WHERE date IN ({$date})";
        $ret = oo::commonOprDb('statistics')->getAll($sql,MYSQLI_ASSOC);
        return json_encode($ret);
    }
  
  	public function getCache(){
        $ret = oo::commonOprModel('readconfig')->getSysCon('Advertisement',"cache");
        die($ret);
    }

    public function saveCache($param){
        $temp  = $param['data'];
        $table = otable::systemConfig();
        $sql = "UPDATE {$table} SET content='{$temp}' WHERE item = 'Advertisement' AND title = 'cache'";
        oo::commonOprDb('common')->getOne($sql,1);
        oo::commonOprRedis('common')->hDel(okeys::systemConfig(),'Advertisement-cache');
    }
  
}