You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.2 KiB

  1. <?php
  2. namespace backend\modules\shop\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use backend\modules\shop\models\ars\Delivery;
  7. /**
  8. * DeliverySearch represents the model behind the search form of `backend\modules\shop\models\ars\Delivery`.
  9. */
  10. class DeliverySearch extends Delivery
  11. {
  12. /**
  13. * @return array
  14. * 增加创建时间查询字段
  15. */
  16. public function attributes()
  17. {
  18. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['id', 'order_id', 'type', 'status', 'updated_at', 'created_at'], 'integer'],
  27. [['shipping_name', 'shipping_id', 'goods', 'decription'], 'safe'],
  28. ['created_at_range','safe'],
  29. ];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function scenarios()
  35. {
  36. // bypass scenarios() implementation in the parent class
  37. return Model::scenarios();
  38. }
  39. /**
  40. * @return array
  41. * 列格式
  42. */
  43. public function columns()
  44. {
  45. return [
  46. [
  47. 'class' => 'blobt\grid\CheckboxColumn',
  48. 'width' => '2%',
  49. 'align' => 'center'
  50. ],
  51. 'id',
  52. 'order_id',
  53. 'shipping_name',
  54. 'shipping_id',
  55. 'type',
  56. // 'goods',
  57. 'status',
  58. 'decription',
  59. //'updated_at',
  60. [
  61. 'attribute' => 'created_at',
  62. 'value' => function ($model) {
  63. return date('Y-m-d H:i:s', $model->created_at);
  64. }
  65. ],
  66. [
  67. 'class' => 'iron\grid\ActionColumn',
  68. 'align' => 'center',
  69. ],
  70. ];
  71. }
  72. /**
  73. * @param $params
  74. * @return ActiveDataProvider
  75. * 不分页的所有数据
  76. */
  77. public function allData($params)
  78. {
  79. $query = Delivery::find();
  80. $dataProvider = new ActiveDataProvider([
  81. 'query' => $query,
  82. 'pagination' => false,
  83. 'sort' => false
  84. ]);
  85. $this->load($params);
  86. return $this->filter($query, $dataProvider);
  87. }
  88. /**
  89. * Creates data provider instance with search query applied
  90. *
  91. * @param array $params
  92. *
  93. * @return ActiveDataProvider
  94. */
  95. public function search($params)
  96. {
  97. $query = Delivery::find();
  98. // add conditions that should always apply here
  99. $dataProvider = new ActiveDataProvider([
  100. 'query' => $query,
  101. 'pagination' => [
  102. 'pageSizeLimit' => [1, 200]
  103. ],
  104. 'sort' => [
  105. 'defaultOrder' => [
  106. 'id' => SORT_DESC,
  107. ]
  108. ],
  109. ]);
  110. $this->load($params);
  111. return $this->filter($query, $dataProvider);
  112. }
  113. /**
  114. * @param $query
  115. * @param $dataProvider
  116. * @return ActiveDataProvider
  117. * 条件筛选
  118. */
  119. private function filter($query, $dataProvider){
  120. if (!$this->validate()) {
  121. // uncomment the following line if you do not want to return any records when validation fails
  122. // $query->where('0=1');
  123. return $dataProvider;
  124. }
  125. // grid filtering conditions
  126. $query->andFilterWhere([
  127. 'id' => $this->id,
  128. 'order_id' => $this->order_id,
  129. 'type' => $this->type,
  130. 'status' => $this->status,
  131. 'updated_at' => $this->updated_at,
  132. 'created_at' => $this->created_at,
  133. ]);
  134. $query->andFilterWhere(['like', 'shipping_name', $this->shipping_name])
  135. ->andFilterWhere(['like', 'shipping_id', $this->shipping_id])
  136. ->andFilterWhere(['like', 'goods', $this->goods])
  137. ->andFilterWhere(['like', 'decription', $this->decription]);
  138. if ($this->created_at_range) {
  139. $arr = explode(' ~ ', $this->created_at_range);
  140. $start = strtotime($arr[0]);
  141. $end = strtotime($arr[1]) + 3600 * 24;
  142. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  143. }
  144. return $dataProvider;
  145. }
  146. }