| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace biz\user\classes;
- use biz\merchant\classes\MerchantAssetClass;
- use biz\merchant\services\MerchantService;
- use biz\message\classes\ChatClass;
- use biz\user\services\UserIntegralService;
- use common\components\business;
- use common\components\configDict;
- use common\components\util;
- use common\services\ImageService;
- use common\services\xhMerchantExtendService;
- use common\services\xhUserCapitalService;
- use common\services\xhUserIntegralService;
- use common\services\xhUserMergeService;
- use Yii;
- use biz\base\classes\BaseClass;
- class UserClass extends BaseClass
- {
-
- public static $baseFile = '\biz\user\models\User';
-
- //用户和收入的来源
- public static $sourceType = [
- 2 => '朋友圈',
- 1 => '门店',
- 0 => '公众号',
- 3 => '淘宝',
- ];
-
- //用户来源分类
- public static $userSource = [
- 0 => ['name' => 'official', 'title' => '公众号'],
- 1 => ['name' => 'alipay', 'title' => '支付宝'],
- 2 => ['name' => 'mini', 'title' => '小程序'],
- ];
-
- //用户来源分类ID
- public static $userSourceId = [
- 'official' => ['id' => 0, 'name' => 'official'],
- 'alipay' => ['id' => 1, 'name' => 'alipay'],
- 'mini' => ['id' => 2, 'name' => 'mini'],
- ];
-
- //组合客户基本和资产信息
- public static function groupUserBaseInfo($list)
- {
- //取客户资产
- $ids = array_column($list, 'id');
- $ids = array_unique($ids);
- $asset = UserAssetClass::getByUserIds($ids, 'userId');
-
- $merchantIdList = array_column($list, 'merchantId');
- $merchantIdList = array_filter(array_unique($merchantIdList));
- $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
-
- foreach ($list as $key => $val) {
- $userId = $val['id'];
- //访问时间
- $visitTime = date("m-d H:i", $val['visitTime']);
- if (date("m-d") == date("m-d", $val['visitTime'])) {
- $visitTime = '今天 ' . date("H:i", $val['visitTime']);
- }
- $list[$key]['visitTimeFormat'] = $visitTime;
- $list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : [];
- //客户发来新消息数
- $list[$key]['unReadNum'] = ChatClass::getUnreadNum($userId);
- $list[$key] = business::formatUserAvatar($list[$key]);
- $merchantId = $val['merchantId'];
- $list[$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
- }
- return $list;
- }
-
- //获取客户基本和资产信息 shish 2019.11.30
- public static function getUserInfo($id)
- {
- $list = self::getByIds([$id]);
- $data = self::groupUserBaseInfo($list);
- return current($data);
- }
-
- //根据手机号查询客户 shish 2019.12.1
- public static function getByMobile($mobile, $merchantId)
- {
- return self::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
- }
-
- public static function generateUser($getUserInfo, $merchantId)
- {
- //替换掉微信不支持的图片
- $date = date("Y-m-d H:i:s");
- $time = time();
- $getUserInfo['userName'] = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
- $getUserInfo['realName'] = isset($getUserInfo['realName']) && !empty($getUserInfo['realName']) ? $getUserInfo['realName'] : '';
- $getUserInfo['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知
- $getUserInfo['identity'] = 0;
- $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0;
- $getUserInfo['subscribe'] = $subscribe;
- $getUserInfo['merchantId'] = $merchantId;
- $getUserInfo['status'] = 0;
- //默认没有完整用户信息
- $isFull = isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl']) ? 1 : 0;
- $getUserInfo['isFull'] = $isFull;
- $getUserInfo['createTime'] = $date;
- $getUserInfo['avatar'] = '';
- $getUserInfo['password'] = '';
- $getUserInfo['payPassword'] = '';
- $getUserInfo['mobile'] = isset($getUserInfo['mobile']) ? $getUserInfo['mobile'] : 0;
- $getUserInfo['addTime'] = $time;
- $getUserInfo['visitTime'] = $time;
- $getUserInfo['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0;
- $getUserInfo['hasSubscribe'] = 0;
- //生成用户基础信息
- $user = self::add($getUserInfo);
- $userId = $user['id'];
- if (empty($user['userName'])) {
- self::updateById($userId, ['userName' => $userId]);
- }
-
- $assetData = ['userId' => $userId, 'merchantId' => $merchantId, 'createTime' => $date];
- //生成用户资产信息
- UserAssetClass::add($assetData);
-
- //保存头像
- if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
- if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
- $imgUrl = $getUserInfo['headImgUrl'];
- $name = ImageService::generateAvatar($merchantId, $userId, $imgUrl);
- self::updateById($userId, ['avatar' => $name]);
- }
- }
-
- //商家客户粉丝 +1
- $type = $subscribe == 1 ? 3 : 1;
- MerchantAssetClass::addUserNum($merchantId, $type);
- return $user;
- }
-
- public static function updateUser($originalUser, $getUserInfo, $merchantId)
- {
- $data = [];
- $assetData = [];
- $userId = $originalUser['id'];
- //替换掉微信不支持的图片
- $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
- if (!empty($userName)) {
- $data['userName'] = $userName;
- }
- if (isset($getUserInfo['openId']) && !empty($getUserInfo['openId'])) {
- $openId = $getUserInfo['openId'];
- $data['openId'] = $openId;
- }
- if (isset($getUserInfo['miniOpenId']) && !empty($getUserInfo['miniOpenId'])) {
- $data['miniOpenId'] = $getUserInfo['miniOpenId'];
- }
- if (isset($getUserInfo['unionId']) && !empty($getUserInfo['unionId'])) {
- $data['unionId'] = $getUserInfo['unionId'];
- }
- if (isset($getUserInfo['sex'])) {
- $data['sex'] = $getUserInfo['sex'];
- }
- if (isset($getUserInfo['subscribe'])) {
- $data['subscribe'] = $getUserInfo['subscribe'];
- }
- if (isset($getUserInfo['subscribeTime'])) {
- $data['subscribeTime'] = $getUserInfo['subscribeTime'];
- }
- $data['merchantId'] = $merchantId;
- if (isset($getUserInfo['alipayId']) && !empty($getUserInfo['alipayId'])) {
- $data['alipayId'] = $getUserInfo['alipayId'];
- }
- if (isset($getUserInfo['isFull'])) {
- $data['isFull'] = $getUserInfo['isFull'];
- }
- if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
- $data['isFull'] = 1;
- }
- /**更新头像**/
- if (isset($originalUser['avatar']) == false || empty($originalUser['avatar'])) {
- if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
- $imgUrl = $getUserInfo['headImgUrl'];
- $name = ImageService::generateAvatar($merchantId, $userId, $imgUrl);
- $data['avatar'] = $name;
- }
- }
-
- /**商家增加粉丝**/
- if (isset($originalUser['subscribe']) && $originalUser['subscribe'] == 0 && isset($getUserInfo['subscribe']) && $getUserInfo['subscribe'] == 1) {
- MerchantAssetClass::addUserNum($merchantId, 2);
- }
-
- //更新访问时间
- self::updateById($userId, $data);
- if (!empty($assetData)) {
- UserAssetClass::updateByUserId($userId, $assetData);
- }
- $user = self::getById($userId);
- return $user;
- }
-
- public static function getByMiniOpenId($miniOpenId, $merchantId)
- {
- return self::getByCondition(['merchantId' => $merchantId, 'miniOpenId' => $miniOpenId]);
- }
-
- public static function getByAlipayId($alipayId, $merchantId)
- {
- return self::getByCondition(['merchantId' => $merchantId, 'alipayId' => $alipayId]);
- }
-
- public static function getByOpenId($openId, $merchantId)
- {
- return self::getByCondition(['merchantId' => $merchantId, 'openId' => $openId]);
- }
-
- public static function getByUnionId($unionId, $merchantId)
- {
- return self::getByCondition(['merchantId' => $merchantId, 'unionId' => $unionId]);
- }
-
- /**
- * 合并用户
- */
- public static function mergeUser($delUser, $retainUser)
- {
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
-
- $delUserId = $delUser['id'];
- $retainUserId = $retainUser['id'];
-
- $userName = $retainUser['userName'];
- $merchantId = $retainUser['merchantId'];
-
- //合并记录
- $return = xhUserMergeService::add(['delUserId' => $delUserId, 'merchantId' => $merchantId, 'retainUserId' => $retainUserId, 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s")]);
- $targetId = $return['id'];
-
-
- $typeList = configDict::getConfig('capitalType');
- $capitalType = $typeList['xhMergeUser']['id'];
- $now = time();
- $date = date("Y-m-d H:i:s");
-
- //数据迁移
- $updateData = [];
- $updateKey = [
- 'mobile',
- 'password',
- 'payPassword',
- 'subscribe',
- 'hasSubscribe',
- 'subscribeTime',
- 'alipayId',
- 'openId',
- 'miniOpenId',
- 'unionId',
- ];
- foreach ($updateKey as $key) {
- if (isset($delUser[$key]) && !empty($delUser[$key])) {
- $updateData[$key] = $delUser[$key];
- }
- }
- if (!empty($updateData)) {
- $updateData['visitTime'] = time();
- self::updateById($retainUserId, $updateData);
- }
-
- //资产累加
- $addData = [];
- $delUserAsset = UserAssetClass::getByUserId($delUserId);
- $retainUserAsset = UserAssetClass::getByUserId($retainUserId);
- if (!empty($delUserAsset)) {
- //累计消费
- $delTotalExpend = isset($delUserAsset['totalExpend']) ? $delUserAsset['totalExpend'] : 0;
- if (!empty($delTotalExpend)) {
- $addData['totalExpend'] = $retainUserAsset['totalExpend'] + $delTotalExpend;
- //变化记录要增加
- }
- //累计收入
- $delTotalIncome = isset($delUserAsset['totalIncome']) ? $delUserAsset['totalIncome'] : 0;
- if (!empty($delTotalIncome)) {
- $addData['totalIncome'] = $retainUserAsset['totalIncome'] + $delTotalIncome;
- //变化记录要增加
- }
- //累计充值
- $delTotalRecharge = isset($delUserAsset['totalRecharge']) ? $delUserAsset['totalRecharge'] : 0;
- if (!empty($delTotalRecharge)) {
- $addData['totalRecharge'] = $retainUserAsset['totalRecharge'] + $delTotalRecharge;
- //变化记录要增加
- }
- //累计购买数量
- $delTotalBuyNum = isset($delUserAsset['totalBuyNum']) ? $delUserAsset['totalBuyNum'] : 0;
- if (!empty($delTotalBuyNum)) {
- $addData['totalBuyNum'] = $retainUserAsset['totalBuyNum'] + $delTotalBuyNum;
- //变化记录要增加
- }
- //余额
- $delBalance = isset($delUserAsset['balance']) ? $delUserAsset['balance'] : 0;
- if (!empty($delBalance)) {
- $addData['balance'] = $retainUserAsset['balance'] + $delBalance;
- //变化记录要增加
-
- $userCapitalData = [
- 'relateId' => (string)$targetId,
- 'balance' => $addData['balance'],
- 'totalIncome' => $retainUserAsset['totalIncome'] + $delBalance,
- 'totalExpend' => $retainUserAsset['totalExpend'],
- 'amount' => $delBalance,
- 'io' => 1,
- 'payWay' => 0,
- 'event' => '帐号合并',
- 'merchantId' => $merchantId,
- 'userId' => $retainUserId,
- 'alipayId' => '',
- 'userName' => $userName,
- 'operateId' => 0,
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- ];
- xhUserCapitalService::add($userCapitalData);//用户资金流水增加
-
-
- }
-
- //积分
- $delIntegral = isset($delUserAsset['integral']) ? $delUserAsset['integral'] : 0;
- if (!empty($delIntegral)) {
- $addData['integral'] = $retainUserAsset['integral'] + $delIntegral;
- //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
-
- $integralData = [
- 'userId' => $retainUserId,
- 'userName' => $userName,
- 'merchantId' => $merchantId,
- 'relateId' => (string)$targetId,
- 'integral' => $addData['integral'],
- 'num' => $delIntegral,
- 'io' => 1,
- 'payWay' => 0,
- 'alipayId' => '',
- 'event' => '帐号合并',
- 'operatorId' => 0,
- 'createTime' => $date,
- 'capitalType' => $capitalType,
- 'addTime' => $now,
- ];
- xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
-
- $preLevel = $retainUserAsset['member'];
- $preIntegral = $retainUserAsset['integral'];//可抵用型积分
- $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- UserIntegralService::increaseToUpgrade($preIntegral, $addData['integral'], $preLevel, $retainUserId, $merchantId, $merchantExtend);
-
- }
- //积分
- $delPoint = isset($delUserAsset['point']) ? $delUserAsset['point'] : 0;
- if (!empty($delPoint)) {
- $addData['point'] = $retainUserAsset['point'] + $delPoint;
- //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
-
- }
- if (!empty($addData)) {
- UserAssetClass::updateByUserId($retainUserId, $addData);
- }
- }
-
- //清除不需要的用户
- $clearData = ['openId' => '', 'miniOpenId' => '', 'unionId' => '', 'alipayId' => '', 'merchantId' => 0];
- self::updateById($delUserId, $clearData);
- UserAssetClass::updateByUserId($delUserId, ['merchantId' => 0]);
- //清除支付宝用户
- $clearAlipayData = ['alipayId' => '', 'alipayAccount' => '', 'merchantId' => 0];
- UserAlipayClass::updateByUserId($delUserId, $clearAlipayData);
-
- $transaction->commit();
-
- return self::getById($retainUserId);
-
- } catch (Exception $e) {
- $transaction->rollBack();
-
- Yii::info("合并userid $delUserId $retainUserId 时事务回滚,原因:" . $e->getMessage());
-
- return [];
- }
-
- }
-
- //找出相同unionId的用户
- public static function getSameUnionId($merchantId, $unionId)
- {
- return self::getAllByCondition(['merchantId' => $merchantId, 'unionId' => $unionId], 'addTime desc', '*');
- }
-
- public static function valid($info, $merchantId)
- {
- if (empty($info)) {
- util::fail('找不到客户');
- }
- if ($info['merchantId'] != $merchantId) {
- util::fail('这个客户您没有权限操作');
- }
- }
-
- //根据消费次数和有没领优惠券来判断是不是新人
- public static function newUser($userInfo)
- {
- $asset = isset($userInfo['userAsset']) ? $userInfo['userAsset'] : [];
- $totalBuyNum = isset($asset['totalBuyNum']) ? $asset['totalBuyNum'] : 0;
- if ($totalBuyNum > 0) {
- return false;
- }
- $getCouponNum = isset($asset['getCouponNum']) ? $asset['getCouponNum'] : 0;
- if ($getCouponNum) {
- return false;
- }
- return true;
- }
-
- public static function getUserByIds($ids)
- {
- $info = self::getByIds($ids, null, 'id');
- if (empty($info)) {
- return [];
- }
- $data = self::groupUserBaseInfo($info);
- return $data;
- }
-
- }
|