KefuTime.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. User::where('user_id', $cs_uid)->where('chat_num', '>', 0)->update(['chat_num'=>Db::raw('chat_num-1')]);
  30. $count = User::where('cs_uid', $cs_uid)->whereIn('service_status', [1,2])->count();
  31. if ($count == 0) {
  32. return false;
  33. }
  34. }
  35. $exits = KefuTime::where('admin_id', $admin_id)->where('type', $type)->where("created_at", '>=', date("Y-m-d"))->order('id', 'desc')->find();
  36. if ($exits && $exits->status == 0) {
  37. $exits->status = 1;
  38. $exits->end_time = time();
  39. $exits->save();
  40. $info = KefuWork::where('admin_id', $admin_id)->where("created_at", '>=', date("Y-m-d"))->find();
  41. if ($type == 1) {
  42. $info->busy_time += $exits->end_time - $exits->start_time;
  43. $info->save();
  44. } elseif ($type == 2) {
  45. $info->online_time += $exits->end_time - $exits->start_time;
  46. $info->save();
  47. } elseif ($type == 3) {
  48. $info->chat_time += $exits->end_time - $exits->start_time;
  49. $info->save();
  50. } elseif ($type == 4) {
  51. $info->service_time += $exits->end_time - $exits->start_time;
  52. $info->save();
  53. }
  54. return true;
  55. }
  56. return false;
  57. }
  58. }