jingcai-php/app/Service/CustomerService.php

41 lines
1.1 KiB
PHP
Executable File

<?php
namespace App\Service;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerFollow;
use App\Utils\ThrowException;
class CustomerService
{
/**
* @param Customer $customer
* @param Customer $follower
* @return void
*/
public function follow(Customer $customer, Customer $follower){
ThrowException::isTrue($customer->id == $follower->id, '不能关注自己');
/** @var CustomerFollow $cf */
$cf = CustomerFollow::withTrashed()->where('follower_id', $follower->id)
->where('customer_id', $customer->id)
->first();
if (!$cf) {
$cf = new CustomerFollow();
$cf->customer_id = $customer->id;
$cf->follower_id = $follower->id;
$cf->save();
} else {
if ($cf->trashed()) {
$cf->restore();
} else {
$cf->delete();
}
}
// 刷新关注人数和粉丝数
$customer->refreshFollowerFans();
$follower->refreshFollowerFans();
}
}