AppointController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use common\library\ZM_Geohash;
  4. use common\models\Appointment;
  5. use common\models\Building;
  6. use common\models\Comment;
  7. use common\models\Designer;
  8. use common\models\ImageSource;
  9. use common\models\Manager;
  10. use common\models\UserCompany;
  11. use common\models\UserInfo;
  12. use yii\rest\ActiveController;
  13. use common\library\Apireturn;
  14. use yii\helpers\ArrayHelper;
  15. use yii\filters\auth\QueryParamAuth;
  16. use yii;
  17. class AppointController extends ActiveController
  18. {
  19. const DISPLAY = 15;//显示条数
  20. public $modelClass = 'common\models';
  21. public function behaviors()
  22. {
  23. return ArrayHelper::merge(parent::behaviors(), [
  24. 'authenticator' => [
  25. 'class' => QueryParamAuth::className(),
  26. 'tokenParam' => "token",//access-token修改为token
  27. 'optional' => [//不需要认证方法名 array
  28. ],
  29. ]
  30. ]);
  31. }
  32. /**
  33. * 预约列表 装修端
  34. * @return array
  35. */
  36. public function actionList()
  37. {
  38. $page = Yii::$app->request->POST('page', 1);
  39. $models = Appointment::find()->select(['{{%appointment}}.uid','pid','booking_time','state','{{%appointment}}.type'])
  40. ->joinWith(['building'=>function($query){
  41. $query->select(['id','name']);}])
  42. ->andwhere('{{%building}}.uid=:uid',[':uid'=>Yii::$app->user->id])
  43. ->with(['userinfo'=>function($query){
  44. $query->select('nickname,portrait,uid');
  45. },]);
  46. $is_member =UserCompany::findOne(['uid'=>Yii::$app->user->id])->getAttribute('member');
  47. if($is_member==UserCompany::ISMEMBER){
  48. $models->with(['user'=>function($query){
  49. $query->select('id,tel');
  50. }]);
  51. }
  52. $models = $models->orderBy('bd_appointment.created_at desc');
  53. $models=$models->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
  54. foreach ($models as $key => $each){
  55. $models[$key]['booking_time']=date('Y-m-d H:i',$models[$key]['booking_time']);
  56. if(!empty($models[$key]['userinfo']) && !empty($models[$key]['userinfo']['portrait']))
  57. $models[$key]['userinfo']['portrait'] = UserInfo::imagesUrl($models[$key]['userinfo']['portrait']);
  58. }
  59. $data['list']=$models;
  60. $data['ismember']=$is_member;
  61. return Apireturn::sent(1,'订单列表获取成功',200,$data);
  62. }
  63. /**
  64. * 预约列表 用户端
  65. * @return array
  66. */
  67. public function actionAppointlist()
  68. {
  69. $page = Yii::$app->request->POST('page', 1);
  70. $latitude=Yii::$app->request->post('latitude','');
  71. $longitude=Yii::$app->request->post('longitude','');
  72. $models = Appointment::find()->select(['{{%appointment}}.uid','pid','booking_time'])->where(array('{{%appointment}}.uid'=>Yii::$app->user->id))
  73. ->with(['userinfo'=>function($query){
  74. $query->select('nickname,portrait,uid');
  75. },'building',]);
  76. $geohash = new ZM_Geohash();
  77. $models = $models->orderBy('bd_appointment.created_at desc');
  78. $models=$models->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
  79. foreach ($models as $key => $each){
  80. $models[$key]['booking_time']=date('Y-m-d H:i',$each['booking_time']);
  81. $images = ImageSource::find()->select('pic')->where('topid=:id and status=:status', [':id' => $each['pid'], ':status' => ImageSource::STATUS_YES])->andWhere(['type' => [20,30,40,50]])->one();
  82. $models[$key]['img']=$images;
  83. if(!empty($latitude)&&!empty($longitude))
  84. {
  85. $distance = $geohash->getDistance($latitude,$longitude,$each['building']['latitude'],$each['building']['longitude']);
  86. $models[$key]['building']['distance'] = sprintf( "%.1f ",$distance/1000);
  87. }
  88. }
  89. return Apireturn::sent(1,'预约列表获取成功',200,$models);
  90. }
  91. /**
  92. * 预约单详情
  93. * @return array
  94. */
  95. public function actionAppointdetail()
  96. {
  97. $page = Yii::$app->request->POST('page', 1);
  98. $id = Yii::$app->request->post('id');
  99. if(empty($id)){
  100. return Apireturn::sent(0,'参数错误',200);
  101. }
  102. $model = Building::find()->where('id=:id',[':id'=>$id])->one();
  103. $comment = Comment::find()->where('pid=:pid',[':pid'=>$id])->all();
  104. $img =$model->allimg;
  105. $data['banner']=$img;
  106. $data['buiding']=$model;
  107. $data['comment']=$comment;
  108. return Apireturn::sent(1,'预约单详情',200,$data);
  109. }
  110. /**
  111. * 设计师删除
  112. * @return array
  113. */
  114. public function actionDeldesigner()
  115. {
  116. $id = Yii::$app->request->post('id');
  117. $deginers = Designer::findOne(['id'=>$id,'status'=>Designer::STATUS_YES,'company'=>Yii::$app->user->id]);
  118. if(!isset($deginers)){
  119. return Apireturn::sent(1,'找不到设计师',200);
  120. }
  121. $deginers->status=Designer::STATUS_NO;
  122. if($deginers->save()){
  123. return Apireturn::sent(0,'删除成功',200);
  124. }
  125. return Apireturn::sent(1,'删除失败',200);
  126. }
  127. /**
  128. * 项目经理删除
  129. * @return array
  130. */
  131. public function actionDelmanager()
  132. {
  133. $id = Yii::$app->request->post('id');
  134. $deginers = Manager::findOne(['id'=>$id,'status'=>Designer::STATUS_YES,'company'=>Yii::$app->user->id]);
  135. if(!isset($deginers)){
  136. return Apireturn::sent(1,'找不到经理',200);
  137. }
  138. $deginers->status=Manager::STATUS_NO;
  139. if($deginers->save()){
  140. return Apireturn::sent(0,'删除成功',200);
  141. }
  142. return Apireturn::sent(1,'删除失败',200);
  143. }
  144. /**
  145. * 设计师和管理人缘添加
  146. * @return array
  147. */
  148. public function actionAdddesign()
  149. {
  150. $type = Yii::$app->request->post('type');
  151. $avatar = Yii::$app->request->post('avatar');
  152. $realname = Yii::$app->request->post('realname');
  153. $intro = Yii::$app->request->post('intro');
  154. if(empty($type)||empty($avatar)||empty($realname)||empty($intro)){
  155. return Apireturn::sent(1,'参数错误',200);
  156. }
  157. //10 设计师 11:经理
  158. if($type==10){
  159. $newperson = new Designer();
  160. }
  161. else if($type==11){
  162. $newperson = new Manager();
  163. }
  164. else{
  165. return Apireturn::sent(1,'参数错误',200);
  166. }
  167. $newperson->company=Yii::$app->user->id;
  168. $newperson->realname=$realname;
  169. $newperson->introduction=$intro;
  170. $transaction = Yii::$app->db->beginTransaction();
  171. if($newperson->save()){
  172. $ava = new ImageSource;
  173. $ava->type=$type;
  174. $ava->topid=$newperson->id;
  175. $ava->pic=$avatar;
  176. $ava->thumbnail=$avatar;
  177. $ava->created_at=time();
  178. $ava->updated_at=time();
  179. if($ava->save()){
  180. $transaction->commit();
  181. return Apireturn::sent(0,'添加人员成功',200);
  182. }
  183. else{
  184. $transaction->rollBack();
  185. return Apireturn::sent(1,'添加人员失败',200);
  186. }
  187. }
  188. return Apireturn::sent(1,'添加失败11',200,$newperson->getErrors());
  189. }
  190. /**
  191. * 设计师和管理人员编辑
  192. * @return array
  193. */
  194. public function actionEdit()
  195. {
  196. $id = Yii::$app->request->post('id');
  197. $type = Yii::$app->request->post('type');
  198. $avatar = Yii::$app->request->post('avatar');
  199. $realname = Yii::$app->request->post('realname');
  200. $intro = Yii::$app->request->post('intro');
  201. if(empty($id)||empty($type)||empty($avatar)||empty($realname)||empty($intro)){
  202. return Apireturn::sent(1,'参数错误',200);
  203. }
  204. //10 设计师 11:经理
  205. if($type==10){
  206. $newperson = Designer::findOne(['id'=>$id,'status'=>Designer::STATUS_YES,'company'=>Yii::$app->user->id]);
  207. }
  208. else if($type==11){
  209. $newperson = Designer::findOne(['id'=>$id,'status'=>Designer::STATUS_YES,'company'=>Yii::$app->user->id]);
  210. }
  211. else{
  212. return Apireturn::sent(1,'参数错误',200);
  213. }
  214. if(empty($newperson)){
  215. return Apireturn::sent(1,'找不到该名人缘',200);
  216. }
  217. $newperson->company=Yii::$app->user->id;
  218. $newperson->realname=$realname;
  219. $newperson->introduction=$intro;
  220. $transaction = Yii::$app->db->beginTransaction();
  221. if($newperson->save()){
  222. $ava = ImageSource::findOne(['topid'=>$id,'status'=>ImageSource::STATUS_YES,'type'=>$type]);;
  223. $ava->topid=$id;
  224. $ava->pic=$avatar;
  225. $ava->thumbnail=$avatar;
  226. $ava->updated_at=time();
  227. if($ava->save()){
  228. $transaction->commit();
  229. return Apireturn::sent(0,'修改人员成功',200);
  230. }
  231. else{
  232. $transaction->rollBack();
  233. return Apireturn::sent(1,'修改人员失败',200);
  234. }
  235. }
  236. return Apireturn::sent(1,'修改失败',200);
  237. }
  238. }