AttachmentDao.php 2.8 KB

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