123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/7/26
- * Time: 15:25
- */
- namespace backend\controllers;
- use backend\models\AuthArea;
- use common\models\Appointment;
- use common\models\AppointmentVisit;
- use common\models\Building;
- use common\models\Comment;
- use yii;
- use yii\filters\AccessControl;
- use yii\web\HttpException;
- /**
- * 工地管理
- * @package backend\controllers
- */
- class BuildingController extends BaseController
- {
- public $layout = 'iframe';
- const PAGESIZE = 10;
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => [],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- ];
- }
- /**
- * 工地列表
- */
- public function actionIndex()
- {
- $type = Yii::$app->request->get('type');
- $company = Yii::$app->request->get('company');
- $name = Yii::$app->request->get('name');
- //$mod = Building::find()->where(['posted'=>Building::POSTED_YES]);
- $mod = Building::find()->where(['a.posted'=>Building::POSTED_YES])->select('a.* ,b.company')->from('{{%building}} a')->leftJoin(['b'=>'{{%user_company}}'],'a.uid=b.uid');
- if(!empty($type)){
- $mod->andWhere('a.type=:type',[':type'=>$type]);
- }
- if(!empty($company)){
- $mod->andWhere('b.company like :company',[':company'=>"%$company%"]);
- }
- if(!empty($name)){
- $mod->andWhere('a.name like :name',[':name'=>"%$name%"]);
- }
- $citys = AuthArea::queryAuth();
- if($citys=='all')
- $mod;
- elseif (!empty($citys))
- $mod->andWhere("a.city in ({$citys})");
- else
- $mod->andWhere("a.city = -1");
- $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
- $datas=$mod->offset($pages->offset)->limit($pages->limit)->addOrderBy(['a.id'=>SORT_DESC])->asArray()->all();
- return $this->render('index',['datas'=>$datas,'pages'=>$pages,'type'=>$type,'company'=>$company,'name'=>$name]);
- }
- /**
- * 用户预约列表
- */
- public function actionAppointments()
- {
- $type = Yii::$app->request->get('type');
- $company = Yii::$app->request->get('company');
- $mod = Appointment::find()->where(['a.status'=>Appointment::STATUS_DEAL])->select('a.* ,b.name,b.city,b.address,c.company')->from('{{%appointment}} a')->leftJoin(['b'=>'{{%building}}'],'a.pid=b.id')->leftJoin(['c'=>'{{%user_company}}'],'b.uid=c.uid');
- if(!empty($type)){
- $mod->andWhere('a.type=:type',[':type'=>$type]);
- }
- if(!empty($company)){
- $mod->andWhere('b.name like :company',[':company'=>"%$company%"]);
- }
- $citys = AuthArea::queryAuth();
- if($citys=='all')
- $mod;
- elseif (!empty($citys))
- $mod->andWhere("b.city in ({$citys})");
- else
- $mod->andWhere("b.city = -1");
- $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
- $datas=$mod->offset($pages->offset)->limit($pages->limit)->addOrderBy(['a.id'=>SORT_DESC])->asArray()->all();
- if(!empty($datas))
- {
- foreach ($datas as $key => $val)
- {
- $datas[$key]['visit'] = AppointmentVisit::find()->where(['aid'=>$val['id']])->orderBy("id ASC")->asArray()->all();
- }
- }
- return $this->render('appointments',['datas'=>$datas,'pages'=>$pages,'type'=>$type,'company'=>$company]);
- }
- /**
- * 确认回访
- */
- public function actionHandleApp()
- {
- $result=['sign'=>1,'msg'=>'操作成功'];
- $id = Yii::$app->request->post('id');
- $content = Yii::$app->request->post('content');
- if($content =='')
- {
- $result=['sign'=>0,'msg'=>'回访记录不能为空'];
- return json_encode($result);
- }
- $model = Appointment::find()->where(['id'=>$id])->one();
- if(empty($model)){
- $result=['sign'=>0,'msg'=>'找不到记录'];
- }else{
- if($model->handle != Appointment::HANDLE_YES){
- $model->handle = Appointment::HANDLE_YES;
- $model->updated_at = time();
- if($model->save()){
- $result=['sign'=>1,'msg'=>'操作成功'];
- }
- else{
- $result=['sign'=>0,'msg'=>'操作失败'];
- return json_encode($result);
- }
- }
- $visit_model = new AppointmentVisit();
- $visit_model->aid = $model->id;
- $visit_model->content = $content;
- $visit_model->admin_id = Yii::$app->user->id;
- $visit_model->operation = Yii::$app->user->identity->username;
- $visit_model->c_time = time();
- if($visit_model->save()){
- $result['data'] = array('content'=>$visit_model->content,'operation'=>$visit_model->operation,'c_time'=>date("Y-m-d H:i",$visit_model->c_time));
- }else{
- $result=['sign'=>0,'msg'=>'操作失败'];
- }
- }
- return json_encode($result);
- }
- /**
- * 工地预约记录
- */
- public function actionAppointment()
- {
- $id = \Yii::$app->request->get('id');
- $mod = Appointment::find()->where(['pid'=>$id]);
- $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
- $datas=$mod->offset($pages->offset)->limit($pages->limit)->orderBy('created_at desc')->all();
- return $this->render('appointment',['datas'=>$datas,'pages'=>$pages]);
- }
- /**
- * 工地评论记录
- */
- public function actionComment()
- {
- $id = \Yii::$app->request->get('id');
- $mod = Comment::find()->where('pid=:pid and status=:status',[':pid'=>$id,':status'=>Comment::STATUS_ACTIVE]);
- $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
- $datas=$mod->offset($pages->offset)->limit($pages->limit)->all();
- return $this->render('comment',['datas'=>$datas,'pages'=>$pages]);
- }
- /**
- * 工地详情
- * @return string
- */
- public function actionEdit()
- {
- $id = Yii::$app->request->get('id');
- $data = Yii::$app->db->createCommand('SELECT * from {{%building}} WhERE id = :id ')
- ->bindValue(':id',$id)
- ->queryOne();
- $data['company'] = Yii::$app->db->createCommand('SELECT * from {{%user_company}} WhERE uid = :uid')
- ->bindValue(':uid',$data['uid'])
- ->queryOne();
- $data['manager'] = Yii::$app->db->createCommand('SELECT realname,introduction from {{%manager}} WHERE id =:id')->bindValue(':id',$data['manager_id'])->queryOne();
- $data['designer'] = Yii::$app->db->createCommand('SELECT realname,introduction from {{%designer}} WHERE id =:id')->bindValue(':id',$data['designer_id'])->queryOne();
- return $this->render('edit',['data'=>$data]);
- }
- /**
- * 删除工地
- */
- public function actionDeletebuilding(){
- //post 批量删除
- if(Yii::$app->request->isPost)
- {
- $checks = Yii::$app->request->post('check');
- if(empty($checks) || !is_array($checks)){
- Yii::$app->getSession()->setFlash('error','没有选中项');
- return $this->redirect(Yii::$app->request->referrer);
- }
- $checks = implode(",",$checks);
- $result = Yii::$app->db->createCommand('UPDATE {{%building}} SET posted = '.Building::POSTED_DELETE.' WhERE id in ('.$checks.')')->execute();
- if($result)
- {
- Yii::$app->getSession()->setFlash('success','删除成功');
- return $this->redirect(Yii::$app->request->referrer);
- }else{
- Yii::$app->getSession()->setFlash('error','删除失败');
- return $this->redirect(Yii::$app->request->referrer);
- }
- }
- $id = \Yii::$app->request->get('id');
- $model = Building::findOne($id);//删除样板房记录
- $model->posted = Building::POSTED_DELETE;
- $model->updated_at =time();
- if($model->validate()&&$model->save())
- {
- \Yii::$app->getSession()->setFlash('success','删除成功');
- return $this->redirect(['index']);
- }else{
- var_dump($model->getErrors());exit;
- \Yii::$app->getSession()->setFlash('error','删除失败');
- return $this->redirect(['index']);
- }
- }
- }
|