* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ declare(strict_types=1); namespace App\Repository\Company; use App\Dao\Company\FirstCompanyDao; use App\Exception\ApiException; use App\Repository\AbstractRepository; use Carbon\Carbon; use Exception; use function Hyperf\Collection\collect; /** * Author: ykxiao * Date: 2025/6/5 * Time: 下午9:26 * Description: 平台公司数据仓库类. * * (c) ykxiao * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ class FirstCompanyRepository extends AbstractRepository { public function __construct(FirstCompanyDao $dao) { $this->dao = $dao; } /** * 新增公司. * @throws Exception */ public function addCompany(array $data): void { $count = $this->dao->builder(); // 重复字段验证 $make = $this->dao->make(); $verify = $make->verifyData(clone $count, $data); $collect = clone collect($verify->get()); $errors = []; if ($collect->where('name', '=', $data['name'])->isNotEmpty()) { $errors[] = '公司简称已存在'; } if ($collect->where('full_name', '=', $data['full_name'])->isNotEmpty()) { $errors[] = '公司全称已存在'; } if ($collect->where('mobile', '=', $data['mobile'])->isNotEmpty()) { $errors[] = '负责人手机已存在'; } if (!empty($errors)) { throw new Exception(implode(',', $errors)); } $this->dao->commonCreate($this->paramsData($data)); } /** * 参数处理. */ private function paramsData(array $params): array { if (empty($params['id'])) { return $params; } return [ 'id' => $params['id'], 'domain' => $params['domain'], 'name' => $params['name'], 'full_name' => $params['full_name'], 'company_type' => $params['company_type'], 'address' => $params['address'] ?? '', 'logo' => $params['logo'] ?? '', 'owner' => $params['owner'] ?? '', // 公司负责人 'id_card' => $params['id_card'] ?? '', // 法人身份证 'mobile' => $params['mobile'], 'org_code' => $params['org_code'] ?? '', 'remark' => $params['remark'] ?? '', 'active_status' => $params['active_status'], 'activation_date' => Carbon::parse($params['activation_date'])->timestamp, ]; } /** * 根据公司全称获取公司信息. * @param string $fullName * @param bool $isVerify * @return array|null */ public function getCompanyByFullName(string $fullName, bool $isVerify = true): ?array { $fields = $this->dao->getFields(); $query = $this->dao->builder()->select($fields)->where('full_name', '=', $fullName); $company = $query->first(); if (!$company && $isVerify) { throw new ApiException('公司不存在'); } return $company?->toArray(); } /** * 根据公司ID查询公司信息. */ public function getCompanyById(int $companyId, bool $isVerify = true): array { $fields = $this->dao->getFields(); $query = $this->dao->builder()->select($fields)->where('id', $companyId); $company = $query->first()?->toArray(); if (!$company && $isVerify) { throw new ApiException('公司不存在'); } return $company ?? []; } /** * 获取公司列表. * @param int|string $page * @param int|string $pageSize * @param array $params * @return array * @throws Exception */ public function getList(int|string $page, int|string $pageSize, array $params = []): array { $fields = $this->dao->getFields(); $query = $this->dao->builder()->select($fields); // 构建查询条件 $this->dao->buildWhere($query, $params); return $this->dao->paginate($query, compact('page', 'pageSize')); } /** * 获取公司信息. * @param int|string $companyId * @return array|null * @throws Exception */ public function companyInfo(int|string $companyId): ?array { $query = $this->dao->builder() ->where('id', '=', $companyId); if (!$query->exists()) { throw new ApiException('公司不存在'); } return $query->first()?->toArray(); } }