BannerService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. *
  4. * @file:BannerService.php
  5. * @Date: 2023/6/7
  6. * @Time: 14:29
  7. */
  8. namespace Modules\Operation\Service;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Common\Services\BaseService;
  11. use Modules\Operation\Models\DuanJuBanner;
  12. class BannerService extends BaseService
  13. {
  14. /**
  15. * 轮播图列表
  16. * name: getBannerLists
  17. * @param array $param
  18. * date 2023/06/07 14:40
  19. */
  20. public static function getBannerLists(array $param)
  21. {
  22. $isAll = getProp($param,'is_all',false);
  23. $list = self::getQuerySql($param)->orderBy('sort','desc')->orderBy('status','desc')->orderBy('id','desc');
  24. if ($isAll){
  25. $list = $list->get();
  26. }else{
  27. $list = $list->paginate(getProp($param,'limit',15));
  28. }
  29. if (!$list->isEmpty()){
  30. $types = self::getMiniProgramType();
  31. $types = array_column($types,null,'value');
  32. $videoIds = array_unique(array_column($list->items(),'video_id'));
  33. $videoInfo = DB::table('videos')->whereIn('id',$videoIds)->select('id','name','cover_image')->get();
  34. if ($videoInfo){
  35. $videoInfo = json_decode(json_encode($videoInfo),true);
  36. $videoInfo = array_column($videoInfo,null,'id');
  37. }else{
  38. $videoInfo = [];
  39. }
  40. foreach ($list as $value){
  41. $value->miniprogram_type_text = $types[$value->miniprogram_type]['name'] ?? "-";
  42. $value->videoInfo = $videoInfo[$value->video_id] ?? [];
  43. }
  44. }
  45. return $list;
  46. }
  47. /**
  48. * 查询构建
  49. * name: getQuery
  50. * @param array $param
  51. * date 2023/06/07 14:41
  52. */
  53. private static function getQuerySql(array $param)
  54. {
  55. $sql = DuanJuBanner::query();
  56. if (getProp($param,'title')){
  57. $sql->where("title","like","%{$param['title']}%");
  58. }
  59. if (getProp($param,'miniprogram_type')){
  60. $sql->where("miniprogram_type",$param['miniprogram_type']);
  61. }
  62. return $sql;
  63. }
  64. /**
  65. * 添加轮播图
  66. * name: addBanner
  67. * @param array $param
  68. * date 2023/06/07 15:53
  69. */
  70. public static function addBanner(array $param)
  71. {
  72. $res = DuanJuBanner::create($param);
  73. if ($res){
  74. return "操作成功";
  75. }
  76. self::throwErrMsg("添加失败");
  77. }
  78. /**
  79. * 更新banner
  80. * name: updateBanner
  81. * @param $id
  82. * @param $param
  83. * date 2023/06/08 09:55
  84. */
  85. public static function updateBanner($id, $param)
  86. {
  87. // 禁用检测
  88. if( $param['status'] != 1){
  89. $type = getProp($param,'miniprogram_type');
  90. if (empty($type)){
  91. $type = DuanJuBanner::where('id',$id)->value('miniprogram_type');
  92. }
  93. $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$type)->where('status',1)->value('id');
  94. if (empty($other)){
  95. self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
  96. }
  97. }
  98. $res = DuanJuBanner::where('id',$id)->update($param);
  99. if ($res){
  100. return "操作成功";
  101. }
  102. self::throwErrMsg("操作失败");
  103. }
  104. /*
  105. * 删除
  106. */
  107. public static function delBannerById($id)
  108. {
  109. $info = DuanJuBanner::where('id',$id)->first();
  110. if (is_empty($info)){
  111. return "操作成功";
  112. }
  113. $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$info->miniprogram_type)->where('status',1)->value('id');
  114. if (empty($other)){
  115. self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
  116. }
  117. DuanJuBanner::where('id',$id)->delete();
  118. return "操作成功";
  119. }
  120. }