167 lines
5.8 KiB
PHP
167 lines
5.8 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | 后台 · 企油通企业模型
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types=1);
|
|
|
|
namespace app\store\model\qyt;
|
|
|
|
use app\common\model\qyt\Enterprise as EnterpriseModel;
|
|
use think\facade\Db;
|
|
|
|
class Enterprise extends EnterpriseModel
|
|
{
|
|
/**
|
|
* 列表
|
|
*/
|
|
public function getList(array $param = [])
|
|
{
|
|
$filter = [];
|
|
if (!empty($param['name'])) {
|
|
$filter[] = ['company_name|business_license', 'like', '%' . trim($param['name']) . '%'];
|
|
}
|
|
if (isset($param['status']) && $param['status'] !== '' && $param['status'] !== 'all') {
|
|
$filter[] = ['status', '=', $param['status']];
|
|
}
|
|
if (isset($param['passageway']) && $param['passageway'] !== '' && $param['passageway'] !== 'all') {
|
|
$filter[] = ['passageway', '=', $param['passageway']];
|
|
}
|
|
return $this->where($filter)
|
|
->order(['is_default' => 'desc', 'sort' => 'asc', 'enterprise_id' => 'desc'])
|
|
->paginate(15);
|
|
}
|
|
|
|
/**
|
|
* 全部启用中的企业(下拉选择用)
|
|
*/
|
|
public static function options(): array
|
|
{
|
|
return (new static)->where('is_enable', 1)
|
|
->order(['is_default' => 'desc', 'sort' => 'asc'])
|
|
->field(['enterprise_id', 'company_name', 'business_license', 'passageway', 'status', 'is_default'])
|
|
->select()->toArray();
|
|
}
|
|
|
|
/**
|
|
* 新增
|
|
*/
|
|
public function add(array $data): bool
|
|
{
|
|
if (!$this->validForm($data)) {
|
|
return false;
|
|
}
|
|
$data['store_id'] = self::$storeId;
|
|
$data['create_time'] = time();
|
|
$data['update_time'] = time();
|
|
return $this->transaction(function () use ($data) {
|
|
$ok = $this->save($data) !== false;
|
|
// 设为默认时,把其他企业的默认标记清掉
|
|
$ok && !empty($data['is_default']) && $this->clearOtherDefault((int) $this->enterprise_id);
|
|
return $ok;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
*/
|
|
public function edit(array $data): bool
|
|
{
|
|
if (!$this->validForm($data, (int) $this['enterprise_id'])) {
|
|
return false;
|
|
}
|
|
$data['update_time'] = time();
|
|
return $this->transaction(function () use ($data) {
|
|
$ok = $this->save($data) !== false;
|
|
$ok && !empty($data['is_default']) && $this->clearOtherDefault((int) $this['enterprise_id']);
|
|
return $ok;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 删除:已产生订单的企业不允许删除,改用停用
|
|
*/
|
|
public function remove(): bool
|
|
{
|
|
$used = Db::name('partners_order')->where('qyt_enterprise_id', $this['enterprise_id'])->count();
|
|
if ($used > 0) {
|
|
$this->error = "该企业已产生 {$used} 笔订单,不能删除;如需停用请把「启用」关掉";
|
|
return false;
|
|
}
|
|
$bound = Db::name('partners')->where('qyt_enterprise_id', $this['enterprise_id'])->count();
|
|
if ($bound > 0) {
|
|
$this->error = "还有 {$bound} 个合作商绑定了该企业,请先改绑";
|
|
return false;
|
|
}
|
|
if ($this['is_default']) {
|
|
$this->error = '默认企业不能删除,请先把默认设到其他企业';
|
|
return false;
|
|
}
|
|
return $this->delete();
|
|
}
|
|
|
|
/**
|
|
* 设为默认
|
|
*/
|
|
public function setDefault(): bool
|
|
{
|
|
if (!$this['is_enable']) {
|
|
$this->error = '已停用的企业不能设为默认';
|
|
return false;
|
|
}
|
|
return $this->transaction(function () {
|
|
$this->save(['is_default' => 1, 'update_time' => time()]);
|
|
$this->clearOtherDefault((int) $this['enterprise_id']);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/** 清掉其他企业的默认标记 */
|
|
protected function clearOtherDefault(int $keepId): void
|
|
{
|
|
Db::name('qyt_enterprise')
|
|
->where('enterprise_id', '<>', $keepId)
|
|
->where('is_default', 1)
|
|
->update(['is_default' => 0, 'update_time' => time()]);
|
|
}
|
|
|
|
/**
|
|
* 表单校验
|
|
*/
|
|
protected function validForm(array $data, int $selfId = 0): bool
|
|
{
|
|
if (empty($data['company_name'])) {
|
|
$this->error = '请填写企业名称';
|
|
return false;
|
|
}
|
|
$license = trim((string) ($data['business_license'] ?? ''));
|
|
if ($license === '') {
|
|
$this->error = '请填写统一社会信用代码';
|
|
return false;
|
|
}
|
|
if (!preg_match('/^[0-9A-Z]{18}$/', $license)) {
|
|
$this->error = '统一社会信用代码应为 18 位大写字母或数字,请检查是否混入了空格或括号';
|
|
return false;
|
|
}
|
|
$exist = Db::name('qyt_enterprise')->where('business_license', $license);
|
|
$selfId > 0 && $exist->where('enterprise_id', '<>', $selfId);
|
|
if ($exist->count() > 0) {
|
|
$this->error = '该统一社会信用代码已存在';
|
|
return false;
|
|
}
|
|
if (!isset(self::PASSAGEWAY_CODE[$data['passageway'] ?? ''])) {
|
|
$this->error = '支付通道取值不合法';
|
|
return false;
|
|
}
|
|
// 支付宝企业码通道必须有机构号,否则每一单都会在下发时失败并退款
|
|
if (($data['passageway'] ?? '') === 'alipay' && trim((string) ($data['institution_id'] ?? '')) === '') {
|
|
$this->error = '支付宝企业码通道必须填写机构号(institution_id)';
|
|
return false;
|
|
}
|
|
if (!empty($data['contact_mobile']) && !preg_match('/^1[3-9]\d{9}$/', (string) $data['contact_mobile'])) {
|
|
$this->error = '企业联系人电话格式不正确';
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|