GenerationController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/7/24
  6. * Time: 11:32
  7. */
  8. namespace console\controllers;
  9. use common\library\ZM_Geohash;
  10. use common\models\Building;
  11. use common\models\User;
  12. use yii\console\Controller;
  13. use yii\web\HttpException;
  14. class GenerationController extends Controller{
  15. public function actionSetCompany(){
  16. }
  17. /**
  18. * 生成工地信息
  19. * @return bool
  20. */
  21. public function actionSetNearby(){
  22. $datas = UserInfo::find()->where('username like "2000%"');
  23. $initLat = 24.49;
  24. $initLong = 118.18;
  25. $genhash = new ZM_Geohash();
  26. $time = time();
  27. foreach($datas->batch(100) as $userdatas){
  28. foreach($userdatas as $data){
  29. if(mt_rand(0,10)>8){ //不是所有都添加
  30. $model = $data->nearby;
  31. if(empty($model)){
  32. $model = new NearbyUser();
  33. $model->userid = $data->id;
  34. $model->create_at = $time;
  35. }
  36. $model->latitude = bcadd($initLat,bcdiv(mt_rand(-200,200),1999,6),6);
  37. $model->longitude = bcadd($initLong,bcdiv(mt_rand(-200,200),1999,6),6);
  38. $model->code = $genhash->encode($model->latitude,$model->longitude);
  39. $model->update_at = $time-mt_rand(1,3600);
  40. if(!$model->save()){
  41. echo 'not finished!';
  42. return false;
  43. }
  44. }
  45. }
  46. unset($data);
  47. }
  48. echo 'finished end.';
  49. }
  50. public function actionUser(){
  51. $this->generationUser(11);
  52. }
  53. public function actionBuilding(){
  54. $this->generationBuilding('0592',20);
  55. }
  56. public function generationUser($count){
  57. for($i=0;$i<$count;$i++){
  58. $model= new User();
  59. $model->username = 'test'.$i;
  60. $model->email =$model->username.'@qq.com';
  61. $model->setPassword('test'.$i);
  62. $model->generateAuthKey();
  63. $model->created_at = time();
  64. $model->updated_at = time();
  65. if($model->validate() && $model->save()){
  66. }else{
  67. return new HttpException(402,'Initialize account failed ');
  68. }
  69. }
  70. }
  71. public function generationBuilding($city,$count =10){
  72. $initLat = 24.49;
  73. $initLong = 118.18;
  74. $time = time();
  75. $genhash = new ZM_Geohash();
  76. for($i = 1;$i<$count;$i++){
  77. $building = new Building();
  78. $building->uid = rand(1,10);
  79. $building->status = 1;
  80. $building->created_at = $time;
  81. $building->updated_at = time();
  82. $building->latitude = bcadd($initLat,bcdiv(mt_rand(-200,200),1999,6),6);
  83. $building->longitude = bcadd($initLong,bcdiv(mt_rand(-200,200),1999,6),6);
  84. $building->hashcode = $genhash->encode($building->latitude,$building->longitude);
  85. $building->type = $this->randValue(array('1','2'));
  86. $building->opening_begin = $time;
  87. $building->opening_end = $time+(24*3600)*30;
  88. $building->contact = '电话:114';
  89. $building->stage = $this->randValue(array('竣工','拆改','水电','防水','泥瓦','木工','油漆'));
  90. $building->pattern = $this->randValue(array('半包','全包','拎包入住'));
  91. $building->budget = 10.2;
  92. $building->style = $this->randValue(array('欧式','中国风','现代简约','地中海','新古典','田园','日式'));
  93. $building->city =$city;
  94. $building->district =$city;
  95. $building->address = '地址';
  96. $building->layout = $this->randValue(array('二室一厅','一室一厅','三室一厅'));
  97. $building->acreage = $this->randValue(array('120','180','220'));
  98. if($building->validate() && $building->save()){
  99. echo $building->id;
  100. }else{
  101. var_dump( $building->getErrors());
  102. return false;
  103. }
  104. }
  105. }
  106. private function randValue($array){
  107. $rand_key = array_rand($array,1);
  108. return $array[$rand_key];
  109. }
  110. }