Pricetrends.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:44
  7. */
  8. namespace common\models;
  9. class Pricetrends extends Common
  10. {
  11. public function rules()
  12. {
  13. return [
  14. [['city_id','price','price_at','trend','contrast'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
  15. [['city_id','price','price_at'],'required','message'=>'{attribute}不能为空'],
  16. ['price_at','date', 'format'=>'yyyy-mm-dd','message'=>'时间格式为:1990-01-01'],
  17. ['price','number','message'=>'价格请输入数字','max'=>200000],
  18. ['trend','in','range'=>[1,2]],
  19. ['contrast','string','max'=>50],
  20. ];
  21. }
  22. public function attributeLabels()
  23. {
  24. return [
  25. 'city_id'=>'区域',
  26. 'price'=>'价格',
  27. 'price_at'=>'时间',
  28. 'trend'=>'趋势',
  29. 'contrast'=>'价格对比',
  30. ];
  31. }
  32. public function FindById($id)
  33. {
  34. return self::findOne($id);
  35. }
  36. public function Authenticator($input)
  37. {
  38. $this->load($input,'');
  39. if($this->validate()) return $this;
  40. return $this->errors;
  41. }
  42. //后台区域分组数据显示
  43. public function getList($page)
  44. {
  45. $query = self::find();
  46. $query->andWhere(['pfg_pricetrends.del'=>$this->setDel]);
  47. $query->andFilterWhere(['pfg_pricetrends.city_id'=>$this->city_id]);
  48. $query->select(['pfg_category_city.city_name','pfg_pricetrends.city_id','pfg_pricetrends.price','pfg_pricetrends.price_at','pfg_pricetrends.id','pfg_pricetrends.create_at']);
  49. $query->leftJoin('pfg_category_city','pfg_pricetrends.city_id = pfg_category_city.id');
  50. $query->groupBy('pfg_pricetrends.city_id');
  51. if(!empty($page['page']))
  52. {
  53. $query->offset = ($page['page'] - 1) * $page['limit'];
  54. $query->limit = $page['limit'];
  55. }
  56. return $query->asArray()->all();
  57. }
  58. //后台显示数据条数
  59. public function Total()
  60. {
  61. $query = self::find();
  62. $query->andWhere(['pfg_pricetrends.del'=>$this->setDel]);
  63. $query->andFilterWhere(['pfg_pricetrends.city_id'=>$this->city_id]);
  64. $query->leftJoin('pfg_category_city','pfg_pricetrends.city_id = pfg_category_city.id');
  65. $query->groupBy('pfg_pricetrends.city_id');
  66. return $query->count();
  67. }
  68. //单个区域数据显示
  69. public function getCityList($page)
  70. {
  71. $query = self::find();
  72. $query->andWhere(['pfg_pricetrends.del'=>$this->setDel]);
  73. $query->andFilterWhere(['pfg_pricetrends.city_id'=>$this->city_id]);
  74. $query->select(['pfg_category_city.city_name','pfg_pricetrends.city_id','pfg_pricetrends.price','pfg_pricetrends.price_at','pfg_pricetrends.id','pfg_pricetrends.create_at','pfg_pricetrends.contrast','pfg_pricetrends.trend']);
  75. $query->leftJoin('pfg_category_city','pfg_pricetrends.city_id = pfg_category_city.id');
  76. $query->orderBy(['pfg_pricetrends.price_at'=>SORT_DESC]);
  77. if(!empty($page['page']))
  78. {
  79. $query->offset = ($page['page'] - 1) * $page['limit'];
  80. $query->limit = $page['limit'];
  81. }
  82. return $query->asArray()->all();
  83. }
  84. //单个区域数据条数
  85. public function cityTotal()
  86. {
  87. $query = self::find();
  88. $query->andWhere(['pfg_pricetrends.del'=>$this->setDel]);
  89. $query->andFilterWhere(['pfg_pricetrends.city_id'=>$this->city_id]);
  90. $query->leftJoin('pfg_category_city','pfg_pricetrends.city_id = pfg_category_city.id');
  91. $query->groupBy('pfg_pricetrends.city_id');
  92. return $query->count();
  93. }
  94. public function CityRecordPrice()
  95. {
  96. $query = self::find();
  97. $query->select(['pfg_pricetrends.price', 'UNIX_TIMESTAMP(pfg_pricetrends.price_at) as price_at', 'pfg_category_city.city_name']);
  98. $query->andWhere(['pfg_pricetrends.is_show' => 1]);
  99. $query->andWhere(['pfg_pricetrends.del' => $this->setDel]);
  100. $query->andWhere(['pfg_pricetrends.city_id' => $this->city_id]);
  101. $query->leftJoin('pfg_category_city', 'pfg_pricetrends.city_id=pfg_category_city.id');
  102. $query->orderBy(['price_at' => SORT_ASC]);
  103. return $query->asArray()->all();
  104. }
  105. }