<?php
/**
 * Created by PhpStorm.
 * User: cy 707670631@qq.com
 * Date: 2017/4/27
 * Time: 9:43
 */

namespace Open\Model;

class ServerModel extends BaseModel{


    protected $_validate = [
        ['game_id',  'require', '游戏不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
        ['server_name',  'require', '区服名称不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
        ['start_time',  'require', '开始时间不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
    ];

    protected $_auto = [
        ['create_time',       'time',         self::MODEL_INSERT,  'function'],
        ['status',       '0',         self::MODEL_INSERT],
    ];


    public function getDataLists($map,$p,$order="create_time desc",$row=10){
        $page = $this->dealPage($p);
        $data = $this->alias("s")
            ->field("s.id,s.game_id,s.game_name,s.server_version,s.server_name,s.start_time,s.create_time,s.show_status")
            ->where($map)
            ->order($order)
            ->page($page,$row)
            ->select();
        $count = $this->alias("s")->where($map)->count();
        $result['data'] = $data;
        $result['count'] = $count;
        return $result;
    }
    /**
     * [获取单个礼包信息]
     * @return [mixed] [description]
     * author cy 707670631
     */
    public function detail($gift_id,$user_id){
        $map['id']=$gift_id;
        $map['developers']=$user_id;
        $data=$this
            ->where($map)
            ->find();
        return $data;
    }
    /**
     * [删除礼包]
     * @return [mixed] [description]
     * author cy 707670631
     */
    public function del($ids){
        $map['id']=array('in',$ids);
        $res=$this->where($map)->delete();        
        if($res){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 游戏信息保存、更新
     * @return bool|mixed
     * author: xmy 280564871@qq.com
     */
    public function upDate($user_id,$game_id=""){
        $data = $this->create();
        if(!$data){
            return false;
        }
        $data['game_name']   = get_game_name($data['game_id']);
        $data['developers']  = UID;
        $data['show_status'] = 1;
        $data['start_time']  = strtotime($data['start_time']);
        if(empty($data['id'])){ //新增数据
            $id = $this->add($data); //添加基础内容
            if(!$id){
                $this->error = '新增基础内容出错!';
                return false;
            }
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新基础内容出错!';
                return false;
            }
        }
        return $data;
    }
}