AttachmentDao.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. * @param array $where
  39. * @return BaseQuery
  40. * @author xaboy
  41. * @day 2020-04-15
  42. */
  43. public function search(array $where)
  44. {
  45. $query = Attachment::getDB()->order('create_time DESC');
  46. if (isset($where['user_type'])) $query->where('user_type', (int)$where['user_type']);
  47. if (isset($where['upload_type'])) $query->where('upload_type', (int)$where['upload_type']);
  48. if (isset($where['attachment_category_id']) && $where['attachment_category_id'])
  49. $query->where('attachment_category_id', (int)$where['attachment_category_id']);
  50. if (isset($where['attachment_name']) && $where['attachment_name'])
  51. $query->whereLike('attachment_name', "%{$where['attachment_name']}%");
  52. $query->order('create_time DESC');
  53. return $query;
  54. }
  55. /**
  56. * @param int $id
  57. * @param int $userType
  58. * @return int
  59. * @throws DbException
  60. * @author xaboy
  61. * @day 2020-04-16
  62. */
  63. public function delete(int $id, $userType = 0)
  64. {
  65. return ($this->getModel())::getDB()->where('user_type', $userType)->where($this->getPk(), $id)->delete();
  66. }
  67. /**
  68. * @param array $ids
  69. * @param int $userType
  70. * @return int
  71. * @throws DbException
  72. * @author xaboy
  73. * @day 2020-04-15
  74. */
  75. public function batchDelete(array $ids, $userType = 0)
  76. {
  77. $data = ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->select();
  78. foreach ($data as $datum) {
  79. try {
  80. if ($datum['upload_type'] < 1) {
  81. $url = systemConfig('site_url');
  82. $info = str_replace($url, '', $datum['attachment_src']);
  83. $key = public_path() . $info;
  84. } else {
  85. $info = parse_url($datum['attachment_src']);
  86. $key = ltrim($info['path'], '/');
  87. }
  88. $upload = UploadService::create($datum['upload_type']);
  89. $upload->delete($key);
  90. } catch (Exception $e) {
  91. Log::info('删除存储图片失败,类型:' . $datum['upload_type'] . ',KEY:' . $key);
  92. }
  93. }
  94. return ($this->getModel())::getDB()->where('user_type', $userType)->whereIn($this->getPk(), $ids)->delete();
  95. }
  96. /**
  97. * @param int $id
  98. * @param int $userType
  99. * @return bool
  100. * @author xaboy
  101. * @day 2020-04-16
  102. */
  103. public function exists(int $id, $userType = 0)
  104. {
  105. return ($this->getModel())::getDB()->where($this->getPk(), $id)->count() > 0;
  106. }
  107. /**
  108. * @param array $ids
  109. * @param array $data
  110. * @param int $user_type
  111. * @return int
  112. * @throws DbException
  113. * @author xaboy
  114. * @day 2020-04-16
  115. */
  116. public function batchChange(array $ids, array $data, int $user_type = 0)
  117. {
  118. return ($this->getModel())::getDB()->where('user_type', $user_type)->whereIn($this->getPk(), $ids)->update($data);
  119. }
  120. public function clearCache()
  121. {
  122. return Attachment::getDB()->where('user_type', -1)->delete();
  123. }
  124. }