<?php
defined('IN_WEB') or die('Include Error!');
/**
 * 机器人相关
 */

class Shop
{
    public function getRecommend($param){
        return oo::commonOprModel('readconfig')->getSysCon('Shop',"recommend");
    }

    public function saveRecommend($param){
        return oo::commonOprModel('readconfig')->setSysCon('Shop',"recommend",$param['data']);
    }

    public function config($param){
        $type = oo::functions()->uint($param['type']);
        if(empty($type))
            return json_encode(['code'=>-1]);
        $tb = otable::goodsCrazy();
        $sql = "SELECT * FROM {$tb} WHERE gtype={$type}";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $returnData = [
            'code'=>1,
            'data'=>$ret,
        ];
        if($type != 21){
            return json_encode($returnData);
        }

        //钻石商城和兑换列表
        $dtb = otable::diamondCrazy();
        $sql = "SELECT * FROM {$dtb} WHERE pid IN(1, 2, 3,6) order by gid";
        $returnData['exchanges'] = oo::commonOprDb('common')->getAll($sql,1);           
        $sql = "SELECT * FROM {$dtb} WHERE pid IN(4, 5) order by gid";
        $returnData['notenough'] = oo::commonOprDb('common')->getAll($sql,1);
        return json_encode($returnData);
    }

    public function saveConfig($param){
        $type = oo::functions()->escape($param['type']);
        $data = json_decode($param['data']);
        $tb = otable::goodsCrazy();
        foreach ($data as $key=>$row){
            switch ($type){
                case 1:
                    $sql = "UPDATE {$tb} SET gattribute='{$row->gattribute}',gsku='{$row->gsku}',gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',goriginReward='{$row->goriginReward}',gbaseReward='{$row->gbaseReward}',gimg='{$row->gimg}',gstatus='{$row->gstatus}',gprice='{$row->gprice}' WHERE gid={$row->gid}";
                    break;
                case 2:
                    $sql = "UPDATE {$tb} SET gattribute='{$row->gattribute}',gsku='{$row->gsku}',gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',gimg='{$row->gimg}',gstatus='{$row->gstatus}',gprice='{$row->gprice}' WHERE gid={$row->gid}";
                    break;
                case 3:
                    $sql = "UPDATE {$tb} SET gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',goriginReward='{$row->goriginReward}',gbaseReward='{$row->gbaseReward}',gimg='{$row->gimg}',gstatus='{$row->gstatus}' WHERE gid={$row->gid}";
                    break;
                case 4:
                    $sql = "UPDATE {$tb} SET gsku='{$row->gsku}',gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',goriginReward='{$row->goriginReward}',gbaseReward='{$row->gbaseReward}',gimg='{$row->gimg}',gstatus='{$row->gstatus}',gprice='{$row->gprice}' WHERE gid={$row->gid}";
                    break;
                case 5:
                    $sql = "UPDATE {$tb} SET gsku='{$row->gsku}',gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',goriginReward='{$row->goriginReward}',gbaseReward='{$row->gbaseReward}',gimg='{$row->gimg}',gstatus='{$row->gstatus}',gprice='{$row->gprice}' WHERE gid={$row->gid}";
                    break;
                case 6:
                    $sql = "UPDATE {$tb} SET gsku='{$row->gsku}',gname='{$row->gname}',gname_en='{$row->gname_en}',gdesc='{$row->gdesc}',gdesc_en='{$row->gdesc_en}',goriginReward='{$row->goriginReward}',gbaseReward='{$row->gbaseReward}',gimg='{$row->gimg}',gstatus='{$row->gstatus}',gprice='{$row->gprice}',gdiscount='{$row->gdiscount}' WHERE gid={$row->gid}";
                    break;
                case 9:
                    $sql = "UPDATE {$tb} SET gsku='{$row->gsku}',gname='{$row->gname}',gdesc='{$row->gdesc}',gbaseReward='{$row->gbaseReward}',gprice='{$row->gprice}',gMax='{$row->gMax}' WHERE gid={$row->gid}";
                    break;
                default:
                    $sql = "UPDATE {$tb} SET ";
                    $update = '';
                    foreach ($row as $x=>$y){
                        $update .= "$x='$y',";
                    }
                    $update = trim($update,',');
                    $sql = $sql.$update." WHERE gid='{$key}'";
            }
            $rs = oo::commonOprDb('common')->query($sql);
        }
        if($type == 21){
            $exchanges = json_decode($param['exchanges']);
            $tb = otable::diamondCrazy();
            foreach ($exchanges as $row){
                $sql = "UPDATE {$tb} SET name='{$row->name}',price='{$row->price}',gbaseReward='{$row->gbaseReward}',times={$row->times} WHERE gid={$row->gid}";
                $rs = oo::commonOprDb('common')->query($sql);
            }
            $notenough = json_decode($param['notenough']);
            foreach ($notenough as $row){
                $sql = "UPDATE {$tb} SET name='{$row->name}',price='{$row->price}',gbaseReward='{$row->gbaseReward}',gaddRatio={$row->gaddRatio} WHERE gid={$row->gid}";
                $rs = oo::commonOprDb('common')->query($sql);
            }
            oo::commonOprRedis("common")->delete(okeys::shopDiamond()); //清除缓存
            oo::commonOprRedis("common")->delete(okeys::notEnouthDiamond()); //清除缓存
        }
        return $rs ? $rs : 0;
    }

