ProjectAssociation.php 2.0 KB
<?php

namespace App\Models\ProjectAssociation;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class ProjectAssociation extends Model
{
    protected $table = 'gl_project_association';

    /**
     * 保存|修改数据
     * @param array $data
     * @return bool
     */
    public function saveData(array $data): bool
    {
        # V6项目ID
        $project_id = (int)$data['project_id'] ?? 0;
        $isRes      = self::query()->where('project_id', $project_id)->first();
        if (!$isRes) {
            $isRes             = new self();
            $isRes->project_id = $project_id;
        }
        # AICC朋友ID
        $isRes->friend_id = $data['friend_id'] ?? 0;
        # AICC朋友昵称
        $isRes->nickname = $data['nickname'] ?? 0;
        # AICC用户ID
        $isRes->user_id = $data['user_id'] ?? 0;
        # AICC用户姓名
        $isRes->user_name = $data['user_name'] ?? '';
        # AICC朋友头像
        $isRes->image = $data['image'] ?? '';
        return $isRes->save();
    }

    /**
     * 检查项目是否存在
     * @param $project_id
     * @return Builder|Model|object|null
     */
    public function check($project_id)
    {
        return self::query()->where('project_id', $project_id)->first();
    }

    /**
     * @param int $page
     * @param int $perPage
     * @return array
     */
    public function allData(int $page = 1, int $perPage = 15)
    {
        $status      = 1; # 1 - 正常, 0 - 禁用
        $lists       = self::query()->where('status', $status)
                           ->where('project_id', '!=', 0)
                           ->where('friend_id', '!=', 0)
                           ->where('user_id', '!=', 0)
                           ->paginate($perPage, ['project_id', 'friend_id', 'user_id'], 'page', $page);
        $items       = $lists->Items();
        $totalPage   = $lists->lastPage();
        $total       = $lists->total();
        $currentPage = $lists->currentPage();
        return compact('total', 'items', 'totalPage', 'currentPage');
    }
}