MchBrandsForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * MchBrandsForm.php
  4. * todo 文件描述
  5. * Created on 2025/5/13 08:47
  6. * @author: hankaige
  7. */
  8. namespace app\modules\admin\models;
  9. use app\models\Admin;
  10. use app\models\Mch;
  11. use app\models\MchBrands;
  12. use yii\base\Model;
  13. class MchBrandsForm extends Model
  14. {
  15. public $user_id;
  16. public $name;
  17. public $sort;
  18. public $status = -1;
  19. public $username;
  20. public $password;
  21. public $keywords = '';
  22. public $id;
  23. public $ids;
  24. public function rules(): array
  25. {
  26. return [
  27. [['store_id', 'user_id', 'sort', 'id', 'status'], 'integer'],
  28. [['name', 'username', 'password', 'keywords'], 'string', 'max' => 255],
  29. ['ids', 'safe']
  30. ];
  31. }
  32. public function getBrandList(): array
  33. {
  34. $query = MchBrands::find()->with(['user'])->where(['store_id' => get_store_id(), 'is_delete' => MchBrands::NOT_DELETE])->orderBy('sort ASC');
  35. if ($this->keywords) {
  36. $query->andWhere(['like', 'name', $this->keywords]);
  37. }
  38. if($this->status != -1){
  39. $query->andWhere(['status' => $this->status]);
  40. }
  41. $result = pagination_make($query, TRUE);
  42. foreach ($result['list'] as &$item) {
  43. $item['mchCount'] = Mch::find()->where(['brands_id' => $item['id'], 'is_delete' => 0])->count();
  44. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  45. }
  46. return $result;
  47. }
  48. public function getBrandItem()
  49. {
  50. $item = MchBrands::find()->with(['user'])->where(['id' => $this->id])->asArray()->one();
  51. if ($item) {
  52. $itemAdmin = Admin::find()->where(['type_id' => $item['id'], 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL])->one();
  53. $item['username'] = $itemAdmin ? $itemAdmin->username : '';
  54. $item['password'] = $itemAdmin ? $itemAdmin->password : '';
  55. }
  56. return $item ?? [];
  57. }
  58. public function brandsSave()
  59. {
  60. $isInster = FALSE;
  61. if (empty($this->id)) {
  62. $model = new MchBrands();
  63. $model->store_id = get_store_id();
  64. $isInster = TRUE;
  65. } else {
  66. $model = MchBrands::findOne($this->id);
  67. }
  68. $t = \Yii::$app->db->beginTransaction();
  69. try {
  70. if (empty($this->user_id)) {
  71. throw new \Exception('请选择品牌管理员');
  72. }
  73. if (empty($this->name)) {
  74. throw new \Exception('请设置品牌名称');
  75. }
  76. if (empty($this->username)) {
  77. throw new \Exception('请设置管理员登陆账号');
  78. }
  79. if (empty($this->password) && $isInster) {
  80. throw new \Exception('请设置管理员登陆密码');
  81. }
  82. $model->name = $this->name;
  83. $model->user_id = $this->user_id;
  84. $model->sort = $this->sort;
  85. $model->status = $this->status;
  86. if (!$model->save()) {
  87. $t->rollBack();
  88. throw new \Exception('创建品牌失败');
  89. }
  90. // 查询管理员表数据
  91. $admin = Admin::findOne(['type_id' => $model->id, 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL]);
  92. if (!$admin) {
  93. $admin = new Admin();
  94. $admin->type = Admin::ADMIN_TYPE_MCH_BRANDS;
  95. $admin->type_id = $model->id;
  96. $admin->store_id = get_store_id();
  97. }
  98. $admin->access_token = \Yii::$app->security->generateRandomString();
  99. $admin->username = $this->username;
  100. $admin->password = \Yii::$app->security->generatePasswordHash(trim($this->password));
  101. if (!$admin->save()) {
  102. $t->rollBack();
  103. throw new \Exception('创建品牌管理员账号失败');
  104. }
  105. $t->commit();
  106. return ['code' => 0, 'msg' => '操作成功'];
  107. } catch (\Exception $e) {
  108. return ['code' => 1, 'msg' => $e->getMessage()];
  109. }
  110. }
  111. public function deleteBrand()
  112. {
  113. if (!is_array($this->ids)) {
  114. return ['code' => 1, 'msg' => '请求参数错误'];
  115. }
  116. $brandsList = MchBrands::find()->where(['id' => $this->ids, 'is_delete' => MchBrands::NOT_DELETE])->all();
  117. $count = count($brandsList);
  118. $errNum = 0;
  119. foreach ($brandsList as $item) {
  120. // 先检查是否存在绑定品牌的店铺
  121. $mchNum = Mch::find()->where(['brands_id' => $item->id, 'is_delete' => 0])->count();
  122. if ($mchNum > 0) {
  123. $errNum++;
  124. continue;
  125. }
  126. $t = \Yii::$app->db->beginTransaction();
  127. $item->is_delete = MchBrands::IS_DELETE;
  128. if (!$item->save()) {
  129. $t->rollBack();
  130. $errNum++;
  131. continue;
  132. }
  133. // 删除管理员
  134. $admin = Admin::findOne(['type_id' => $item->id, 'type' => Admin::ADMIN_TYPE_MCH_BRANDS, 'is_delete' => Admin::ADMIN_NORMAL]);
  135. $admin->is_delete = Admin::ADMIN_DELETE;
  136. if (!$admin->save()) {
  137. $t->rollBack();
  138. $errNum++;
  139. continue;
  140. }
  141. $t->commit();
  142. }
  143. return ['code' => 0, 'msg' => "执行完毕,应执行{$count},执行失败:{$errNum}条"];
  144. }
  145. public function changeBrandsStatus()
  146. {
  147. $model = MchBrands::findOne(['id' => $this->id, 'is_delete' => MchBrands::NOT_DELETE]);
  148. if (!$model) {
  149. return ['code' => 1, 'msg' => '品牌分类不存在'];
  150. }
  151. $model->status = $this->status;
  152. if ($model->save()) {
  153. return ['code' => 0, 'msg' => '修改成功'];
  154. } else {
  155. return ['code' => 1, 'msg' => '修改失败'];
  156. }
  157. }
  158. public function getBrandsSelectList()
  159. {
  160. $query = MchBrands::find()->where(['store_id' => get_store_id(), 'is_delete' => MchBrands::NOT_DELETE])->select('id,name')->orderBy('sort ASC');
  161. return $query->asArray()->all();
  162. }
  163. }