CustomMsgSendTimeConfig.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Modules\OfficialAccount\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomMsgSendTimeConfig extends Model
  6. {
  7. protected $tables = 'custom_msg_send_time_configs';
  8. protected $guarded = [];
  9. /**
  10. *获取站点下客服消息发送时间配置
  11. */
  12. static function getConfigs($params){
  13. return self::where('is_enable','1')->where($params)->get();
  14. }
  15. /**
  16. *批量创建客服消息发送时间配置
  17. */
  18. static function BatchCreateSendTimeConfigs($distribution_channel_ids = [],$custom_category = '',$send_times = [])
  19. {
  20. $now = date('Y-m-d H:i:s');
  21. $insert_data = array();
  22. if(!($distribution_channel_ids&&$custom_category&&$send_times)){
  23. return false;
  24. }
  25. foreach ($distribution_channel_ids as $distribution_channel_id){
  26. $params['distribution_channel_id'] = $distribution_channel_id;
  27. $params['is_enable'] = 1;
  28. foreach ($send_times as $send_time){
  29. $params['custom_category'] = $custom_category;
  30. $params['send_time'] = $send_time;
  31. $params['created_at'] = $now;
  32. $params['updated_at'] = $now;
  33. $insert_data[] = $params;
  34. }
  35. }
  36. try {
  37. DB::beginTransaction();
  38. //弃用现有配
  39. DB::table('custom_msg_send_time_configs')
  40. ->whereIn('distribution_channel_id',$distribution_channel_ids)
  41. ->where('custom_category' , $custom_category)
  42. ->update(['is_enable'=>0])
  43. ;
  44. //更新最新配置
  45. $res = DB::table('custom_msg_send_time_configs')->insert($insert_data);
  46. DB::commit();
  47. return true;
  48. } catch (Exception $e) {
  49. Log::error('book_stacks confirm custom_msg_send_time_configs_insert :' . $e->getMessage());
  50. return false;
  51. }
  52. }
  53. /**
  54. *获取站点下客服消息发送时间配置
  55. */
  56. static function getTimeConfig($channel_id='',$params=[]){
  57. $res = self::where('is_enable','1')
  58. ->where('distribution_channel_id',$channel_id)->orderBy('send_time');
  59. if(isset($params['custom_category'])&&$params['custom_category']){
  60. $res->where('custom_category' , $params['custom_category']);
  61. }
  62. if(isset($params['begin_time'])&&$params['begin_time']){
  63. $res->where('send_time' , '>',$params['begin_time']);
  64. }
  65. if(isset($params['end_time'])&&$params['end_time']){
  66. $res->where('send_time' , '<',$params['end_time']);
  67. }
  68. return $res->first();
  69. }
  70. }