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.

192 lines
5.3 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. [
  52. 'attribute' => 'id',
  53. 'width' => '7%',
  54. ],
  55. [
  56. 'attribute' => 'order_id',
  57. 'width' => '7%',
  58. ],
  59. [
  60. 'attribute' => 'shipping_name',
  61. 'width' => '10%',
  62. ],
  63. [
  64. 'attribute' => 'shipping_id',
  65. 'width' => '10%',
  66. ],
  67. [
  68. 'attribute' => 'type',
  69. 'width' => '8%',
  70. 'value' => function ($model) {
  71. return Delivery::dropDown('type', $model->type);
  72. }
  73. ],
  74. [
  75. 'attribute' => 'status',
  76. 'width' => '7%',
  77. ],
  78. [
  79. 'attribute' => 'decription',
  80. 'width' => '15%',
  81. ],
  82. [
  83. 'attribute' => 'created_at',
  84. 'width' => '10%',
  85. 'value' => function ($model) {
  86. return date('Y-m-d H:i:s', $model->created_at);
  87. }
  88. ],
  89. [
  90. 'class' => 'iron\grid\ActionColumn',
  91. 'align' => 'center',
  92. 'config' => [
  93. [
  94. 'name' => 'view',
  95. 'icon' => 'list',
  96. 'title' => '详情',
  97. ],
  98. [
  99. 'name' => 'update',
  100. 'icon' => 'pencil',
  101. 'title' => '修改'
  102. ],
  103. ],
  104. ],
  105. ];
  106. }
  107. /**
  108. * @param $params
  109. * @return ActiveDataProvider
  110. * 不分页的所有数据
  111. */
  112. public function allData($params)
  113. {
  114. $query = Delivery::find();
  115. $dataProvider = new ActiveDataProvider([
  116. 'query' => $query,
  117. 'pagination' => false,
  118. 'sort' => false
  119. ]);
  120. $this->load($params);
  121. return $this->filter($query, $dataProvider);
  122. }
  123. /**
  124. * Creates data provider instance with search query applied
  125. *
  126. * @param array $params
  127. *
  128. * @return ActiveDataProvider
  129. */
  130. public function search($params)
  131. {
  132. $query = Delivery::find();
  133. // add conditions that should always apply here
  134. $dataProvider = new ActiveDataProvider([
  135. 'query' => $query,
  136. 'pagination' => [
  137. 'pageSizeLimit' => [1, 200]
  138. ],
  139. 'sort' => [
  140. 'defaultOrder' => [
  141. 'id' => SORT_DESC,
  142. ]
  143. ],
  144. ]);
  145. $this->load($params);
  146. return $this->filter($query, $dataProvider);
  147. }
  148. /**
  149. * @param $query
  150. * @param $dataProvider
  151. * @return ActiveDataProvider
  152. * 条件筛选
  153. */
  154. private function filter($query, $dataProvider){
  155. if (!$this->validate()) {
  156. // uncomment the following line if you do not want to return any records when validation fails
  157. // $query->where('0=1');
  158. return $dataProvider;
  159. }
  160. // grid filtering conditions
  161. $query->andFilterWhere([
  162. 'id' => $this->id,
  163. 'order_id' => $this->order_id,
  164. 'type' => $this->type,
  165. 'status' => $this->status,
  166. 'updated_at' => $this->updated_at,
  167. 'created_at' => $this->created_at,
  168. ]);
  169. $query->andFilterWhere(['like', 'shipping_name', $this->shipping_name])
  170. ->andFilterWhere(['like', 'shipping_id', $this->shipping_id])
  171. ->andFilterWhere(['like', 'goods', $this->goods])
  172. ->andFilterWhere(['like', 'decription', $this->decription]);
  173. if ($this->created_at_range) {
  174. $arr = explode(' ~ ', $this->created_at_range);
  175. $start = strtotime($arr[0]);
  176. $end = strtotime($arr[1]) + 3600 * 24;
  177. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  178. }
  179. return $dataProvider;
  180. }
  181. }