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.

161 lines
4.7 KiB

  1. <?php
  2. namespace common\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use common\models\ars\ExpressTemplate;
  7. /**
  8. * ExpressTemplateSearch represents the model behind the search form of `common\models\ars\ExpressTemplate`.
  9. */
  10. class ExpressTemplateSearch extends ExpressTemplate
  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', 'billing_type', 'extra_weight_type', 'exemption_type', 'basic_price', 'extra_price', 'exemption_amount', 'support_taking', 'updated_at', 'created_at'], 'integer'],
  27. [['name', 'province', 'city', 'area', 'taking_site'], '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. 'name',
  53. 'province',
  54. 'city',
  55. 'area',
  56. //'billing_type',
  57. //'extra_weight_type',
  58. //'exemption_type',
  59. //'basic_price',
  60. //'extra_price',
  61. //'exemption_amount',
  62. //'support_taking',
  63. //'taking_site',
  64. //'updated_at',
  65. //'created_at',
  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 = ExpressTemplate::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 = ExpressTemplate::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. 'billing_type' => $this->billing_type,
  129. 'extra_weight_type' => $this->extra_weight_type,
  130. 'exemption_type' => $this->exemption_type,
  131. 'basic_price' => $this->basic_price,
  132. 'extra_price' => $this->extra_price,
  133. 'exemption_amount' => $this->exemption_amount,
  134. 'support_taking' => $this->support_taking,
  135. 'updated_at' => $this->updated_at,
  136. 'created_at' => $this->created_at,
  137. ]);
  138. $query->andFilterWhere(['like', 'name', $this->name])
  139. ->andFilterWhere(['like', 'province', $this->province])
  140. ->andFilterWhere(['like', 'city', $this->city])
  141. ->andFilterWhere(['like', 'area', $this->area])
  142. ->andFilterWhere(['like', 'taking_site', $this->taking_site]);
  143. if ($this->created_at_range) {
  144. $arr = explode(' ~ ', $this->created_at_range);
  145. $start = strtotime($arr[0]);
  146. $end = strtotime($arr[1]) + 3600 * 24;
  147. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  148. }
  149. return $dataProvider;
  150. }
  151. }