KefuTime.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\admin\model;
  3. use app\BaseModel;
  4. use \think\facade\Db;
  5. class KefuTime extends BaseModel
  6. {
  7. protected $autoWriteTimestamp = true;
  8. protected $createTime = 'created_at';
  9. protected $updateTime = 'updated_at';
  10. public static function addData($admin_id, $type) {
  11. if ($type == 3 || $type == 4) {
  12. $info = self::where('admin_id', $admin_id)->where('type', $type)->where('status', 0)->find();
  13. if ($info) {
  14. return $info;
  15. }
  16. }
  17. return self::create([
  18. 'admin_id' => $admin_id,
  19. 'type' => $type,
  20. 'start_time' => time(),
  21. 'status' => 0,
  22. ]);
  23. }
  24. //结束服务时间
  25. public static function endData($admin_id, $type, $cs_uid = 0) {
  26. //结束接线(需要判断当前没有任何接线的用户,才结束接线时间)
  27. if ($type == 3) {
  28. //更新客服接线数量
  29. $chat_num = User::where('cs_uid',$cs_uid)->whereIn('service_status',[1,2])->count();
  30. User::where('user_id', $cs_uid)->update(['chat_num'=>$chat_num]);
  31. $count = User::where('cs_uid', $cs_uid)->whereIn('service_status', [1,2])->count();
  32. if ($count == 0) {
  33. return false;
  34. }
  35. }
  36. $exits = KefuTime::where('admin_id', $admin_id)->where('type', $type)->where("created_at", '>=', date("Y-m-d"))->order('id', 'desc')->find();
  37. if ($exits && $exits->status == 0) {
  38. $exits->status = 1;
  39. $exits->end_time = time();
  40. $exits->save();
  41. $info = KefuWork::where('admin_id', $admin_id)->where("created_at", '>=', date("Y-m-d"))->find();
  42. if ($type == 1) {
  43. $info->busy_time += $exits->end_time - $exits->start_time;
  44. $info->save();
  45. } elseif ($type == 2) {
  46. $info->online_time += $exits->end_time - $exits->start_time;
  47. $info->save();
  48. } elseif ($type == 3) {
  49. $info->chat_time += $exits->end_time - $exits->start_time;
  50. $info->save();
  51. } elseif ($type == 4) {
  52. $info->service_time += $exits->end_time - $exits->start_time;
  53. $info->save();
  54. }
  55. return true;
  56. }
  57. return false;
  58. }
  59. }