CategoresRepository.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 CategoresRepository
  13. {
  14. /**
  15. * 获得分级 列表
  16. * @param $merID
  17. * @return array
  18. * @author Qinii
  19. */
  20. public function getFormatList($merID,$status = null)
  21. {
  22. return formatCategory($this->dao->getAll($merID,$status)->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)
  32. {
  33. return formatCategory($this->dao->getAll($merID,$status)->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)
  43. {
  44. $data = $this->dao->getAllOptions($merId);
  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)
  68. $check = $level;
  69. return ($check < $this->dao->getMaxLevel()) ? true : false;
  70. }
  71. /**
  72. * 根据ID 查询是否存在
  73. * @param int $merId
  74. * @param int $id
  75. * @param null $except
  76. * @return mixed
  77. * @author Qinii
  78. */
  79. public function merExists(int $merId, int $id, $except = null)
  80. {
  81. return ($this->dao->merFieldExists($merId, $this->getPk(), $id, $except));
  82. }
  83. /**
  84. * 通过id获取 path
  85. * @param int $id
  86. * @return string
  87. * @throws DataNotFoundException
  88. * @throws DbException
  89. * @throws ModelNotFoundException
  90. * @author Qinii
  91. */
  92. public function getPathById(int $id)
  93. {
  94. if ($this->dao->getPathById($id))
  95. $path = $this->dao->getPathById($id).$id.$this->dao->getPathTag();
  96. return $path ?? $this->dao->getPathTag();
  97. }
  98. /**
  99. * 通过id获取 + 1等级
  100. * @param int $id
  101. * @return int|mixed
  102. * @throws DataNotFoundException
  103. * @throws DbException
  104. * @throws ModelNotFoundException
  105. * @author Qinii
  106. */
  107. public function getLevelById(int $id)
  108. {
  109. $level = 0;
  110. if(($parentLevel = $this->dao->getLevelById($id)) !== null)
  111. $level = $parentLevel + 1;
  112. return $level;
  113. }
  114. /**
  115. * 编辑时 子集是否 超过最低限制
  116. * @param int $id
  117. * @param int $pid
  118. * @return bool
  119. * @throws DataNotFoundException
  120. * @throws DbException
  121. * @throws ModelNotFoundException
  122. * @author Qinii
  123. */
  124. public function checkChildLevel(int $id,int $pid)
  125. {
  126. $childLevel = max($this->dao->getChildLevelById($id)); //1
  127. $changLevel = $childLevel + ($this->changeLevel($id,$pid)); //2
  128. return $this->checkLevel(0, $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. return ($this->dao->getLevelById($pid) + 1 ) - ($this->dao->getLevelById($id));
  142. }
  143. /**
  144. * 检测 是否修改到 子集
  145. * @param int $id
  146. * @param int $pid
  147. * @throws DataNotFoundException
  148. * @throws DbException
  149. * @throws ModelNotFoundException
  150. * @author Qinii
  151. */
  152. public function checkChangeToChild(int $id, int $pid)
  153. {
  154. return ($this->dao->checkChangeToChild($id,$pid) > 0) ? false : true;
  155. }
  156. /**
  157. * 子集是否存在
  158. * @param int $id
  159. * @return bool
  160. * @author Qinii
  161. */
  162. public function hasChild(int $id)
  163. {
  164. return (($this->dao->hasChild($id)) > 0) ? true : false ;
  165. }
  166. /**
  167. * 添加
  168. * @param array $data
  169. * @return \app\common\dao\BaseDao|\think\Model
  170. * @throws DataNotFoundException
  171. * @throws DbException
  172. * @throws ModelNotFoundException
  173. * @author Qinii
  174. */
  175. public function create(array $data)
  176. {
  177. $data[$this->dao->getPath()] = $this->getPathById($data[$this->dao->getParentId()]);
  178. $data[$this->dao->getLevel()] = $this->getLevelById($data[$this->dao->getParentId()]);
  179. return ($this->dao->create($data));
  180. }
  181. /**
  182. * 修改
  183. * @param int $id
  184. * @param array $data
  185. * @author Qinii
  186. */
  187. public function update(int $id,array $data)
  188. {
  189. if($this->checkUpdate($id,$data['pid'])){
  190. $this->dao->update($id,$data);
  191. } else {
  192. $data[$this->dao->getPath()] = $this->getPathById($data[$this->dao->getParentId()]);
  193. $data[$this->dao->getLevel()] = $this->getLevelById($data[$this->dao->getParentId()]);
  194. $data['change'] = [
  195. 'oldPath' => $this->dao->getPathById($id).$id.$this->getPathTag(),
  196. 'newPath' => $data[$this->dao->getPath()].$id.$this->getPathTag(),
  197. 'changeLevel' => $this->changeLevel($id,$data[$this->getParentId()]),
  198. ];
  199. $this->dao->updateParent($id,$data);
  200. }
  201. }
  202. }