AttachmentDao.php 2.5 KB

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