Is_Login(); // 权限校验 $this->Is_Power(); //要执行的表 $this->table = M('activity_mengbao'); } /** * [Index 列表] * @author Devil * @blog http://gong.gg/ * @version 0.0.1 * @datetime 2016-12-06T21:31:53+0800 */ public function Index() { $where = array(); $listRows = I('listRows') ? I('listRows') : 100; $keyword = I("keyword", "", "trim"); $keyword && $where['name'] = array('like', '%' . $keyword . '%'); if (isset($keyword) && !empty($keyword)) { $this->assign('keyword', $keyword); } // var_dump(I('post.'));die; $page = I('p') ? I('p') : 1; $this->assign('p', $page); $list = D('Admin/Mengbao')->get_mengbao_list($where, $page, $listRows, $count); // echo "
";
        // var_dump($list);  die;  
        //分页
        $Page = new \Think\Page($count, $listRows);
        $Page->parameter .= "&p=[PAGE]";
        $Page->parameter .= "&listRows=" . $listRows;
        $Page->parameter .= "&keyword=" .$keyword;
        $Page = $this->page_config($Page);
        $show = $Page->show();
        $this->assign('page', $show);

        $this->assign('List', $list);
        $this->display('Index');
    }

    /**
     * [ExcelExport excel文件导出]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-01-10T15:46:00+0800
     */
    public function ExcelExport()
    {
        $where = array();
        $data = M('activity_mengbao')->where($where)->order('score desc , id asc')->select();

        $title = array(
            'id'						 =>	array('col' => 'A', 'name' => 'ID'),
            'name'						 =>	array('col' => 'B', 'name' => '名字'),
            'uid'						 =>	array('col' => 'C', 'name' => 'uid'),
            'phone'					     =>	array('col' => 'D', 'name' => '联系人手机号'),
            'score'					     =>	array('col' => 'E', 'name' => '得票数'),
            'is_show'                    => array('col' => 'F', 'name' => '在线状态(1在线,0下线)')
        );
        // Excel驱动导出数据
        $excel = new \My\Excel(array('filename' => '萌宝排行榜', 'title' => $title, 'data' => $data, 'msg' => L('common_not_data_tips')));
        $excel->Export();
    }

    /**
     * [SaveInfo 文章添加/编辑页面]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-14T21:37:02+0800
     */
    public function SaveInfo() {
        // 文章信息
        if (empty($_REQUEST['id'])) {
            $data = array();
        } else {
            $data = $this->table->find(I('id'));
            if (empty($data)) {
                $data = array('id' => I('id'));
            }
        }
        if ($_REQUEST['map']) {
            $map = $_REQUEST['map'];
        }
        //素材列表
        if (I('id')) {
            $sourceList = M('activity_mengbao')->where(['id' => array('neq', I('id'))])->field('id as source_id,name as source_name')->select();
        }
        $this->assign('sourceList', $sourceList);

        $this->assign('data', $data);
        $this->display('SaveInfo');
    }

    /**
     * [Save 文章添加/编辑]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-14T21:37:02+0800
     */
    public function Save() {
        // 是否ajax请求
        if (!IS_AJAX) {
            $this->error(L('common_unauthorized_access'));
        }
        // 添加
        if (empty($_POST['id'])) {
            $this->Add();

            // 编辑
        } else {
            $this->Edit();
        }
    }

    /**
     * [Add 文章添加]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-18T16:20:59+0800
     */
    private function Add() {
        $m = $this->table;
        $data = array();
        // 额外数据处理
        // $data['created_at'] = date('Y-m-d H:i:s', time());
        // $data['name'] = I('name');
        // if ($data['name']) {
        //     $data['first_string'] = $this->first_string($data['name']);
        // }
        $data['uid'] = I('uid');
        $data['name'] = I('name');
        $data['age'] = I('age');
        $data['image'] = I('image');
        $data['desc'] = I('desc');
        $data['phone'] = I('phone');
        $data['media_id'] = I('media_id');
        $data['media_num'] = I('media_num');
        $data['score'] = I('score');
        $data['video'] = I('video');
        $data['is_show'] = I('is_show');

        // 数据添加
        if ($m->add($data)) {
            $this->ajaxReturn(L('common_operation_add_success'));
        } else {
            $this->ajaxReturn(L('common_operation_add_error'), -100);
        }
    }

    /**
     * [Edit 文章编辑]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-17T22:13:40+0800
     */
    private function Edit() {
        $m = $this->table;
        $source_id = I('id');
        $data = array();
        // 额外数据处理
        // $data['name'] = I('name');
        // if ($data['name']) {
        //     $data['first_string'] = $this->first_string($data['name']);
        // }
        $data['uid'] = I('uid');
        $data['name'] = I('name');
        $data['age'] = I('age');
        $data['image'] = I('image');
        $data['desc'] = I('desc');
        $data['phone'] = I('phone');
        $data['media_id'] = I('media_id');
        $data['media_num'] = I('media_num');
        $data['score'] = I('score');
        $data['video'] = I('video');
        $data['is_show'] = I('is_show');
        // 数据更新

        if (false !== $m->where(array('id' => I('id')))->save($data)) {
            $this->ajaxReturn(L('common_operation_edit_success'));
        } else {
//             echo M()->getLastsql();die;
            $this->ajaxReturn(L('common_operation_edit_error'), -100);
        }
    }

    /**
     * [Delete 删除]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-15T11:03:30+0800
     */
    public function Delete() {
        // 是否ajax请求
        if (!IS_AJAX) {
            $this->error(L('common_unauthorized_access'));
        }

        // 删除数据
        if (!empty($_POST['id'])) {
            // 更新
            $id = I('id');
            $update = ['id'=>$id,'is_show'=>0];
            if ($this->table->save($update)) {
                $this->ajaxReturn(L('common_operation_delete_success'));
            } else {
                $this->ajaxReturn(L('common_operation_delete_error'), -100);
            }
        } else {
            $this->ajaxReturn(L('common_param_error'), -1);
        }
    }

    public function importExcel()
    {
        $param['title'] = array(
            "id"            => array('name' => 'id'),
            "uid"           => array('name' => 'uid'),
            "phone"         => array('name' => 'phone'),
            "name"          => array('name' => 'name'),
            "age"           => array('name' => 'age'),
            "desc"          => array('name' => 'desc'),
            'score'         => array('name' => 'score'),
            "image"         => array('name' => 'image'),
            "video"         => array('name' => 'video'),
            "media_id"      => array('name' => 'media_id'),
            "media_num"     => array('name' => 'media_num'),
            "is_show"       => array('name' => 'is_show')
        );
        $excel       = new \My\Excel($param);
        $excel_data        = $excel->Import($_FILES['excel']['tmp_name']);
        $data        = array_filter($excel_data);
        if(!$data){
            $msg = "检查标题是否对应正确";
            goto end;
        }
        $update_data = $insert_data = array();
        foreach ($data as $index=>$row) {
            $row_num = $index + 2;
            if(!$row = array_filter($row)){
                $msg = '检查数据行: '.$row_num;
                goto end;
            }
            //存在ID即代表更新
            if($row['id']){
                if(count($row) > 1){
                    //查看该素材是否存在,如果存在则更新,不存在则插入
                    if(M('activity_mengbao')->where(['id'=>$row['id']])->find()){
                        if(M('activity_mengbao')->save($row) === false){
                            Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
                            $msg = '数据行更新失败: ' . $row_num . ' ,请告知技术进行处理';
                            goto end;
                        }
                    }
                }else{
                    $msg = '检查数据行: '.$row_num;
                    goto end;
                }
            }else{
                //新插入时检查注入所必须字段
                $must_fields = array('name','image','phone','uid');
                foreach ($must_fields as $field) {
                    if(!isset($row[$field])){
                        $msg = '检查数据行: '.$row_num;
                        goto end;
                    }
                }
                // $row['first_string'] = $this->first_string($row['name']);//首字母
                $row['created_at'] = date('Y-m-d H:i:s');
                if(!M('activity_mengbao')->add($row)){
                    Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
                    $msg = '数据行插入失败: ' . $insertID . ' ,请告知技术进行处理';
                    goto end;
                }

                $insert_data[] = $row;
            }
        }
        $msg = '导入完成';
        goto end;

        end:
        $responsedata = array("msg" => $msg, "res" => 1, 'data' => []);
        echo json_encode($responsedata,JSON_UNESCAPED_UNICODE);
        exit;
    }

    /**
     * 批量更新
     * @param  string $table_name [description]
     * @param  array  $data       [description]
     * @param  string $field      [description]
     * @return [type]             [description]
     */
    public function batch_update($table_name = '', $data = array(), $field = '') {
        if (!$table_name || !$data || !$field) {
            return false;
        } else {
            $sql = 'UPDATE ' . C('DB_PREFIX') . $table_name;
        }
        $con = array();
        $con_sql = array();
        $fields = array();
        foreach ($data as $key => $value) {
            $x = 0;
            foreach ($value as $k => $v) {
                if ($k != $field && !$con[$x] && $x == 0) {
                    $con[$x] = " set {$k} = (CASE {$field} ";
                } elseif ($k != $field && !$con[$x] && $x > 0) {
                    $con[$x] = " {$k} = (CASE {$field} ";
                }
                if ($k != $field) {
                    $temp = $value[$field];
                    $con_sql[$x] .= " WHEN '{$temp}' THEN '{$v}' ";
                    $x++;
                }
            }
            $temp = $value[$field];
            if (!in_array($temp, $fields)) {
                $fields[] = $temp;
            }
        }
        $num = count($con) - 1;
        foreach ($con as $key => $value) {
            foreach ($con_sql as $k => $v) {
                if ($k == $key && $key < $num) {
                    $sql .= $value . $v . ' end),';
                } elseif ($k == $key && $key == $num) {
                    $sql .= $value . $v . ' end)';
                }
            }
        }
        $str = implode(',', $fields);
        $sql .= " where {$field} in({$str})";
        $res = M($table_name)->execute($sql);
        return $res;
    }

    /**
     * 上传图片文件
     * @return [type] [description]
     */
    public function uploadPics() {
        $temp_config['pathFormat'] = C('uploaderSourceImg');
        $temp_config['maxSize'] = C('ueditor_config.imageMaxSize');
        $temp_config['allowFiles'] = C('ueditor_config.imageAllowFiles');
        $up = new \My\Uploader('pics', $temp_config);
        $info = $up->getFileInfo();
        if ($info['state'] == 'SUCCESS') {
            $this->ajaxReturn($info['state'], 200);
        } else {
            $this->ajaxReturn($info['state'], 400);
        }
    }


}