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.

193 lines
6.4 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. 'template' => '{express-area-view} {express-area-update}',
  82. 'buttons' => [
  83. 'express-area-update' => function ($url, $model, $key) {
  84. $options = [
  85. 'title' => Yii::t('yii', '修改'),
  86. 'aria-label' => Yii::t('yii', '修改'),
  87. 'data-method' => 'post',
  88. 'class' => 'oi oi-pencil',
  89. ];
  90. return Html::a('', $url . '&expressTemplateId=' . $model->express_template, $options);
  91. },
  92. 'express-area-view' => function ($url, $model, $key) {
  93. $options = [
  94. 'title' => Yii::t('yii', '查看'),
  95. 'aria-label' => Yii::t('yii', '查看'),
  96. 'data-method' => 'post',
  97. 'class' => 'oi oi-list',
  98. ];
  99. return Html::a('', $url . '&expressTemplateId=' . $model->express_template, $options);
  100. },
  101. ],
  102. 'config' => [],
  103. ],
  104. ];
  105. }
  106. /**
  107. * @param $params
  108. * @return ActiveDataProvider
  109. * 不分页的所有数据
  110. */
  111. public function allData($params)
  112. {
  113. $query = ExpressArea::find();
  114. $dataProvider = new ActiveDataProvider([
  115. 'query' => $query,
  116. 'pagination' => false,
  117. 'sort' => false
  118. ]);
  119. $this->load($params);
  120. return $this->filter($query, $dataProvider);
  121. }
  122. /**
  123. * Creates data provider instance with search query applied
  124. *
  125. * @param array $params
  126. *
  127. * @return ActiveDataProvider
  128. */
  129. public function search($params, $expressTemplateId)
  130. {
  131. $query = ExpressArea::find()->where(['express_template' => $expressTemplateId]);
  132. // add conditions that should always apply here
  133. $dataProvider = new ActiveDataProvider([
  134. 'query' => $query,
  135. 'pagination' => [
  136. 'pageSizeLimit' => [1, 200]
  137. ],
  138. 'sort' => [
  139. 'defaultOrder' => [
  140. 'id' => SORT_DESC,
  141. ]
  142. ],
  143. ]);
  144. $this->load($params);
  145. return $this->filter($query, $dataProvider);
  146. }
  147. /**
  148. * @param $query
  149. * @param $dataProvider
  150. * @return ActiveDataProvider
  151. * 条件筛选
  152. */
  153. private function filter($query, $dataProvider){
  154. if (!$this->validate()) {
  155. // uncomment the following line if you do not want to return any records when validation fails
  156. // $query->where('0=1');
  157. return $dataProvider;
  158. }
  159. // grid filtering conditions
  160. $query->andFilterWhere([
  161. 'id' => $this->id,
  162. 'express_template' => $this->express_template,
  163. 'extra_price' => $this->extra_price,
  164. 'basic_price' => $this->basic_price,
  165. 'basic_count' => $this->basic_count,
  166. 'extra_count' => $this->extra_count,
  167. 'updated_at' => $this->updated_at,
  168. 'created_at' => $this->created_at,
  169. ]);
  170. $query->andFilterWhere(['like', 'province', $this->province])
  171. ->andFilterWhere(['like', 'city', $this->city])
  172. ->andFilterWhere(['like', 'area', $this->area]);
  173. if ($this->created_at_range) {
  174. $arr = explode(' ~ ', $this->created_at_range);
  175. $start = strtotime($arr[0]);
  176. $end = strtotime($arr[1]) + 3600 * 24;
  177. $query->andFilterWhere(['between', 'created_at', $start, $end]);
  178. }
  179. return $dataProvider;
  180. }
  181. }