| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * MchBrandsForm.php
- * todo 文件描述
- * Created on 2025/5/13 08:47
- * @author: hankaige
- */
- namespace app\modules\admin\models;
- use app\models\Admin;
- use app\models\Mch;
- use app\models\MchBrands;
- use yii\base\Model;
- class MchBrandsForm extends Model
- {
- public $user_id;
- public $name;
- public $sort;
- public $status = -1;
- public $username;
- public $password;
- public $keywords = '';
- public $id;
- public $ids;
- public function rules(): array
- {
- return [
- [['store_id', 'user_id', 'sort', 'id', 'status'], 'integer'],
- [['name', 'username', 'password', 'keywords'], 'string', 'max' => 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();
- }
- }
|