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.4 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', 'updated_at', 'created_at'], 'integer'],
  27. [['name'], '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. 'config' => [
  57. [
  58. 'name' => 'view',
  59. 'icon' => 'list',
  60. 'title' => '详情',
  61. ],
  62. [
  63. 'name' => 'update',
  64. 'icon' => 'pencil',
  65. 'title' => '修改'
  66. ],
  67. [
  68. 'name' => 'express-area-list',
  69. 'icon' => 'hard-drive',
  70. 'title' => '配送区域'
  71. ],
  72. [
  73. 'name' => 'delete',
  74. 'icon' => 'trash',
  75. 'title' => '删除',
  76. 'contents' => '确定删除?'
  77. ]
  78. ],
  79. ],
  80. ];
  81. }
  82. /**
  83. * @param $params
  84. * @return ActiveDataProvider
  85. * 不分页的所有数据
  86. */
  87. public function allData($params)
  88. {
  89. $query = ExpressTemplate::find();
  90. $dataProvider = new ActiveDataProvider([
  91. 'query' => $query,
  92. 'pagination' => false,
  93. 'sort' => false
  94. ]);
  95. $this->load($params);
  96. return $this->filter($query, $dataProvider);
  97. }
  98. /**
  99. * Creates data provider instance with search query applied
  100. *
  101. * @param array $params
  102. *
  103. * @return ActiveDataProvider
  104. */
  105. public function search($params)
  106. {
  107. $query = ExpressTemplate::find();
  108. // add conditions that should always apply here
  109. $dataProvider = new ActiveDataProvider([
  110. 'query' => $query,
  111. 'pagination' => [
  112. 'pageSizeLimit' => [1, 200]
  113. ],
  114. 'sort' => [
  115. 'defaultOrder' => [
  116. 'id' => SORT_DESC,
  117. ]
  118. ],
  119. ]);
  120. $this->load($params);
  121. return $this->filter($query, $dataProvider);
  122. }
  123. /**
  124. * @param $query
  125. * @param $dataProvider
  126. * @return ActiveDataProvider
  127. * 条件筛选
  128. */
  129. private function filter($query, $dataProvider){
  130. if (!$this->validate()) {
  131. // uncomment the following line if you do not want to return any records when validation fails
  132. // $query->where('0=1');
  133. return $dataProvider;
  134. }
  135. // grid filtering conditions
  136. $query->andFilterWhere([
  137. 'id' => $this->id,
  138. 'calculation_type' => $this->calculation_type,
  139. 'updated_at' => $this->updated_at,
  140. 'created_at' => $this->created_at,
  141. ]);
  142. $query->andFilterWhere(['like', 'name', $this->name]);
  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. }