HouseAlbum.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:33
  7. */
  8. namespace common\models;
  9. //use yii\db\ActiveRecord;
  10. //use yii\behaviors\TimestampBehavior;
  11. class HouseAlbum extends Common
  12. {
  13. // public $setDel = 1;
  14. // public function behaviors()
  15. // {
  16. // return [
  17. // [
  18. // 'class' => TimestampBehavior::className(),
  19. // 'attributes' => [
  20. // # 创建之前
  21. // ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
  22. // # 修改之前
  23. // ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
  24. // ],
  25. // #设置默认值
  26. // 'value' => $_SERVER['REQUEST_TIME']
  27. // ]
  28. // ];
  29. // }
  30. public function rules()
  31. {
  32. return [
  33. [['hid', 'album_id'], 'required', 'message' => '{attribute}不能为空'],
  34. ['sort', 'number'],
  35. ['hid', 'number'],
  36. ['title', 'string', 'max' => 50],
  37. ['del', 'in', 'range' => [1, 2]],
  38. ['del', 'default', 'value' => 1]
  39. ];
  40. }
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'album_id' => '所属栏目',
  45. ];
  46. }
  47. public function Authenticator($input)
  48. {
  49. $this->load($input, '');
  50. if (!$this->validate()) return $this->errors;
  51. return $this;
  52. }
  53. /*
  54. * 相册分组,获取每组的个数和组名。
  55. * */
  56. public function AlbumGroup($input)
  57. {
  58. $query = self::find();
  59. $query->select(['pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid', "count('pfg_house_album.album_id') as num"]);
  60. $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
  61. $query->andWhere(['pfg_house_album.del' => $this->setDel]);
  62. $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
  63. $query->groupBy('pfg_house_album.album_id');
  64. $query->orderBy(['pfg_category_album.sort' => SORT_DESC]);
  65. return $query->asArray()->all();
  66. }
  67. /*
  68. * 相册分组,获取每组的个数和组名。(取出主图)
  69. * */
  70. public function AlbumGroupzhu($input)
  71. {
  72. $query = self::find();
  73. $query->select(['pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid','pfg_house_album.album_id']);
  74. $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
  75. $query->andWhere(['pfg_house_album.del' => $this->setDel]);
  76. $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
  77. $query->orderBy(['pfg_category_album.sort' => SORT_DESC,'pfg_house_album.zhu'=>SORT_DESC]);
  78. $data = $query->asArray()->all();
  79. $arr = [];
  80. $arr_data = [];
  81. foreach ($data as $val){
  82. if(!in_array($val['album_id'],$arr)){
  83. $arr[] = $val['album_id'];
  84. $arr_data[$val['album_id']] = $val;
  85. $arr_data[$val['album_id']]['num'] = 1;
  86. }else{
  87. $arr_data[$val['album_id']]['num'] += 1;
  88. }
  89. }
  90. return array_values($arr_data);
  91. }
  92. /**
  93. * 分组后 各取一张
  94. * 2020.10.4 lyy 修改 (直接查询暂时无法进行排序分组获取) 改为 遍历分组获取排序最高
  95. */
  96. public function Groupfind($input, $limit = null)
  97. {
  98. $query = self::find();
  99. $query->select(['pfg_category_album.name','pfg_house_album.sort', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid']);
  100. $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
  101. $query->andWhere(['pfg_house_album.del' => $this->setDel]);
  102. $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
  103. if(!empty($limit)){
  104. $query->limit = $limit;
  105. }
  106. $query->orderBy(['pfg_house_album.sort' => SORT_DESC, 'pfg_house_album.create_at' => SORT_DESC]);
  107. // $query->groupBy('pfg_house_album.album_id');
  108. $data = $query->asArray()->all();
  109. $list = [];
  110. foreach ($data as $val){
  111. if(!isset($list[$val['album_id']])){
  112. $list[$val['album_id']] = $val;
  113. $list[$val['album_id']]['album_total'] = 1;
  114. }elseif($list[$val['album_id']]['sort'] < $val['sort']){
  115. $val['album_total'] = $list[$val['album_id']]['album_total']+1;
  116. $list[$val['album_id']] = $val;
  117. }else{
  118. $list[$val['album_id']]['album_total'] += 1;
  119. }
  120. }
  121. return array_values($list);
  122. }
  123. /**
  124. * 分组后 各取一张 加总数
  125. */
  126. public function Groupfind_Total($input, $limit = null)
  127. {
  128. $query = self::find();
  129. $query->select(['COUNT(*) as album_total', 'pfg_category_album.name', 'pfg_house_album.img', 'pfg_house_album.album_id', 'pfg_house_album.state', 'pfg_house_album.hid']);
  130. $query->andWhere(['pfg_house_album.hid' => $input['hid']]);
  131. $query->andWhere(['pfg_house_album.del' => $this->setDel]);
  132. $query->leftJoin('pfg_category_album', 'pfg_house_album.album_id = pfg_category_album.id');
  133. $query->limit = $limit;
  134. $query->groupBy('pfg_house_album.album_id');
  135. $query->orderBy(['pfg_category_album.sort' => SORT_DESC, 'pfg_house_album.zhu' => SORT_DESC, 'pfg_house_album.sort' => SORT_DESC]);
  136. return $query->asArray()->all();
  137. }
  138. /*
  139. * 移动端 随机获取一张图片
  140. * */
  141. public function RandFind()
  142. {
  143. $query = self::find();
  144. $query->select(['img']);
  145. $query = $this->QueryWhere($query);
  146. return $query->orderBy('RAND()')->asArray()->one();
  147. }
  148. public function getList()
  149. {
  150. $query = self::find();
  151. $query = $this->QueryWhere($query);
  152. return $query->asArray()->all();
  153. }
  154. public function HidTotal()
  155. {
  156. $query = self::find();
  157. $query->select(["count('hid') as num", 'hid']);
  158. $query = $this->QueryWhere($query);
  159. $query->andWhere(['NOT', ['img' => '']]);
  160. return $query->groupBy('hid')->asArray()->all();
  161. }
  162. protected function QueryWhere($query)
  163. {
  164. $query->andFilterWhere(['hid' => $this->hid]);
  165. $query->andFilterWhere(['del' => $this->setDel]);
  166. $query->andFilterWhere(['album_id' => $this->album_id]);
  167. $query->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC]);
  168. return $query;
  169. }
  170. public function Total()
  171. {
  172. $query = self::find();
  173. $query = $this->QueryWhere($query);
  174. return $query->count();
  175. }
  176. }