<?php
/**
 *
 * @file:BannerService.php
 * @Date: 2023/6/7
 * @Time: 14:29
 */


namespace Modules\Operation\Service;

use Illuminate\Support\Facades\DB;
use Modules\Common\Services\BaseService;
use Modules\Operation\Models\DuanJuBanner;

class BannerService extends BaseService
{

    /**
     *  轮播图列表
     * name: getBannerLists
     * @param array $param
     * date 2023/06/07 14:40
     */
    public static function getBannerLists(array $param)
    {
        $isAll = getProp($param,'is_all',false);
        $list = self::getQuerySql($param)->orderBy('sort','desc')->orderBy('status','desc')->orderBy('id','desc');

        if ($isAll){
             $list =  $list->get();
        }else{
           $list = $list->paginate(getProp($param,'limit',15));
        }
        if (!$list->isEmpty()){
            $types = self::getMiniProgramType();
            $types = array_column($types,null,'value');
            $videoIds = array_unique(array_column($list->items(),'video_id'));
            $videoInfo = DB::table('videos')->whereIn('id',$videoIds)->select('id','name','cover_image')->get();

            if ($videoInfo){
                $videoInfo  = json_decode(json_encode($videoInfo),true);

                $videoInfo = array_column($videoInfo,null,'id');
            }else{
                $videoInfo = [];
            }

            foreach ($list as $value){
                $value->miniprogram_type_text = $types[$value->miniprogram_type]['name'] ?? "-";
                $value->videoInfo = $videoInfo[$value->video_id] ?? [];
            }
        }
        return $list;
    }

    /**
     *  查询构建
     * name: getQuery
     * @param array $param
     * date 2023/06/07 14:41
     */
    private static function getQuerySql(array $param)
    {
        $sql = DuanJuBanner::query();

        if (getProp($param,'title')){
            $sql->where("title","like","%{$param['title']}%");
        }
        if (getProp($param,'miniprogram_type')){
            $sql->where("miniprogram_type",$param['miniprogram_type']);
        }
        return $sql;
    }

    /**
     *  添加轮播图
     * name: addBanner
     * @param array $param
     * date 2023/06/07 15:53
     */
    public static function addBanner(array $param)
    {

        $res = DuanJuBanner::create($param);
        if ($res){
            return "操作成功";
        }
        self::throwErrMsg("添加失败");
    }

    /**
     *  更新banner
     * name: updateBanner
     * @param $id
     * @param $param
     * date 2023/06/08 09:55
     */
    public static function updateBanner($id, $param)
    {
        // 禁用检测
       if( $param['status'] != 1){
           $type = getProp($param,'miniprogram_type');
           if (empty($type)){
               $type = DuanJuBanner::where('id',$id)->value('miniprogram_type');
           }
           $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$type)->where('status',1)->value('id');
           if (empty($other)){
               self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
           }
       }

       $res = DuanJuBanner::where('id',$id)->update($param);
       if ($res){
           return "操作成功";
       }
        self::throwErrMsg("操作失败");
    }

    /*
     * 删除
     */
    public static function delBannerById($id)
    {
        $info = DuanJuBanner::where('id',$id)->first();
       if (is_empty($info)){
           return  "操作成功";
       }

        $other = DuanJuBanner::where('id','<>',$id)->where('miniprogram_type',$info->miniprogram_type)->where('status',1)->value('id');
        if (empty($other)){
            self::throwErrMsg("此类型小程序应最少保障一张可用轮播图");
        }
        DuanJuBanner::where('id',$id)->delete();
        return "操作成功";
    }
}
