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.

183 lines
5.8 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\Order;
  7. /**
  8. * OrderSearch represents the model behind the search form of ` backend\modules\shop\models\ars\Order`.
  9. */
  10. class OrderSearch extends Order
  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', 'user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount', 'updated_at', 'created_at'], 'integer'],
  27. [['order_sn', 'invoice_id', 'consignee', 'phone', 'province', 'city', 'area', 'payment_sn', 'remarks', 'discount_description'], '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. 'user_id',
  53. 'order_sn',
  54. 'invoice_id',
  55. 'status',
  56. //'type',
  57. //'goods_count',
  58. //'goods_amount',
  59. //'shipping_amount',
  60. //'shipping_type',
  61. //'consignee',
  62. //'phone',
  63. //'province',
  64. //'city',
  65. //'area',
  66. //'taking_site',
  67. //'pay_type',
  68. //'pay_at',
  69. //'payment_sn',
  70. //'payment_amount',
  71. //'receivables',
  72. //'remarks',
  73. //'discount_amount',
  74. //'discount_description',
  75. //'updated_at',
  76. //'created_at',
  77. [
  78. 'class' => 'iron\grid\ActionColumn',
  79. 'align' => 'center',
  80. ],
  81. ];
  82. }
  83. /**
  84. * @param $params
  85. * @return ActiveDataProvider
  86. * 不分页的所有数据
  87. */
  88. public function allData($params)
  89. {
  90. $query = Order::find();
  91. $dataProvider = new ActiveDataProvider([
  92. 'query' => $query,
  93. 'pagination' => false,
  94. 'sort' => false
  95. ]);
  96. $this->load($params);
  97. return $this->filter($query, $dataProvider);
  98. }
  99. /**
  100. * Creates data provider instance with search query applied
  101. *
  102. * @param array $params
  103. *
  104. * @return ActiveDataProvider
  105. */
  106. public function search($params)
  107. {
  108. $query = Order::find();
  109. // add conditions that should always apply here
  110. $dataProvider = new ActiveDataProvider([
  111. 'query' => $query,
  112. 'pagination' => [
  113. 'pageSizeLimit' => [1, 200]
  114. ],
  115. 'sort' => [
  116. 'defaultOrder' => [
  117. 'id' => SORT_DESC,
  118. ]
  119. ],
  120. ]);
  121. $this->load($params);
  122. return $this->filter($query, $dataProvider);
  123. }
  124. /**
  125. * @param $query
  126. * @param $dataProvider
  127. * @return ActiveDataProvider
  128. * 条件筛选
  129. */
  130. private function filter($query, $dataProvider){
  131. if (!$this->validate()) {
  132. // uncomment the following line if you do not want to return any records when validation fails
  133. // $query->where('0=1');
  134. return $dataProvider;
  135. }
  136. // grid filtering conditions
  137. $query->andFilterWhere([
  138. 'id' => $this->id,
  139. 'user_id' => $this->user_id,
  140. 'status' => $this->status,
  141. 'type' => $this->type,
  142. 'goods_count' => $this->goods_count,
  143. 'goods_amount' => $this->goods_amount,
  144. 'shipping_amount' => $this->shipping_amount,
  145. 'shipping_type' => $this->shipping_type,
  146. 'taking_site' => $this->taking_site,
  147. 'pay_type' => $this->pay_type,
  148. 'pay_at' => $this->pay_at,
  149. 'payment_amount' => $this->payment_amount,
  150. 'receivables' => $this->receivables,
  151. 'discount_amount' => $this->discount_amount,
  152. 'updated_at' => $this->updated_at,
  153. 'created_at' => $this->created_at,
  154. ]);
  155. $query->andFilterWhere(['like', 'order_sn', $this->order_sn])
  156. ->andFilterWhere(['like', 'invoice_id', $this->invoice_id])
  157. ->andFilterWhere(['like', 'consignee', $this->consignee])
  158. ->andFilterWhere(['like', 'phone', $this->phone])
  159. ->andFilterWhere(['like', 'province', $this->province])
  160. ->andFilterWhere(['like', 'city', $this->city])
  161. ->andFilterWhere(['like', 'area', $this->area])
  162. ->andFilterWhere(['like', 'payment_sn', $this->payment_sn])
  163. ->andFilterWhere(['like', 'remarks', $this->remarks])
  164. ->andFilterWhere(['like', 'discount_description', $this->discount_description]);
  165. if ($this->created_at_range) {
  166. $arr = explode(' ~ ', $this->created_at_range);
  167. $start = strtotime($arr[0]);
  168. $end = strtotime($arr[1]) + 3600 * 24;
  169. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  170. }
  171. return $dataProvider;
  172. }
  173. }