AttachmentDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 think\db\BaseQuery;
  16. use think\db\exception\DbException;
  17. /**
  18. * Class AttachmentDao
  19. * @package app\common\dao\system\attachment
  20. * @author xaboy
  21. * @day 2020-04-16
  22. */
  23. class AttachmentDao extends BaseDao
  24. {
  25. /**
  26. * @return BaseModel
  27. * @author xaboy
  28. * @day 2020-03-30
  29. */
  30. protected function getModel(): string
  31. {
  32. return Attachment::class;
  33. }
  34. /**
  35. * @param array $where
  36. * @return BaseQuery
  37. * @author xaboy
  38. * @day 2020-04-15
  39. */
  40. public function search(array $where)
  41. {
  42. $query = Attachment::getDB()->order('create_time DESC');
  43. if (isset($where['user_type'])) $query->where('user_type', (int)$where['user_type']);
  44. if (isset($where['upload_type'])) $query->where('upload_type', (int)$where['upload_type']);
  45. if (isset($where['attachment_category_id']) && $where['attachment_category_id'])
  46. $query->where('attachment_category_id', (int)$where['attachment_category_id']);
  47. if (isset($where['attachment_name']) && $where['attachment_name'])
  48. $query->whereLike('attachment_name', "%{$where['attachment_name']}%");
  49. $query->order('create_time DESC');
  50. return $query;
  51. }
  52. /**
  53. * @param int $id
  54. * @param int $userType
  55. * @return int
  56. * @throws DbException
  57. * @author xaboy
  58. * @day 2020-04-16
  59. */
  60. public function delete(int $id, $userType = 0)
  61. {
  62. return ($this->getModel())::getDB()->where('user_type', $userType)->where($this->getPk(), $id)->delete();
  63. }
  64. /**
  65. * @param array $ids
  66. * @param int $userType
  67. * @return int
  68. * @throws DbException
  69. * @author xaboy
  70. * @day 2020-04-15
  71. */
  72. public function batchDelete(array $ids, $userType = 0)
  73. {
  74. return ($this->getModel())::getDB()->where('user_type', $userType)->whereIn($this->getPk(), $ids)->delete();
  75. }
  76. /**
  77. * @param int $id
  78. * @param int $userType
  79. * @return bool
  80. * @author xaboy
  81. * @day 2020-04-16
  82. */
  83. public function exists(int $id, $userType = 0)
  84. {
  85. return ($this->getModel())::getDB()->where($this->getPk(), $id)->count() > 0;
  86. }
  87. /**
  88. * @param array $ids
  89. * @param array $data
  90. * @param int $user_type
  91. * @return int
  92. * @throws DbException
  93. * @author xaboy
  94. * @day 2020-04-16
  95. */
  96. public function batchChange(array $ids, array $data, int $user_type = 0)
  97. {
  98. return ($this->getModel())::getDB()->where('user_type', $user_type)->whereIn($this->getPk(), $ids)->update($data);
  99. }
  100. public function clearCache()
  101. {
  102. return Attachment::getDB()->where('user_type', -1)->delete();
  103. }
  104. }