CategoresRepository.php 5.5 KB

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