diff --git a/app/controller/CustomerController.php b/app/controller/CustomerController.php index aa17637..0f1e638 100644 --- a/app/controller/CustomerController.php +++ b/app/controller/CustomerController.php @@ -4,7 +4,9 @@ namespace app\controller; use app\model\Channel; use app\model\Customer; +use app\model\CustomerHis; use app\model\Follow; +use app\model\User; use app\service\UserService; use app\util\Util; use config\constants\Constants; @@ -129,6 +131,8 @@ class CustomerController $follow = $request->input('follow', ''); //跟进状态 $keyword = $request->input('keyword', ''); //follow_last $follow_last = $request->input('follow_last', ''); //最后跟进:1:当天跟进客户,2:近三天跟进客户,3:近七天跟进客户 + $tagId = $request->input('tag_id', ''); //打有某个标签的客户 + $seas = $request->input('seas', ''); //公海客户 $where = []; @@ -136,7 +140,11 @@ class CustomerController if (Util::isEmpty($userId)) { return Util::fail(['user_id' => $userId], trans('user_none')); } - $where[] = ['user_id', $userId]; + if($seas == 'seas'){ + $where[] = ['user_id', 0]; + }else{ + $where[] = ['user_id', $userId]; + } //验证权限 // if ($request->user['role'] != 1) { // $where[] =['user_id',$userId] ; @@ -160,18 +168,21 @@ class CustomerController // 执行分页查询 //关联标签tags //关联标签及分组tags.group - $query = Customer::where($where) + $query = Customer::with('tags')->where($where) ->orderBy('id', 'desc'); + if(!empty($tagId)){ + $query->with(['tags' => function($query) use ($tagId){ + $query->where('id', $tagId); + }]); + } if (!Util::isEmpty($keyword)) { $query->where(function ($query) use ($keyword) { $query->where('contacts', 'like', "%{$keyword}%") ->orWhere('phone', 'like', "%{$keyword}%"); }); - } if (!Util::isEmpty($grade)) { $query->whereIn('grade', $grade); - } $recycle = Util::getParam($request,'recycle', ''); @@ -301,6 +312,92 @@ class CustomerController } } + /** + * 领取公海客户 + * @param Request $request + * @return \support\Response + */ + public function claim(Request $request) + { + $customerId = Util::getParam($request, 'customer_id'); + $userId = $request->user['id']; + if (Util::isEmpty($customerId)) { + return Util::fail([], trans('customer_id').trans('not_null')); + } + $data = [ + 'user_id' => $request->user['id'] + ]; + $customer= Customer::find($customerId); + + if($customer->user_id != 0){ + return Util::fail('', trans('claim_fail')); + } + + $customer->restore(); + $res = $customer->update($data); + + //记录客户关系变化 + $his = CustomerHis::create([ + 'customer_id' => $customerId, + 'user_his' => 0, + 'user_id' => $userId, + ]); + + if ($res) { + return Util::success($res, trans('success')); + } else { + return Util::fail($res, trans('fail')); + } + } + + + /** + * 批量转让 + * @param Request $request + * @return \support\Response + */ + public function cede(Request $request) + { + $customerId = Util::getParam($request, 'customer_id'); + $user_sn = Util::getParam($request, 'user_sn'); + $userId = $request->user['id']; + if (Util::isEmpty($user_sn)) { + return Util::fail([], 'ID'.trans('not_null')); + } + + $user = User::where(['sn' => $user_sn])->first(); + if (Util::isEmpty($user)) { + return Util::fail(null, trans('user_none')); + } + + if(Util::isEmpty($customerId)){ + $data = [ + 'user_id' => $userId + ]; + $query= Customer::where($data); + }else{ + $query= Customer::whereIn('id',explode(',',$customerId)); + } + $customer = $query->get(); + + foreach ($customer as $item) { + //记录客户关系变化 + $his = CustomerHis::create([ + 'customer_id' => $item->id, + 'user_his' => $item->user_id, + 'user_id' => $user['id'], + ]); + } + $res = $query->update(['user_id' => $user->id]); + + + if ($res) { + return Util::success($res, trans('success')); + } else { + return Util::fail($res, trans('fail')); + } + } + /** * 跟进状态 * @param Request $request diff --git a/app/controller/UserController.php b/app/controller/UserController.php index a1fb4ff..a52cd7c 100644 --- a/app/controller/UserController.php +++ b/app/controller/UserController.php @@ -197,7 +197,7 @@ class UserController $user['avatar_url'] = $user['avatar']; $ossservice = new \app\service\OssService(); $user['avatar'] = $user['avatar'] ? $ossservice->generatePrivateUrl($user['avatar']) : ''; - return Util::success($user, trans('save_success')); + return Util::success($user, trans('success')); } /** @@ -208,27 +208,18 @@ class UserController */ public function save(Request $request) { - $name = Util::getParam($request, 'name'); - $sex = Util::getParam($request, 'sex'); - $position = Util::getParam($request, 'position'); - $country_id = Util::getParam($request, 'country_id'); - $sign = Util::getParam($request, 'sign'); $id = $request->user['id']; $avatar = Util::getParam($request, 'avatar_url'); - $mobile = Util::getParam($request, 'mobile'); - $data = [ - 'name' => $name, - 'sex' => $sex, - 'position' => $position, - 'country_id' => $country_id, - 'sign' => $sign, - 'avatar' => $avatar, - 'mobile' => $mobile - ]; + $user = User::where('id', $id)->first(); + // 只填充前端传递的字段 + $user->fill($request->only(['name', 'sex', 'position', 'country_id', 'sign', 'mobile', 'address'])); - $res = User::where('id', $id) - ->update($data); + if(!Util::isEmpty($avatar)){ + $user->avatar = $avatar; + } + + $res = $user->save(); if ($res) { return Util::success($res, trans('save_success')); diff --git a/app/model/Customer.php b/app/model/Customer.php index d5f4985..52d30b0 100644 --- a/app/model/Customer.php +++ b/app/model/Customer.php @@ -121,4 +121,11 @@ class Customer extends Model return $this->belongsToMany(Tag::class, 'customer_tag', 'customer_id', 'tag_id') ->withTimestamps(); // 如果需要记录关联时间 } + + + // 定义与 变更历史 的一对多关系 + public function his() + { + return $this->hasMany(CustomerHis::class, 'customer_id', 'id'); + } } \ No newline at end of file diff --git a/app/model/CustomerHis.php b/app/model/CustomerHis.php new file mode 100644 index 0000000..9301b66 --- /dev/null +++ b/app/model/CustomerHis.php @@ -0,0 +1,68 @@ + 'datetime:Y-m-d H:i:s', // 指定序列化格式 +// 'created_at' => 'timestamp', // 指定序列化格式 + ]; + + /** + * 重新格式化时间 + * @param DateTimeInterface $date + * @return string + */ + protected function serializeDate(DateTimeInterface $date): string + { + return $date->format(Carbon::parse($date)->toDateTimeString()); + } + + // 定义反向多对一关联 + public function customer() + { + return $this->belongsTo(Customer::class, 'customer_id', 'id'); + } + + // 禁用 updated_at 的自动更新 + public function setUpdatedAt($value) + { + return $this; // 不做任何操作 + } +} \ No newline at end of file diff --git a/app/process/Task.php b/app/process/Task.php index 3c94374..3248a90 100644 --- a/app/process/Task.php +++ b/app/process/Task.php @@ -4,6 +4,7 @@ namespace app\process; use app\controller\FeishuController; use app\model\Customer; +use app\model\CustomerHis; use app\model\FeishuUser; use app\model\User; use support\Log; @@ -71,6 +72,29 @@ class Task Log::info('send follow start',[time()]); }); + + // 每小时执行一次 + new Crontab('0 10 * * * *', function(){ + echo date('Y-m-d H:i:s') . "\n"; + Log::info('send high seas start',[time()]); + + $where = [ + ['deleted_at', '<=', time() - 3600], + ['user_id', '<>', 0], + ]; + $customer = Customer::where($where)->onlyTrashed()->get(); + foreach ($customer as $item) { + $item->update(['user_id' => 0]); + CustomerHis::create([ + 'customer_id' => $item->_id, + 'user_his' => $item->user_id, + 'user_id' => 0, + ]); + } + + Log::info('send high seas start',[time()]); + }); + } } \ No newline at end of file diff --git a/resource/translations/en/messages.php b/resource/translations/en/messages.php index 2a20ae4..14f43de 100644 --- a/resource/translations/en/messages.php +++ b/resource/translations/en/messages.php @@ -6,6 +6,7 @@ return [ 'login_success' => ' login success', 'password_error' => ' password error', 'user_none' => ' user none', + 'user_id' => ' user ID', 'user_login' => ' Please log in first', 'not_null' => ' not null', 'code_error' => ' code error', @@ -30,4 +31,5 @@ return [ 'id_empty' => ' ID cannot be empty', 'customer_id' => ' customer ID', 'tag_group_id' => ' tag group ID', + 'claim_fail' => ' Cannot claim ownership of customers', ]; \ No newline at end of file diff --git a/resource/translations/zh_CN/messages.php b/resource/translations/zh_CN/messages.php index 1a6428c..07f384d 100644 --- a/resource/translations/zh_CN/messages.php +++ b/resource/translations/zh_CN/messages.php @@ -6,6 +6,7 @@ return [ 'login_success' => ' 登录成功', 'password_error' => ' 密码错误', 'user_none' => ' 用户不存在', + 'user_id' => ' 用户ID', 'user_login' => ' 请先登录', 'not_null' => ' 不能为空', 'code_error' => ' 验证码错误', @@ -30,4 +31,5 @@ return [ 'id_empty' => ' id不能为空', 'customer_id' => ' 客户ID', 'tag_group_id' => ' 标签组ID', + 'claim_fail' => ' 不能领取有归属客户', ]; \ No newline at end of file