tp3.2 实现增删改查


 */
class BusinessController extends AdminController {
    /**
     * 商务管理首页
     * @author mzc
     * @date 2018/5/24 10:40
     */
    public function index(){
        $keyword = I('keyword', '', 'string');
        $map['status'] = 1;
        if(!empty($keyword)){
            $map['gamename|cpinfo|created_user'] = ['like','%'.$keyword.'%'];
        }
        //商务管理列表查询
        $businessList = M('business', 'cy_')
            ->field('*')
            ->where($map)
            ->order('id desc')
            ->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))
            ->select();

        $this->assign('businessList', $businessList);

        $totalNum = M('business', 'cy_')->where($map)->count();
        $pages = new \Common\Util\Page($totalNum,C('ADMIN_PAGE_ROWS'));
        $this->assign('_page',$pages->show());
        $this->display();
    }

    /**
     * 添加开服页面
     */
    public function add()
    {
        if (IS_POST) {
            try{
                $user = session("user_auth");
                $businessModel = D('business');

                $gamename = trim(I('post.gamename'));
                if(!empty($gamename)){
                    $count = $businessModel->checkGamenameIsExist($gamename);
                    if($count>0){
                        throw new \Exception('游戏已经存在');
                    }
                }

                if($data = $businessModel->create()){
                    $data['collaboration_at'] = strtotime($data['collaboration_at']);
                    $data['created_user'] = $user['username'];
                    $data['created_at'] = time();
                    if ($id = $businessModel->add($data)) {
                        addOperationRecord(53, '用户【' . session('user_auth.nickname') . '】新增了商务管理ID为【' .$id . '】的商务管理!');
                        $this->success('添加游戏成功', U('Business/index'));
                    } else {
                        $this->error('添加游戏失败');
                    }
                }else{
                    $this->error('添加游戏失败,原因:'.$businessModel->getError());
                }
            }catch (\Exception  $ex){
                $this->error('添加游戏失败,原因:'.$ex->getMessage());
            }
        } else {
            $this->display('add_edit');
        }
    }


    /**
     * 修改商务管理
     */
    public function edit($id)
    {
        $id = I('get.id');
        $businessModel = D('Business')->where(array('id' => $id));
        if (IS_POST) {
            try {
                //验证游戏是否存在(修改了并且游戏名称与原来不一样才提示)
                $oldgamename = trim(I('post.oldgamename'));
                $gamename = trim(I('post.gamename'));
                if($gamename!=$oldgamename&&!empty($gamename)){
                    $count = $businessModel->checkGamenameIsExist($gamename);
                    if($count>0){
                        throw new \Exception('游戏已经存在');
                    }
                }

                $user = session("user_auth");
                if ($data = $businessModel->create()) {
                    $data['collaboration_at'] = strtotime($data['collaboration_at']);
                    $data['created_user'] = $user['username'];
                    $data['created_at'] = time();

                    unset($data['id']);
                    if ($a = $businessModel->where(['id'=>$id])->save($data)) {
                        addOperationRecord(53, '用户【' . session('user_auth.nickname') . '】修改了商务管理ID为【' . $id . '】的商务管理!');
                        $this->success('修改游戏成功', U('Business/index'));
                    } else {
                        $this->error('修改游戏失败');
                    }
                } else {
                    $this->error('修改游戏失败,原因:' . $businessModel->getError());
                }
            }catch(\Exception $ex){
                $this->error('修改游戏失败,原因:'.$ex->getMessage());
            }
        } else {
            $business = $businessModel->find();
            $this->assign('business', $business);
            $this->display('add_edit');
        }
    }


    /**
     * 删除商务管理
     */
    public function delete()
    {
        $return = array('code' => 1, 'message' => "请求无效", 'data' => array());
        if (IS_POST) {
            $id = $_POST['id'];
            if (!$id) {
                $return['message'] = 'id参数不能为空!';
                $this->ajaxReturn($return);
            }

            $gameModel = D('Game');
            $count = $gameModel->where(['business_id'=>$id])->count();
            if($count>0){
                $return['message'] = '该商务管理下存在游戏,不能删除';
                $this->ajaxReturn($return);
            }

            //游戏信息
            $model =  M('Business','cy_');
            $status =  $model->where(array('id' => $id))->delete();
            if ($status) {
                $return['code'] = 0;
                $return['message'] = '删除游戏成功!';
                addOperationRecord(53, '用户【' . session('user_auth.nickname') . '】删除了商务管理ID为【' .$id . '】的商务管理!');
                $this->ajaxReturn($return);
            } else {
                $return['message'] = '删除游戏失败!';
                $this->ajaxReturn($return);
            }
        } else {
            $this->ajaxReturn($return);
        }
    }
}

你可能感兴趣的:(tp3.2)