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.

235 lines
7.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\Order;
  7. use Yii;
  8. /**
  9. * OrderSearch represents the model behind the search form of ` backend\modules\shop\models\ars\Order`.
  10. */
  11. class OrderSearch extends Order
  12. {
  13. /**
  14. * @return array
  15. * 增加创建时间查询字段
  16. */
  17. public function attributes()
  18. {
  19. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function rules()
  25. {
  26. return [
  27. [['id', 'user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount', 'updated_at', 'created_at'], 'integer'],
  28. [['order_sn', 'invoice_id', 'consignee', 'phone', 'province', 'city', 'area', 'payment_sn', 'remarks', 'discount_description'], 'safe'],
  29. ['created_at_range','safe'],
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function scenarios()
  36. {
  37. // bypass scenarios() implementation in the parent class
  38. return Model::scenarios();
  39. }
  40. /**
  41. * @return array
  42. * 列格式
  43. */
  44. public function columns()
  45. {
  46. return [
  47. [
  48. 'class' => 'blobt\grid\CheckboxColumn',
  49. 'width' => '2%',
  50. 'align' => 'center'
  51. ],
  52. [
  53. 'attribute' => 'id',
  54. 'width' => '5%',
  55. ],
  56. [
  57. 'attribute' => 'user_id',
  58. 'width' => '7%',
  59. // 'value' => function ($model) {
  60. // return base64_decode($model->user->nickname);
  61. // }
  62. ],
  63. [
  64. 'attribute' => 'order_sn',
  65. 'width' => '8%',
  66. ],
  67. [
  68. 'attribute' => 'invoice_id',
  69. 'width' => '8%',
  70. ],
  71. [
  72. 'attribute' => 'status',
  73. 'width' => '5%',
  74. ],
  75. [
  76. 'attribute' => 'type',
  77. 'width' => '5%',
  78. ],
  79. [
  80. 'label' => '订单信息',
  81. 'format' => 'raw',
  82. 'value' => function ($model) {
  83. return Yii::$app->controller->renderPartial('order_info', ['model' => $model]);
  84. },
  85. ],
  86. [
  87. 'label' => '物流信息',
  88. 'format' => 'raw',
  89. 'value' => function ($model) {
  90. return Yii::$app->controller->renderPartial('shipping_info', ['model' => $model]);
  91. },
  92. ],
  93. [
  94. 'label' => '收货信息',
  95. 'format' => 'raw',
  96. 'value' => function ($model) {
  97. return Yii::$app->controller->renderPartial('table_consignee_info', ['model' => $model]);
  98. },
  99. ],
  100. [
  101. 'attribute' => 'remarks',
  102. 'width' => '8%',
  103. ],
  104. [
  105. 'attribute' => 'updated_at',
  106. 'width' => '8%',
  107. 'value' => function ($model) {
  108. return date('Y-m-d H:i:s', $model->updated_at);
  109. }
  110. ],
  111. // [
  112. // 'attribute' => 'created_at',
  113. // 'value' => function ($model) {
  114. // return date('Y-m-d H:i:s', $model->created_at);
  115. // }
  116. // ],
  117. [
  118. 'class' => 'iron\grid\ActionColumn',
  119. 'align' => 'center',
  120. 'config' => [
  121. [
  122. 'name' => 'view',
  123. 'icon' => 'list',
  124. 'title' => '详情',
  125. ],
  126. [
  127. 'name' => 'update',
  128. 'icon' => 'pencil',
  129. 'title' => '修改'
  130. ]
  131. ],
  132. ],
  133. ];
  134. }
  135. /**
  136. * @param $params
  137. * @return ActiveDataProvider
  138. * 不分页的所有数据
  139. */
  140. public function allData($params)
  141. {
  142. $query = Order::find();
  143. $dataProvider = new ActiveDataProvider([
  144. 'query' => $query,
  145. 'pagination' => false,
  146. 'sort' => false
  147. ]);
  148. $this->load($params);
  149. return $this->filter($query, $dataProvider);
  150. }
  151. /**
  152. * Creates data provider instance with search query applied
  153. *
  154. * @param array $params
  155. *
  156. * @return ActiveDataProvider
  157. */
  158. public function search($params)
  159. {
  160. $query = Order::find();
  161. // add conditions that should always apply here
  162. $dataProvider = new ActiveDataProvider([
  163. 'query' => $query,
  164. 'pagination' => [
  165. 'pageSizeLimit' => [1, 200]
  166. ],
  167. 'sort' => [
  168. 'defaultOrder' => [
  169. 'id' => SORT_DESC,
  170. ]
  171. ],
  172. ]);
  173. $this->load($params);
  174. return $this->filter($query, $dataProvider);
  175. }
  176. /**
  177. * @param $query
  178. * @param $dataProvider
  179. * @return ActiveDataProvider
  180. * 条件筛选
  181. */
  182. private function filter($query, $dataProvider){
  183. if (!$this->validate()) {
  184. // uncomment the following line if you do not want to return any records when validation fails
  185. // $query->where('0=1');
  186. return $dataProvider;
  187. }
  188. // grid filtering conditions
  189. $query->andFilterWhere([
  190. 'id' => $this->id,
  191. 'user_id' => $this->user_id,
  192. 'status' => $this->status,
  193. 'type' => $this->type,
  194. 'goods_count' => $this->goods_count,
  195. 'goods_amount' => $this->goods_amount,
  196. 'shipping_amount' => $this->shipping_amount,
  197. 'shipping_type' => $this->shipping_type,
  198. 'taking_site' => $this->taking_site,
  199. 'pay_type' => $this->pay_type,
  200. 'pay_at' => $this->pay_at,
  201. 'payment_amount' => $this->payment_amount,
  202. 'receivables' => $this->receivables,
  203. 'discount_amount' => $this->discount_amount,
  204. 'updated_at' => $this->updated_at,
  205. 'created_at' => $this->created_at,
  206. ]);
  207. $query->andFilterWhere(['like', 'order_sn', $this->order_sn])
  208. ->andFilterWhere(['like', 'invoice_id', $this->invoice_id])
  209. ->andFilterWhere(['like', 'consignee', $this->consignee])
  210. ->andFilterWhere(['like', 'phone', $this->phone])
  211. ->andFilterWhere(['like', 'province', $this->province])
  212. ->andFilterWhere(['like', 'city', $this->city])
  213. ->andFilterWhere(['like', 'area', $this->area])
  214. ->andFilterWhere(['like', 'payment_sn', $this->payment_sn])
  215. ->andFilterWhere(['like', 'remarks', $this->remarks])
  216. ->andFilterWhere(['like', 'discount_description', $this->discount_description]);
  217. if ($this->created_at_range) {
  218. $arr = explode(' ~ ', $this->created_at_range);
  219. $start = strtotime($arr[0]);
  220. $end = strtotime($arr[1]) + 3600 * 24;
  221. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  222. }
  223. return $dataProvider;
  224. }
  225. }