UserLabelRelationship.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace common\models\user;
  3. use Yii;
  4. use yii\db\Exception;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%user_label_relationship}}".
  8. *
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $user_id 对应qimall_user.id
  13. * @property int $label_id 对应qimall_user_label.id
  14. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class UserLabelRelationship extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%user_label_relationship}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'user_id', 'label_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => '商城ID',
  44. 'user_id' => '对应qimall_user.id',
  45. 'label_id' => '对应qimall_user_label.id',
  46. 'status' => '状态[-1:删除;0:禁用;1启用]',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. ];
  50. }
  51. /**
  52. * 关联用户关系表
  53. * @Author: lun
  54. * @DateTime: 2022/3/14 0014 10:18
  55. * @Copyright: copyright (c) 2021 广东七件事集团
  56. */
  57. public function getUserLabel()
  58. {
  59. return $this->hasOne(UserLabel::class, ['id' => 'label_id']);
  60. }
  61. /**
  62. * 批量编辑标签
  63. * @Author: lun
  64. * @DateTime: 2022/3/14 0014 11:01
  65. * @Copyright: copyright (c) 2021 广东七件事集团
  66. * @param $mall_id
  67. * @param $user_label_arr
  68. */
  69. public static function batchSetData($mall_id, $user_label_arr, $is_batch = true)
  70. {
  71. $trans = Yii::$app->db->beginTransaction();
  72. try {
  73. $del_label_ids = [];// 减少会员数量id
  74. $inc_label_ids = [];// 增加会员数量id
  75. foreach ($user_label_arr as $user_id => $label_ids) {
  76. // 获取目前此用户的所有标签id
  77. $user_label_ids = self::find()->select('label_id')->where(['user_id' => $user_id])->asArray()->column();
  78. // 判断是否单个用户操作,并且是清空标签
  79. if ($is_batch == false && empty($label_ids)) {
  80. // 更新标签用户数量
  81. if ($user_label_ids) UserLabel::updateAllCounters(['user_num' => -1], ['id' => $user_label_ids]);
  82. continue;
  83. }
  84. // json解析
  85. $label_ids = Json::decode($label_ids, true);
  86. // 获取标签差集,获取成功则减少标签用户数量
  87. if ($is_batch == false) {
  88. foreach ($user_label_ids as $lid) {
  89. if (!in_array($lid, $label_ids)) {
  90. $del_label_ids[$lid] = empty($del_label_ids[$lid]) ? 1 : $del_label_ids[$lid] + 1;
  91. }
  92. }
  93. }
  94. // 获取标签差集,获取成功则增加标签用户数量
  95. foreach ($label_ids as $lid) {
  96. if (!in_array($lid, $user_label_ids)) {
  97. $inc_label_ids[$lid] = empty($inc_label_ids[$lid]) ? 1 : $inc_label_ids[$lid] + 1;
  98. }
  99. }
  100. // 删除此用户所有标签
  101. !$is_batch && self::deleteAll(['user_id' => $user_id]);
  102. // 循环插入标签
  103. foreach ($label_ids as $label_id) {
  104. $res = self::setData($mall_id, $user_id, $label_id);
  105. if ($res == false) throw new \Exception(self::getStaticError());
  106. }
  107. }
  108. // 处理标签用户数量减少
  109. if (!empty($del_label_ids)) {
  110. foreach ($del_label_ids as $label_id => $num) {
  111. if($label_id) {
  112. $label_info = UserLabel::findOne($label_id);
  113. $label_info->user_num = $label_info->user_num <= $num ? 0 : $label_info->user_num - $num;
  114. if (!$label_info->save()) throw new Exception('扣减标签用户数量失败:' . $label_info->getErrorMessage());
  115. }
  116. }
  117. }
  118. // 处理标签用户数量增加
  119. if (!empty($inc_label_ids)) {
  120. foreach ($inc_label_ids as $label_id => $num) {
  121. UserLabel::updateAllCounters(['user_num' => $num], ['id' => $label_id]);
  122. }
  123. }
  124. $trans->commit();
  125. return true;
  126. } catch (\Exception $e) {
  127. $trans->rollBack();
  128. self::setStaticError('行数:'.$e->getLine().',原因:'.$e->getMessage());
  129. return false;
  130. }
  131. }
  132. /**
  133. * 保存数据
  134. * @Author: lun
  135. * @DateTime: 2022/3/14 0014 11:01
  136. * @Copyright: copyright (c) 2021 广东七件事集团
  137. * @param $mall_id
  138. * @param $user_id
  139. * @param $label_id
  140. */
  141. public static function setData($mall_id, $user_id, $label_id)
  142. {
  143. try {
  144. $model = self::findOne(['user_id' => $user_id, 'label_id' => $label_id]);
  145. // 判断是否是插入
  146. if (empty($model)) {
  147. $model = new self();
  148. $model->mall_id = $mall_id;
  149. $model->user_id = $user_id;
  150. $model->label_id = $label_id;
  151. $model->loadDefaultValues();
  152. if (!$model->save()) throw new Exception($model->getErrorMessage());
  153. }
  154. return true;
  155. } catch (\Exception $e) {
  156. self::setStaticError($e->getMessage());
  157. return false;
  158. }
  159. }
  160. }