CategoresRepository.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 crmeb\traits;
  12. trait CategoresRepository
  13. {
  14. /**
  15. * 获得分级 列表
  16. * @param $merID
  17. * @return array
  18. * @author Qinii
  19. */
  20. public function getFormatList($merID,$status = null,$type = 0)
  21. {
  22. return formatCategory($this->dao->getAll($merID,$status,$type)->hidden(['path'])->toArray(), $this->dao->getPk());
  23. }
  24. /**
  25. * @Author:Qinii
  26. * @Date: 2020/5/28
  27. * @param $merID
  28. * @param null $status
  29. * @return array
  30. */
  31. public function getApiFormatList($merID,$status = null,$type = 0)
  32. {
  33. return formatCategory($this->dao->getAll($merID,$status,$type)->hidden(['path','level','mer_id','create_time'])->toArray(), $this->dao->getPk());
  34. }
  35. /**
  36. * 筛选用
  37. * @Author:Qinii
  38. * @Date: 2020/5/16
  39. * @param int $merId
  40. * @return array
  41. */
  42. public function getTreeList(int $merId = 0,$status = 0,$type = 0)
  43. {
  44. $data = $this->dao->getAllOptions($merId,$status,$type);
  45. return formatCascaderData($data,'cate_name');
  46. }
  47. /**
  48. * 提交的pid 是否等于当前pid
  49. * @param int $id
  50. * @param int $pid
  51. * @author Qinii
  52. */
  53. public function checkUpdate(int $id,int $pid)
  54. {
  55. return (($this->dao->get($id))[$this->dao->getParentId()] == $pid) ? true : false;
  56. }
  57. /**
  58. * 检测是否超过最低等级限制
  59. * @param int $id
  60. * @param int $level
  61. * @return bool
  62. * @author Qinii
  63. */
  64. public function checkLevel(int $id,int $level = 0)
  65. {
  66. $check = $this->getLevelById($id);
  67. if($level) $check = $level;
  68. return ($check < $this->dao->getMaxLevel()) ? true : false;
  69. }
  70. /**
  71. * 根据ID 查询是否存在
  72. * @param int $merId
  73. * @param int $id
  74. * @param null $except
  75. * @return mixed
  76. * @author Qinii
  77. */
  78. public function merExists(int $merId, int $id, $except = null)
  79. {
  80. return ($this->dao->merFieldExists($merId, $this->getPk(), $id, $except));
  81. }
  82. /**
  83. * 通过id获取 path
  84. * @param int $id
  85. * @return string
  86. * @throws DataNotFoundException
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. * @author Qinii
  90. */
  91. public function getPathById(int $id)
  92. {
  93. $path = $this->dao->getPathById($id);
  94. if ($path) $path = $path.$id.$this->dao->getPathTag();
  95. return $path ?? $this->dao->getPathTag();
  96. }
  97. /**
  98. * 通过id获取 + 1等级
  99. * @param int $id
  100. * @return int|mixed
  101. * @throws DataNotFoundException
  102. * @throws DbException
  103. * @throws ModelNotFoundException
  104. * @author Qinii
  105. */
  106. public function getLevelById(int $id)
  107. {
  108. $level = 0;
  109. if(($parentLevel = $this->dao->getLevelById($id)) !== null)
  110. $level = $parentLevel + 1;
  111. return $level;
  112. }
  113. /**
  114. * 编辑时 子集是否 超过最低限制
  115. * @param int $id
  116. * @param int $pid
  117. * @return bool
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. * @author Qinii
  122. */
  123. public function checkChildLevel(int $id,int $pid)
  124. {
  125. //计算子集最大的数组等级
  126. $childLevel = max($this->dao->getChildLevelById($id)); //1
  127. $changLevel = $childLevel + ($this->changeLevel($id,$pid)); //2
  128. return $this->checkLevel($id, $changLevel);
  129. }
  130. /**
  131. * 变动等级差
  132. * @param int $id
  133. * @param int $pid
  134. * @return int|mixed
  135. * @author Qinii
  136. * 0->1 1-> 2 2-> 3
  137. * 3->2 2-> 1 1-> 0
  138. */
  139. public function changeLevel(int $id,int $pid)
  140. {
  141. $level = $this->dao->getLevelById($pid);
  142. return $level ? ($level + 1 ) - ($level) : $level;
  143. }
  144. /**
  145. * 检测 是否修改到 子集
  146. * @param int $id
  147. * @param int $pid
  148. * @throws DataNotFoundException
  149. * @throws DbException
  150. * @throws ModelNotFoundException
  151. * @author Qinii
  152. */
  153. public function checkChangeToChild(int $id, int $pid)
  154. {
  155. return ($this->dao->checkChangeToChild($id,$pid) > 0) ? false : true;
  156. }
  157. /**
  158. * 子集是否存在
  159. * @param int $id
  160. * @return bool
  161. * @author Qinii
  162. */
  163. public function hasChild(int $id)
  164. {
  165. return (($this->dao->hasChild($id)) > 0) ? true : false ;
  166. }
  167. /**
  168. * 添加
  169. * @param array $data
  170. * @return \app\common\dao\BaseDao|\think\Model
  171. * @throws DataNotFoundException
  172. * @throws DbException
  173. * @throws ModelNotFoundException
  174. * @author Qinii
  175. */
  176. public function create(array $data)
  177. {
  178. $data[$this->dao->getPath()] = $this->getPathById($data[$this->dao->getParentId()]);
  179. $data[$this->dao->getLevel()] = $this->getLevelById($data[$this->dao->getParentId()]);
  180. return ($this->dao->create($data));
  181. }
  182. /**
  183. * 修改
  184. * @param int $id
  185. * @param array $data
  186. * @author Qinii
  187. */
  188. public function update(int $id,array $data)
  189. {
  190. if($this->checkUpdate($id,$data['pid'])){
  191. $this->dao->update($id,$data);
  192. } else {
  193. $data[$this->dao->getPath()] = $this->getPathById($data[$this->dao->getParentId()]);
  194. $data[$this->dao->getLevel()] = $this->getLevelById($data[$this->dao->getParentId()]);
  195. $data['change'] = [
  196. 'oldPath' => $this->dao->getPathById($id).$id.$this->getPathTag(),
  197. 'newPath' => $data[$this->dao->getPath()].$id.$this->getPathTag(),
  198. 'changeLevel' => $this->changeLevel($id,$data[$this->getParentId()]),
  199. ];
  200. $this->dao->updateParent($id,$data);
  201. }
  202. }
  203. }