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.

187 lines
5.9 KiB

  1. <?php
  2. namespace backend\modules\shop\models\searchs;
  3. use backend\modules\shop\models\ars\City;
  4. use backend\modules\shop\models\ars\Province;
  5. use yii\base\Model;
  6. use yii\data\ActiveDataProvider;
  7. use yii\helpers\ArrayHelper;
  8. use backend\modules\shop\models\ars\ExpressArea;
  9. use yii;
  10. use yii\bootstrap4\Html;
  11. /**
  12. * ExpressAreaSearch represents the model behind the search form of `backend\modules\shop\models\ars\ExpressArea`.
  13. */
  14. class ExpressAreaSearch extends ExpressArea
  15. {
  16. /**
  17. * @return array
  18. * 增加创建时间查询字段
  19. */
  20. public function attributes()
  21. {
  22. return ArrayHelper::merge(['created_at_range'], parent::attributes());
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['id', 'express_template', 'extra_price', 'basic_price', 'basic_count', 'extra_count', 'updated_at', 'created_at'], 'integer'],
  31. [['province', 'city', 'area'], 'safe'],
  32. ['created_at_range','safe'],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function scenarios()
  39. {
  40. // bypass scenarios() implementation in the parent class
  41. return Model::scenarios();
  42. }
  43. /**
  44. * @return array
  45. * 列格式
  46. */
  47. public function columns()
  48. {
  49. return [
  50. [
  51. 'class' => 'blobt\grid\CheckboxColumn',
  52. 'width' => '2%',
  53. 'align' => 'center'
  54. ],
  55. 'id',
  56. ['attribute' => 'city',
  57. 'value' => function ($model) {
  58. $expressAreas = ExpressArea::findOne($model->id);
  59. $expressAresCityIdArr = explode(',', $expressAreas->city);
  60. $cities = [];
  61. $provinces = Province::find()->cache(0)->all();
  62. foreach ($provinces as $k => $v) {
  63. $cityId = City::find()
  64. ->select(['city_id'])
  65. ->where(['province_id' => $v->province_id])
  66. ->column();
  67. if (empty(array_diff($cityId, $expressAresCityIdArr))) {
  68. $cities[] = $v->name;
  69. }else{
  70. foreach (\backend\modules\shop\models\ars\City::find()->andWhere(['in', 'city_id', array_diff($cityId, array_diff($cityId, $expressAresCityIdArr))])->all() as $city) {
  71. $cities[] = $city->name;
  72. }
  73. }
  74. }
  75. return implode(' , ', $cities);
  76. },
  77. ],
  78. [
  79. 'class' => 'iron\grid\ActionColumn',
  80. 'align' => 'center',
  81. 'config' => [
  82. [
  83. 'name' => 'express-area-view',
  84. 'icon' => 'list',
  85. 'title' => '详情',
  86. ],
  87. [
  88. 'name' => 'express-area-update',
  89. 'icon' => 'pencil',
  90. 'title' => '修改'
  91. ],
  92. [
  93. 'name' => 'express-area-delete',
  94. 'icon' => 'trash',
  95. 'title' => '删除',
  96. 'contents' => '确定删除?'
  97. ]
  98. ],
  99. ],
  100. ];
  101. }
  102. /**
  103. * @param $params
  104. * @return ActiveDataProvider
  105. * 不分页的所有数据
  106. */
  107. public function allData($params)
  108. {
  109. $query = ExpressArea::find();
  110. $dataProvider = new ActiveDataProvider([
  111. 'query' => $query,
  112. 'pagination' => false,
  113. 'sort' => false
  114. ]);
  115. $this->load($params);
  116. return $this->filter($query, $dataProvider);
  117. }
  118. /**
  119. * Creates data provider instance with search query applied
  120. *
  121. * @param array $params
  122. *
  123. * @return ActiveDataProvider
  124. */
  125. public function search($params, $expressTemplateId)
  126. {
  127. $query = ExpressArea::find()->where(['express_template' => $expressTemplateId]);
  128. // add conditions that should always apply here
  129. $dataProvider = new ActiveDataProvider([
  130. 'query' => $query,
  131. 'pagination' => [
  132. 'pageSizeLimit' => [1, 200]
  133. ],
  134. 'sort' => [
  135. 'defaultOrder' => [
  136. 'id' => SORT_DESC,
  137. ]
  138. ],
  139. ]);
  140. $this->load($params);
  141. return $this->filter($query, $dataProvider);
  142. }
  143. /**
  144. * @param $query
  145. * @param $dataProvider
  146. * @return ActiveDataProvider
  147. * 条件筛选
  148. */
  149. private function filter($query, $dataProvider){
  150. if (!$this->validate()) {
  151. // uncomment the following line if you do not want to return any records when validation fails
  152. // $query->where('0=1');
  153. return $dataProvider;
  154. }
  155. // grid filtering conditions
  156. $query->andFilterWhere([
  157. 'id' => $this->id,
  158. 'express_template' => $this->express_template,
  159. 'extra_price' => $this->extra_price,
  160. 'basic_price' => $this->basic_price,
  161. 'basic_count' => $this->basic_count,
  162. 'extra_count' => $this->extra_count,
  163. 'updated_at' => $this->updated_at,
  164. 'created_at' => $this->created_at,
  165. ]);
  166. $query->andFilterWhere(['like', 'province', $this->province])
  167. ->andFilterWhere(['like', 'city', $this->city])
  168. ->andFilterWhere(['like', 'area', $this->area]);
  169. if ($this->created_at_range) {
  170. $arr = explode(' ~ ', $this->created_at_range);
  171. $start = strtotime($arr[0]);
  172. $end = strtotime($arr[1]) + 3600 * 24;
  173. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  174. }
  175. return $dataProvider;
  176. }
  177. }