Files
wh-api/app/Repository/Company/CompanyRepository.php
ykxiao 0b2299c427
Some checks failed
Build Docker / build (push) Has been cancelled
协程版仓库后端项目
2025-07-08 14:59:47 +08:00

87 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Author: ykxiao
* Date: 2025/6/5
* Time: 下午6:00
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* 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\CompanyDao;
use App\Repository\AbstractRepository;
use Exception;
use function Hyperf\Collection\collect;
/**
* Author: ykxiao
* Date: 2025/6/5
* Time: 下午6:01
* Description: CompanyRepository类用于提供公司相关的数据访问和操作。它继承自AbstractRepository类并实现了CompanyDao接口。
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
class CompanyRepository extends AbstractRepository
{
public function __construct(CompanyDao $dao)
{
$this->dao = $dao;
}
/**
* 添加公司。
* @param array $data
* @return void
* @throws Exception
*/
public function add(array $data): void
{
$count = $this->dao->builder();
if (!empty($data['id'])) {
$count->where('id', '!=', $data['id']);
}
// 重复验证邮箱和手机号码
$make = $this->dao->make();
$verify = $make->verifyData(clone $count);
$collect = clone collect($verify->get());
$errors = [];
if ($collect->where('name', '=', $data['name'])->isNotEmpty()) {
$errors[] = '公司名称已存在';
}
if (!empty($errors)) {
throw new Exception(implode(',', $errors));
}
$this->dao->commonCreate($this->paramsData($data));
}
/**
* 处理参数数据。
* @param array $params
* @return array
*/
private function paramsData(array $params): array
{
if (empty($params['id'])) {
return $params;
}
return [
'id' => $params['id'],
'company_type' => $params['company_type'], // 公司类别1开证 2收货
'name' => $params['name'], // 公司名称
'status' => $params['status'] // 状态0禁用 1启用
];
}
}