ProjectAssociationLogic.php 3.4 KB
<?php

namespace App\Http\Logic\Aside\ProjectAssociation;

use App\Enums\Common\Code;
use App\Http\Logic\Logic;
use App\Models\ProjectAssociation\ProjectAssociation;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ProjectAssociationLogic extends Logic
{
    public function saveWeChatData($data)
    {
        $wx = new ProjectAssociation();
        DB::beginTransaction();
        try {
            $status = $wx->saveData($data);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            $e->getMessage();
            errorLog('V6与AICC关联失败', $wx, $e);
            $this->fail('请检查操作是否正确!', Code::SERVER_MYSQL_ERROR);
        }
        return $status;
    }

    /**
     * status - 正常
     * @param $project_id
     * @return ProjectAssociation|Builder|Model|object|null
     */
    public function normal($project_id)
    {
        return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_NORMAL)->first();
    }


    /**
     * status - 禁用
     * @param $project_id
     * @return ProjectAssociation|Builder|Model|object|null
     */
    public function disabled($project_id)
    {
        return ProjectAssociation::query()->whereProjectId($project_id)->whereStatus(ProjectAssociation::STATUS_DISABLED)->first();
    }

    /**
     * 初始化数据/修改数据
     * @param int $project_id
     * @param int $status
     * @return bool
     */
    public function saveProject($project_id, $status)
    {
        $bool = false;
        DB::beginTransaction();
        $isRes = $this->disabled($project_id);
        if (is_null($isRes)) {
            $isRes = new ProjectAssociation();
        }
        $isRes->project_id = $project_id;
        $isRes->user_id    = (int)env('AICC_WECHAT_USER_ID');
        $isRes->status     = $status;
        try {
            $bool = $isRes->save();
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
        }
        return $bool;
    }

    /**
     * 获取AICC微信列表数据
     * @param ProjectAssociation $res
     * @param bool $cache
     * @return mixed
     */
    public function getAiccWechatLists($res, $cache = false)
    {
        $redis_key = 'aicc_friend_lists_' . (int)env('AICC_WECHAT_USER_ID');
        $result    = $cache ? false : redis_get($redis_key);
        if (empty($result)) {
            $url    = env('AICC_URL') . env('AICC_WECHAT_FRIEND_API_URL');
            $result = curlGet($url);
            redis_set($redis_key, json_encode($result), 60);
        } else {
            $result = json_decode($result, true);
        }
        $result['info'] = [
            'friend_id' => $res->friend_id ?? 0,
            'nickname'  => $res->nickname ?? '',
            'user_name' => $res->user_name ?? '',
            'image'     => $res->image ?? '',
        ];
        return $result;
    }

    /**
     * 关闭状态
     * @param ProjectAssociation $res
     * @param int $status 1 - 正常, 0 - 禁用
     * @return bool
     */
    public function closedState($res, $status)
    {
        DB::beginTransaction();
        $bool = false;
        try {
            $res->status = $status;
            $bool = $res->save();
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
        }
        return $bool;
    }
}