ArticleService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AccountModel;
  13. use App\Models\ArticleModel;
  14. use App\Services\BaseService;
  15. /**
  16. * 文章管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class ArticleService
  20. * @package App\Services\Common
  21. */
  22. class ArticleService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * ArticleService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new ArticleModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $status = isset($params['status'])? $params['status'] : 0;
  54. $type = isset($params['type'])? $params['type'] : 0;
  55. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  56. if($status>0){
  57. $where['a.status'] = $status;
  58. }
  59. if($type>0){
  60. $where['a.type'] = $type;
  61. }
  62. if($cateId>0){
  63. $where['a.cate_id'] = $cateId;
  64. }
  65. $list = $this->model->from('article as a')
  66. ->leftJoin('article_cate as b', 'b.id', '=', 'a.cate_id')
  67. ->where($where)
  68. ->where(function ($query) use($params){
  69. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  70. if($keyword){
  71. $query->where('a.title','like',"%{$keyword}%")->orWhere('b.name','like',"%{$keyword}%");
  72. }
  73. })
  74. ->select(['a.*','b.name as cate_name'])
  75. ->orderBy('a.create_time','desc')
  76. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  77. $list = $list? $list->toArray() :[];
  78. if($list){
  79. foreach($list['data'] as &$item){
  80. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  81. $item['cover'] = $item['cover']? get_image_url($item['cover']) : '';
  82. }
  83. }
  84. return [
  85. 'pageSize'=> $pageSize,
  86. 'total'=>isset($list['total'])? $list['total'] : 0,
  87. 'list'=> isset($list['data'])? $list['data'] : []
  88. ];
  89. }
  90. /**
  91. * 添加或编辑
  92. * @return array
  93. * @since 2020/11/11
  94. * @author laravel开发员
  95. */
  96. public function edit()
  97. {
  98. $data = request()->all();
  99. return parent::edit($data); // TODO: Change the autogenerated stub
  100. }
  101. /**
  102. * 获取店铺交易统计
  103. * @param $shopId
  104. * @param int $type
  105. * @param int $coinType
  106. * @return mixed
  107. */
  108. public function getInfoByCate($cateId)
  109. {
  110. return $this->model->where(['cate_id'=> $cateId,'status'=>1,'mark'=>1])
  111. ->orderBy('create_time','desc')
  112. ->first();
  113. }
  114. }