ProjectAssociation.php 1.8 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
    {
        $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;
        }
        $isRes->wx_id        = $data['wx_id'];
        $isRes->wx_nickname  = $data['wx_nickname'];
        $isRes->wx_user_id   = $data['wx_user_id'];
        $isRes->wx_user_name = $data['wx_user_name'];
        $isRes->wx_image     = $data['wx_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)
                           ->whereNotNull('project_id')
                           ->whereNotNull('wx_user_id')
                           ->paginate($perPage, ['project_id', 'wx_id', 'wx_user_id'], 'page', $page);
        $items       = $lists->Items();
        $totalPage   = $lists->lastPage();
        $total       = $lists->total();
        $currentPage = $lists->currentPage();
        return compact('total', 'items', 'totalPage', 'currentPage');
    }
}