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.

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