CustomSendMsgs.php 5.7 KB

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