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.

197 lines
6.1 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\Goods;
  7. /**
  8. * GoodsSearch represents the model behind the search form of `common\models\ars\Goods`.
  9. */
  10. class GoodsSearch extends Goods
  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', 'pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template', 'created_at', 'updated_at'], 'integer'],
  27. [['name', 'sn', 'code', 'unit', 'brief', '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. 'pid',
  53. 'cat_id',
  54. 'brand_id',
  55. 'shop_cat_id',
  56. //'name',
  57. //'sn',
  58. //'code',
  59. //'supplier_id',
  60. //'weight',
  61. //'length',
  62. //'width',
  63. //'height',
  64. //'diameter',
  65. //'unit',
  66. //'sold_count',
  67. //'limit_count',
  68. //'stock',
  69. //'stock_warn',
  70. //'market_price',
  71. //'price',
  72. //'brief',
  73. //'description',
  74. //'image',
  75. //'model_id',
  76. //'is_sale',
  77. //'sort_order',
  78. //'bouns_points',
  79. //'experience_points',
  80. //'is_delete',
  81. //'express_template',
  82. //'created_at',
  83. //'updated_at',
  84. [
  85. 'class' => 'iron\grid\ActionColumn',
  86. 'align' => 'center',
  87. ],
  88. ];
  89. }
  90. /**
  91. * @param $params
  92. * @return ActiveDataProvider
  93. * 不分页的所有数据
  94. */
  95. public function allData($params)
  96. {
  97. $query = Goods::find();
  98. $dataProvider = new ActiveDataProvider([
  99. 'query' => $query,
  100. 'pagination' => false,
  101. 'sort' => false
  102. ]);
  103. $this->load($params);
  104. return $this->filter($query, $dataProvider);
  105. }
  106. /**
  107. * Creates data provider instance with search query applied
  108. *
  109. * @param array $params
  110. *
  111. * @return ActiveDataProvider
  112. */
  113. public function search($params)
  114. {
  115. $query = Goods::find();
  116. // add conditions that should always apply here
  117. $dataProvider = new ActiveDataProvider([
  118. 'query' => $query,
  119. 'pagination' => [
  120. 'pageSizeLimit' => [1, 200]
  121. ],
  122. 'sort' => [
  123. 'defaultOrder' => [
  124. 'id' => SORT_DESC,
  125. ]
  126. ],
  127. ]);
  128. $this->load($params);
  129. return $this->filter($query, $dataProvider);
  130. }
  131. /**
  132. * @param $query
  133. * @param $dataProvider
  134. * @return ActiveDataProvider
  135. * 条件筛选
  136. */
  137. private function filter($query, $dataProvider){
  138. if (!$this->validate()) {
  139. // uncomment the following line if you do not want to return any records when validation fails
  140. // $query->where('0=1');
  141. return $dataProvider;
  142. }
  143. // grid filtering conditions
  144. $query->andFilterWhere([
  145. 'id' => $this->id,
  146. 'pid' => $this->pid,
  147. 'cat_id' => $this->cat_id,
  148. 'brand_id' => $this->brand_id,
  149. 'shop_cat_id' => $this->shop_cat_id,
  150. 'supplier_id' => $this->supplier_id,
  151. 'weight' => $this->weight,
  152. 'length' => $this->length,
  153. 'width' => $this->width,
  154. 'height' => $this->height,
  155. 'diameter' => $this->diameter,
  156. 'sold_count' => $this->sold_count,
  157. 'limit_count' => $this->limit_count,
  158. 'stock' => $this->stock,
  159. 'stock_warn' => $this->stock_warn,
  160. 'market_price' => $this->market_price,
  161. 'price' => $this->price,
  162. 'image' => $this->image,
  163. 'model_id' => $this->model_id,
  164. 'is_sale' => $this->is_sale,
  165. 'sort_order' => $this->sort_order,
  166. 'bouns_points' => $this->bouns_points,
  167. 'experience_points' => $this->experience_points,
  168. 'is_delete' => $this->is_delete,
  169. 'express_template' => $this->express_template,
  170. 'created_at' => $this->created_at,
  171. 'updated_at' => $this->updated_at,
  172. ]);
  173. $query->andFilterWhere(['like', 'name', $this->name])
  174. ->andFilterWhere(['like', 'sn', $this->sn])
  175. ->andFilterWhere(['like', 'code', $this->code])
  176. ->andFilterWhere(['like', 'unit', $this->unit])
  177. ->andFilterWhere(['like', 'brief', $this->brief])
  178. ->andFilterWhere(['like', 'description', $this->description]);
  179. if ($this->created_at_range) {
  180. $arr = explode(' ~ ', $this->created_at_range);
  181. $start = strtotime($arr[0]);
  182. $end = strtotime($arr[1]) + 3600 * 24;
  183. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  184. }
  185. return $dataProvider;
  186. }
  187. }