BlogModel.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace App\Web\Models;
  3. use App\Models\BaseModel;
  4. use Illuminate\Support\Facades\DB;
  5. class BlogModel extends BaseModel
  6. {
  7. /**
  8. * 状态字段
  9. */
  10. const DELETED_AT = 'status';
  11. /**
  12. * @var string
  13. */
  14. protected $table = 'blog';
  15. /**
  16. * 获取文章列表
  17. * */
  18. public function getBlogList($params,$fields='a.*'){
  19. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  20. $where=[];
  21. if (isset($params['is_admin'])) {
  22. $fields='a.id,a.plate_id,a.title,a.pub_date,a.image_url,a.virtual_view,a.description,a.status,a.update_time,a.create_time,a.total_view,a.is_top,a.sort,a.seo_id,c.seo_title,c.seo_keyword,c.seo_describe,c.urla,c.trans_status,a.is_top,a.is_recommend';
  23. }
  24. if(isset($params['status'])){
  25. $where[]=['a.status','=',$params['status']];
  26. }else{
  27. $where[]=['a.status','<',2];
  28. }
  29. if(!empty($params['plate_id'])){
  30. $where[]=['a.plate_id','=',$params['plate_id']];
  31. }
  32. if(!empty($params['lt_total_view'])){
  33. $where[]=['a.total_view','<',$params['lt_total_view']];
  34. }
  35. $query= $this->newInstance()->alias('a')->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')->where($where);
  36. if (!empty($params['keyword'])) {
  37. $keyword = $params['keyword'];
  38. $query->where('a.title', 'like', "%" . $keyword . "%");
  39. }
  40. if(!empty($params['sort'])){
  41. if($params['sort']=='sort_ascending'){
  42. $query->orderBy('sort','asc');
  43. }else if($params['sort']=='sort_descending'){
  44. $query->orderBy('sort','desc');
  45. }else if($params['sort']=='pub_date_ascending'){
  46. $query->orderBy('pub_date','asc');
  47. }else if($params['sort']=='pub_date_descending'){
  48. $query->orderBy('pub_date','desc');
  49. }else if($params['sort']=='create_time_ascending'){
  50. $query->orderBy('create_time','asc');
  51. }else if($params['sort']=='create_time_descending'){
  52. $query->orderBy('create_time','desc');
  53. }else if($params['sort']=='update_time_ascending'){
  54. $query->orderBy('update_time','asc');
  55. }else if($params['sort']=='update_time_descending'){
  56. $query->orderBy('update_time','desc');
  57. }
  58. }else{
  59. $query->orderBy('is_top','desc')
  60. ->orderBy('sort');
  61. }
  62. $totalCount = $query->count();
  63. $list= $query->skip($skip)
  64. ->limit($pageSize)
  65. ->selectRaw($fields)
  66. ->get();
  67. if(!empty($list)){
  68. $list=$list->toArray();
  69. }else{
  70. $list=[];
  71. }
  72. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  73. return $result;
  74. }
  75. /**
  76. * 获取文章分类未关联的文章
  77. * */
  78. public function getUnrelatedBlogByTypeIds($params){
  79. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  80. $typeId = $params['type_id'] ?? '';
  81. $keyword = $params['keyword'] ?? '';
  82. $where=[];
  83. if ($keyword) {
  84. $keyword = addslashes($keyword);
  85. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  86. }
  87. $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  88. ->leftJoin('blog_type_relation as b', function ($join)use($typeId) {
  89. $join->on('a.id', '=', 'b.blog_id')
  90. ->where('b.status', '=', 0)
  91. ->where('b.type_id', '=', $typeId);
  92. })->where('a.status','<',2)->whereNull('b.id')->count();
  93. $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  94. $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  95. ->leftJoin('blog_type_relation as b', function ($join)use($typeId) {
  96. $join->on('a.id', '=', 'b.blog_id')
  97. ->where('b.status', '=', 0)
  98. ->where('b.type_id', '=', $typeId);
  99. })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
  100. ->limit($pageSize)
  101. ->selectRaw($fields)
  102. ->orderBy('sort')
  103. ->get();
  104. if(!empty($list)){
  105. $list=$list->toArray();
  106. }else{
  107. $list=[];
  108. }
  109. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  110. return $result;
  111. }
  112. /**
  113. * 获取文章标签未关联的文章
  114. * */
  115. public function getUnrelatedBlogByTagIds($params){
  116. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  117. $tagId = $params['tag_id'] ?? '';
  118. $keyword = $params['keyword'] ?? '';
  119. $where=[];
  120. if ($keyword) {
  121. $keyword = addslashes($keyword);
  122. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  123. }
  124. $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  125. ->leftJoin('blog_tag_relation as b', function ($join)use($tagId) {
  126. $join->on('a.id', '=', 'b.blog_id')
  127. ->where('b.status', '=', 0)
  128. ->where('b.tag_id', '=', $tagId);
  129. })->where('a.status','<',2)->whereNull('b.id')->count();
  130. $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  131. $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  132. ->leftJoin('blog_tag_relation as b', function ($join)use($tagId) {
  133. $join->on('a.id', '=', 'b.blog_id')
  134. ->where('b.status', '=', 0)
  135. ->where('b.tag_id', '=', $tagId);
  136. })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
  137. ->limit($pageSize)
  138. ->selectRaw($fields)
  139. ->orderBy('sort')
  140. ->get();
  141. if(!empty($list)){
  142. $list=$list->toArray();
  143. }else{
  144. $list=[];
  145. }
  146. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  147. return $result;
  148. }
  149. /**
  150. * 文章保存
  151. * @param array $data
  152. * */
  153. public function saveBlogData($data){
  154. if (!empty($data['id'])) {
  155. $id = $data['id'];
  156. $this->newInstance()->where('id', $data['id'])->update($data);
  157. } else {
  158. $id = $this->newInstance()->insertGetId($data);
  159. }
  160. return $id;
  161. }
  162. /**
  163. * 检查 标签是否唯一
  164. * */
  165. public function checkNameUnique($name,$id=0){
  166. $where=[];
  167. if(!empty($id)){
  168. $where[]=['id', '<>', $id];
  169. }
  170. $where[]=['status', '<', 2];
  171. $where[]=['title', '=', trim($name)];
  172. return $this->checkFieldUnique('title',$where);
  173. }
  174. /**
  175. * 获取文章详情
  176. * @param array $params
  177. * */
  178. public function getBlogInfo($params=[]){
  179. if(empty($params)){
  180. return [];
  181. }
  182. $where=[];
  183. $where[]=['status','<',2];
  184. if(!empty($params['id'])){
  185. $where['id']=$params['id'];
  186. }
  187. if(isset($params['status'])) {
  188. $where['status'] = $params['status'];
  189. }
  190. $info= $this->newInstance()->where($where)->first();
  191. if(!empty($info)){
  192. return $info->toArray();
  193. }else{
  194. return [];
  195. }
  196. }
  197. /**
  198. * 获取文章基本列表
  199. * */
  200. public function getPublishBlogList($params,$fields='a.*'){
  201. $where=[];
  202. $where['a.status']=0;
  203. if(!empty($params['plate_id'])){
  204. $where['a.plate_id']=$params['plate_id'];
  205. }
  206. $query= $this->newInstance()->alias('a')
  207. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  208. ->where($where);
  209. $pageSize = $this->getPageSize($params);
  210. if(!empty($params['sort'])){
  211. if(!empty($params['sort']['is_hot'])){
  212. $query=$query->orderBy('a.total_view','desc');
  213. }
  214. if(!empty($params['sort']['is_top'])){
  215. $query=$query->orderBy('a.is_top','desc');
  216. }
  217. if(!empty($params['sort']['is_recommend'])){
  218. $query=$query->orderBy('a.is_recommend','desc');
  219. }
  220. if(!empty($params['sort']['pub_date'])){
  221. $query=$query->orderBy('a.pub_date','desc');
  222. }
  223. }
  224. if (!empty($params['keyword'])) {
  225. $keyword = $params['keyword'];
  226. $query=$query->where(function ($queryStr) use ($keyword) {
  227. $queryStr->where('a.title', 'like', "%" . $keyword . "%")
  228. ->orWhere('a.description', 'like', '%' . $keyword . '%');
  229. });
  230. }
  231. $list= $query->limit($pageSize)->selectRaw($fields)->get();
  232. if(!empty($list)){
  233. $list=$list->toArray();
  234. }else{
  235. $list=[];
  236. }
  237. return $list;
  238. }
  239. /**
  240. * 根据类型ids获取个类型文章
  241. * */
  242. public function getRenderListByTypeIds($typeIds,$params,$fields='a.*'){
  243. $where=[];
  244. $where['a.status']=0;
  245. $where['b.status']=0;
  246. if(!empty($params['plate_id'])){
  247. $where['a.plate_id']=$params['plate_id'];
  248. }
  249. $pageSize = $this->getPageSize($params);
  250. $sql = '';
  251. foreach ($typeIds as $item){
  252. $sqlStr= $this->newInstance()->alias('a')
  253. ->leftJoin('blog_type_relation as b', 'a.id', '=', 'b.blog_id')
  254. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  255. ->where($where)
  256. ->where('b.type_id','=',DB::raw($item))
  257. ->orderBy('a.is_top','desc')
  258. ->orderBy('a.pub_date','desc')
  259. ->limit($pageSize)->selectRaw($fields);
  260. if (empty($sql)) {
  261. $sql = $sqlStr;
  262. } else {
  263. $sql->unionAll($sqlStr);
  264. }
  265. }
  266. $list=[];
  267. if(!empty($sql)){
  268. $list = $sql->get();
  269. if($list){
  270. $list=$list->toArray();
  271. }
  272. }
  273. return $list;
  274. }
  275. /**
  276. * 根据类型id获取文章
  277. * */
  278. public function getPublishBlogListByTypeId($params,$fields='a.*'){
  279. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  280. $where=[];
  281. $where['a.status']=0;
  282. if(!empty($params['type_id'])){
  283. $where['b.type_id']= $params['type_id'];
  284. $where['b.status']=0;
  285. }
  286. if (!empty($params['plate_id'])) {
  287. $where['a.plate_id'] = $params['plate_id'];
  288. }
  289. $typeId = empty($params['type_id'])?0:$params['type_id'];
  290. $list = $this->newInstance()->alias('a')->selectRaw($fields)
  291. ->leftJoin('blog_type_relation as b', 'a.id', '=', 'b.blog_id')
  292. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  293. ->where($where)
  294. ->groupBy('a.id')
  295. ->orderBy('a.is_top','desc')
  296. ->orderBy('a.pub_date','desc')
  297. ->paginate($pageSize)
  298. ->toArray();
  299. return $list;
  300. }
  301. /**
  302. * 根据标签id获取文章
  303. * */
  304. public function getPublishBlogListByTagId($params,$fields='a.*'){
  305. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  306. $where=[];
  307. $where['a.status']=0;
  308. if(!empty($params['tag_id'])){
  309. $where['b.tag_id']= $params['tag_id'];
  310. $where['b.status']=0;
  311. }
  312. if (!empty($params['plate_id'])) {
  313. $where['a.plate_id'] = $params['plate_id'];
  314. }
  315. $totalCount= $this->newInstance()->alias('a')
  316. ->leftJoin('blog_tag_relation as b', 'a.id', '=', 'b.blog_id')
  317. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  318. ->where($where)->count();
  319. $list= $this->newInstance()->alias('a')
  320. ->leftJoin('blog_tag_relation as b', 'a.id', '=', 'b.blog_id')
  321. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  322. ->where($where)
  323. ->orderBy('a.is_top','desc')
  324. ->orderBy('a.pub_date','desc')
  325. ->limit($pageSize)->selectRaw($fields)->get();
  326. if($list){
  327. $list=$list->toArray();
  328. }else{
  329. $list=[];
  330. }
  331. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  332. return $result;
  333. }
  334. /**
  335. * 根据类型ids获取标签文章
  336. * */
  337. public function getRenderListByTagIds($tagIds,$params,$fields='a.*'){
  338. $where=[];
  339. $where['a.status']=0;
  340. $where['b.status']=0;
  341. if(!empty($params['plate_id'])){
  342. $where['a.plate_id']=$params['plate_id'];
  343. }
  344. $pageSize = $this->getPageSize($params);
  345. $sql = '';
  346. foreach ($tagIds as $item){
  347. $sqlStr= $this->newInstance()->alias('a')
  348. ->leftJoin('blog_tag_relation as b', 'a.id', '=', 'b.blog_id')
  349. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  350. ->where($where)
  351. ->where('b.tag_id','=',DB::raw($item))
  352. ->orderBy('a.is_top','desc')
  353. ->orderBy('a.pub_date','desc')
  354. ->limit($pageSize)->selectRaw($fields);
  355. if (empty($sql)) {
  356. $sql = $sqlStr;
  357. } else {
  358. $sql->unionAll($sqlStr);
  359. }
  360. }
  361. $list=[];
  362. if(!empty($sql)){
  363. $list = $sql->get();
  364. if($list){
  365. $list=$list->toArray();
  366. }
  367. }
  368. return $list;
  369. }
  370. /**
  371. * 获取板块文章
  372. * */
  373. public function getNewestPlateList($plateIds,$params,$fields='a.*'){
  374. $where=[];
  375. $where['a.status']=0;
  376. $pageSize = $this->getPageSize($params);
  377. $sql = '';
  378. foreach ($plateIds as $item){
  379. $sqlStr= $this->newInstance()->alias('a')
  380. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  381. ->where($where)
  382. ->where('a.plate_id','=',DB::raw($item))
  383. ->orderBy('a.is_top','desc')
  384. ->orderBy('a.sort','asc')
  385. ->limit($pageSize)->selectRaw($fields);
  386. if (empty($sql)) {
  387. $sql = $sqlStr;
  388. } else {
  389. $sql->unionAll($sqlStr);
  390. }
  391. }
  392. $list=[];
  393. if(!empty($sql)){
  394. $list = $sql->get();
  395. if($list){
  396. $list=$list->toArray();
  397. }
  398. }
  399. return $list;
  400. }
  401. public function updatePv($id){
  402. DB::update('update blog set votes = 1');
  403. }
  404. /**
  405. * 获取文章基本列表 分页
  406. * @param $params
  407. * @param string $fields
  408. * @return array
  409. */
  410. public function getPublishBlogListByPage($params, string $fields = 'a.*')
  411. {
  412. $where = [];
  413. $where['a.status'] = 0;
  414. // $where['c.status'] = 0;
  415. /*if ($params['plate_id'] === 12) {
  416. dd($where);
  417. }*/
  418. $query = $this->newInstance()->alias('a')
  419. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  420. ->leftJoin('blog_type_relation as c', 'a.id', '=', 'c.blog_id')
  421. ->leftJoin('blog_tag_relation as d', 'a.id', '=', 'd.blog_id');
  422. if (!empty($params['plate_id'])) {
  423. $where['a.plate_id'] = $params['plate_id'];
  424. }
  425. if (!empty($params['type_id'])) {
  426. if (is_array($params['type_id'])) {
  427. $query = $query->whereIn('c.type_id', $params['type_id']);
  428. } else {
  429. $where['c.type_id'] = $params['type_id'];
  430. }
  431. $where['c.status'] = 0;
  432. }
  433. if (!empty($params['tag_id'])) {
  434. if (is_array($params['tag_id'])) {
  435. $query = $query->whereIn('d.tag_id', $params['tag_id']);
  436. } else {
  437. $where['d.tag_id'] = $params['tag_id'];
  438. }
  439. $where['d.status'] = 0;
  440. }
  441. $query = $query->where($where);
  442. $pageSize = $this->getPageSize($params);
  443. if (!empty($params['sort'])) {
  444. if (!empty($params['sort']['is_hot'])) {
  445. $query = $query->orderBy('a.total_view', 'desc');
  446. }
  447. if (!empty($params['sort']['is_top'])) {
  448. $query = $query->orderBy('a.is_top', 'desc');
  449. }
  450. if (!empty($params['sort']['is_recommend'])) {
  451. $query = $query->orderBy('a.is_recommend', 'desc');
  452. }
  453. if (!empty($params['sort']['pub_date'])) {
  454. $query = $query->orderBy('a.pub_date', 'desc');
  455. }
  456. } else {
  457. $query = $query->orderBy('a.sort');
  458. }
  459. if (!empty($params['keyword'])) {
  460. $keyword = $params['keyword'];
  461. $query = $query->where(function ($queryStr) use ($keyword) {
  462. $queryStr->where('a.title', 'like', "%" . $keyword . "%")
  463. ->orWhere('a.description', 'like', '%' . $keyword . '%');
  464. });
  465. }
  466. $list = $query->selectRaw($fields)->groupBy('a.id')->paginate($pageSize);
  467. if (!empty($list)) {
  468. $list = $list->toArray();
  469. } else {
  470. $list = [];
  471. }
  472. return $list;
  473. }
  474. public function getPrevBlog($plateId, $blogId)
  475. {
  476. $data = $this->alias('a')
  477. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  478. ->where('a.plate_id', $plateId)
  479. ->where('a.id', '<', $blogId)
  480. ->where('a.status', 0)
  481. ->selectRaw('a.id,b.urla,a.title,a.image_url,a.image_alt,a.description')
  482. ->orderBy('a.id', 'asc')
  483. ->first();
  484. return $data ? $data->toArray(): [];
  485. }
  486. public function getNextBlog($plateId, $blogId)
  487. {
  488. $data = $this->alias('a')
  489. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  490. ->where('a.plate_id', $plateId)
  491. ->where('a.id', '>', $blogId)
  492. ->where('a.status', 0)
  493. ->selectRaw('a.id,b.urla,a.title,a.image_url,a.image_alt,a.description')
  494. ->orderBy('a.id', 'asc')
  495. ->first();
  496. return $data ? $data->toArray() : [];
  497. }
  498. }