AttachmentCategoryDao.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\AttachmentCategory;
  6. use think\Collection;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\exception\ValidateException;
  11. use think\Model;
  12. /**
  13. * Class AttachmentCategoryDao
  14. * @package app\common\dao\system\attachment
  15. * @author xaboy
  16. * @day 2020-04-22
  17. */
  18. class AttachmentCategoryDao extends BaseDao
  19. {
  20. /**
  21. * @return BaseModel
  22. * @author xaboy
  23. * @day 2020-03-30
  24. */
  25. protected function getModel(): string
  26. {
  27. return AttachmentCategory::class;
  28. }
  29. /**
  30. * @param int $mer_id
  31. * @return Collection
  32. * @throws DataNotFoundException
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. * @author xaboy
  36. * @day 2020-04-15
  37. */
  38. public function getAll($mer_id = 0)
  39. {
  40. return AttachmentCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->select();
  41. }
  42. /**
  43. * 通过 $attachmentCategoryEName 获取主键
  44. * @param string $attachmentCategoryEName 需要检测的数据
  45. * @return int
  46. * @author 张先生
  47. * @date 2020-03-30
  48. */
  49. public function getPkByAttachmentCategoryEName($attachmentCategoryEName)
  50. {
  51. return AttachmentCategory::getInstance()->where('attachment_category_enname', $attachmentCategoryEName)->value($this->getPk());
  52. }
  53. /**
  54. * 通过id 获取path
  55. * @param int $id 需要检测的数据
  56. * @return string
  57. * @author 张先生
  58. * @date 2020-03-30
  59. */
  60. public function getPathById($id)
  61. {
  62. return AttachmentCategory::getInstance()->where($this->getPk(), $id)->value('path');
  63. }
  64. /**
  65. * 通过id获取所有子集的id
  66. * @param int $id 需要检测的数据
  67. * @return array
  68. * @author 张先生
  69. * @date 2020-03-30
  70. */
  71. public function getIdListContainsPath($id)
  72. {
  73. return AttachmentCategory::getInstance()
  74. ->where($this->getPk(), $id)
  75. ->whereOrRaw("locate ('/{$id}/', path)")
  76. ->column($this->getPk());
  77. }
  78. /**
  79. * @param int $mer_id
  80. * @return array
  81. * @author xaboy
  82. * @day 2020-04-20
  83. */
  84. public function getAllOptions($mer_id = 0)
  85. {
  86. return AttachmentCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->column('pid,attachment_category_name', 'attachment_category_id');
  87. }
  88. /**
  89. * @param int $merId
  90. * @param int $id
  91. * @param null $except
  92. * @return bool
  93. * @author xaboy
  94. * @day 2020-04-15
  95. */
  96. public function merExists(int $merId, int $id, $except = null)
  97. {
  98. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  99. }
  100. /**
  101. * @param int $merId
  102. * @param $field
  103. * @param $value
  104. * @param null $except
  105. * @return bool
  106. * @author xaboy
  107. * @day 2020-04-15
  108. */
  109. public function merFieldExists(int $merId, $field, $value, $except = null)
  110. {
  111. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  112. $query->where($field, '<>', $except);
  113. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  114. }
  115. /**
  116. * @param int $id
  117. * @param int $merId
  118. * @return array|Model|null
  119. * @throws DataNotFoundException
  120. * @throws DbException
  121. * @throws ModelNotFoundException
  122. * @author xaboy
  123. * @day 2020-04-15
  124. */
  125. public function get($id, $merId = 0)
  126. {
  127. return ($this->getModel())::getDB()->where('mer_id', $merId)->find($id);
  128. }
  129. /**
  130. * @param int $id
  131. * @param int $merId
  132. * @return int
  133. * @throws DbException
  134. * @author xaboy
  135. * @day 2020-04-15
  136. */
  137. public function delete(int $id, $merId = 0)
  138. {
  139. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->delete();
  140. }
  141. /**
  142. * @param string $oldPath
  143. * @param string $path
  144. * @throws DataNotFoundException
  145. * @throws DbException
  146. * @throws ModelNotFoundException
  147. * @author xaboy
  148. * @day 2020-04-30
  149. */
  150. public function updatePath(string $oldPath, string $path)
  151. {
  152. AttachmentCategory::getDB()->whereLike('path', $oldPath . '%')->field('attachment_category_id,path')->select()->each(function ($val) use ($oldPath, $path) {
  153. $newPath = str_replace($oldPath, $path, $val['path']);
  154. if (substr_count(trim($newPath, '/'), '/') > 1) throw new ValidateException('素材分类最多添加三级');
  155. AttachmentCategory::getDB()->where('attachment_category_id', $val['attachment_category_id'])->update(['path' => $newPath]);
  156. });
  157. }
  158. }