AttachmentCategoryDao.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 获取所有附件分类
  40. *
  41. * 本函数用于从数据库中检索所有附件分类。可以通过可选参数$mer_id筛选属于特定商户ID的分类。
  42. * 返回的结果集将按照排序字段'sort'降序排列。
  43. *
  44. * @param int $mer_id 商户ID,用于过滤分类。默认为0,表示获取所有商户的分类。
  45. * @return array 返回符合条件的附件分类列表。
  46. */
  47. public function getAll($mer_id = 0)
  48. {
  49. // 使用AttachmentCategory类的静态方法getDB来获取数据库对象
  50. // 然后通过where方法设置查询条件,order方法设置排序方式,最后调用select方法执行查询
  51. return AttachmentCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->select();
  52. }
  53. /**
  54. * 通过 $attachmentCategoryEName 获取主键
  55. * @param string $attachmentCategoryEName 需要检测的数据
  56. * @return int
  57. * @author 张先生
  58. * @date 2020-03-30
  59. */
  60. public function getPkByAttachmentCategoryEName($attachmentCategoryEName)
  61. {
  62. return AttachmentCategory::getInstance()->where('attachment_category_enname', $attachmentCategoryEName)->value($this->getPk());
  63. }
  64. /**
  65. * 通过id 获取path
  66. * @param int $id 需要检测的数据
  67. * @return string
  68. * @author 张先生
  69. * @date 2020-03-30
  70. */
  71. public function getPathById($id)
  72. {
  73. return AttachmentCategory::getInstance()->where($this->getPk(), $id)->value('path');
  74. }
  75. /**
  76. * 通过id获取所有子集的id
  77. * @param int $id 需要检测的数据
  78. * @return array
  79. * @author 张先生
  80. * @date 2020-03-30
  81. */
  82. public function getIdListContainsPath($id)
  83. {
  84. return AttachmentCategory::getInstance()
  85. ->where($this->getPk(), $id)
  86. ->whereOrRaw("locate ('/{$id}/', path)")
  87. ->column($this->getPk());
  88. }
  89. /**
  90. * 获取所有选项
  91. * 此方法用于查询并返回所有附件分类的信息,特别适用于需要展示所有分类列表的场景。
  92. * 通过指定mer_id,可以筛选特定商家的附件分类,适用于多商家系统中数据隔离的场景。
  93. * 返回的结果以二维数组形式呈现,方便直接用于前端展示或进一步的数据处理。
  94. *
  95. * @param int $mer_id 商家ID,用于筛选特定商家的附件分类,默认为0,表示查询所有商家的分类。
  96. * @return array 返回包含附件分类ID和名称的二维数组,数组的键为附件分类ID,值为附件分类名称。
  97. */
  98. public function getAllOptions($mer_id = 0)
  99. {
  100. // 使用AttachmentCategory类的getDB方法获取数据库操作对象
  101. // 然后通过where方法指定查询条件,order方法指定排序方式
  102. // 最后使用column方法查询并返回指定字段的值,这里返回的是pid和attachment_category_name字段
  103. return AttachmentCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->column('pid,attachment_category_name', 'attachment_category_id');
  104. }
  105. /**
  106. * 检查指定商户是否存在指定的ID
  107. *
  108. * 本函数通过调用merFieldExists方法来判断指定商户ID在特定字段中是否存在。
  109. * 主要用于验证商户ID的有效性,以及在某些情况下排除特定ID的检查。
  110. *
  111. * @param int $merId 商户的ID,用于标识特定的商户。
  112. * @param int $id 需要检查的ID,看它是否属于指定的商户。
  113. * @param mixed $except 可选参数,用于指定需要排除的ID,默认为null。
  114. * @return bool 如果指定的ID存在于商户中则返回true,否则返回false。
  115. */
  116. public function merExists(int $merId, int $id, $except = null)
  117. {
  118. // 调用merFieldExists方法来检查指定的ID是否存在于商户ID字段中
  119. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  120. }
  121. /**
  122. * 检查指定商家是否存在特定字段的特定值。
  123. *
  124. * 该方法用于查询数据库中是否存在特定条件的记录。具体来说,它首先检查传入的除外条件($except),
  125. * 如果除外条件存在,则在查询时排除这些条件。然后,它查询指定商家($merId)的记录中,
  126. * 指定字段($field)的值是否等于传入的值($value)。如果存在匹配的记录,则返回true,否则返回false。
  127. *
  128. * @param int $merId 商家ID,用于限定查询的商家范围。
  129. * @param string $field 要检查的字段名。
  130. * @param mixed $value 要检查的字段值。
  131. * @param mixed $except 除外条件,即在查询时不应该匹配的值。
  132. * @return bool 如果存在匹配的记录则返回true,否则返回false。
  133. */
  134. public function merFieldExists(int $merId, $field, $value, $except = null)
  135. {
  136. // 获取模型对应的数据库实例,并根据$except参数应用条件。
  137. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  138. // 如果$except存在,则添加不等于($<>_)查询条件。
  139. $query->where($field, '<>', $except);
  140. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  141. }
  142. /**
  143. * 根据ID和商家ID获取数据
  144. *
  145. * 本函数用于从数据库中检索指定ID和商家ID对应的数据行。
  146. * 它首先通过调用getModel方法来获取模型实例,然后使用该实例的getDB方法来获得数据库操作对象。
  147. * 接着,通过where方法指定查询条件为商家ID为$merId,最后使用find方法根据$id查找数据。
  148. *
  149. * @param int $id 数据行的唯一标识ID
  150. * @param int $merId 商家的唯一标识ID,默认为0,表示系统默认商家
  151. * @return object 返回查询结果的对象,如果未找到则为null
  152. */
  153. public function get($id, $merId = 0)
  154. {
  155. // 通过模型获取数据库操作对象,并根据$id和$merId查询数据
  156. return ($this->getModel())::getDB()->where('mer_id', $merId)->find($id);
  157. }
  158. /**
  159. * 删除记录
  160. *
  161. * 本函数用于根据给定的ID和可选的merId删除数据库中的记录。
  162. * 它首先获取模型对应的数据库实例,然后构建一个查询条件,包括主键ID和mer_id(如果提供),
  163. * 最后执行删除操作。此函数返回删除操作的结果。
  164. *
  165. * @param int $id 主键ID,用于指定要删除的记录。
  166. * @param int $merId 商户ID,可选参数,用于指定特定商户的记录。默认为0,表示不区分商户。
  167. * @return int 返回删除操作影响的行数。
  168. */
  169. public function delete(int $id, $merId = 0)
  170. {
  171. // 获取模型对应的数据库实例,并构建删除条件
  172. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->delete();
  173. }
  174. /**
  175. * 更新附件路径。
  176. * 当旧路径被新路径替换时,此方法用于更新所有关联的附件路径。
  177. * 主要用于处理附件分类路径的更新,确保路径更新后仍符合系统规定的最多三级分类的要求。
  178. *
  179. * @param string $oldPath 旧的附件路径。
  180. * @param string $path 新的附件路径。
  181. *
  182. * @throws ValidateException 如果新路径超过三级分类限制,则抛出验证异常。
  183. */
  184. public function updatePath(string $oldPath, string $path)
  185. {
  186. // 使用like查询匹配以旧路径开头的所有附件分类记录,并选择需要的字段
  187. AttachmentCategory::getDB()->whereLike('path', $oldPath . '%')->field('attachment_category_id,path')->select()->each(function ($val) use ($oldPath, $path) {
  188. // 替换记录中的旧路径为新路径
  189. $newPath = str_replace($oldPath, $path, $val['path']);
  190. // 检查新路径是否超过三级分类的限制
  191. if (substr_count(trim($newPath, '/'), '/') > 1) {
  192. throw new ValidateException('素材分类最多添加三级');
  193. }
  194. // 更新附件分类的路径
  195. AttachmentCategory::getDB()->where('attachment_category_id', $val['attachment_category_id'])->update(['path' => $newPath]);
  196. });
  197. }
  198. }