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

/**
* 玩家反馈
*/

class ModelFeedback
{
    public $path = WWWROOT.'images/uploads/';
    public function user_add($data){
        $feedback['user_id']= (int)$data['user_id'];
        $feedback['type']= (string)$data['type'];
        $feedback['content'] = (string)$data['content'];
        $feedback['orientation']= 1;
        $feedback['have_read']= 0;
		$feedback['platform'] = $data['sid'] == 2?'ios':'android';
        $feedback['content'] =  base64_encode($feedback['content']);
        if(!empty($data['file'])){
            $image = str_replace(" ","+",$data['file']);
            $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
            if (strstr($image,",")){
                $image = explode(',',$image);
                $image = $image[1];
            }
            $date = date("Ymd",time());
            $path = $this->path.$date;
            if (!is_dir($path)){ //判断目录是否存在 不存在就创建
                mkdir($path,0777,true);
            }
            $imageSrc= $path."/". $imageName; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
            if ($r) {
                $feedback['content'] = "https://".THIS_URL.'/images/uploads/'.$date.'/'.$imageName;
                $feedback['img'] = "https://".THIS_URL.'/images/uploads/'.$date.'/'.$imageName;
            }
        }

        $feedback['created_at']= time();
        $tb = otable::FeedBack2();
        $sql= funs::db_insertSQL($tb,$feedback);
        oo::commonOprDb('common')->query($sql,false);
        return ['code'=>200,'msg'=>'success','data'=>[]];
    }

    public function user_get($uid){
        $data = [];
        $w = " user_id={$uid} AND orientation=2 AND have_read=0";
        $data = $this->selectFromDb($w);
        return ['code'=>200,'msg'=>'success','data'=>$data];
    }

    public function user_read($uid,$ids=''){
        $tb = otable::FeedBack2();
        $sql = "UPDATE {$tb} SET have_read=1 WHERE user_id={$uid} AND orientation=2 AND have_read=0";
        oo::commonOprDb('common')->query($sql,false);
        return ['code'=>200,'msg'=>'success','data'=>[]];
    }

    public function sys_get($data){
        $where_str=' 1 ';
        if(isset($data['user_id'])){
            $where_str.=" and user_id ={$data['user_id']}";
        }
        if(isset($data['type'])){
            $where_str.=" and type ={$data['type']}";
        }
        if(isset($data['start_at'])){
            $where_str.=" and created_at >={$data['start_at']}";
        }
        if(isset($data['end_at'])){
            $where_str.=" and created_at <={$data['end_at']}";
        }

        if(isset($data['have_read'])){
            $where_str.=" and have_read ={$data['have_read']}";
        }
        if(isset($data['orientation'])){
            $where_str.=" and orientation ={$data['orientation']}";
        }

        $res1 = $this->selectFromDb($where_str);
        $total =count($res1);
        if(isset($data['user_id'])){
            $where_str .=" order by created_at desc ";
        }else{
            $tb = otable::FeedBack2();
            $where_str .=" AND id in ( SELECT MAX(id) FROM {$tb} GROUP BY user_id ) order by id desc ";
        }

        if(!isset($data['page'])){
            $page=1;
        }else{
            $page=(int)$data['page'];
        }
        if(isset($data['limit'])){
            $limit=(int)$data['limit'];
            $where_str .= ' limit '.(($page-1)*$limit).','.$limit;
        }else{
            $limit=10;
            $where_str .= ' limit '.(($page-1)*$limit).','.$limit;
        }
        $res2 = $this->selectFromDb($where_str);
        foreach($res2 as $key => $row){
            $temp = oo::commonOprModel('ipaddress')->getUserCountry($row['user_id']);
            $res2[$key]['ip'] = $temp['ip'];//IP
            $res2[$key]['country_code'] = $temp['country_code'];//地区
        }
        return ['code'=>1,'data'=>$res2,'page'=>$page,'total'=>$total];
    }

    public function sys_add($data){
        $feedback['user_id'] = $data['user_id'];
        $feedback['content'] = $data['content'];
        $feedback['orientation']= 2;
        $feedback['have_read']= 0;
        $feedback['type']= $data['type'];
        $feedback['created_at']= time();
        $tb = otable::FeedBack2();
        $sql= funs::db_insertSQL($tb,$feedback);
        oo::commonOprDb('common')->query($sql,false);
        $id = oo::commonOprDb('common')->insertID();

        $tb = otable::FeedBack2();
        $sql = "UPDATE {$tb} SET have_read=1 WHERE user_id={$feedback['user_id']} AND orientation=1";
        $res = oo::commonOprDb('common')->query($sql,false);
        return ['code'=>200,'msg'=>'ok','data'=>[]];
    }

    public function sys_read($uid){
        $tb = otable::FeedBack2();
        $sql = "UPDATE {$tb} SET have_read=1 WHERE user_id={$uid} AND orientation=1";
        oo::commonOprDb('common')->query($sql,false);
        return ['code'=>200,'msg'=>'ok'];
    }

    public function selectFromDb($w,$new = false){
        $tb = otable::FeedBack2();
        $sql = "SELECT * FROM {$tb} WHERE $w";

        $data = oo::commonOprDb('common')->getAll($sql,1);
        if(!empty($data)){
            foreach ($data as $k=>$v){
                if(funs::is_base64($v['content'])){
                    $data[$k]['content'] = base64_decode($v['content']);
                }

            }
        }
        return $data;
    }
}