AttachmentCategoryDao.php 5.4 KB

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