HelpModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace App\Web\Models;
  3. use App\Models\BaseModel;
  4. use Illuminate\Support\Facades\DB;
  5. class HelpModel extends BaseModel
  6. {
  7. /**
  8. * 状态字段
  9. */
  10. const DELETED_AT = 'status';
  11. /**
  12. * @var string
  13. */
  14. protected $table = 'help';
  15. /**
  16. * 获取文章列表
  17. * */
  18. public function getHelpList($params,$fields='a.*'){
  19. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  20. $where=[];
  21. if(isset($params['status'])){
  22. $where[]=['a.status','=',$params['status']];
  23. }else{
  24. $where[]=['a.status','<',2];
  25. }
  26. if(!empty($params['lt_total_view'])){
  27. $where[]=['a.total_view','<',$params['lt_total_view']];
  28. }
  29. $query= $this->newInstance()->alias('a')->where($where);
  30. if (!empty($params['keyword'])) {
  31. $keyword = $params['keyword'];
  32. $query->where('a.title', 'like', "%" . $keyword . "%");
  33. }
  34. if(!empty($params['sort'])){
  35. if($params['sort']=='sort_ascending'){
  36. $query->orderBy('sort','asc');
  37. }else if($params['sort']=='sort_descending'){
  38. $query->orderBy('sort','desc');
  39. }else if($params['sort']=='pub_date_ascending'){
  40. $query->orderBy('pub_date','asc');
  41. }else if($params['sort']=='pub_date_descending'){
  42. $query->orderBy('pub_date','desc');
  43. }else if($params['sort']=='create_time_ascending'){
  44. $query->orderBy('create_time','asc');
  45. }else if($params['sort']=='create_time_descending'){
  46. $query->orderBy('create_time','desc');
  47. }else if($params['sort']=='update_time_ascending'){
  48. $query->orderBy('update_time','asc');
  49. }else if($params['sort']=='update_time_descending'){
  50. $query->orderBy('update_time','desc');
  51. }
  52. }else{
  53. $query->orderBy('sort');
  54. }
  55. $totalCount = $query->count();
  56. $list= $query->skip($skip)
  57. ->limit($pageSize)
  58. ->selectRaw($fields)
  59. ->get();
  60. if(!empty($list)){
  61. $list=$list->toArray();
  62. }else{
  63. $list=[];
  64. }
  65. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  66. return $result;
  67. }
  68. /**
  69. * 获取文章分类未关联的文章
  70. * */
  71. public function getUnrelatedHelpByTypeIds($params){
  72. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  73. $typeId = $params['type_id'] ?? '';
  74. $keyword = $params['keyword'] ?? '';
  75. $where=[];
  76. if ($keyword) {
  77. $keyword = addslashes($keyword);
  78. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  79. }
  80. $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  81. ->leftJoin('help_type_relation as b', function ($join)use($typeId) {
  82. $join->on('a.id', '=', 'b.help_id')
  83. ->where('b.status', '=', 0)
  84. ->where('b.type_id', '=', $typeId);
  85. })->where('a.status','<',2)->whereNull('b.id')->count();
  86. $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  87. $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  88. ->leftJoin('help_type_relation as b', function ($join)use($typeId) {
  89. $join->on('a.id', '=', 'b.help_id')
  90. ->where('b.status', '=', 0)
  91. ->where('b.type_id', '=', $typeId);
  92. })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
  93. ->limit($pageSize)
  94. ->selectRaw($fields)
  95. ->orderBy('sort')
  96. ->get();
  97. if(!empty($list)){
  98. $list=$list->toArray();
  99. }else{
  100. $list=[];
  101. }
  102. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  103. return $result;
  104. }
  105. /**
  106. * 获取文章标签未关联的文章
  107. * */
  108. public function getUnrelatedHelpByTagIds($params){
  109. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  110. $tagId = $params['tag_id'] ?? '';
  111. $keyword = $params['keyword'] ?? '';
  112. $where=[];
  113. if ($keyword) {
  114. $keyword = addslashes($keyword);
  115. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  116. }
  117. $totalCount= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  118. ->leftJoin('help_tag_relation as b', function ($join)use($tagId) {
  119. $join->on('a.id', '=', 'b.help_id')
  120. ->where('b.status', '=', 0)
  121. ->where('b.tag_id', '=', $tagId);
  122. })->where('a.status','<',2)->whereNull('b.id')->count();
  123. $fields='a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  124. $list= $this->newInstance()->buildQuery($where) ->from($this->getTable().' as a')
  125. ->leftJoin('help_tag_relation as b', function ($join)use($tagId) {
  126. $join->on('a.id', '=', 'b.help_id')
  127. ->where('b.status', '=', 0)
  128. ->where('b.tag_id', '=', $tagId);
  129. })->where('a.status','<',2)->whereNull('b.id')->skip($skip)
  130. ->limit($pageSize)
  131. ->selectRaw($fields)
  132. ->orderBy('sort')
  133. ->get();
  134. if(!empty($list)){
  135. $list=$list->toArray();
  136. }else{
  137. $list=[];
  138. }
  139. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  140. return $result;
  141. }
  142. /**
  143. * 文章保存
  144. * @param array $data
  145. * */
  146. public function saveHelpData($data){
  147. if (!empty($data['id'])) {
  148. $id = $data['id'];
  149. $this->newInstance()->where('id', $data['id'])->update($data);
  150. } else {
  151. $id = $this->newInstance()->insertGetId($data);
  152. }
  153. return $id;
  154. }
  155. /**
  156. * 检查 标签是否唯一
  157. * */
  158. public function checkNameUnique($name,$id=0){
  159. $where=[];
  160. if(!empty($id)){
  161. $where[]=['id', '<>', $id];
  162. }
  163. $where[]=['status', '<', 2];
  164. $where[]=['title', '=', trim($name)];
  165. return $this->checkFieldUnique('title',$where);
  166. }
  167. /**
  168. * 获取文章详情
  169. * @param array $params
  170. * */
  171. public function getHelpInfo($params=[]){
  172. if(empty($params)){
  173. return [];
  174. }
  175. $where=[];
  176. $where[]=['status','<',2];
  177. if(!empty($params['id'])){
  178. $where['id']=$params['id'];
  179. }
  180. $info= $this->newInstance()->where($where)->first();
  181. if(!empty($info)){
  182. return $info->toArray();
  183. }else{
  184. return [];
  185. }
  186. }
  187. /**
  188. * 获取文章基本列表
  189. * */
  190. public function getPublishHelpList($params,$fields='a.*'){
  191. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  192. $where=[];
  193. $where['a.status']=0;
  194. $query= $this->newInstance()->alias('a')
  195. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  196. ->where($where);
  197. if(!empty($params['sort'])){
  198. if(!empty($params['sort']['is_hot'])){
  199. $query=$query->orderBy('a.total_view','desc');
  200. }
  201. if(!empty($params['sort']['pub_date'])){
  202. $query=$query->orderBy('a.pub_date','desc');
  203. }
  204. }
  205. if (!empty($params['keyword'])) {
  206. $keyword = $params['keyword'];
  207. $query=$query->where(function ($queryStr) use ($keyword) {
  208. $queryStr->where('a.title', 'like', "%" . $keyword . "%")
  209. ->orWhere('a.description', 'like', '%' . $keyword . '%');
  210. });
  211. }
  212. if(!empty($params['only_List'])){
  213. $list= $query->orderBy('a.id','desc')->selectRaw($fields)->limit($pageSize)->get()->toArray();
  214. }else{
  215. $list= $query->orderBy('a.id','desc')->selectRaw($fields)->paginate($pageSize)->toArray();;
  216. }
  217. if(empty($list)){
  218. $list=[];
  219. }
  220. return $list;
  221. }
  222. /**
  223. * 根据类型ids获取个类型文章
  224. * */
  225. public function getRenderListByTypeIds($typeIds,$params,$fields='a.*'){
  226. $where=[];
  227. $where['a.status']=0;
  228. $where['b.status']=0;
  229. if(!empty($params['plate_id'])){
  230. $where['a.plate_id']=$params['plate_id'];
  231. }
  232. $pageSize = $this->getPageSize($params);
  233. $sql = '';
  234. foreach ($typeIds as $item){
  235. $sqlStr= $this->newInstance()->alias('a')
  236. ->leftJoin('help_type_relation as b', 'a.id', '=', 'b.help_id')
  237. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  238. ->where($where)
  239. ->where('b.type_id','=',DB::raw($item))
  240. ->orderBy('a.pub_date','desc')
  241. ->orderBy('a.id','desc')
  242. ->limit($pageSize)->selectRaw($fields);
  243. if (empty($sql)) {
  244. $sql = $sqlStr;
  245. } else {
  246. $sql->unionAll($sqlStr);
  247. }
  248. }
  249. $list=[];
  250. if(!empty($sql)){
  251. $list = $sql->get();
  252. if($list){
  253. $list=$list->toArray();
  254. }
  255. }
  256. return $list;
  257. }
  258. /**
  259. * 根据类型id获取文章
  260. * */
  261. public function getPublishHelpListByTypeId($params,$fields='a.*'){
  262. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  263. $where=[];
  264. $where['a.status']=0;
  265. if(!empty($params['type_id'])){
  266. $where['b.type_id']= $params['type_id'];
  267. $where['b.status']=0;
  268. }
  269. $list = $this->newInstance()->alias('a')->selectRaw($fields)
  270. ->leftJoin('help_type_relation as b', 'a.id', '=', 'b.help_id')
  271. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  272. ->where($where)
  273. ->groupBy('a.id')
  274. ->orderBy('a.pub_date','desc')
  275. ->orderBy('a.id','desc')
  276. ->paginate($pageSize)
  277. ->toArray();
  278. return $list;
  279. }
  280. /**
  281. * 根据标签id获取文章
  282. * */
  283. public function getPublishHelpListByTagId($params,$fields='a.*'){
  284. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  285. $where=[];
  286. $where['a.status']=0;
  287. if(!empty($params['tag_id'])){
  288. $where['b.tag_id']= $params['tag_id'];
  289. $where['b.status']=0;
  290. }
  291. if (!empty($params['plate_id'])) {
  292. $where['a.plate_id'] = $params['plate_id'];
  293. }
  294. $totalCount= $this->newInstance()->alias('a')
  295. ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
  296. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  297. ->where($where)->count();
  298. $list= $this->newInstance()->alias('a')
  299. ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
  300. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  301. ->where($where)
  302. ->orderBy('a.pub_date','desc')
  303. ->orderBy('a.id','desc')
  304. ->limit($pageSize)->selectRaw($fields)->get();
  305. if($list){
  306. $list=$list->toArray();
  307. }else{
  308. $list=[];
  309. }
  310. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  311. return $result;
  312. }
  313. /**
  314. * 根据类型ids获取标签文章
  315. * */
  316. public function getRenderListByTagIds($tagIds,$params,$fields='a.*'){
  317. $where=[];
  318. $where['a.status']=0;
  319. $where['b.status']=0;
  320. if(!empty($params['plate_id'])){
  321. $where['a.plate_id']=$params['plate_id'];
  322. }
  323. $pageSize = $this->getPageSize($params);
  324. $sql = '';
  325. foreach ($tagIds as $item){
  326. $sqlStr= $this->newInstance()->alias('a')
  327. ->leftJoin('help_tag_relation as b', 'a.id', '=', 'b.help_id')
  328. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  329. ->where($where)
  330. ->where('b.tag_id','=',DB::raw($item))
  331. ->orderBy('a.pub_date','desc')
  332. ->orderBy('a.id','desc')
  333. ->limit($pageSize)->selectRaw($fields);
  334. if (empty($sql)) {
  335. $sql = $sqlStr;
  336. } else {
  337. $sql->unionAll($sqlStr);
  338. }
  339. }
  340. $list=[];
  341. if(!empty($sql)){
  342. $list = $sql->get();
  343. if($list){
  344. $list=$list->toArray();
  345. }
  346. }
  347. return $list;
  348. }
  349. public function updatePv($id){
  350. DB::update('update help set votes = 1');
  351. }
  352. }