'ID', 'mall_id' => '商城ID', 'user_id' => '对应qimall_user.id', 'label_id' => '对应qimall_user_label.id', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } /** * 关联用户关系表 * @Author: lun * @DateTime: 2022/3/14 0014 10:18 * @Copyright: copyright (c) 2021 广东七件事集团 */ public function getUserLabel() { return $this->hasOne(UserLabel::class, ['id' => 'label_id']); } /** * 批量编辑标签 * @Author: lun * @DateTime: 2022/3/14 0014 11:01 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $user_label_arr */ public static function batchSetData($mall_id, $user_label_arr, $is_batch = true) { $trans = Yii::$app->db->beginTransaction(); try { $del_label_ids = [];// 减少会员数量id $inc_label_ids = [];// 增加会员数量id foreach ($user_label_arr as $user_id => $label_ids) { // 获取目前此用户的所有标签id $user_label_ids = self::find()->select('label_id')->where(['user_id' => $user_id])->asArray()->column(); // 判断是否单个用户操作,并且是清空标签 if ($is_batch == false && empty($label_ids)) { // 更新标签用户数量 if ($user_label_ids) UserLabel::updateAllCounters(['user_num' => -1], ['id' => $user_label_ids]); continue; } // json解析 $label_ids = Json::decode($label_ids, true); // 获取标签差集,获取成功则减少标签用户数量 if ($is_batch == false) { foreach ($user_label_ids as $lid) { if (!in_array($lid, $label_ids)) { $del_label_ids[$lid] = empty($del_label_ids[$lid]) ? 1 : $del_label_ids[$lid] + 1; } } } // 获取标签差集,获取成功则增加标签用户数量 foreach ($label_ids as $lid) { if (!in_array($lid, $user_label_ids)) { $inc_label_ids[$lid] = empty($inc_label_ids[$lid]) ? 1 : $inc_label_ids[$lid] + 1; } } // 删除此用户所有标签 !$is_batch && self::deleteAll(['user_id' => $user_id]); // 循环插入标签 foreach ($label_ids as $label_id) { $res = self::setData($mall_id, $user_id, $label_id); if ($res == false) throw new \Exception(self::getStaticError()); } } // 处理标签用户数量减少 if (!empty($del_label_ids)) { foreach ($del_label_ids as $label_id => $num) { if($label_id) { $label_info = UserLabel::findOne($label_id); $label_info->user_num = $label_info->user_num <= $num ? 0 : $label_info->user_num - $num; if (!$label_info->save()) throw new Exception('扣减标签用户数量失败:' . $label_info->getErrorMessage()); } } } // 处理标签用户数量增加 if (!empty($inc_label_ids)) { foreach ($inc_label_ids as $label_id => $num) { UserLabel::updateAllCounters(['user_num' => $num], ['id' => $label_id]); } } $trans->commit(); return true; } catch (\Exception $e) { $trans->rollBack(); self::setStaticError('行数:'.$e->getLine().',原因:'.$e->getMessage()); return false; } } /** * 保存数据 * @Author: lun * @DateTime: 2022/3/14 0014 11:01 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $user_id * @param $label_id */ public static function setData($mall_id, $user_id, $label_id) { try { $model = self::findOne(['user_id' => $user_id, 'label_id' => $label_id]); // 判断是否是插入 if (empty($model)) { $model = new self(); $model->mall_id = $mall_id; $model->user_id = $user_id; $model->label_id = $label_id; $model->loadDefaultValues(); if (!$model->save()) throw new Exception($model->getErrorMessage()); } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } }