CategoresDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 crmeb\traits;
  12. trait CategoresDao
  13. {
  14. public $path = 'path';
  15. public $pathTag = '/';
  16. public $level = 'level';
  17. public $maxLevel = 3;
  18. public $parentId = 'pid';
  19. public $status = 'is_show';
  20. /**
  21. * @return mixed
  22. * @author Qinii
  23. */
  24. public function getPath():string
  25. {
  26. return $this->path;
  27. }
  28. /**
  29. * @return mixed
  30. * @author Qinii
  31. */
  32. public function getPathTag():string
  33. {
  34. return $this->pathTag;
  35. }
  36. /**
  37. * @return mixed
  38. */
  39. public function getLevel():string
  40. {
  41. return $this->level;
  42. }
  43. /**
  44. * @return int
  45. * @author Qinii
  46. */
  47. public function getMaxLevel():int
  48. {
  49. return $this->maxLevel;
  50. }
  51. /**
  52. * @return int
  53. * @return string
  54. */
  55. public function getParentId(): string
  56. {
  57. return $this->parentId;
  58. }
  59. /**
  60. * @return int
  61. * @return string
  62. */
  63. public function getStatus(): string
  64. {
  65. return $this->status;
  66. }
  67. /**
  68. * 获取所有分类
  69. * @param int $mer_id
  70. * @return mixed
  71. * @author Qinii
  72. */
  73. public function getAll($mer_id = 0,$status = null)
  74. {
  75. return $this->getModel()::getDB()->where('mer_id', $mer_id)->when(($status !== null),function($query)use($status){
  76. $query->where($this->getStatus(),$status);
  77. })->order('sort DESC')->select();
  78. }
  79. /**
  80. * 通过id 获取path
  81. * @param int $id 需要检测的数据
  82. * @return string
  83. * @author Qinii
  84. * @date 2020-03-30
  85. */
  86. public function getPathById($id)
  87. {
  88. return ($this->getModel())::getDB()->where($this->getPk(), $id)->value($this->getPath());
  89. }
  90. /**
  91. * 根据id 获取 等级
  92. * @param $id
  93. * @return mixed
  94. * @author Qinii
  95. */
  96. public function getLevelById($id)
  97. {
  98. return ($this->getModel())::getDB()->where($this->getPk(), $id)->value(($this->getLevel()));
  99. }
  100. /**
  101. * 根据 字段名查询
  102. * @param int $merId
  103. * @param $field
  104. * @param $value
  105. * @param null $except
  106. * @return bool
  107. * @author Qinii
  108. */
  109. public function merFieldExists(int $merId, $field, $value, $except = null)
  110. {
  111. return ($this->getModel())::getDB()
  112. ->when($except, function ($query, $except) use ($field) {
  113. $query->where($field, '<>', $except);
  114. })
  115. ->where('mer_id', $merId)
  116. ->where($field, $value)->count() > 0;
  117. }
  118. /**
  119. * 获取子集 等级 数组
  120. * @param $id
  121. * @return array
  122. * @author Qinii
  123. */
  124. public function getChildLevelById($id)
  125. {
  126. $level = ($this->getModel()::getDB())->where($this->getPath(),'like',$this->getPathById($id).$id.$this->getPathTag().'%')->column($this->getLevel());
  127. return (is_array($level) && !empty($level)) ? $level : [0];
  128. }
  129. /**
  130. * 查询修改目标是否为自己到子集
  131. * @param int $id
  132. * @param int $pid
  133. * @return mixed
  134. * @author Qinii
  135. */
  136. public function checkChangeToChild(int $id,int $pid)
  137. {
  138. return ($this->getModel()::getDB())
  139. ->where($this->getPath(),'like',$this->getPathById($id).$id.$this->getPathTag().'%')
  140. ->where($this->getPk(),$pid)
  141. ->count();
  142. }
  143. /**
  144. * 是否存在子集
  145. * @param int $id
  146. * @return mixed
  147. * @author Qinii
  148. */
  149. public function hasChild(int $id)
  150. {
  151. return ($this->getModel()::getDB())->where($this->getPath(),'like',$this->getPathById($id).$id.$this->getPathTag().'%')->count();
  152. }
  153. /**
  154. * 编辑
  155. * @param int $id
  156. * @param array $data
  157. * @author Qinii
  158. */
  159. public function updateParent(int $id,array $data)
  160. {
  161. ($this->getModel()::getDB())->transaction(function()use($id,$data){
  162. $change = $data['change'];
  163. unset($data['change']);
  164. ($this->getModel()::getDB())->where($this->getPk(),$id)->update($data);
  165. $this->updateChild($change['oldPath'],$change['newPath'],$change['changeLevel']);
  166. });
  167. }
  168. /**
  169. * 修改子类
  170. * @param string $oldPath
  171. * @param string $newPath
  172. * @param $changeLevel
  173. * @author Qinii
  174. */
  175. public function updateChild(string $oldPath,string $newPath, $changeLevel)
  176. {
  177. $query = ($this->getModel()::getDB())->where($this->getPath(),'like',$oldPath.'%')
  178. ->select();
  179. if ($query) {
  180. $query->each(function ($item) use ($oldPath, $newPath, $changeLevel) {
  181. $child = ($this->getModel()::getDB())->find($item[$this->getPk()]);
  182. $child->path = str_replace($oldPath, $newPath, $child->path);
  183. $child->level = $child->level + $changeLevel;
  184. $child->save();
  185. });
  186. }
  187. }
  188. /**
  189. * 修改状态
  190. * @param int $id
  191. * @param int $status
  192. * @return mixed
  193. * @author Qinii
  194. */
  195. public function switchStatus(int $id,int $status)
  196. {
  197. return ($this->getModel()::getDB())->where($this->getPk(),$id)->update([
  198. $this->getStatus() => $status
  199. ]);
  200. }
  201. /**
  202. * 获取列表 -- 筛选用
  203. * @Author:Qinii
  204. * @Date: 2020/5/16
  205. * @param int|null $mer_id
  206. * @return mixed
  207. */
  208. public function getAllOptions($mer_id = null,$status = null,$level = null)
  209. {
  210. $field = $this->getParentId().',cate_name';
  211. $query = ($this->getModel()::getDB());
  212. $query->when(($mer_id !== null),function($query)use($mer_id){
  213. $query->where('mer_id', $mer_id);
  214. })
  215. ->when($status,function($query)use($status){
  216. $query->where($this->getStatus(),$status);
  217. })
  218. ->when(($level !== null),function($query)use($level){
  219. $query->where($this->getLevel(),'<',$level);
  220. });
  221. return $query->order('sort DESC')->column($field, $this->getPk());
  222. }
  223. public function switchStatusAndChild(int $id,$status)
  224. {
  225. $parent = $this->getModel()::find($id);
  226. ($this->getModel()::getDB())->where($this->getPk(),$id)->update([$this->getStatus() => $status]);
  227. return ($this->getModel()::getDB())->where('path','like',$parent->path.$id.'/%')->update([$this->getStatus() => $status]);
  228. }
  229. }