MessageService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace app\admin\service\client;
  3. use app\model\ClientMessage;
  4. use app\model\SysUser;
  5. use support\Db;
  6. class MessageService
  7. {
  8. public static function unreadChatMessage()
  9. {
  10. $times = [
  11. strtotime("-40 minutes"),
  12. strtotime("-30 minutes"),
  13. ];
  14. $chats = Db::table('chat_message')
  15. ->selectRaw('join_message_recv_id,join_message_send_id,GROUP_CONCAT(chat_message_id) as chat_message_ids,MIN(chat_message_id) as min_chat_message_id')
  16. ->where('chat_message_status', 'PENDING')
  17. ->whereBetween('chat_message_addtimes',$times)
  18. ->groupBy('join_message_send_id', 'join_message_recv_id')
  19. ->get();
  20. foreach ($chats as $chat) {
  21. // 接收者是后台人员
  22. if (substr($chat->join_message_recv_id, 0, 2) == 'UR') {
  23. continue;
  24. }
  25. if (ClientMessage::where('join_client_message_recv_member_id', $chat->join_message_recv_id)
  26. ->where('client_message_classify','CHAT')
  27. ->whereJsonContains("client_message_extend_json->message_id",strval($chat->min_chat_message_id))
  28. ->exists()) {
  29. continue;
  30. }
  31. // 是客服还是医生
  32. $user = SysUser::select('join_user_role_id', 'user_id')
  33. ->where('user_id',$chat->join_message_send_id)
  34. ->first();
  35. if ($user && $user->join_user_role_id == 30){
  36. $role = '医生';
  37. }elseif ($user && $user->join_user_role_id == 27){
  38. $role = '小悦客服';
  39. }else{
  40. continue;
  41. }
  42. $messageIds = explode(',',$chat->chat_message_ids);
  43. $insertData = [
  44. 'client_message_classify'=>'CHAT',
  45. 'join_client_message_send'=>'SYSTEM',
  46. 'join_client_message_recv_member_id'=>$chat->join_message_recv_id,
  47. 'client_message_sendtime'=>date('Y-m-d H:i:s'),
  48. 'client_message_status'=>'PENDING',
  49. 'client_message_category'=>'NORMAL',
  50. 'client_message_header_json'=>json_encode(['title'=>'未读消息']),
  51. 'client_message_body_json'=>json_encode(['content'=>"您有来自".$role."的消息,请及时查收。"]),
  52. 'client_message_extend_json'=>json_encode(['message_id'=>$messageIds]),
  53. 'client_message_addtimes'=>time()
  54. ];
  55. ClientMessage::insert($insertData);
  56. }
  57. }
  58. }