| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php
- namespace addons\BusinessCard\common\models;
- use addons\BusinessCard\common\enums\CardEnums;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_business_card_user".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id 成为客户的USERID
- * @property int $parent_id 分享者ID
- * @property string $tags 标签逗号隔开
- * @property int $customer_type 客户类型
- * @property int $department_id 部门ID
- * @property int $position_id 职位ID
- * @property int $role_type 角色类型,1:Boos,2:主管,3:员工
- * @property int $is_clue 是否成为线索
- * @property string $customer_nickname 客户昵称
- * @property int $customer_sex 客户性别, 1:男,2:女
- * @property string $customer_mobile 客户手机号
- * @property string $customer_email 客户邮箱
- * @property string $customer_address 客户地址
- * @property string $customer_birthday 客户生日
- * @property string $customer_remark 客户备注
- * @property int|null $share_num 分享次数
- * @property int $status 状态[1启用 0禁用 -1删除]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新事件
- */
- class BusinessCardUser extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_business_card_user';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'parent_id', 'customer_type', 'department_id', 'position_id', 'role_type', 'is_clue', 'customer_sex', 'share_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['tags'], 'string'],
- [['customer_nickname'], 'string', 'max' => 48],
- [['customer_mobile'], 'string', 'max' => 11],
- [['customer_email', 'customer_address'], 'string', 'max' => 120],
- [['customer_birthday'], 'string', 'max' => 8],
- [['customer_remark'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'parent_id' => 'Parent ID',
- 'tags' => 'Tags',
- 'customer_type' => 'Customer Type',
- 'department_id' => 'Department ID',
- 'position_id' => 'Position ID',
- 'role_type' => 'Role Type',
- 'is_clue' => 'Is Clue',
- 'customer_nickname' => 'Customer Nickname',
- 'customer_sex' => 'Customer Sex',
- 'customer_mobile' => 'Customer Mobile',
- 'customer_email' => 'Customer Email',
- 'customer_address' => 'Customer Address',
- 'customer_birthday' => 'Customer Birthday',
- 'customer_remark' => 'Customer Remark',
- 'share_num' => 'Share Num',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');;
- }
- public function getParent()
- {
- return $this->hasOne(User::class, ['id' => 'parent_id']);
- }
- public function getCard()
- {
- return $this->hasOne(BusinessCard::class, ['user_id' => 'user_id']);
- }
- /**
- * 获取排行榜
- * @author hpei
- * @param int mall_id
- * @param string type user|clue
- * @return array [user_id=>num]
- */
- public static function rank($param) {
- $key = $param['type'] == 'user' ? RedisKeyEnum::CARD_USER_STATISTICS : RedisKeyEnum::CARD_CLUE_STATISTICS;
- $cacheKey = RedisKeyEnum::suffix($key, 'mall_id_'. $param['mall_id']);
- $redis = Yii::$app->redis;
- $rank = $redis->zrevrange($cacheKey, 0, 9, 'WITHSCORES');
- if (empty($rank)) {
- $model = self::find()
- ->select(['count(*) count', 'parent_id'])
- ->where(['mall_id' => $param['mall_id']])
- ->andWhere(['<>','status',StatusEnum::DELETE])
- ->orderBy('count desc')
- ->groupBy('parent_id')
- ->limit(10);
- if ($param['type'] == 'clue') $model->andWhere(['=', 'is_clue', 1]);
- $list = $model->asArray()->all();
- if (!empty($list)) {
- $options = [];
- foreach ($list as $val) {
- array_push($options, $val['count'], 'user_id:' . $val['parent_id']);
- }
- $redis->zadd($cacheKey, ...$options);
- }
- } else {
- for ($i = 0; $i < count($rank); $i++) {
- if ($i %2 == 0) {
- $user_id = explode(':', $rank[$i])[1];
- $list[$i] = ['user_id' => $user_id, 'count' => 0];
- } else {
- $list[$i - 1]['count'] = $rank[$i];
- }
- }
- $list = array_values($list);
- }
- return $list;
- }
- /**
- * 批量修改
- *
- * @author hpei
- * @param $params array
- * @param $data array
- * @return bool|Exception
- */
- public static function batchSave($params, $data) {
- try {
- foreach ($params as $user_id) {
- // 获取用户信息
- $user = User::find()->where(['id' => $user_id, 'mall_id' => $data['mall_id']])->asArray()->one();
- if (empty($user)) throw new Exception('商城暂无此用户!');
- // 查询当前用户的名片信息
- $model = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- // 当前用户已存在,则修改用户信息
- if (empty($model)) {
- $model = new self();
- $model->user_id = $user['id'];
- $model->tags = '';
- }
- if (!$model->load($data, '') || !$model->save()) throw new Exception('名片信息保存失败:'.$model->getErrorMessage());
- // 名片修改
- $card = BusinessCard::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($card)) {
- $card = new BusinessCard();
- $card->user_id = $user['id'];
- $card->mobile = $user['mobile'];
- $card->nickname = $user['nickname'];
- $card->avatar_url = $user['avatar_url'];
- }
- $card->department_id = $data['department_id'];
- $card->position_id = $data['position_id'];
- if (!$card->save()) throw new Exception('用户名片保存失败:'.$card->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function batchSaves($params, $data) {
- $t = Yii::$app->db->beginTransaction();
- try {
- $userList = User::lists(['where'=>[['id'=>$params['user_id']]],'index'=>'id']);
- foreach ($params['user_id'] as $user_id){
- if(empty($userList[$user_id])) continue;
- $insert_data = [
- 'mall_id' => $userList[$user_id]['mall_id'],
- 'user_id' => $user_id,
- // 'tags' => '',
- 'parent_id' => $userList[$user_id]['parent_id'],
- 'department_id' => $data['department_id'],
- 'position_id' => $data['position_id'],
- 'role_type' => $data['role_type'] ?? 3, // 默认为员工
- 'is_clue' => !empty($userList[$user_id]['parent_id']) ? 1 : 0
- ];
- // 查看当前用户是否已经存在
- $id = BusinessCardUser::find()->where(['status' => StatusEnum::ENABLED])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['mall_id' => $userList[$user_id]['mall_id']])
- ->select('id')->scalar();
- if (!empty($id)) $insert_data['id'] = $id;
- $res = BusinessCardUser::setData($insert_data);
- if(empty($res)) throw new Exception(BusinessCardUser::getStaticError());
- // card id需要修改职位信息
- $card = BusinessCard::getOne([
- ['mall_id' => $userList[$user_id]['mall_id']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['user_id' => $user_id]
- ]);
- if (!empty($card)) {
- $card->department_id = $data['department_id'];
- $card->position_id = $data['position_id'];
- if (!$card->save()) throw new Exception($card->getErrorMessage());
- }
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 保存数据
- *
- * @author hpei
- * @return bool|Exception
- */
- public static function setData($params) {
- try {
- if (empty($params['id'])) {
- $model = new self();
- } else {
- $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('数据不存在或者已删除');
- }
- if (!$model->load($params, '') || !$model->save(false)) throw new \Exception($model->getErrorMessage());
- return $model->id;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 修改自己客户,部门,小组下的客户数据
- * @author hpei
- * @return bool|Exception
- */
- public static function setCustomerData($params) {
- if (!$params['user_id'] && !$params['parent_id']) throw new \Exception('客户信息上级信息不能为空');
- $userModel = self::findOne(['user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($userModel)) throw new \Exception('数据不存在或者已删除');
- if ($userModel->parent_id != $params['parent_id']) {
- //如果父级不是智能卡片会员
- $parentModel = self::findOne(['user_id' => $params['parent_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($parentModel)) throw new \Exception('父级不存在');
- if ($parentModel->department_id != $userModel->department_id && $parentModel->position_id != $userModel->position_id) throw new \Exception('不允许修改非本部门会员');
- if ($parentModel->role_type >= $userModel->role_type) throw new \Exception('不允许修改职位比自身高的会员');
- if ($parentModel->role_type == 1 && $parentModel->department_id != $userModel->department_id) throw new \Exception('主管不允许修改非本职位会员');
- }
- try {
- $transtcion = Yii::$app->db->beginTransaction();
- if (!$userModel->load($params, '') || !$userModel->save()) {
- self::$static_error = $userModel->getErrorMessage();
- $transtcion->rollBack();
- return false;
- }
- if (isset($params['customer_type']) && !empty($params['customer_type'])) {
- $where = [['=', 'user_id', $params['user_id']], ['=', 'remark', CardEnums::getCustomerTypeMap($params['customer_type'])], ['=', 'log_type', CardEnums::CUSTOMER_TYPE_PURPOSE]];
- $count = BusinessCardFollowLog::count(['where' => $where]);
- if ($count > 0) {
- self::$static_error = '该类型已经添加';
- $transtcion->rollBack();
- return false;
- }
- $data = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'operate_id' => $params['parent_id'],
- 'log_type' => CardEnums::CUSTOMER_TYPE_PURPOSE,
- 'remark' => CardEnums::getCustomerTypeMap($params['customer_type'])
- ];
- $res = BusinessCardFollowLog::setData($data);
- if (!$res) {
- self::$static_error = BusinessCardFollowLog::getStaticError();
- $transtcion->rollBack();
- return false;
- }
- }
- $transtcion->commit();
- return true;
- } catch (\Exception $e) {
- $transtcion->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取转化
- * @author hpei
- * @param array $param
- * @return array
- */
- public static function conversion($params) {
- $model = self::find();
- self::paramPack($params, $model);
- $list = $model->asArray()->all();
- if (empty($list)) return ['count' => 0, 'follow_customer' => 0, 'deal_customer' => 0];
- //跟进中=待成交
- $data['follow_customer'] = isset($list[CardEnums::CUSTOMER_TYPE_ORDER]) ? $list[CardEnums::CUSTOMER_TYPE_ORDER]['count'] : 0;//待成交
- $data['deal_customer'] = isset($list[CardEnums::CUSTOMER_TYPE_MONEY]) ? $list[CardEnums::CUSTOMER_TYPE_MONEY]['count'] : 0;//已成交
- $data['cout'] = array_sum(array_column($list, 'count'));
- return $data;
- }
- /**
- * 获取部门用户ID
- * @author hpei1
- * @param array department_ids
- * @return array user_ids
- */
- public static function getDepartmentUid($department_ids) {
- return self::find()->where(['department_id' => $department_ids])->select(['user_id'])->column();
- }
- /**
- * 修改父级
- * @author hpei
- * @param int user_id
- * @param int parent_id
- * @return void
- */
- public static function setParent($user_id, $parent_id) {
- $card_user = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($card_user)) return;
- $card_user->parent_id = $parent_id;
- if (!$card_user->save()) Yii::error('智能名片修改推荐人' . $card_user->getErrorMessage());
- }
- }
|