ReInquiryCount.php 4.7 KB
<?php

namespace App\Models\Inquiry;

use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Helper\QuanqiusouApi;
use App\Models\Base;
use App\Models\Domain\DomainInfo;
use App\Models\Project\Project;
use App\Models\Task\TaskOwner;
use App\Services\DingService;
use App\Utils\HttpUtils;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

/**
 * Class ReInquiryCount
 * @package App\Models\Inquiry
 * @author zbj
 * @date 2024/10/25
 */
class ReInquiryCount extends Base
{
    /**
     * @var string
     */
    protected $table = 'gl_re_inquiry_count';

    /**
     * @param $task_id
     * @param $domain
     * @param int $num
     * @author zbj
     * @date 2024/10/26
     */
    public static function addInquiryNum($task_id, $domain, $num = 1, $type = 1, $company = '')
    {
        $model = self::where('domain', $domain)->first();

        if (!$model) {
            if($type == 1) {
                //新增时获取公司名
                $company = '';
                $domain_info = DomainInfo::where('domain', $domain)->first();
                if ($domain_info) {
                    $company = Project::where('id', $domain_info['project_id'])->value('company');
                } else {
                    $res = (new QuanqiusouApi())->getV5Agent($domain);
                    if (!empty($res['status']) && $res['status'] == 200) {
                        $company = $res['company_name'];
                    }
                }
            }
            if($type == 2){
                $company = $domain;
            }
            if($type == 3){
                $fob_list = self::getFobProjects();
                $fob = collect($fob_list)->where('postid', $domain)->first();
                $company = $fob['company'] . ' - ' . $fob['plan'];
            }

            $model = new self();
            $model->domain = $domain;
            $model->company = $company;
            $model->type = $type;
        }
        $model->task_ids = $model->task_ids + [$task_id];
        $model->num = $model->num + $num;
        $model->save();

        //数量首次达到100, 给钉钉推送消息
        if($num > 0 && $model->num == 100){
             (new DingService())->handle([
                'keyword' => '询盘数量通知',
                'msg' =>
                    '项目名称:' . $model->company . PHP_EOL .
                    '项目域名:' . $model->domain . PHP_EOL .
                    '询盘数量:' . $model->num,
                'isAtAll' => false, // 是否@所有人
            ], 'https://oapi.dingtalk.com/robot/send?access_token=cd5733d3e6b810a501e3ea20df7c99ecb616aa6754fa048348837d088c1f5b2c');
        }
    }

    public function setTaskIdsAttribute($value)
    {
        $this->attributes['task_ids'] = Arr::arrToSet($value);
    }

    public function getTaskIdsAttribute($value)
    {
        return Arr::setToArr($value);
    }

    public function getTasksAttribute()
    {
        $cache_key = 'ReInquiryCountTasks_' . Arr::arrToSet($this->task_ids);
        $tasks = Cache::get($cache_key);
        if (!$tasks) {
            $tasks = ReInquiryTask::whereIn('id', $this->task_ids)->select(['title', 'industry', 'target', 'status'])->get()->toArray();
            foreach ($tasks as &$task) {
                $target = collect($task['target'])->where('url', $this->domain)->first();
                $task['is_del'] = $target ? 0 : 1;
                $task['agent'] = $target['agent'] ?? '';
                $task['is_v6'] = $target['is_v6'] ?? '';
                $task['agent_group'] = $target['agent_group'] ?? '';
                unset($task['target']);
            }
            Cache::put($cache_key, $tasks, 7200);
        }
        return $tasks;
    }

    public static function getFobProjects(){
        $cache_key = 'GET_BIND_PROJECT_LIST';
        $res = Cache::get($cache_key);
        if(!$res){
            $res = HttpUtils::get('https://fob.ai.cc/api/get_bind_project_list', []);
            $res = json_decode($res, true)['data'] ?? [];
            Cache::put($cache_key, $res, 120);
        }
        return $res;
    }

    public static function getRemainingDays($domain, $type, $project_id)
    {
        $cache_key = 're_inquiry_count_' . $domain . '_' . date('Ymd');
        $data = Cache::get($cache_key);
        if($data === null){
            if($project_id){
                $data = Project::where('id', $project_id)->value('remain_day');
            }else if($type == 3){
                $data = '-';
            }else{
                $res = (new QuanqiusouApi())->getV5RemainDay($domain);
                $data = $res['data']['remain_day'] ??'-';
            }
            Cache::put($cache_key, $data, 24 * 3600);
        }
        return $data;
    }
}