CategoresDao.php 6.3 KB

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