RegionalController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\Area\backend\controllers;
  3. use addons\Area\common\models\AreaRegional;
  4. use addons\Area\common\service\CommunityService;
  5. use common\enums\StatusEnum;
  6. use Yii;
  7. class RegionalController extends BaseController
  8. {
  9. public $enableCsrfValidation = false;
  10. public function actionIndex()
  11. {
  12. if (\Yii::$app->request->isAjax) {
  13. $search = \Yii::$app->request->get();
  14. // 检查提交的参数
  15. if (!\Yii::$app->services->validate->validate($search, [
  16. [['keyword', 'address'], 'string'],
  17. [['page', 'limit', 'is_select'], 'integer'],
  18. ])) {
  19. return $this->error(\Yii::$app->services->validate->getErrorMessage());
  20. }
  21. $where = [['=', 'mall_id', $this->mall_id]];
  22. !empty($search['keyword']) && $where[] = ['or', ['like', 'id', $search['keyword']], ['like', 'regional_name', $search['keyword']]];
  23. !empty($search['address']) && $where[] = ['like', 'area', $search['address']];
  24. if (!empty($search['is_select'])) {
  25. $result = AreaRegional::lists(['where' => $where, 'page' => $search['page'], 'limit' => $search['limit'], 'order' => 'id desc']);
  26. }else {
  27. $result = AreaRegional::listPage(['where' => $where, 'page' => $search['page'], 'limit' => $search['limit'], 'order' => 'id desc']);
  28. }
  29. if (!$result) return $this->error(AreaRegional::getStaticError());
  30. return $this->success('获取大区列表成功', $result);
  31. }
  32. return $this->render('index');
  33. }
  34. public function actionSave()
  35. {
  36. try {
  37. $request = \Yii::$app->request;
  38. if ($request->isPost) {
  39. $data = $request->post();
  40. // 检查提交的参数
  41. if (!\Yii::$app->services->validate->validate($data, [
  42. [['id'], 'integer'],
  43. [['regional_name', 'province_ids', 'area'], 'string']
  44. ])) {
  45. return $this->error(\Yii::$app->services->validate->getErrorMessage());
  46. }
  47. $exist_province_ids = $this->actionExist(true); // 已存在的省份
  48. // 判断是新增还是编辑操作
  49. if (!empty($data['id'])) {
  50. $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $data['id']]);
  51. if(empty($model)) return $this->error('查询不到相关大区信息');
  52. $exist_province_ids = array_diff($exist_province_ids, explode(',', $model->province_ids));
  53. } else {
  54. $model = new AreaRegional();
  55. $model->mall_id = $this->mall_id;
  56. }
  57. // 判断省份是否已经存在
  58. if (count(array_intersect($exist_province_ids, explode(',', $data['province_ids']))) > 0) {
  59. return $this->error('请勿添加已存在的省份');
  60. }
  61. foreach ($data as $k => $value) {
  62. $model->$k = $value;
  63. }
  64. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  65. return $this->success();
  66. }else {
  67. $id = intval($request->get('id', 0));
  68. if (empty($id)) return $this->error('缺少合法的查询参数');
  69. $result = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $id]);
  70. if (empty($result)) return $this->error('未查询到相关大区信息');
  71. return $this->success('查询成功', $result);
  72. }
  73. }catch (\Exception $e) {
  74. return $this->error($e->getMessage());
  75. }
  76. }
  77. public function actionDelete()
  78. {
  79. try {
  80. $request = \Yii::$app->request;
  81. if ($request->isPost) {
  82. $id = intval($request->post('id', 0));
  83. if (empty($id)) return $this->error('缺少合法的查询参数');
  84. $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $id]);
  85. if (empty($model)) return $this->error('未查询到相关大区信息');
  86. if (!$model->delete()) throw new \Exception($model->getErrorMessage());
  87. return $this->success('删除成功');
  88. }
  89. }catch (\Exception $e) {
  90. return $this->error($e->getMessage());
  91. }
  92. }
  93. /*
  94. * 修改状态
  95. */
  96. public function actionStatus()
  97. {
  98. try {
  99. $data = \Yii::$app->request->post();
  100. if (!\Yii::$app->services->validate->validate($data, [
  101. [['id', 'status'], 'integer'],
  102. [['id', 'status'], 'required'],
  103. [['status'], 'in', 'range' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  104. ])) {
  105. return $this->error(\Yii::$app->services->validate->getErrorMessage());
  106. }
  107. $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $data['id']]);
  108. if (empty($model)) return $this->error('未查询到相关大区信息');
  109. $model->status = $data['status'];
  110. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  111. return $this->success('修改状态成功');
  112. }catch (\Exception $e) {
  113. return $this->error($e->getMessage());
  114. }
  115. }
  116. /*
  117. * 查询已经存在的省份
  118. */
  119. public function actionExist(bool $res_array = false)
  120. {
  121. $regionals = AreaRegional::lists(['where' => [['mall_id' => $this->mall_id]]], false);
  122. if (empty($regionals)) return $res_array ? [] : $this->error(AreaRegional::getStaticError());
  123. $province_ids = [];
  124. foreach ($regionals as $regional) {
  125. if (empty($regional['province_ids'])) continue;
  126. $ids = explode(',', $regional['province_ids']);
  127. $province_ids = array_merge($province_ids, $ids);
  128. }
  129. $province_ids = array_values(array_unique($province_ids));
  130. if (!$res_array) {
  131. return $this->success('获取成功', $province_ids);
  132. }else {
  133. return $province_ids;
  134. }
  135. }
  136. }