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

namespace Open\Model;

class GiftbagModel extends BaseModel{


    protected $_validate = [
        ['game_id',  'require', '游戏不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
        ['giftbag_name',  'require', '礼包名称不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
        ['giftbag_version',  'require', '运营平台不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH],
        ['start_time',  'require', '开始时间不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_INSERT],
        ['end_time',  'require', '结束时间不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH],
        /* ['digest',  'require', '领取方式不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH],
        ['desribe',  'require', '礼包内容不能为空',         self::MUST_VALIDATE,  'regex',  self::MODEL_BOTH], */
    ];

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


    public function getDataLists($map,$p,$order="create_time desc",$row=10){
        $page = $this->dealPage($p);
        $data = $this->alias("gb")
            ->field("gb.id,gb.game_id,gb.game_name,gb.status,gb.create_time,gb.end_time,gb.apply_status,gb.giftbag_version,gb.giftbag_name,gb.novice_num")
            ->where($map)
            ->order($order)
            ->page($page,$row)
            ->select();
        $count = $this->alias("g")->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;
        }
        $file = file_get_contents($_FILES['file']['tmp_name']);
        $txt = R('File/uploadFile',array('flag'=>true));
        if ($txt['status']) {
                $data['giftbag_url'] = $txt['path'];
                $data['giftbag_filename'] = $txt['name'];
                $data['giftbag_fileid']=$txt['id'];
        }
        $file1 = array_filter(explode(',',preg_replace('/[\\r\\n]/',',',$file)));
        $data['novice'] = implode(',',$file1);
        $data['game_name']=get_game_name($data['game_id']);
        $data['novice_num']=count($file1);
        $data['developers']=UID;
        $data['start_time']=strtotime($data['start_time']);
        $data['end_time']=strtotime($data['end_time']);
        $data['apply_status'] = 1;
        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;
    }
}