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.

145 lines
4.0 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\ExpressTemplate;
  7. /**
  8. * ExpressTemplateSearch represents the model behind the search form of ` backend\modules\shop\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', 'calculation_type', 'basic_price', 'basic_count', 'extra_price', 'extra_count', 'updated_at', 'created_at'], 'integer'],
  27. [['name', 'province', 'city', 'area'], '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. [
  54. 'class' => 'iron\grid\ActionColumn',
  55. 'align' => 'center',
  56. ],
  57. ];
  58. }
  59. /**
  60. * @param $params
  61. * @return ActiveDataProvider
  62. * 不分页的所有数据
  63. */
  64. public function allData($params)
  65. {
  66. $query = ExpressTemplate::find();
  67. $dataProvider = new ActiveDataProvider([
  68. 'query' => $query,
  69. 'pagination' => false,
  70. 'sort' => false
  71. ]);
  72. $this->load($params);
  73. return $this->filter($query, $dataProvider);
  74. }
  75. /**
  76. * Creates data provider instance with search query applied
  77. *
  78. * @param array $params
  79. *
  80. * @return ActiveDataProvider
  81. */
  82. public function search($params)
  83. {
  84. $query = ExpressTemplate::find();
  85. // add conditions that should always apply here
  86. $dataProvider = new ActiveDataProvider([
  87. 'query' => $query,
  88. 'pagination' => [
  89. 'pageSizeLimit' => [1, 200]
  90. ],
  91. 'sort' => [
  92. 'defaultOrder' => [
  93. 'id' => SORT_DESC,
  94. ]
  95. ],
  96. ]);
  97. $this->load($params);
  98. return $this->filter($query, $dataProvider);
  99. }
  100. /**
  101. * @param $query
  102. * @param $dataProvider
  103. * @return ActiveDataProvider
  104. * 条件筛选
  105. */
  106. private function filter($query, $dataProvider){
  107. if (!$this->validate()) {
  108. // uncomment the following line if you do not want to return any records when validation fails
  109. // $query->where('0=1');
  110. return $dataProvider;
  111. }
  112. // grid filtering conditions
  113. $query->andFilterWhere([
  114. 'id' => $this->id,
  115. 'calculation_type' => $this->calculation_type,
  116. 'basic_price' => $this->basic_price,
  117. 'basic_count' => $this->basic_count,
  118. 'extra_price' => $this->extra_price,
  119. 'extra_count' => $this->extra_count,
  120. 'updated_at' => $this->updated_at,
  121. 'created_at' => $this->created_at,
  122. ]);
  123. $query->andFilterWhere(['like', 'name', $this->name])
  124. ->andFilterWhere(['like', 'province', $this->province])
  125. ->andFilterWhere(['like', 'city', $this->city])
  126. ->andFilterWhere(['like', 'area', $this->area]);
  127. if ($this->created_at_range) {
  128. $arr = explode(' ~ ', $this->created_at_range);
  129. $start = strtotime($arr[0]);
  130. $end = strtotime($arr[1]) + 3600 * 24;
  131. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  132. }
  133. return $dataProvider;
  134. }
  135. }