BuildingController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/7/26
  6. * Time: 15:25
  7. */
  8. namespace backend\controllers;
  9. use backend\models\AuthArea;
  10. use common\models\Appointment;
  11. use common\models\AppointmentVisit;
  12. use common\models\Building;
  13. use common\models\Comment;
  14. use yii;
  15. use yii\filters\AccessControl;
  16. use yii\web\HttpException;
  17. /**
  18. * 工地管理
  19. * @package backend\controllers
  20. */
  21. class BuildingController extends BaseController
  22. {
  23. public $layout = 'iframe';
  24. const PAGESIZE = 10;
  25. public function behaviors()
  26. {
  27. return [
  28. 'access' => [
  29. 'class' => AccessControl::className(),
  30. 'rules' => [
  31. [
  32. 'actions' => [],
  33. 'allow' => true,
  34. 'roles' => ['@'],
  35. ],
  36. ],
  37. ],
  38. ];
  39. }
  40. /**
  41. * 工地列表
  42. */
  43. public function actionIndex()
  44. {
  45. $type = Yii::$app->request->get('type');
  46. $company = Yii::$app->request->get('company');
  47. $name = Yii::$app->request->get('name');
  48. //$mod = Building::find()->where(['posted'=>Building::POSTED_YES]);
  49. $mod = Building::find()->where(['a.posted'=>Building::POSTED_YES])->select('a.* ,b.company')->from('{{%building}} a')->leftJoin(['b'=>'{{%user_company}}'],'a.uid=b.uid');
  50. if(!empty($type)){
  51. $mod->andWhere('a.type=:type',[':type'=>$type]);
  52. }
  53. if(!empty($company)){
  54. $mod->andWhere('b.company like :company',[':company'=>"%$company%"]);
  55. }
  56. if(!empty($name)){
  57. $mod->andWhere('a.name like :name',[':name'=>"%$name%"]);
  58. }
  59. $citys = AuthArea::queryAuth();
  60. if($citys=='all')
  61. $mod;
  62. elseif (!empty($citys))
  63. $mod->andWhere("a.city in ({$citys})");
  64. else
  65. $mod->andWhere("a.city = -1");
  66. $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
  67. $datas=$mod->offset($pages->offset)->limit($pages->limit)->addOrderBy(['a.id'=>SORT_DESC])->asArray()->all();
  68. return $this->render('index',['datas'=>$datas,'pages'=>$pages,'type'=>$type,'company'=>$company,'name'=>$name]);
  69. }
  70. /**
  71. * 用户预约列表
  72. */
  73. public function actionAppointments()
  74. {
  75. $type = Yii::$app->request->get('type');
  76. $company = Yii::$app->request->get('company');
  77. $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');
  78. if(!empty($type)){
  79. $mod->andWhere('a.type=:type',[':type'=>$type]);
  80. }
  81. if(!empty($company)){
  82. $mod->andWhere('b.name like :company',[':company'=>"%$company%"]);
  83. }
  84. $citys = AuthArea::queryAuth();
  85. if($citys=='all')
  86. $mod;
  87. elseif (!empty($citys))
  88. $mod->andWhere("b.city in ({$citys})");
  89. else
  90. $mod->andWhere("b.city = -1");
  91. $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
  92. $datas=$mod->offset($pages->offset)->limit($pages->limit)->addOrderBy(['a.id'=>SORT_DESC])->asArray()->all();
  93. if(!empty($datas))
  94. {
  95. foreach ($datas as $key => $val)
  96. {
  97. $datas[$key]['visit'] = AppointmentVisit::find()->where(['aid'=>$val['id']])->orderBy("id ASC")->asArray()->all();
  98. }
  99. }
  100. return $this->render('appointments',['datas'=>$datas,'pages'=>$pages,'type'=>$type,'company'=>$company]);
  101. }
  102. /**
  103. * 确认回访
  104. */
  105. public function actionHandleApp()
  106. {
  107. $result=['sign'=>1,'msg'=>'操作成功'];
  108. $id = Yii::$app->request->post('id');
  109. $content = Yii::$app->request->post('content');
  110. if($content =='')
  111. {
  112. $result=['sign'=>0,'msg'=>'回访记录不能为空'];
  113. return json_encode($result);
  114. }
  115. $model = Appointment::find()->where(['id'=>$id])->one();
  116. if(empty($model)){
  117. $result=['sign'=>0,'msg'=>'找不到记录'];
  118. }else{
  119. if($model->handle != Appointment::HANDLE_YES){
  120. $model->handle = Appointment::HANDLE_YES;
  121. $model->updated_at = time();
  122. if($model->save()){
  123. $result=['sign'=>1,'msg'=>'操作成功'];
  124. }
  125. else{
  126. $result=['sign'=>0,'msg'=>'操作失败'];
  127. return json_encode($result);
  128. }
  129. }
  130. $visit_model = new AppointmentVisit();
  131. $visit_model->aid = $model->id;
  132. $visit_model->content = $content;
  133. $visit_model->admin_id = Yii::$app->user->id;
  134. $visit_model->operation = Yii::$app->user->identity->username;
  135. $visit_model->c_time = time();
  136. if($visit_model->save()){
  137. $result['data'] = array('content'=>$visit_model->content,'operation'=>$visit_model->operation,'c_time'=>date("Y-m-d H:i",$visit_model->c_time));
  138. }else{
  139. $result=['sign'=>0,'msg'=>'操作失败'];
  140. }
  141. }
  142. return json_encode($result);
  143. }
  144. /**
  145. * 工地预约记录
  146. */
  147. public function actionAppointment()
  148. {
  149. $id = \Yii::$app->request->get('id');
  150. $mod = Appointment::find()->where(['pid'=>$id]);
  151. $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
  152. $datas=$mod->offset($pages->offset)->limit($pages->limit)->orderBy('created_at desc')->all();
  153. return $this->render('appointment',['datas'=>$datas,'pages'=>$pages]);
  154. }
  155. /**
  156. * 工地评论记录
  157. */
  158. public function actionComment()
  159. {
  160. $id = \Yii::$app->request->get('id');
  161. $mod = Comment::find()->where('pid=:pid and status=:status',[':pid'=>$id,':status'=>Comment::STATUS_ACTIVE]);
  162. $pages = new yii\data\Pagination(["totalCount"=>$mod->count(),"pageSize"=>self::PAGESIZE]);
  163. $datas=$mod->offset($pages->offset)->limit($pages->limit)->all();
  164. return $this->render('comment',['datas'=>$datas,'pages'=>$pages]);
  165. }
  166. /**
  167. * 工地详情
  168. * @return string
  169. */
  170. public function actionEdit()
  171. {
  172. $id = Yii::$app->request->get('id');
  173. $data = Yii::$app->db->createCommand('SELECT * from {{%building}} WhERE id = :id ')
  174. ->bindValue(':id',$id)
  175. ->queryOne();
  176. $data['company'] = Yii::$app->db->createCommand('SELECT * from {{%user_company}} WhERE uid = :uid')
  177. ->bindValue(':uid',$data['uid'])
  178. ->queryOne();
  179. $data['manager'] = Yii::$app->db->createCommand('SELECT realname,introduction from {{%manager}} WHERE id =:id')->bindValue(':id',$data['manager_id'])->queryOne();
  180. $data['designer'] = Yii::$app->db->createCommand('SELECT realname,introduction from {{%designer}} WHERE id =:id')->bindValue(':id',$data['designer_id'])->queryOne();
  181. return $this->render('edit',['data'=>$data]);
  182. }
  183. /**
  184. * 删除工地
  185. */
  186. public function actionDeletebuilding(){
  187. //post 批量删除
  188. if(Yii::$app->request->isPost)
  189. {
  190. $checks = Yii::$app->request->post('check');
  191. if(empty($checks) || !is_array($checks)){
  192. Yii::$app->getSession()->setFlash('error','没有选中项');
  193. return $this->redirect(Yii::$app->request->referrer);
  194. }
  195. $checks = implode(",",$checks);
  196. $result = Yii::$app->db->createCommand('UPDATE {{%building}} SET posted = '.Building::POSTED_DELETE.' WhERE id in ('.$checks.')')->execute();
  197. if($result)
  198. {
  199. Yii::$app->getSession()->setFlash('success','删除成功');
  200. return $this->redirect(Yii::$app->request->referrer);
  201. }else{
  202. Yii::$app->getSession()->setFlash('error','删除失败');
  203. return $this->redirect(Yii::$app->request->referrer);
  204. }
  205. }
  206. $id = \Yii::$app->request->get('id');
  207. $model = Building::findOne($id);//删除样板房记录
  208. $model->posted = Building::POSTED_DELETE;
  209. $model->updated_at =time();
  210. if($model->validate()&&$model->save())
  211. {
  212. \Yii::$app->getSession()->setFlash('success','删除成功');
  213. return $this->redirect(['index']);
  214. }else{
  215. var_dump($model->getErrors());exit;
  216. \Yii::$app->getSession()->setFlash('error','删除失败');
  217. return $this->redirect(['index']);
  218. }
  219. }
  220. }