ExpressFeeController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace addons\TinyShop\merchant\modules\common\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use common\enums\StatusEnum;
  6. use common\helpers\ArrayHelper;
  7. use common\helpers\ResultHelper;
  8. use common\traits\MerchantCurd;
  9. use addons\TinyShop\common\models\common\ExpressFee;
  10. use addons\TinyShop\merchant\controllers\BaseController;
  11. /**
  12. * 运费模板
  13. *
  14. * Class ExpressFeeController
  15. * @package addons\TinyShop\merchant\modules\common\controllers
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class ExpressFeeController extends BaseController
  19. {
  20. use MerchantCurd;
  21. /**
  22. * @var ExpressFee
  23. */
  24. public $modelClass = ExpressFee::class;
  25. /**
  26. * 物流快递id
  27. *
  28. * @var integer
  29. */
  30. public $company_id;
  31. /**
  32. * @throws \yii\base\InvalidConfigException
  33. */
  34. public function init()
  35. {
  36. parent::init();
  37. $this->company_id = Yii::$app->request->get('company_id');
  38. }
  39. /**
  40. * 首页
  41. *
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. $data = $this->modelClass::find()
  47. ->where(['>=', 'status', StatusEnum::DISABLED])
  48. ->andWhere(['company_id' => $this->company_id])
  49. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  50. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  51. $models = $data->offset($pages->offset)
  52. ->orderBy('is_default desc, id desc')
  53. ->limit($pages->limit)
  54. ->asArray()
  55. ->all();
  56. foreach ($models as &$model) {
  57. $ids = ArrayHelper::merge(explode(',', $model['province_ids']), explode(',', $model['city_ids']));
  58. $model['region'] = ArrayHelper::itemsMerge(Yii::$app->services->provinces->findByIds($ids), 0, 'id', 'pid', 'child');
  59. }
  60. return $this->render($this->action->id, [
  61. 'models' => $models,
  62. 'pages' => $pages,
  63. 'company_id' => $this->company_id,
  64. ]);
  65. }
  66. /**
  67. * 编辑/创建
  68. *
  69. * @return mixed
  70. */
  71. public function actionEdit()
  72. {
  73. $request = Yii::$app->request;
  74. $id = $request->get('id', null);
  75. $model = $this->findModel($id);
  76. if ($model->load($request->post()) && $model->save()) {
  77. return $this->redirect(['index', 'company_id' => $this->company_id]);
  78. }
  79. // 设定禁用默认地区
  80. if (($default = Yii::$app->tinyShopService->expressFee->findDefaultFee($this->company_id)) && $default->id != $model->id) {
  81. $model->is_default = 0;
  82. }
  83. // 不可选省市区数据
  84. list ($allProvinceIds, $allCityIds, $allAreaIds) = Yii::$app->tinyShopService->expressFee->getNotChoose($this->company_id);
  85. return $this->render($this->action->id, [
  86. 'model' => $model,
  87. 'company_id' => $this->company_id,
  88. 'allProvinceIds' => $allProvinceIds,
  89. 'allCityIds' => $allCityIds,
  90. 'allAreaIds' => $allAreaIds,
  91. ]);
  92. }
  93. /**
  94. * 伪删除
  95. *
  96. * @param $id
  97. * @return mixed
  98. */
  99. public function actionDestroy($id)
  100. {
  101. if (!($model = $this->modelClass::findOne($id))) {
  102. return $this->message("找不到数据", $this->redirect(['index', 'company_id' => $this->company_id]), 'error');
  103. }
  104. $model->status = StatusEnum::DELETE;
  105. if ($model->save()) {
  106. return $this->message("删除成功", $this->redirect(['index', 'company_id' => $this->company_id]));
  107. }
  108. return $this->message("删除失败", $this->redirect(['index', 'company_id' => $this->company_id]), 'error');
  109. }
  110. /**
  111. * 批量删除
  112. *
  113. * @param $id
  114. * @return mixed
  115. */
  116. public function actionDestroyAll()
  117. {
  118. $ids = Yii::$app->request->post('ids', []);
  119. if (empty($ids)) {
  120. return ResultHelper::json(422, '请选择需要操作的记录');
  121. }
  122. $this->modelClass::updateAll(['status' => StatusEnum::DELETE],
  123. ['and', ['in', 'id', $ids], ['merchant_id' => $this->getMerchantId()]]);
  124. return ResultHelper::json(200, '批量删除成功');
  125. }
  126. /**
  127. * 返回模型
  128. *
  129. * @param $id
  130. * @return \yii\db\ActiveRecord
  131. */
  132. protected function findModel($id)
  133. {
  134. /* @var $model \yii\db\ActiveRecord */
  135. if (empty($id) || empty(($model = $this->modelClass::find()->where(['id' => $id])->andWhere(['merchant_id' => $this->getMerchantId()])->one()))) {
  136. $model = new $this->modelClass;
  137. $model->company_id = $this->company_id;
  138. return $model->loadDefaultValues();
  139. }
  140. return $model;
  141. }
  142. }
粤ICP备19079148号