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.

187 lines
5.1 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' => 'invoice_sn',
  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. ],
  100. ];
  101. }
  102. /**
  103. * @param $params
  104. * @return ActiveDataProvider
  105. * 不分页的所有数据
  106. */
  107. public function allData($params)
  108. {
  109. $query = Delivery::find();
  110. $dataProvider = new ActiveDataProvider([
  111. 'query' => $query,
  112. 'pagination' => false,
  113. 'sort' => false
  114. ]);
  115. $this->load($params);
  116. return $this->filter($query, $dataProvider);
  117. }
  118. /**
  119. * Creates data provider instance with search query applied
  120. *
  121. * @param array $params
  122. *
  123. * @return ActiveDataProvider
  124. */
  125. public function search($params)
  126. {
  127. $query = Delivery::find();
  128. // add conditions that should always apply here
  129. $dataProvider = new ActiveDataProvider([
  130. 'query' => $query,
  131. 'pagination' => [
  132. 'pageSizeLimit' => [1, 200]
  133. ],
  134. 'sort' => [
  135. 'defaultOrder' => [
  136. 'id' => SORT_DESC,
  137. ]
  138. ],
  139. ]);
  140. $this->load($params);
  141. return $this->filter($query, $dataProvider);
  142. }
  143. /**
  144. * @param $query
  145. * @param $dataProvider
  146. * @return ActiveDataProvider
  147. * 条件筛选
  148. */
  149. private function filter($query, $dataProvider){
  150. if (!$this->validate()) {
  151. // uncomment the following line if you do not want to return any records when validation fails
  152. // $query->where('0=1');
  153. return $dataProvider;
  154. }
  155. // grid filtering conditions
  156. $query->andFilterWhere([
  157. 'id' => $this->id,
  158. 'order_id' => $this->order_id,
  159. 'type' => $this->type,
  160. 'status' => $this->status,
  161. 'updated_at' => $this->updated_at,
  162. 'created_at' => $this->created_at,
  163. ]);
  164. $query->andFilterWhere(['like', 'shipping_name', $this->shipping_name])
  165. ->andFilterWhere(['like', 'shipping_id', $this->shipping_id])
  166. ->andFilterWhere(['like', 'goods', $this->goods])
  167. ->andFilterWhere(['like', 'decription', $this->decription]);
  168. if ($this->created_at_range) {
  169. $arr = explode(' ~ ', $this->created_at_range);
  170. $start = strtotime($arr[0]);
  171. $end = strtotime($arr[1]) + 3600 * 24;
  172. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  173. }
  174. return $dataProvider;
  175. }
  176. }