SystemConfigDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system\config;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\config\SystemConfig;
  15. use think\Collection;
  16. use think\db\BaseQuery;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class SystemConfigDao
  22. * @package app\common\dao\system\config
  23. * @author xaboy
  24. * @day 2020-03-27
  25. */
  26. class SystemConfigDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return SystemConfig::class;
  36. }
  37. /**
  38. * 检查分类ID是否已存在
  39. *
  40. * 本函数用于确定给定的分类ID是否在系统中已存在。这可以通过查询配置表中是否存在对应的分类ID来实现。
  41. * 主要用于配置管理部分,确保新的分类ID不会与现有的ID冲突。
  42. *
  43. * @param int $classify_id 分类ID,用于唯一标识一个分类。
  44. * @return bool 返回true如果分类ID已存在,否则返回false。
  45. */
  46. public function classifyIdExists(int $classify_id)
  47. {
  48. // 调用fieldExists方法检查给定的分类ID是否存在于config_classify_id字段中
  49. return $this->fieldExists('config_classify_id', $classify_id);
  50. }
  51. /**
  52. * 检查配置中是否存在指定的键。
  53. *
  54. * 本函数用于确定配置数据集中是否包含一个特定的键。这在需要验证配置项是否存在时非常有用,
  55. * 例如,在尝试访问或修改配置之前。
  56. *
  57. * @param string $key 需要检查的配置键名。
  58. * @param int|null $except 可选参数,指定一个键值以排除在检查之外,默认为null。
  59. * @return bool 如果指定的键存在则返回true,否则返回false。
  60. */
  61. public function keyExists($key, ?int $except = null): bool
  62. {
  63. // 调用fieldExists方法来检查配置键是否存在于数据集中。
  64. // 这里将'config_key'作为字段名传递,用于指定检查的字段。
  65. return $this->fieldExists('config_key', $key, $except);
  66. }
  67. /**
  68. * 根据条件搜索系统配置信息
  69. *
  70. * 本函数用于构建一个搜索系统配置的查询。它根据传入的条件数组来筛选数据库中的配置项。
  71. * 可以根据配置名、配置键、分类ID、父分类ID和用户类型进行搜索。
  72. *
  73. * @param array $where 包含搜索条件的数组,其中键是条件名称,值是条件值。
  74. * 支持的条件有:keyword(关键字),pid(父分类ID),config_classify_id(分类ID),user_type(用户类型)。
  75. * @return \Illuminate\Database\Query\Builder|static 返回构建好的查询对象,可以进一步调用其他查询方法或执行查询。
  76. */
  77. public function search(array $where)
  78. {
  79. // 从SystemConfig类中获取数据库实例
  80. $query = SystemConfig::getDB();
  81. // 如果条件数组中包含keyword且不为空,则按照关键字搜索配置名或配置键
  82. if (isset($where['keyword']) && $where['keyword'] !== '' )
  83. $query->whereLike('config_name|config_key', '%' . $where['keyword'] . '%');
  84. // 如果条件数组中包含pid且不为空,则按照父分类ID搜索
  85. if (isset($where['pid']) && $where['pid'] !== '')
  86. $query->where('config_classify_id', $where['pid']);
  87. // 如果条件数组中包含config_classify_id且不为空,则按照分类ID搜索
  88. if (isset($where['config_classify_id']) && $where['config_classify_id'] !== '')
  89. $query->where('config_classify_id', $where['config_classify_id']);
  90. // 如果条件数组中包含user_type且不为空,则按照用户类型搜索
  91. if (isset($where['user_type']) && $where['user_type'] !== '')
  92. $query->where('user_type', $where['user_type']);
  93. // 返回构建好的查询对象
  94. return $query;
  95. }
  96. /**
  97. * 根据配置分类ID和用户类型获取配置数据
  98. *
  99. * 本函数通过指定的配置分类ID($cid)和用户类型($user_type),从数据库中查询并返回相应的配置信息。
  100. * 查询条件包括配置的状态为启用(1)以及配置分类ID和用户类型与参数值匹配。查询结果将按照排序字段(sort)降序
  101. * 和配置ID(config_id)升序进行排序。
  102. *
  103. * @param int $cid 配置分类ID,用于筛选特定分类的配置。
  104. * @param int $user_type 用户类型,用于筛选适用于特定用户类型的配置。
  105. * @return array 返回符合条件的配置数据数组,包含多个配置项。
  106. */
  107. public function cidByConfig(int $cid, int $user_type)
  108. {
  109. // 使用SystemConfig类的数据库操作方法,根据配置分类ID、用户类型和状态查询配置数据
  110. // 并按照排序字段降序和配置ID升序返回查询结果
  111. return SystemConfig::getDB()->where('config_classify_id', $cid)->where('user_type', $user_type)->where('status', 1)
  112. ->order('sort DESC, config_id ASC')->select();
  113. }
  114. /**
  115. * 获取配置项的交集
  116. * 通过给定的分类ID和配置键列表,查询系统配置表中满足条件的配置项的类型和名称,并按配置键返回结果。
  117. * 此函数主要用于处理配置数据的筛选,确保返回的配置项既属于指定的分类ID(或分类ID列表),又包含在指定的键列表中,且状态为启用。
  118. *
  119. * @param mixed $cid 分类ID,可以是单个ID或ID数组
  120. * @param array $keys 配置键列表
  121. * @return array 返回格式为[key => [config_type, config_name]]的数组,其中key为配置键
  122. */
  123. public function intersectionKey($cid, $keys): array
  124. {
  125. // 根据分类ID的类型(数组或非数组),使用不同的条件查询方式
  126. return SystemConfig::where('config_classify_id', is_array($cid) ? 'IN' : '=', $cid)
  127. // 确保配置键在给定的列表中
  128. ->whereIn('config_key', $keys)
  129. // 只返回状态为启用的配置项
  130. ->where('status', 1)
  131. // 按配置键分组,返回配置类型和名称
  132. ->column('config_type,config_name', 'config_key');
  133. }
  134. }