Files
MBTI_wang/api/app/model/PricingConfig.php
Ghost 95e4effcd9 feat: 企业计费服务与超管/管理端设置及定价规则文档
1、修复了财务、Analyze/Test 等与定价、账务相关的接口行为。

2、新增 EnterpriseBillingService 与定价企业账务规则说明文档。

3、优化超管 Settings、PricingConfig 及小程序 app 入口逻辑。

Made-with: Cursor
2026-04-13 10:38:56 +08:00

102 lines
3.3 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
namespace app\model;
use think\Model;
/**
* 定价配置模型
* 实际表名 = 数据库前缀 + pricing_config例如 .env 中 DATABASE_PREFIX=mbti_ 时为 mbti_pricing_config
*/
class PricingConfig extends Model
{
// 表名(不含前缀);最终访问表 = config(database.prefix) + pricing_config
protected $name = 'pricing_config';
// 设置字段信息(匹配数据库字段命名:驼峰命名)
protected $schema = [
'id' => 'int',
'type' => 'string',
'enterpriseId' => 'int',
'config' => 'string',
'createdAt' => 'int',
'updatedAt' => 'int',
];
// 自动时间戳(使用驼峰命名,时间戳格式)
protected $autoWriteTimestamp = 'int';
// 时间戳字段名(驼峰命名,匹配数据库)
protected $createTime = 'createdAt';
protected $updateTime = 'updatedAt';
// 时间字段类型(时间戳格式)
protected $type = [
'createdAt' => 'integer',
'updatedAt' => 'integer',
];
// JSON字段自动转换
protected $json = ['config'];
/**
* 配置修改器自动转换为JSON
*/
public function setConfigAttr($value)
{
if (is_array($value)) {
return json_encode($value, JSON_UNESCAPED_UNICODE);
}
return $value;
}
/**
* 配置获取器自动解析JSON
*/
public function getConfigAttr($value)
{
if (is_string($value)) {
return json_decode($value, true);
}
return $value;
}
/**
* 按类型与可选企业ID取定价配置
*
* personal个人版优先级
* 1. admin_personal + enterpriseId企业专属管理端配置有 eid 时)
* 2. personal + null超管全局仅未绑定企业或该企业未配置时使用
*
* enterprise企业版优先级
* 1. admin_enterprise + enterpriseId有 eid 时)
* 2. enterprise + null超管全局兜底
*
* @param string $type personal|enterprise|deep
* @param int|null $enterpriseId 有则优先读该企业专属配置
* @return \app\model\PricingConfig|null
*/
public static function getByTypeAndEnterprise(string $type, ?int $enterpriseId = null): ?self
{
if ($type === 'personal') {
if (!empty($enterpriseId)) {
$row = self::where('type', 'admin_personal')->where('enterpriseId', $enterpriseId)->find();
if ($row) return $row;
}
// 超管全局个人定价(最后兜底,仅管理端完全未配置时使用)
return self::where('type', 'personal')->whereNull('enterpriseId')->find();
}
if ($type === 'enterprise') {
if (!empty($enterpriseId)) {
$row = self::where('type', 'admin_enterprise')->where('enterpriseId', $enterpriseId)->find();
if ($row) return $row;
}
return self::where('type', 'enterprise')->whereNull('enterpriseId')->find();
}
if ($type === 'deep') {
return self::where('type', 'deep')->whereNull('enterpriseId')->find();
}
return self::where('type', $type)->whereNull('enterpriseId')->find();
}
}