CategoresRepository.php 5.5 KB

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