Channel.php 1.8 KB
<?php

namespace App\Models\Channel;

use Illuminate\Database\Eloquent\Model;
use phpDocumentor\Reflection\Types\Self_;

/**
 * Class Channel
 * @package App\Models
 * @author zbj
 * @date 2023/6/27
 */
class Channel extends Model
{
    //设置关联表名
    protected $table = 'gl_channel';


    public static function sync($data, $zone_id){
        $channel = self::where('source_id', $data['id'])->first();
        if(!$channel){
            $channel = new self();
        }
        $channel->zone_id = $zone_id;
        $channel->title = $data['company_name'];
        $channel->alias = $data['company_alias'];
        $channel->province = $data['province'];
        $channel->contact_name = $data['contact_name'];
        $channel->contact_mobile = $data['contact_mobile'];
        $channel->source_id = $data['id'];
        $channel->save();
        return $channel;
    }

    public static function getProjectChannel($source_id, $sales){
        $channel = self::where('source_id', $source_id)->first();
        if(!$channel){
            return [];
        }
        $userModel = new User();
        $user = $userModel->read(['name'=>$sales]);
        if(!$user){
            $data = [
                'channel_id' => $channel['id'] ?? 0,
                'name'=>$sales
            ];
            $user['id'] = $userModel->addReturnId($data);
        }
        return [
            'zone_id' => $channel['zone_id'] ?? 0,
            'channel_id' => $channel['id'] ?? 0,
            'user_id' => $user['id'] ?? 0,
        ];
    }

    public static function getChannelText($sales_id){
        $user = User::where('id', $sales_id)->select(['name', 'channel_id'])->first();
        if(!$user){
            return $sales_id;
        }
        $channel_alias = self::where('id', $user['channel_id'])->value('alias');
        return $channel_alias . '-' . $user['name'];
    }
}