<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2018/3/13
 * Time: 11:31
 */

namespace App\Modules\Channel\Services;
use App\Modules\Channel\Models\ChannelSubscribeSetting;

class ChannelSubscribeSettingService
{
    /**
     * @param $distribution_channel_id
     * @return mixed
     */
    public static  function getSubscribeTypeByChannelId($distribution_channel_id){
        return ChannelSubscribeSetting::where('distribution_channel_id',$distribution_channel_id)->select('force_subscribe_type','resource_url')->first();
    }

    public static  function getSubscribeTypeByAppId($appid){
        return ChannelSubscribeSetting::where('appid',$appid)->select('id','force_subscribe_type','resource_url')->first();
    }

    /**
     * @param array $data
     * @return bool
     */
    public static function updateOrSettingForceSubScribe(array $data){
        if(empty($data['distribution_channel_id']) || empty($data['force_subscribe_type']) || empty($data['appid'])){
            return false;
        }
        $old = self::getSubscribeTypeByAppId($data['appid']);
        if($old){
            $old->force_subscribe_type = $data['force_subscribe_type'];
            if(isset($data['resource_url'])){
                $old->resource_url = $data['resource_url'];
            }
            $old->save();
            return true;
        }else{
            ChannelSubscribeSetting::create($data);
            return true;
        }
    }
}