AttachmentDao.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\attachment;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\attachment\Attachment;
  15. use crmeb\services\UploadService;
  16. use think\db\BaseQuery;
  17. use think\db\exception\DbException;
  18. use think\Exception;
  19. use think\facade\Log;
  20. /**
  21. * Class AttachmentDao
  22. * @package app\common\dao\system\attachment
  23. * @author xaboy
  24. * @day 2020-04-16
  25. */
  26. class AttachmentDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return Attachment::class;
  36. }
  37. /**
  38. * 根据条件搜索附件信息。
  39. *
  40. * 本函数用于构建一个搜索附件的数据库查询。它允许根据不同的条件进行筛选,
  41. * 包括用户类型、上传类型、附件分类ID和附件名称。查询结果将按照创建时间降序排列。
  42. *
  43. * @param array $where 包含搜索条件的数组。数组的键是条件的名称,值是条件的值。
  44. * 支持的条件有:user_type, upload_type, attachment_category_id, attachment_name。
  45. * 如果条件的值为空或不存在,则该条件将被忽略。
  46. * @return \yii\db\Query 查询对象,包含了所有的搜索条件和排序规则。可以进一步调用其他方法,如执行查询或获取数据。
  47. */
  48. public function search(array $where)
  49. {
  50. // 初始化查询,从附件表中按创建时间降序排列
  51. $query = Attachment::getDB()->order('create_time DESC');
  52. // 如果指定了用户类型,则添加到查询条件中
  53. if (isset($where['user_type'])) $query->where('user_type', (int)$where['user_type']);
  54. // 如果指定了上传类型,则添加到查询条件中
  55. if (isset($where['upload_type'])) $query->where('upload_type', (int)$where['upload_type']);
  56. // 如果指定了附件分类ID,并且ID不为空,则添加到查询条件中
  57. if (isset($where['attachment_category_id']) && $where['attachment_category_id'])
  58. $query->where('attachment_category_id', (int)$where['attachment_category_id']);
  59. // 如果指定了附件名称,并且名称不为空,则添加到查询条件中,使用LIKE进行模糊匹配
  60. if (isset($where['attachment_name']) && $where['attachment_name'])
  61. $query->whereLike('attachment_name', "%{$where['attachment_name']}%");
  62. // 再次设置创建时间降序排列,以确保排序规则的一致性
  63. $query->order('create_time DESC');
  64. // 返回构建好的查询对象
  65. return $query;
  66. }
  67. /**
  68. * 删除特定条件下的记录。
  69. *
  70. * 本函数用于根据用户类型和ID删除数据库中的记录。它首先获取模型对应的数据库实例,
  71. * 然后构建一个查询条件,包括用户类型和ID,最后执行删除操作。
  72. *
  73. * @param int $id 主键ID,表示要删除的具体记录的ID。
  74. * @param int $userType 用户类型,可选参数,默认为0。用于指定要删除的记录的用户类型。
  75. * @return int 返回删除操作影响的行数。
  76. */
  77. public function delete(int $id, $userType = 0)
  78. {
  79. // 获取模型对应的数据库实例,并构建删除条件,最后执行删除操作
  80. return ($this->getModel())::getDB()->where('user_type', $userType)->where($this->getPk(), $id)->delete();
  81. }
  82. /**
  83. * 批量删除指定ID的记录,并删除对应的附件。
  84. *
  85. * 此方法首先尝试从数据库中选取待删除的记录,然后根据这些记录逐个删除对应的附件。
  86. * 如果附件是存储在本地,则直接删除;如果是外部存储(如OSS),则调用相应的删除接口。
  87. * 最后,实际从数据库中删除这些记录。
  88. *
  89. * @param array $ids 需要删除的记录ID数组。
  90. * @param int $userType 用户类型标识,默认为0,用于限定删除记录的用户类型。
  91. * @return int 返回实际删除的记录数。
  92. */
  93. public function batchDelete(array $ids, $userType = 0)
  94. {
  95. // 根据提供的ID数组查询数据库,获取相关记录。
  96. $rest = ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->select();
  97. // 如果查询结果为空或错误,则直接返回空。
  98. if (is_null($rest) || count($rest) < 0) return '';
  99. $uploadConfig = systemConfig('upload_type') ?? 1;
  100. // 将查询结果分组,每组最多30条,以优化后续的附件删除操作。
  101. $arr = array_chunk($rest->toArray(), 30);
  102. foreach ($arr as $data) {
  103. foreach ($data as $datum) {
  104. if($uploadConfig == $datum['upload_type']) {
  105. try {
  106. // 根据上传类型处理附件路径,准备删除操作。
  107. if ($datum['upload_type'] < 1) {
  108. $url = systemConfig('site_url');
  109. $info = str_replace($url, '', $datum['attachment_src']);
  110. $key = public_path() . $info;
  111. } else {
  112. $info = parse_url($datum['attachment_src']);
  113. $key = ltrim($info['path'], '/');
  114. }
  115. // 创建上传服务实例,根据类型执行附件删除。
  116. $upload = UploadService::create($datum['upload_type']);
  117. $upload->delete($key);
  118. } catch (Exception $e) {
  119. // 如果删除附件失败,记录日志。
  120. Log::info('删除存储图片失败,类型:' . $datum['upload_type'] . ',KEY:' . $key);
  121. }
  122. }
  123. }
  124. }
  125. // 实际从数据库中删除记录,返回删除的记录数。
  126. return ($this->getModel())::getDB()->where('user_type', $userType)->whereIn($this->getPk(), $ids)->delete();
  127. }
  128. /**
  129. * 检查给定ID的数据是否存在。
  130. *
  131. * 本函数通过查询数据库来确定是否存在具有特定ID的记录。它使用了链式调用,
  132. * 先获取模型实例,然后获取数据库实例,最后通过指定主键ID和用户类型来执行计数查询。
  133. * 如果查询结果的计数大于0,则表示存在该ID的记录;否则,表示不存在。
  134. *
  135. * @param int $id 需要检查的记录的ID。这是主键值,用于唯一标识一条记录。
  136. * @param int $userType 可选参数,用于指定用户的类型。默认值为0,表示所有用户类型。
  137. * 这可以根据具体业务需求来过滤数据。
  138. * @return bool 如果记录存在,则返回true;如果记录不存在,则返回false。
  139. */
  140. public function exists(int $id, $userType = 0)
  141. {
  142. // 通过模型获取数据库实例,并使用主键ID和可选的用户类型执行查询,然后检查查询结果的数量是否大于0。
  143. return ($this->getModel())::getDB()->where($this->getPk(), $id)->count() > 0;
  144. }
  145. /**
  146. * 批量更新指定ID用户的特定数据
  147. *
  148. * 本函数用于根据提供的用户ID数组和更新数据数组,批量更新符合用户类型条件的用户数据。
  149. * 主要适用于需要对一批用户数据进行相同操作的场景,如批量修改用户状态、权限等。
  150. *
  151. * @param array $ids 用户ID数组,指定需要更新数据的用户
  152. * @param array $data 更新的数据数组,包含需要修改的字段及其新值
  153. * @param int $user_type 用户类型标志,默认为0,用于筛选特定类型的用户进行更新
  154. * @return int 返回影响的行数,即被成功更新的用户数量
  155. */
  156. public function batchChange(array $ids, array $data, int $user_type = 0)
  157. {
  158. // 通过模型获取数据库实例,并构造更新查询条件
  159. // 其中,where('user_type', $user_type) 筛选用户类型,whereIn($this->getPk(), $ids) 筛选指定ID的用户
  160. // 最后,执行更新操作并返回影响的行数
  161. return ($this->getModel())::getDB()->where('user_type', $user_type)->whereIn($this->getPk(), $ids)->update($data);
  162. }
  163. /**
  164. * 清理附件缓存。
  165. *
  166. * 此方法用于删除用户类型为-1的附件记录。这可能是为了回收空间、修复错误的用户类型数据,
  167. * 或者在系统更新用户类型逻辑时清理不再需要的附件记录。
  168. *
  169. * @return int 返回删除的记录数。这可以让调用者知道清理操作的影响范围。
  170. */
  171. public function clearCache()
  172. {
  173. // 使用Attachment类的数据库访问对象,并构造一个条件查询:删除用户类型为-1的附件记录。
  174. return Attachment::getDB()->where('user_type', -1)->delete();
  175. }
  176. }