CustomSendMsgs.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Modules\OfficialAccount\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomSendMsgs extends Model
  6. {
  7. protected $tables = 'custom_send_msgs';
  8. protected $fillable = ['task_id', 'user_num', 'appid', 'name', 'send_time', 'content', 'redirect_url', 'status', 'distribution_channel_id', 'subscribe_time', 'sex', 'balance', 'order_type', 'category_id', 'del_flag', 'is_full_send', 'trusteeship', 'batch_no', 'is_show_list', 'description', 'book_name', 'chapter_name', 'is_activity','subscribe_time_attach', 'custom_type'];
  9. /**
  10. * 判断1小时内是否有相同的客服消息插入过
  11. */
  12. static function isSendCustomerAtSameTime($distribution_channel_id, $appid, $send_time)
  13. {
  14. return self::where(['distribution_channel_id' => $distribution_channel_id, 'appid' => $appid, 'del_flag' => 0])->where('status', '!=', '4')->where('send_time', '<', date('Y-m-d H:i:s', strtotime($send_time) + 3600))->where('send_time', '>', date('Y-m-d H:i:s', strtotime($send_time) - 3600))->first();
  15. }
  16. /**
  17. * 判断1小时内是否有相同的客服消息插入过
  18. */
  19. static function isSendCustomerAtSameTimeAndSex($distribution_channel_id, $appid, $send_time, $sex)
  20. {
  21. return self::where(['distribution_channel_id' => $distribution_channel_id, 'appid' => $appid, 'sex' => $sex, 'del_flag' => 0])->where('status', '!=', '4')->where('send_time', '<', date('Y-m-d H:i:s', strtotime($send_time) + 3600))->where('send_time', '>', date('Y-m-d H:i:s', strtotime($send_time) - 3600))->first();
  22. }
  23. /**
  24. * 根据渠道号获取客服消息
  25. */
  26. static function customSendMsgsByChannelId($distribution_channel_id)
  27. {
  28. return self::where('distribution_channel_id', isset($distribution_channel_id) ? $distribution_channel_id : '')->where('del_flag', 0)->where('is_show_list', 1)->orderBy('id', 'desc')->paginate();
  29. }
  30. /**
  31. * 根据渠道号和自定义标识获取客服消息
  32. */
  33. static function customSendMsgsByChannelIdAndTrusteeship($distribution_channel_id, $trusteeship = 1)
  34. {
  35. return self::where('distribution_channel_id', $distribution_channel_id)->where('trusteeship', $trusteeship)->where('del_flag', 0)->orderBy('id', 'desc')->paginate();
  36. }
  37. /**
  38. * 根据自定义标识获取所有批次客服消息
  39. */
  40. static function customSendMsgsByTrusteeship($trusteeship = 1)
  41. {
  42. return self::where('trusteeship', $trusteeship)->where('del_flag', 0)->select('custom_send_msgs.*', 'custom_send_msgs.created_at as create_time')->orderBy('batch_no', 'desc')->orderBy('id', 'desc')->paginate();
  43. }
  44. /**
  45. * 根据批次号获取客服消息
  46. */
  47. static function customSendMsgsByTrusteeshipAndBacthNo($trusteeship = 1, $batch_no)
  48. {
  49. return self::where('trusteeship', $trusteeship)->where('batch_no', $batch_no)->where('del_flag', 0)->orderBy('id', 'desc')->get();
  50. }
  51. /**
  52. * 根据task_id获取客服消息
  53. */
  54. static function customSendMsgsByTaskid($task_id)
  55. {
  56. return self::where('task_id', isset($task_id) ? $task_id : '')->first();
  57. }
  58. /**
  59. * 根据id获取客服消息
  60. */
  61. static function customSendMsgsById($id)
  62. {
  63. return self::where('id', isset($id) ? $id : '')->first();
  64. }
  65. /**
  66. * 根据状态获取客服消息
  67. */
  68. static function customSendMsgsByStatusStandBy($status)
  69. {
  70. return self::where(['status' => $status, 'del_flag' => 0])->get();
  71. }
  72. /**
  73. * 根据分页获取客服消息
  74. */
  75. static function customSendMsgsPaginate($start_time, $end_time, $is_all = false)
  76. {
  77. $search_obj = self::select('custom_send_msgs.distribution_channel_id as site_id',
  78. 'custom_send_msgs.name as task_name',
  79. DB::raw('date(custom_send_msgs.created_at) as created_date'),
  80. 'custom_send_msgs.send_time',
  81. 'custom_send_msgs.user_num as suc_send_num',
  82. DB::raw('"" as template_name'),
  83. DB::raw('"" as tag'),
  84. DB::raw('(select group_concat(nickname) from official_accounts where distribution_channel_id=custom_send_msgs.distribution_channel_id group by distribution_channel_id) as official_account_name')//official_account_name
  85. )
  86. ->where(['del_flag' => 0, 'status' => 3])
  87. ->where('custom_send_msgs.created_at', '>=', $start_time)
  88. ->where('custom_send_msgs.created_at', '<', $end_time)
  89. ->orderBy('id', 'desc');
  90. if ($is_all) {
  91. return $search_obj->get();
  92. } else {
  93. return $search_obj->paginate();
  94. }
  95. }
  96. static function searchCustomSendMsgs($param, $is_all = false)
  97. {
  98. $res = self::where('distribution_channel_id', $param['distribution_channel_id'])
  99. ->select('*')
  100. ->where('del_flag', 0)
  101. ->where('is_show_list', 1);
  102. if (isset($param['start_time']) && $param['start_time']) {
  103. $res->where('custom_send_msgs.created_at', '>=', $param['start_time']);
  104. }
  105. if (isset($param['end_time']) && $param['end_time']) {
  106. $res->where('custom_send_msgs.created_at', '<=', $param['end_time']);
  107. }
  108. if (isset($param['book_name']) && $param['book_name']) {
  109. $res->where('custom_send_msgs.book_name', 'like', '%' . $param['book_name'] . '%');
  110. }
  111. if (isset($param['task_name']) && $param['task_name']) {
  112. $res->where('custom_send_msgs.name', 'like', '%' . $param['task_name'] . '%');
  113. }
  114. if ($is_all) {
  115. $custom_send_msgs = $res->orderBy('id', 'desc')->get();
  116. } else {
  117. $custom_send_msgs = $res->orderBy('id', 'desc')->paginate();
  118. }
  119. return $custom_send_msgs;
  120. }
  121. }