Notice.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/17
  6. * Time: 下午4:44
  7. */
  8. namespace App\Modules\Notice\Models;
  9. use App\Modules\Book\Services\BookConfigService;
  10. use Illuminate\Database\Eloquent\Model;
  11. /**
  12. * Class Notice 公告
  13. * @package App\Modules\Notice\Models
  14. */
  15. class Notice extends Model
  16. {
  17. protected $tables = 'notices';
  18. protected $fillable = ['title', 'content', 'notice_type_id', 'is_popup', 'status'];
  19. /**
  20. * 根据 notice_type_id 获取公告列表
  21. * @param $notice_type_id
  22. * @param $status
  23. * @return mixed
  24. */
  25. static function getList($notice_type_id = '', $status = '')
  26. {
  27. $search_object = self::select(['notices.id','title','content','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
  28. ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
  29. ->whereExists(function ($query) {
  30. $query->select('id')->from('notice_types')->whereRaw('notices.notice_type_id=notice_types.id');
  31. })
  32. ->orderBy('status','asc')
  33. ->orderBy('notices.id','desc');
  34. if($notice_type_id) $search_object->where('notice_type_id',$notice_type_id);
  35. if(is_numeric($status)) $search_object->where('status', $status);
  36. $data = $search_object->paginate();
  37. foreach ($data as $item) {
  38. if ($item && isset($item->content)) {
  39. $item->content = self::getReplyConent($item->content);
  40. }
  41. }
  42. return $data;
  43. }
  44. /**
  45. * 根据 notice_type_id 获取简易公告列表
  46. * @param $notice_type_id
  47. * @param $status
  48. * @return mixed
  49. */
  50. static function getSimpleNoticeList($notice_type_id = '', $status = '')
  51. {
  52. $search_object = self::select(['notices.id','title','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
  53. ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
  54. ->whereExists(function ($query) {
  55. $query->select('id')->from('notice_types')->whereRaw('notices.notice_type_id=notice_types.id');
  56. })
  57. ->orderBy('status','asc')
  58. ->orderBy('notices.id','desc');
  59. if($notice_type_id) $search_object->where('notice_type_id',$notice_type_id);
  60. if(is_numeric($status)) $search_object->where('status', $status);
  61. $data = $search_object->paginate();
  62. return $data;
  63. }
  64. /**
  65. * 获取单个通知
  66. * @param $id
  67. * @return mixed
  68. */
  69. static function getItem($id) {
  70. $search_object = self::select(['notices.id','title','content','is_popup', 'status', 'notices.updated_at','notice_types.name as type_name', 'notice_types.id as type_id'])
  71. ->leftjoin('notice_types','notice_types.id','=','notices.notice_type_id')
  72. ->orderBy('status','desc')
  73. ->orderBy('notices.id','desc')
  74. ->where('notices.id', $id);
  75. $data = $search_object->first();
  76. if ($data && isset($data->content)) {
  77. $data->content = self::getReplyConent($data->content);
  78. }
  79. return $data;
  80. }
  81. static function getReplyConent($content)
  82. {
  83. //《《11444》》
  84. if ($content) {
  85. $findEnd = "》》";
  86. $findBegin = "《《";
  87. $findBeginIndex = strpos($content, $findBegin);
  88. if ($findBeginIndex !== false) {
  89. $findBeginIndex = $findBeginIndex + strlen($findBegin);
  90. $findEndIndex = strpos($content, $findEnd);
  91. if ($findEndIndex !== false) {
  92. //获取图书id
  93. $bid = substr($content, $findBeginIndex, $findEndIndex - $findBeginIndex);
  94. if ($bid) {
  95. //通过图书id获取图书的信息
  96. $bookConfig = BookConfigService::getBookById($bid);
  97. if ($bookConfig) {
  98. //将特殊字符替换成 书本名称
  99. $content = str_replace($findBegin . $bid . $findEnd, $bookConfig->book_name, $content);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. return $content;
  106. }
  107. }