<?php
|
namespace app\api\model\plus\business;
|
use app\common\model\plus\business\Business as CommonBusiness;
|
use app\common\model\plus\user\User;
|
use app\api\model\plus\business\Saving as SavingModel;
|
class Business extends CommonBusiness
|
{
|
/**
|
* 设置默认名片
|
* @param $business_card_id
|
* @return false|int
|
* @throws \think\exception\DbException
|
*/
|
public function editDefault($business_card_id='')
|
{
|
$model=$this->get($business_card_id);
|
//(new User())->where(['user_id'=>$model['user_id']])->update(['business_card_id' => $business_card_id]);
|
$this->where(['user_id' => $model['user_id']])->update(['is_default' => 0]);
|
return $model->save(['is_default' => 1]);
|
}
|
|
/**
|
* 获取默认名片
|
* @param $user_id
|
* @return array|bool|\PDOStatement|string|\think\Model|null
|
* @throws \think\db\exception\DataNotFoundException
|
* @throws \think\db\exception\ModelNotFoundException
|
* @throws \think\exception\DbException
|
*/
|
public function getDefault($user_id){
|
return $this->order('create_time', 'desc')->where(['user_id' => $user_id, 'is_default' => 1])->with(['image', 'logoImage'])->find();
|
}
|
|
/**
|
* 获取名片数量
|
* @param $user_id
|
* @return int|string
|
* @throws \think\Exception
|
*/
|
public function getQuantity($user_id)
|
{
|
return $this->where('user_id', $user_id)->count();
|
}
|
|
/**
|
* 记录浏览
|
*/
|
public function recordBrowse($business_id, $user_id)
|
{
|
$savingModel = new SavingModel();
|
$data = [
|
'user_id' => $user_id,
|
'business_id' => $business_id,
|
'affiliation_id' => $this->where('business_id', $business_id)->value('user_id'),
|
'type' => 10
|
];
|
return $savingModel->add($data);
|
}
|
|
/**
|
* 获取名片统计
|
*/
|
public function getStatistics($user_id)
|
{
|
$savingModel = new SavingModel();
|
return [
|
'cards_count' => $this->where(['user_id' => $user_id])->count(),
|
'browse_stats' => $savingModel->getBrowseStats($user_id)
|
];
|
}
|
|
/**
|
* 获取名片列表(含统计信息)
|
*/
|
public function getListWithStats($user_id)
|
{
|
$cards = $this->with(['template'])->where(['user_id' => $user_id])->select();
|
$savingModel = new SavingModel();
|
|
foreach ($cards as &$card) {
|
$card['browse_count'] = $savingModel->where(['business_id' => $card['business_id'], 'type' => 10])->count();
|
}
|
|
return $cards;
|
}
|
}
|