255], ['ids', 'safe'] ]; } public function getBrandList(): array { $query = MchBrands::find()->with(['user'])->where(['store_id' => get_store_id(), 'is_delete' => MchBrands::NOT_DELETE])->orderBy('sort ASC'); if ($this->keywords) { $query->andWhere(['like', 'name', $this->keywords]); } if($this->status != -1){ $query->andWhere(['status' => $this->status]); } $result = pagination_make($query, TRUE); foreach ($result['list'] as &$item) { $item['mchCount'] = Mch::find()->where(['brands_id' => $item['id'], 'is_delete' => 0])->count(); $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } return $result; } public function getBrandItem() { $item = MchBrands::find()->with(['user'])->where(['id' => $this->id])->asArray()->one(); if ($item) { $itemAdmin = Admin::find()->where(['type_id' => $item['id'], 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL])->one(); $item['username'] = $itemAdmin ? $itemAdmin->username : ''; $item['password'] = $itemAdmin ? $itemAdmin->password : ''; } return $item ?? []; } public function brandsSave() { $isInster = FALSE; if (empty($this->id)) { $model = new MchBrands(); $model->store_id = get_store_id(); $isInster = TRUE; } else { $model = MchBrands::findOne($this->id); } $t = \Yii::$app->db->beginTransaction(); try { if (empty($this->user_id)) { throw new \Exception('请选择品牌管理员'); } if (empty($this->name)) { throw new \Exception('请设置品牌名称'); } if (empty($this->username)) { throw new \Exception('请设置管理员登陆账号'); } if (empty($this->password) && $isInster) { throw new \Exception('请设置管理员登陆密码'); } $model->name = $this->name; $model->user_id = $this->user_id; $model->sort = $this->sort; $model->status = $this->status; if (!$model->save()) { $t->rollBack(); throw new \Exception('创建品牌失败'); } // 查询管理员表数据 $admin = Admin::findOne(['type_id' => $model->id, 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL]); if (!$admin) { $admin = new Admin(); $admin->type = Admin::ADMIN_TYPE_MCH_BRANDS; $admin->type_id = $model->id; $admin->store_id = get_store_id(); } $admin->access_token = \Yii::$app->security->generateRandomString(); $admin->username = $this->username; $admin->password = \Yii::$app->security->generatePasswordHash(trim($this->password)); if (!$admin->save()) { $t->rollBack(); throw new \Exception('创建品牌管理员账号失败'); } $t->commit(); return ['code' => 0, 'msg' => '操作成功']; } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } public function deleteBrand() { if (!is_array($this->ids)) { return ['code' => 1, 'msg' => '请求参数错误']; } $brandsList = MchBrands::find()->where(['id' => $this->ids, 'is_delete' => MchBrands::NOT_DELETE])->all(); $count = count($brandsList); $errNum = 0; foreach ($brandsList as $item) { // 先检查是否存在绑定品牌的店铺 $mchNum = Mch::find()->where(['brands_id' => $item->id, 'is_delete' => 0])->count(); if ($mchNum > 0) { $errNum++; continue; } $t = \Yii::$app->db->beginTransaction(); $item->is_delete = MchBrands::IS_DELETE; if (!$item->save()) { $t->rollBack(); $errNum++; continue; } // 删除管理员 $admin = Admin::findOne(['type_id' => $item->id, 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL]); $admin->is_delete = Admin::ADMIN_DELETE; if (!$admin->save()) { $t->rollBack(); $errNum++; continue; } $t->commit(); } return ['code' => 0, 'msg' => "执行完毕,应执行{$count},执行失败:{$errNum}条"]; } public function changeBrandsStatus() { $model = MchBrands::findOne(['id' => $this->id, 'is_delete' => MchBrands::NOT_DELETE]); if (!$model) { return ['code' => 1, 'msg' => '品牌分类不存在']; } $model->status = $this->status; if ($model->save()) { return ['code' => 0, 'msg' => '修改成功']; } else { return ['code' => 1, 'msg' => '修改失败']; } } public function getBrandsSelectList() { $query = MchBrands::find()->where(['store_id' => get_store_id(), 'is_delete' => MchBrands::NOT_DELETE])->select('id,name')->orderBy('sort ASC'); return $query->asArray()->all(); } }