| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace addons\DoubleCopy\common\models;
- use addons\DoubleCopy\common\logic\DoubleCopyLogic;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_double_copy_relation}}".
- *
- * @property int $id 主键
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $pid 用户上级id
- * @property int $deep 用户与上级的层级
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 修改时间
- */
- class DoubleCopyRelation extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_double_copy_relation}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'pid', 'deep'], 'required'],
- [['mall_id', 'user_id', 'pid', 'deep', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'pid' => '用户上级id',
- 'deep' => '用户与上级的层级',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 查询数据
- * @Author:lun
- * @Date:2023-11-28 18:18
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $params
- * @return bool
- */
- public static function setData($params)
- {
- try {
- if (empty($params['pid'])) return true;// 没有上级无需保存
- $check = self::find()->where(['user_id' => $params['user_id'], 'pid' => $params['pid'], 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($check) return true;// 已存在此关系链
- $model = new self();
- $model->deep = 1;
- $model->loadDefaultValues();
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- // 获取上级关系链
- $parent_relation = self::getRelationByUserId($params['pid']);
- $rows = [];
- $time = time();
- foreach ($parent_relation as $relation) {
- $rows[] = [
- 'mall_id' => $relation['mall_id'],
- 'user_id' => $params['user_id'],
- 'pid' => $relation['pid'],
- 'deep' => $relation['deep'] + 1,
- 'created_at' => $time,
- 'updated_at' => $time,
- ];
- }
- // 批量增加上级关系链
- if (!empty($rows)) {
- $field = ['mall_id','user_id','pid','deep','created_at','updated_at'];
- $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $rows)->execute();
- if ($res == false) throw new Exception("批量关系链出错");
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 根据用户id获取关系链
- * @Author:lun
- * @Date:2023-11-28 18:21
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $user_id
- * @param $sort
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getRelationByUserId($user_id, $sort = 'asc')
- {
- $select = 'mall_id,user_id,pid,deep,created_at';
- return self::find()->select($select)->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->orderBy('deep '.$sort)->asArray()->all();
- }
- /**
- * 根据上级id获取关系链
- * @Author:lun
- * @Date:2023-11-28 18:21
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $pid
- * @param $sort
- * @return array|\yii\db\ActiveRecord[]
- */
- public static function getRelationByPid($pid, $sort = 'desc')
- {
- $select = 'user_id,pid,deep,created_at';
- return self::find()->select($select)->where(['pid' => $pid, 'status' => StatusEnum::ENABLED])->orderBy('deep '.$sort)->asArray()->all();
- }
- /**
- * 获取上级关系uids
- * @Author:lun
- * @Date:2023-11-28 19:00
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $user_id
- * @param $sort
- * @return array
- */
- public static function getRelationPids($user_id, $sort = 'asc')
- {
- return self::find()->select('pid')->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->orderBy('deep '.$sort)->column();
- }
- /**
- * 递归获取上级关系链
- * @Author:lun
- * @Date:2023-11-28 18:39
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $user_id
- * @param $relation
- * @return array|mixed
- */
- public static function recursionRelation($user_id, &$relations = [])
- {
- // 获取当前用户信息
- $user = DoubleCopyUser::find()->select('mall_id,user_id,pid')->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if (!empty($user['pid'])) {
- $relations[] = $user['pid'];
- self::recursionRelation($user['pid'], $relations);
- }
- return $relations;
- }
- /**
- * 断层重置关系链
- * @Author:lun
- * @Date:2023-11-30 14:50
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $mall_id
- * @param $pid
- * @return void
- */
- public static function fractureResetRelation($mall_id, $pid)
- {
- $offset = 1;
- $limit = 1000;
- while ($list = self::find()->select('user_id')->where(['mall_id' => $mall_id, 'pid' => $pid, 'status' => StatusEnum::ENABLED])->orderBy('id asc')->offset(($offset - 1) * $limit)->limit($limit)->asArray()->all()) {
- foreach ($list as $item) {
- self::resetRelation($mall_id, $item['user_id']);
- // 修改平台补贴用户ID
- $user = DoubleCopyUser::findOne(['user_id' => $item['user_id'], 'status' => StatusEnum::ENABLED]);
- if (!empty($user)) {
- $parent_ids = self::getRelationPids($user->user_id);// 获取用户上级关系链
- $user->topid = DoubleCopyLogic::computeTopid($user->pid, $parent_ids);
- $user->save();
- }
- }
- $offset++;
- }
- }
- /**
- * 重置用户关系链
- * @Author:lun
- * @Date:2023-11-28 18:49
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $user_id
- * @return true
- */
- public static function resetRelation($mall_id, $user_id)
- {
- $relations[] = $user_id;
- self::recursionRelation($user_id, $relations);// 生成新的关系链
- // 软删除旧关系链
- self::updateAll(['status' => StatusEnum::DELETE], ['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if ($relations) {
- $rows = [];
- $time = time();
- foreach ($relations as $deep => $pid) {
- if ($pid == $user_id) continue;
- $rows[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'pid' => $pid,
- 'deep' => $deep,
- 'created_at' => $time,
- 'updated_at' => $time,
- ];
- }
- // 批量增加上级关系链
- if (!empty($rows)) {
- $field = ['mall_id','user_id','pid','deep','created_at','updated_at'];
- $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $rows)->execute();
- if ($res == false) throw new Exception("用户id={$user_id}重置关系链失败");
- }
- }
- return true;
- }
- /**
- * 重置商城所有用户二二复制关系链
- * @Author:lun
- * @Date:2023-11-29 18:10
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $mall_id
- * @return void
- */
- public static function resetMallUser($mall_id)
- {
- try {
- $offset = 1;
- $limit = 1000;
- while ($list = DoubleCopyUser::find()->select('user_id')->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->orderBy('id asc')->offset(($offset - 1) * $limit)->limit($limit)->asArray()->all()) {
- foreach ($list as $item) {
- self::resetRelation($mall_id, $item['user_id']);
- }
- $offset++;
- }
- } catch (\Exception $e) {
- Yii::error($e->getMessage());
- }
- }
- /**
- * 获取用户团队关系
- * @Author:lun
- * @Date:2023-11-29 18:48
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $user_id
- * @param $deep
- * @param $limit
- * @return array[]
- */
- public static function getTeamRelation($user_id, $deep, $limit, $is_unshift_self = true)
- {
- // 获取用户最高层级
- $top_deep = self::find()->select('deep')->where(['pid' => $user_id, 'status' => StatusEnum::ENABLED])->orderBy('deep desc')->scalar();
- $select_deep = $deep > $top_deep ? 0 : $top_deep - $deep;
- $list = self::find()->where(['pid' => $user_id, 'status' => StatusEnum::ENABLED])
- ->andWhere(['>=', 'deep', $select_deep])
- ->orderBy('deep desc')->limit($limit)
- ->asArray()->all();
- $is_unshift_self && array_unshift($list, ['user_id' => $user_id, 'pid' => 0, 'deep' => 0]);
- $user_ids = array_column($list, 'user_id');
- // 获取这些用户的上级id
- $parent_arr = User::find()->select('id,nickname,mobile,parent_id')->where(['id' => $user_ids, 'status' => StatusEnum::ENABLED])->indexBy('id')->asArray()->all();
- foreach ($list as $key => $item){
- $list[$key]['pid'] = $parent_arr[$item['user_id']]['parent_id'];
- $list[$key]['nickname'] = $parent_arr[$item['user_id']]['nickname'];
- $list[$key]['mobile'] = $parent_arr[$item['user_id']]['mobile'];
- }
- return ['list' => $list];
- }
- /**
- * 树状图组合
- * @Author:lun
- * @Date:2023-12-04 17:38
- * @Copyright:copyright(c)2023 广东七件事集团
- * @param $list
- * @param $pid
- * @return array
- */
- public static function listToTree($list, $pid = 0)
- {
- $arr = array_filter($list,function ($v) use ($pid){
- if($pid == $v['pid']){
- return true;
- }
- return false;
- });
- $arr = ArrayHelper::arraySort($arr, 'pos_num');
- $arr = array_values($arr);
- $res = array_map(function ($v) use ($list){
- $v['children'] = self::listToTree($list,$v['user_id']);
- return $v;
- },$arr);
- return $res;
- }
- }
|