BannerService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. foreach ($list as $value){
  33. $value->miniprogram_type_text = $types[$value->miniprogram_type]['name'] ?? "-";
  34. }
  35. }
  36. return $list;
  37. }
  38. public static function getMiniProgramType(){
  39. return [
  40. ['value' => 1,'name' =>"微信小程序"],
  41. ['value' => 2,'name' =>"抖音小程序"],
  42. ];
  43. }
  44. /**
  45. * 查询构建
  46. * name: getQuery
  47. * @param array $param
  48. * date 2023/06/07 14:41
  49. */
  50. private static function getQuerySql(array $param)
  51. {
  52. $sql = DuanJuBanner::query();
  53. if (getProp($param,'title')){
  54. $sql->where("title","like","%{$param['title']}%");
  55. }
  56. return $sql;
  57. }
  58. /**
  59. * 添加轮播图
  60. * name: addBanner
  61. * @param array $param
  62. * date 2023/06/07 15:53
  63. */
  64. public static function addBanner(array $param)
  65. {
  66. $res = DuanJuBanner::create($param);
  67. if ($res){
  68. return "操作成功";
  69. }
  70. self::throwErrMsg("添加失败");
  71. }
  72. /**
  73. * 更新banner
  74. * name: updateBanner
  75. * @param $id
  76. * @param $param
  77. * date 2023/06/08 09:55
  78. */
  79. public static function updateBanner($id, $param)
  80. {
  81. // 禁用检测
  82. if( $param['status'] != 1){
  83. $type = getProp($param,'miniprogram_type');
  84. if (empty($type)){
  85. $type = DuanJuBanner::where('id',$id)->value('miniprogram_type');
  86. }
  87. $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$type)->where('status',1)->value('id');
  88. if (empty($other)){
  89. self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
  90. }
  91. }
  92. $res = DuanJuBanner::where('id',$id)->update($param);
  93. if ($res){
  94. return "操作成功";
  95. }
  96. self::throwErrMsg("操作失败");
  97. }
  98. /*
  99. * 删除
  100. */
  101. public static function delBannerById($id)
  102. {
  103. $info = DuanJuBanner::where('id',$id)->first();
  104. if (is_empty($info)){
  105. return "操作成功";
  106. }
  107. $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$info->miniprogram_type)->where('status',1)->value('id');
  108. if (empty($other)){
  109. self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
  110. }
  111. DuanJuBanner::where('id',$id)->delete();
  112. return "操作成功";
  113. }
  114. }