| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace addons\BusinessCard\common\models;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_business_card".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property string $mobile 电话
- * @property string $nickname 姓名
- * @property string $avatar_url 头像
- * @property int $department_id 部门ID
- * @property int $position_id 职位ID
- * @property string $phone 座机
- * @property string $email 邮箱
- * @property string $company 公司
- * @property string $address 地址
- * @property string $sign 个性签名
- * @property string $video 视频
- * @property string $video_cover_pic 视频
- * @property string|null $tags 标签,多个以逗号隔开
- * @property string|null $images 图文链接
- * @property int $status 状态[1启用 0禁用 -1删除]
- * @property int|null $share_num 分享次数
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $browse_num 浏览量(名片)
- * @property int $like_num 点赞数
- * @property string $share_pic 点赞数
- * @property string $wechat 点赞数
- * @property string $phone_key_text 点赞数
- * @property string $lat 点赞数
- * @property string $lng 点赞数
- */
- class BusinessCard extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_business_card';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'department_id', 'position_id', 'status', 'share_num', 'created_at', 'updated_at', 'browse_num', 'like_num', 'sex', 'zodiac', 'marry_status', 'province_id', 'city_id', 'mobile', 'education_id'], 'integer'],
- [['images', 'active_city', 'wechat_code'], 'safe'],
- // [['mobile'], 'string', 'max' => 11],
- [['nickname', 'phone'], 'safe'],
- [['avatar_url', 'email', 'video', 'video_cover_pic', 'share_pic', 'wechat', 'phone_key_text'], 'safe'],
- [['company'], 'string', 'max' => 48],
- [['address', 'sign'], 'string', 'max' => 120],
- [['tags'], 'safe'],
- [['lng', 'lat'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'mobile' => 'Mobile',
- 'nickname' => 'Nickname',
- 'avatar_url' => 'Avatar Url',
- 'department_id' => 'Department ID',
- 'position_id' => 'Position ID',
- 'phone' => 'Phone',
- 'email' => 'Email',
- 'company' => 'Company',
- 'address' => 'Address',
- 'sign' => 'Sign',
- 'video' => 'Video',
- 'tags' => 'Tags',
- 'images' => 'Images',
- 'status' => 'Status',
- 'share_num' => 'Share Num',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'browse_num' => 'Browse Num',
- 'like_num' => 'Like Num',
- ];
- }
- /**
- * 保存数据
- *
- * @author hpei
- * @return bool|object
- */
- public static function setData($params) {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) throw new \Exception('卡片不存在或者已删除');
- if (isset($params['user_id']) && $model->user_id != $params['user_id']) throw new \Exception('不允许修改他人卡片');
- } else {
- if (self::find()->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])->count()) {
- throw new \Exception('卡片已存在');
- }
- $model = new self();
- }
- if($params['status']!=StatusEnum::DELETE){
- $card_user = BusinessCardUser::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- if(empty($card_user)){
- $card_user = new BusinessCardUser();
- $card_user->role_type = 3; // 默认职位为员工
- }
- $partent_id = User::findOne(['id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- $card_user->mall_id = $params['mall_id'];
- $card_user->tags = !empty($params['tags']) ? $params['tags'] : null;
- $card_user->user_id = $params['user_id'];
- $card_user->parent_id = $partent_id->parent_id;
- $card_user->department_id = $params['department_id'];
- $card_user->position_id = $params['position_id'];
- if (!$card_user->save(false)) {
- throw new \Exception($card_user->getErrorMessage());
- }
- }
- $params['share_pic'] = $params['share_pic'] ?? '';
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取名片列表
- *
- * @author hpei
- * @param array $where last_id,keyword
- * @param string $order
- */
- public static function getCardList($where, $order = 'id desc') {
- $select = ['avatar_url','share_num', 'user_id', 'company', 'position_id', 'email', 'mobile', 'id', 'nickname', 'created_at'];
- $params = [
- 'where' => $where,
- 'select' => $select,
- 'order' => $order,
- 'limit' => 10,
- 'with' => ['position' => function($qurey){
- $qurey->select(['name','id']);
- }],
- ];
- $list = self::listPage($params);
- return $list;
- }
- public function getPosition()
- {
- return $this->hasOne(BusinessCardPosition::class, ['id' => 'position_id']);
- }
- public function getParent()
- {
- return $this->hasOne(User::class, ['id' => 'parent_id']);
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, avatar_url, nickname, username, mobile');
- }
- public function getCardUser()
- {
- return $this->hasOne(BusinessCardUser::class, ['user_id' => 'user_id']);
- }
- /**
- * 获取卡片详情
- *
- * @param $params
- * @param bool $info
- * @return array|mixed
- * @author hpei
- */
- public static function getCardInfo($params, $info = true) {
- $card_info = self::getOne($params, true);
- if (empty($card_info)) return [];
- if ($info) {
- if ($card_info['tags']) $card_info['tags'] = explode(',' ,$card_info['tags']);
- if ($card_info['images']) $card_info['images'] = explode(',' ,$card_info['images']);
- $position_info = BusinessCardPosition::find()
- ->where(['id' => [$card_info['position_id'], $card_info['department_id']]])
- ->andWhere(['=', 'status', StatusEnum::ENABLED])
- ->select('name')
- ->indexBy('id')
- ->column();
- $card_info['position_name'] = isset($position_info[$card_info['position_id']]) ? $position_info[$card_info['position_id']] : '';
- $card_info['department_name'] = isset($position_info[$card_info['department_id']]) ? $position_info[$card_info['department_id']] : '';
- }
- return $card_info;
- }
- }
|