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.

241 lines
7.8 KiB

  1. <?php
  2. namespace backend\modules\goods\models\searchs;
  3. use yii\base\Model;
  4. use yii\data\ActiveDataProvider;
  5. use yii\helpers\ArrayHelper;
  6. use backend\modules\goods\models\ars\Goods;
  7. use yii\bootstrap4\Html;
  8. use backend\modules\goods\models\ars\Category;
  9. use backend\modules\goods\models\ars\ShopCategory;
  10. /**
  11. * GoodsSearch represents the model behind the search form of `backend\modules\goods\models\ars\Goods`.
  12. */
  13. class GoodsSearch extends Goods
  14. {
  15. /**
  16. * @return array
  17. * 增加创建时间查询字段
  18. */
  19. public function attributes()
  20. {
  21. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['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'],
  30. [['name', 'sn', 'code', 'unit', 'brief', 'description'], 'safe'],
  31. ['created_at_range','safe'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function scenarios()
  38. {
  39. // bypass scenarios() implementation in the parent class
  40. return Model::scenarios();
  41. }
  42. /**
  43. * @return array
  44. * 列格式
  45. */
  46. public function columns()
  47. {
  48. return [
  49. [
  50. 'class' => 'blobt\grid\CheckboxColumn',
  51. 'width' => '2%',
  52. 'align' => 'center'
  53. ],
  54. 'id',
  55. ['attribute' => 'image',
  56. 'contentOptions' => [
  57. 'align' => 'center',
  58. ],
  59. 'width'=>'10%',
  60. 'format' => 'raw',
  61. 'value' => function ($model) {
  62. return $model->image ?
  63. Html::img(['/'.$model->imageFile->path], ['style' => 'width:80px'])
  64. : '<div class="table_not_setting">未设置</div>';
  65. }
  66. ],
  67. 'name',
  68. [
  69. 'attribute' => 'cat_id',
  70. 'width' => '10%',
  71. 'value' => function ($model) {
  72. return $model->category ? $model->category->name : '';
  73. },
  74. ],
  75. [
  76. 'attribute' => 'shop_cat_id',
  77. 'width' => '10%',
  78. 'value' => function ($model) {
  79. return $model->shopCategory ? $model->shopCategory->name : '';
  80. },
  81. ],
  82. [
  83. 'attribute' => 'stock',
  84. 'width' => '5%',
  85. 'value' => function ($model) {
  86. if ($model->stock == -1) {
  87. return '未开启';
  88. } else {
  89. return $model->stock;
  90. }
  91. },
  92. ],
  93. 'price',
  94. ['attribute' => 'is_sale',
  95. 'width' => '5%',
  96. 'value' =>
  97. function ($model) {
  98. return $model->is_sale==Goods::IS_SALE_YES ? '在售' : '不在售';
  99. },
  100. ],
  101. 'updated_at:datetime',
  102. [
  103. 'class' => 'iron\grid\ActionColumn',
  104. 'align' => 'center',
  105. 'config' => [
  106. [
  107. 'name' => 'view',
  108. 'icon' => 'list',
  109. 'title' => '详情',
  110. ],
  111. [
  112. 'name' => 'update',
  113. 'icon' => 'pencil',
  114. 'title' => '修改'
  115. ],
  116. [
  117. 'name' => 'edit-sku',
  118. 'icon' => 'hard-drive',
  119. 'title' => '商品sku'
  120. ],
  121. [
  122. 'name' => 'delete',
  123. 'icon' => 'trash',
  124. 'title' => '删除',
  125. 'contents' => '确定删除?'
  126. ]
  127. ],
  128. ],
  129. ];
  130. }
  131. /**
  132. * @param $params
  133. * @return ActiveDataProvider
  134. * 不分页的所有数据
  135. */
  136. public function allData($params)
  137. {
  138. $query = Goods::find();
  139. $dataProvider = new ActiveDataProvider([
  140. 'query' => $query,
  141. 'pagination' => false,
  142. 'sort' => false
  143. ]);
  144. $this->load($params);
  145. return $this->filter($query, $dataProvider);
  146. }
  147. /**
  148. * Creates data provider instance with search query applied
  149. *
  150. * @param array $params
  151. *
  152. * @return ActiveDataProvider
  153. */
  154. public function search($params)
  155. {
  156. $query = Goods::find()->where(['is_delete' => Goods::IS_DELETE_NO]);
  157. // add conditions that should always apply here
  158. $dataProvider = new ActiveDataProvider([
  159. 'query' => $query,
  160. 'pagination' => [
  161. 'pageSizeLimit' => [1, 200]
  162. ],
  163. 'sort' => [
  164. 'defaultOrder' => [
  165. 'id' => SORT_DESC,
  166. ]
  167. ],
  168. ]);
  169. $this->load($params);
  170. return $this->filter($query, $dataProvider);
  171. }
  172. /**
  173. * @param $query
  174. * @param $dataProvider
  175. * @return ActiveDataProvider
  176. * 条件筛选
  177. */
  178. private function filter($query, $dataProvider){
  179. $query->andFilterWhere(['is_delete' => Goods::IS_DELETE_NO]);
  180. if (!$this->validate()) {
  181. // uncomment the following line if you do not want to return any records when validation fails
  182. // $query->where('0=1');
  183. return $dataProvider;
  184. }
  185. // grid filtering conditions
  186. $query->andFilterWhere([
  187. 'id' => $this->id,
  188. 'pid' => $this->pid,
  189. 'cat_id' => $this->cat_id,
  190. 'brand_id' => $this->brand_id,
  191. 'shop_cat_id' => $this->shop_cat_id,
  192. 'supplier_id' => $this->supplier_id,
  193. 'weight' => $this->weight,
  194. 'length' => $this->length,
  195. 'width' => $this->width,
  196. 'height' => $this->height,
  197. 'diameter' => $this->diameter,
  198. 'sold_count' => $this->sold_count,
  199. 'limit_count' => $this->limit_count,
  200. 'stock' => $this->stock,
  201. 'stock_warn' => $this->stock_warn,
  202. 'market_price' => $this->market_price,
  203. 'price' => $this->price,
  204. 'image' => $this->image,
  205. 'model_id' => $this->model_id,
  206. 'is_sale' => $this->is_sale,
  207. 'sort_order' => $this->sort_order,
  208. 'bouns_points' => $this->bouns_points,
  209. 'experience_points' => $this->experience_points,
  210. 'is_delete' => $this->is_delete,
  211. 'express_template' => $this->express_template,
  212. 'created_at' => $this->created_at,
  213. 'updated_at' => $this->updated_at,
  214. ]);
  215. $query->andFilterWhere(['like', 'name', $this->name])
  216. ->andFilterWhere(['like', 'sn', $this->sn])
  217. ->andFilterWhere(['like', 'code', $this->code])
  218. ->andFilterWhere(['like', 'unit', $this->unit])
  219. ->andFilterWhere(['like', 'brief', $this->brief])
  220. ->andFilterWhere(['like', 'description', $this->description]);
  221. if ($this->created_at_range) {
  222. $arr = explode(' ~ ', $this->created_at_range);
  223. $start = strtotime($arr[0]);
  224. $end = strtotime($arr[1]) + 3600 * 24;
  225. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  226. }
  227. return $dataProvider;
  228. }
  229. }