CategoresDao.php 6.3 KB

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