AttachmentRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\repositories\system\attachment;
  3. //附件
  4. use app\common\dao\BaseDao;
  5. use app\common\dao\system\attachment\AttachmentDao;
  6. use app\common\repositories\BaseRepository;
  7. use FormBuilder\Factory\Elm;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use think\facade\Route;
  12. use think\Model;
  13. /**
  14. * Class BaseRepository
  15. * @package common\repositories
  16. * @mixin AttachmentDao
  17. */
  18. class AttachmentRepository extends BaseRepository
  19. {
  20. /**
  21. * @var AttachmentCategoryRepository
  22. */
  23. private $attachmentCategoryRepository;
  24. /**
  25. * AttachmentRepository constructor.
  26. * @param AttachmentDao $dao
  27. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  28. */
  29. public function __construct(AttachmentDao $dao, AttachmentCategoryRepository $attachmentCategoryRepository)
  30. {
  31. /**
  32. * @var AttachmentDao
  33. */
  34. $this->dao = $dao;
  35. $this->attachmentCategoryRepository = $attachmentCategoryRepository;
  36. }
  37. /**
  38. * @param array $where
  39. * @param int $page
  40. * @param int $limit
  41. * @return array
  42. * @throws DataNotFoundException
  43. * @throws DbException
  44. * @throws ModelNotFoundException
  45. * @author zfy
  46. * @day 2020-04-15
  47. */
  48. public function getList(array $where, int $page, int $limit)
  49. {
  50. $query = $this->search($where);
  51. $count = $query->count($this->dao->getPk());
  52. $list = $query->page($page, $limit)->hidden(['upload_type', 'user_type', 'user_id'])
  53. ->select();
  54. return compact('count', 'list');
  55. }
  56. /**
  57. * @param int $uploadType
  58. * @param int $userType
  59. * @param int $userId
  60. * @param array $data
  61. * @return BaseDao|Model
  62. * @author zfy
  63. * @day 2020-04-15
  64. */
  65. public function create(int $uploadType, int $userType, int $userId, array $data)
  66. {
  67. $data['upload_type'] = $uploadType;
  68. $data['user_type'] = $userType;
  69. $data['user_id'] = $userId;
  70. return $this->dao->create($data);
  71. }
  72. /**
  73. * @param array $ids
  74. * @param int $categoryId
  75. * @param int $merId
  76. * @return int
  77. * @throws DbException
  78. * @author zfy
  79. * @day 2020-04-16
  80. */
  81. public function batchChangeCategory(array $ids, int $categoryId, $merId = 0)
  82. {
  83. return $this->dao->batchChange($ids, ['attachment_category_id' => $categoryId], $merId);
  84. }
  85. public function form(int $id,int $merId)
  86. {
  87. if ($merId) {
  88. $action = 'merchantAttachmentUpdate';
  89. } else {
  90. $action = 'systemAttachmentUpdate';
  91. }
  92. $formData = $this->dao->get($id)->toArray();
  93. $form = Elm::createForm(Route::buildUrl($action, is_null($id) ? [] : ['id' => $id])->build());
  94. $form->setRule([
  95. Elm::input('attachment_name', '名称')->required(),
  96. ]);
  97. return $form->setTitle('编辑配置')->formData($formData);
  98. }
  99. }