    public function level(){
        $tb = otable::goodsCoin();
        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('common')->getAll($sql,1);
        $temp = [];
        $i = 0;
        foreach ($ret as $key=>$row){
            $temp[$i]['levelId'] = $row['levelId'];
            $temp[$i][$row['gsku']] = $row['num'];
            if(($key+1)%6 == 0){
                $i += 1;
            }
        }
        $returnData = [
            'code'=>1,
            'data'=>$temp,
        ];
        return json_encode($returnData);
    }

    public function saveLevel($param){
        $data = json_decode($param['data'],true);
        $tb = otable::goodsCoin();

        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('common')->getAll($sql,MYSQLI_ASSOC);
        $id = [];
        foreach ($ret as $row){
            $id[] = $row['levelId'];
        }

        foreach ($data as $row){
            if(in_array($row['levelId'],$id)){
                foreach ($row['data'] as $key=>$col){
                    $sql = "UPDATE {$tb} SET num='{$col}' WHERE levelId={$row['levelId']} AND gsku='{$key}'";
                    $rs = oo::commonOprDb('common')->query($sql);
//                    oo::commonOprCache('memcache')->delete(okeys::GoodsCoin($row['levelId'],str_replace('com.gworld.cc.','',$key)));
                    oo::commonOprRedis('config')->delete(okeys::GoodsCoin($row['levelId'],str_replace('com.gworld.cc.','',$key)));
                }
            }else{
                foreach ($row['data'] as $key=>$col){
                    $sql = "INSERT INTO {$tb} (levelId,gsku,num) VALUES ({$row['levelId']},'{$key}','{$col}')";
                    $rs = oo::commonOprDb('common')->query($sql);
                }
            }
        }
        return $rs?$rs:0;
    }
  
  	public function CardChest(){
        $tb = otable::CardChest();
        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('common')->getAll($sql,MYSQLI_ASSOC);
        $returnData = [
            'code'=>1,
            'data'=>$ret,
        ];
        return json_encode($returnData);
    }


    public function saveCardChest($param){
        $data = json_decode($param['data'],true);
        $tb = otable::CardChest();
        $sql = "SELECT * FROM {$tb} WHERE 1";
        $ret = oo::commonOprDb('common')->getAll($sql,MYSQLI_ASSOC);
        $id = [];
        foreach ($ret as $row){
            $id[] = $row['levelId'];
        }
        foreach ($data as $row){
            $superChest = empty($row['superChest']) ? 0 : $row['superChest'];
            if(in_array($row['levelId'],$id)){
                $sql = "UPDATE {$tb} SET woodenChest='{$row['woodenChest']}',goldenChest='{$row['goldenChest']}',magicalChest='{$row['magicalChest']}',superChest='{$superChest}' WHERE levelId={$row['levelId']}";
                $rs = oo::commonOprDb('common')->query($sql);
            }else{
                $sql = "INSERT INTO {$tb} (levelId,woodenChest,goldenChest,magicalChest,superChest) VALUES ({$row['levelId']},'{$row['woodenChest']}','{$row['goldenChest']}','{$row['magicalChest']}','{$superChest}')";
                $rs = oo::commonOprDb('common')->query($sql);
            }
        }
        oo::commonOprRedis('Config')->delete(okeys::CardChestConfig());
        return $rs?$rs:0;
    }
  
  	public function payDistributed($param){
        $this->payBlackList['uid'] = oo::commonOprModel("config")->payWhiteList();
        $stime = $param['stime'];
        $etime = $param['etime'];
        $out = [];
        $days = $this->diffDay($stime,$etime);
        $tb = otable::summarylist();
        
        $blackUidArr = $this->payBlackList['uid'];
        $blackUidStr = implode(',', $blackUidArr);
        for($i=0;$i<=$days;$i++){
            $s   = strtotime("$stime +$i days 00:00:00");
            $e   = strtotime("$stime +$i days 23:59:59");
            $sql = "SELECT * FROM {$tb} WHERE sl_status=2 AND sl_sid != -1 AND sl_pay_time>={$s} AND sl_pay_time<{$e} AND sl_uid NOT IN ({$blackUidStr}) ";
            $ret = oo::commonOprDb('payment')->getAll($sql,MYSQLI_ASSOC);
            $out[] = $ret;
        }

        $tb   = otable::goodsCrazy();
        $sql  = "SELECT gid,gname,gpid FROM {$tb} WHERE 1";
        $good = oo::commonOprDb('payment')->getAll($sql,MYSQLI_ASSOC);
        return oo::response(1,['good'=>$good,'data'=>$out]);
    }

    public function diffDay($start_date,$end_date){
        $datetime_start = new DateTime($start_date);
        $datetime_end = new DateTime($end_date);
        $days = $datetime_start->diff($datetime_end)->days;
        return $days;
    }

    public function setSerialPack($param){
        $data = json_decode($param['data'],true);
        $tb = otable::goodsCrazy();
        foreach ($data as $gid=>$gInfo){
            $set = "";
            foreach ($gInfo as $key=>$value){
                $set .= "`{$key}`='{$value}',";
            }
            $set = trim($set,',');
            $rs  = oo::commonOprDb('common')->query("UPDATE {$tb} SET {$set} WHERE gid={$gid}");
        }
        return $rs;
    }
}
