WebSeoModel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Web\Models;
  3. use App\Models\BaseModel;
  4. use App\Web\Services\WebService;
  5. use Illuminate\Support\Facades\DB;
  6. class WebSeoModel extends BaseModel
  7. {
  8. /**
  9. * 状态字段
  10. */
  11. const DELETED_AT = 'status';
  12. /**
  13. * @var string
  14. */
  15. protected $table = 'web_seo';
  16. /**
  17. * 检查Urla是否唯一
  18. * */
  19. public function checkUrlaUnique($urla,$id=0){
  20. if(empty($urla)){
  21. return true;
  22. }else{
  23. $where=[];
  24. if(!empty($id)){
  25. $where[]=['id', '<>', $id];
  26. }
  27. $where[]=['status', '<', 2];
  28. $where[]=['urla', '=', trim($urla)];
  29. return $this->checkFieldUnique('urla',$where);
  30. }
  31. }
  32. /**
  33. * Seo优化保存
  34. * @param array $data
  35. * */
  36. public function saveSeoData($data){
  37. if (!empty($data['id'])) {
  38. $id = $data['id'];
  39. $this->newInstance()->where('id', $data['id'])->update($data);
  40. } else {
  41. $id = $this->newInstance()->insertGetId($data);
  42. }
  43. return $id;
  44. }
  45. /**
  46. * 根据关联关系删除Seo
  47. * */
  48. public function delSeoByRelation($params){
  49. $ret=false;
  50. if(!empty($params['relation_id'])&&!empty($params['relation_table'])){
  51. $ret= $this->newInstance()
  52. ->where('relation_id', $params['relation_id'])
  53. ->where('relation_table', $params['relation_table'])
  54. ->update(['status'=>2,'update_time'=>nowTime()]);
  55. }
  56. return $ret;
  57. }
  58. /**
  59. * 根际id列表获取数据列表
  60. * */
  61. public function getSeoListByIds($ids){
  62. $list=$this->newInstance()
  63. ->where('status','=',0)
  64. ->whereIn('id',$ids)->get();
  65. if(!empty($list)){
  66. $list=$list->toArray();
  67. }else{
  68. $list=[];
  69. }
  70. return $list;
  71. }
  72. /**
  73. * 获取Seo详情
  74. * @param array $params
  75. * */
  76. public function getSeoInfo($params=[]){
  77. if(empty($params)){
  78. return [];
  79. }
  80. $where=[];
  81. $where[]=['status','<',2];
  82. if(!empty($params['id'])){
  83. $where['id']=$params['id'];
  84. }
  85. if(!empty($params['urla'])){
  86. $where['urla']=$params['urla'];
  87. }
  88. $info= $this->newInstance()->where($where)->first();
  89. if(!empty($info)){
  90. return $info->toArray();
  91. }else{
  92. return [];
  93. }
  94. }
  95. public function getSeoList($params=[],$fields='a.*'){
  96. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  97. $where=[];
  98. if(isset($params['status'])){
  99. $where[]=['a.status','=',$params['status']];
  100. }else{
  101. $where[]=['a.status','<',2];
  102. }
  103. $query= $this->newInstance()->alias('a')->where($where);
  104. if (!empty($params['keyword'])) {
  105. $keyword = $params['keyword'];
  106. $query->where('a.seo_title', 'like', "%" . $keyword . "%");
  107. }
  108. $totalCount = $query->count();
  109. $list= $query->skip($skip)
  110. ->limit($pageSize)
  111. ->selectRaw($fields)
  112. ->get();
  113. if(!empty($list)){
  114. $list=$list->toArray();
  115. }else{
  116. $list=[];
  117. }
  118. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  119. return $result;
  120. }
  121. public function checkUrlaIsExits($urlas)
  122. {
  123. $where=[];
  124. if(!empty($id)){
  125. $where[]=['id', '<>', $id];
  126. }
  127. $where[]=['status', '<', 2];
  128. $where['urla'] = [['in', $urlas]];
  129. return $this->newInstance()->buildQuery($where)->selectRaw('id,urla')->get()->toArray();
  130. }
  131. public function updateSeoInfo($urla, $newUrla, $seoTitle, $seoKeyword, $seoDes, $relationTable = 'web_static_page')
  132. {
  133. $update = [];
  134. if (!empty($newUrla)) {
  135. $update['urla'] = $newUrla;
  136. }
  137. if (!empty($seoTitle)) {
  138. $update['seo_title'] = $seoTitle;
  139. }
  140. if (!empty($seoKeyword)) {
  141. $update['seo_keyword'] = $seoKeyword;
  142. }
  143. if (!empty($seoDes)) {
  144. $update['seo_describe'] = $seoDes;
  145. }
  146. if(!empty($update)) {
  147. return $this->newInstance()
  148. ->where('urla', $urla)
  149. //->where('relation_table', $relationTable)
  150. ->update($update);
  151. }
  152. }
  153. public function updateTransStatus($id,$transStatus) {
  154. $update = [];
  155. if (!empty($transStatus)) {
  156. $update['trans_status'] = $transStatus;
  157. }
  158. if(!empty($update)) {
  159. return $this->newInstance()
  160. ->where('id', $id)
  161. ->update($update);
  162. }
  163. return $id;
  164. }
  165. }