<?php
/**
 *
 * @file:Channel.php
 * @Date: 2023/6/8
 * @Time: 11:17
 */


namespace Modules\Operation\Service;

use Modules\Common\Services\BaseService;
use Modules\Operation\Models\NavPages;

class ChannelServic extends BaseService
{

    public static function ChannelList($param)
    {
        $sql = self::getQuerySql($param);
        $isAll = getProp($param,'is_all',false);
        if ($isAll){
            $list = $sql->get();
        }else{
            $list = $sql->paginate(getProp($param,'limit',15));
        }
        if (!$list->isEmpty()){
            $type = array_column(self::getNavPagesType(),null,'value');
            $minParamType = array_column(self::getMiniProgramType(),null,'value');
            foreach ($list as  $value){
                $value->type_txt = $type[$value->type]['name'] ?? "-";
                $value->miniprogram_type_text = $minParamType[$value->miniprogram_type]['name'] ?? "-";
                $value->duanjus = collect($value->duanjus)->sortBy('sort')->toArray();
            }
        }
        return $list;
    }

    public static function getQuerySql($param)
    {
        $sql = NavPages::query()->where('is_del','=',0)->orderBy('status','desc')->orderBy('id','desc');
        if (getProp($param,'type')){
            $sql->where('type',$param['type']);
        }
        if (getProp($param,'miniprogram_type')){
            $sql->where("miniprogram_type",$param['miniprogram_type']);
        }
        return $sql;
    }

    /**
     *  添加
     * name: addChannel
     * @param mixed $param
     * @return string|void
     * date 2023/06/08 15:11
     */
    public static function addChannel(mixed $param)
    {
        $res = NavPages::create($param);
        if ($res){
            if ($param['status'] == 1){
                NavPages::where('id','<>',$res->id)->where('type',$param['type'])->where('miniprogram_type',$param['miniprogram_type'])->update(['status' => 0]);
            }
            return "操作成功";
        }
        self::throwErrMsg("添加失败");
    }

    // 启用
    public static function enableChannel($id)
    {
        $info = NavPages::where('id',$id)->first();
        if(is_empty($info)){
            self::throwErrMsg("频道配置不存在");
        }
        NavPages::where('type',$info->type)->where('miniprogram_type',$info->miniprogram_type)->update(['status'=>0]);
        NavPages::where('id',$id)->update(['status'=>1]);
        return "操作成功";
    }
    //  保存配置
    public static function setChannel($id, array $set)
    {
        $info = NavPages::where('id',$id)->first();
        if(is_empty($info)){
            self::throwErrMsg("频道配置不存在");
        }
        $res = NavPages::where('id',$id)->update(['duanjus' => $set]);
        if ($res){
            return "操作成功";
        }
        self::throwErrMsg("操作失败");
    }

    // 删除
    public static function delChannel($id)
    {
        $info = NavPages::where('id',$id)->where('is_del',0)->first();
        if(is_empty($info)){
            self::throwErrMsg("频道配置不存在");
        }
        if ($info->status == 1){
            self::throwErrMsg("请先禁用");
        }
        $res = NavPages::where('id',$id)->update(['is_del' =>1,'del_at' => get_date(),'status' => 0]);
        if ($res){
            return "操作成功";
        }
        self::throwErrMsg("操作失败");
    }
}
