| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace addons\Area\backend\controllers;
- use addons\Area\common\models\AreaRegional;
- use addons\Area\common\service\CommunityService;
- use common\enums\StatusEnum;
- use Yii;
- class RegionalController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- $search = \Yii::$app->request->get();
- // 检查提交的参数
- if (!\Yii::$app->services->validate->validate($search, [
- [['keyword', 'address'], 'string'],
- [['page', 'limit', 'is_select'], 'integer'],
- ])) {
- return $this->error(\Yii::$app->services->validate->getErrorMessage());
- }
- $where = [['=', 'mall_id', $this->mall_id]];
- !empty($search['keyword']) && $where[] = ['or', ['like', 'id', $search['keyword']], ['like', 'regional_name', $search['keyword']]];
- !empty($search['address']) && $where[] = ['like', 'area', $search['address']];
- if (!empty($search['is_select'])) {
- $result = AreaRegional::lists(['where' => $where, 'page' => $search['page'], 'limit' => $search['limit'], 'order' => 'id desc']);
- }else {
- $result = AreaRegional::listPage(['where' => $where, 'page' => $search['page'], 'limit' => $search['limit'], 'order' => 'id desc']);
- }
- if (!$result) return $this->error(AreaRegional::getStaticError());
- return $this->success('获取大区列表成功', $result);
- }
- return $this->render('index');
- }
- public function actionSave()
- {
- try {
- $request = \Yii::$app->request;
- if ($request->isPost) {
- $data = $request->post();
- // 检查提交的参数
- if (!\Yii::$app->services->validate->validate($data, [
- [['id'], 'integer'],
- [['regional_name', 'province_ids', 'area'], 'string']
- ])) {
- return $this->error(\Yii::$app->services->validate->getErrorMessage());
- }
- $exist_province_ids = $this->actionExist(true); // 已存在的省份
- // 判断是新增还是编辑操作
- if (!empty($data['id'])) {
- $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $data['id']]);
- if(empty($model)) return $this->error('查询不到相关大区信息');
- $exist_province_ids = array_diff($exist_province_ids, explode(',', $model->province_ids));
- } else {
- $model = new AreaRegional();
- $model->mall_id = $this->mall_id;
- }
- // 判断省份是否已经存在
- if (count(array_intersect($exist_province_ids, explode(',', $data['province_ids']))) > 0) {
- return $this->error('请勿添加已存在的省份');
- }
- foreach ($data as $k => $value) {
- $model->$k = $value;
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $this->success();
- }else {
- $id = intval($request->get('id', 0));
- if (empty($id)) return $this->error('缺少合法的查询参数');
- $result = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $id]);
- if (empty($result)) return $this->error('未查询到相关大区信息');
- return $this->success('查询成功', $result);
- }
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionDelete()
- {
- try {
- $request = \Yii::$app->request;
- if ($request->isPost) {
- $id = intval($request->post('id', 0));
- if (empty($id)) return $this->error('缺少合法的查询参数');
- $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $id]);
- if (empty($model)) return $this->error('未查询到相关大区信息');
- if (!$model->delete()) throw new \Exception($model->getErrorMessage());
- return $this->success('删除成功');
- }
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /*
- * 修改状态
- */
- public function actionStatus()
- {
- try {
- $data = \Yii::$app->request->post();
- if (!\Yii::$app->services->validate->validate($data, [
- [['id', 'status'], 'integer'],
- [['id', 'status'], 'required'],
- [['status'], 'in', 'range' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ])) {
- return $this->error(\Yii::$app->services->validate->getErrorMessage());
- }
- $model = AreaRegional::findOne(['mall_id' => $this->mall_id, 'id' => $data['id']]);
- if (empty($model)) return $this->error('未查询到相关大区信息');
- $model->status = $data['status'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $this->success('修改状态成功');
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /*
- * 查询已经存在的省份
- */
- public function actionExist(bool $res_array = false)
- {
- $regionals = AreaRegional::lists(['where' => [['mall_id' => $this->mall_id]]], false);
- if (empty($regionals)) return $res_array ? [] : $this->error(AreaRegional::getStaticError());
- $province_ids = [];
- foreach ($regionals as $regional) {
- if (empty($regional['province_ids'])) continue;
- $ids = explode(',', $regional['province_ids']);
- $province_ids = array_merge($province_ids, $ids);
- }
- $province_ids = array_values(array_unique($province_ids));
- if (!$res_array) {
- return $this->success('获取成功', $province_ids);
- }else {
- return $province_ids;
- }
- }
- }
|