Headline.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 8:59
  7. */
  8. namespace App\Modules\Promotion\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. /**
  11. * Class Headline 标题
  12. * @package App\Modules\Promotion\Models
  13. */
  14. class Headline extends Model
  15. {
  16. protected $connection = 'api_mysql';
  17. protected $table = 'headlines';
  18. protected $fillable = [
  19. 'id',
  20. 'title',
  21. 'category',
  22. 'serial_number',
  23. 'remark',
  24. 'type',
  25. 'quality',
  26. 'created_at',
  27. 'updated_at'
  28. ];
  29. /**
  30. * category:类型 (1: 男频,2:女频)
  31. * @return mixed 获取类型下的所有的标题
  32. */
  33. static function getAllHeadlines($category)
  34. {
  35. if (1 == $category) {
  36. $category = '男频';
  37. } else {
  38. $category = '女频';
  39. }
  40. return $result = self::select('id', 'title')->where('type', 1)->where('category', $category)->get();
  41. }
  42. /**
  43. * @return mixed 获取所有的标题
  44. */
  45. static function getHeadlines()
  46. {
  47. return $result = self::select('id', 'title')->where('type', 2)->get();
  48. }
  49. /**
  50. * category:类型 (1: 男频,2:女频)
  51. * count: 获取的条数(默认为1条)
  52. * @return mixed 随机获取标题库中的若干条标题
  53. */
  54. static function getRandomHeadline($category, $count = 1)
  55. {
  56. if (1 == $category) {
  57. $category = '男频';
  58. } else {
  59. $category = '女频';
  60. }
  61. return $result = self::select('id', 'title')->where('category', $category)->where('type', 2)->where('remark', 2)->inRandomOrder()->take($count)->get();
  62. }
  63. static function getFullHeadline()
  64. {
  65. return $result = self::select('id', 'title')->where('type', 2)->where('remark', 2)->get();
  66. }
  67. /**
  68. * 添加标题
  69. */
  70. static function addHeadline($data)
  71. {
  72. return self::create($data);
  73. }
  74. /**
  75. * 添加标题
  76. */
  77. static function delHeadlines($ids)
  78. {
  79. return self::whereIn('id', $ids)->delete();
  80. }
  81. /**
  82. * 获取标题库中的serial_number 的最大值
  83. * @return mixed
  84. */
  85. static function getMaxSerialNumber()
  86. {
  87. return self::max('serial_number');
  88. }
  89. static function getHeadlineByParams($param)
  90. {
  91. $search_object = self::orderBy('id', 'desc');
  92. if (isset($param['id']) && $param['id']) {
  93. $search_object->where('id', $param['id']);
  94. }
  95. if (isset($param['remark']) && $param['remark']) {
  96. $search_object->where('remark', $param['remark']);
  97. }
  98. if (isset($param['title']) && $param['title']) {
  99. $search_object->where('title', 'like', '%' . $param['title'] . '%');
  100. }
  101. if (isset($param['category']) && $param['category']) {
  102. //category:类型 (1: 男频,2:女频)
  103. $category = $param['category'];
  104. if (1 == $category) {
  105. $category = '男频';
  106. } elseif (2 == $category) {
  107. $category = '女频';
  108. }
  109. $search_object->where('category', $category);
  110. }
  111. if (isset($param['type']) && $param['type']) {
  112. $search_object->where('type', $param['type']);
  113. }
  114. if (isset($param['quality']) && $param['quality']) {
  115. $search_object->where('quality', $param['quality']);
  116. }
  117. return $search_object->paginate();
  118. }
  119. }