Category.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\platform;
  10. use app\admin\service\Service;
  11. use app\common\models\Category as CategoryModel;
  12. use app\common\models\goods\Goods;
  13. use mall\utils\Data;
  14. class Category extends Service {
  15. /**
  16. * 获取分类树结构
  17. * @param array $condition
  18. * @return array|mixed
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public static function getTree($condition=[]){
  24. $result = CategoryModel::where($condition)->select()->toArray();
  25. return Data::analysisTree(Data::familyProcess($result));
  26. }
  27. /**
  28. * 获取分类列表
  29. * @param $data
  30. * @param array $condition
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public static function getList($data,$condition=[]){
  37. $count = CategoryModel::where($condition)->count();
  38. $result = CategoryModel::where($condition)->order("id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray();
  39. $list = [];
  40. foreach($result as $key=>$value){
  41. $list[] = $value;
  42. $children = self::getChildren($value["id"],null);
  43. $arr = Data::analysisTree(Data::familyProcess($children,[],$value["id"]));
  44. array_splice($list, count($list), 0, $arr);
  45. }
  46. foreach($list as $k=>$item){
  47. $list[$k]['title'] = (empty($item['level']) ? '' : $item['level']) . $item["title"];
  48. }
  49. return ["count"=>$count, "data"=>$list];
  50. }
  51. /**
  52. * 获取分类信息
  53. * @param $id
  54. * @return array
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public static function detail($id,$condition=[]){
  60. return [
  61. "data"=>CategoryModel::where(["id"=>$id])->find(),
  62. "cat"=>self::getTree($condition)
  63. ];
  64. }
  65. /**
  66. * 保存
  67. * @param array $data
  68. * @return bool
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public static function save($data=[]){
  74. if(CategoryModel::where("id",$data["id"])->count()){
  75. if(!self::checkTree(CategoryModel::where(["module"=>$data["module"]])->select()->toArray(),$data)){
  76. throw new \Exception("{$data['title']} 是 ID {$data['pid']} 的父栏目,不能修改!", 0);
  77. }
  78. CategoryModel::where("id",$data["id"])->save($data);
  79. }else{
  80. CategoryModel::create($data);
  81. }
  82. return true;
  83. }
  84. /**
  85. * 删除文章分类
  86. * @param $id
  87. * @return bool
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public static function deleteArchives($id){
  93. // 检测是否有子分类
  94. if(CategoryModel::where("pid",$id)->count() > 0){
  95. throw new \Exception("该分类下有子分类,请先删除子分类。",0);
  96. }
  97. // 检测是否有文章
  98. if(CategoryModel::where("pid",$id)->count() > 0){
  99. throw new \Exception("该分类下有文章,请先删除文章。",0);
  100. }
  101. $row = CategoryModel::where("id",$id)->find();
  102. if(empty($row)){
  103. throw new \Exception("您要删除的内容不存在",0);
  104. }else if($row["is_lock"] == 1){
  105. throw new \Exception("该内容您无权删除!",0);
  106. }
  107. CategoryModel::where("id",$id)->delete();
  108. return true;
  109. }
  110. /**
  111. * 删除商品分类
  112. * @param $id
  113. * @return bool
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public static function deleteGoods($id){
  119. // 检测是否有子分类
  120. if(CategoryModel::where("pid",$id)->count() > 0){
  121. throw new \Exception("该分类下有子分类,请先删除子分类。",0);
  122. }
  123. // 检测是否有商品
  124. if(Goods::where("cat_id",$id)->count() > 0){
  125. return Response::returnArray("该分类下有商品,请先删除商品。",0);
  126. }
  127. $row = CategoryModel::where("id",$id)->find();
  128. if(empty($row)){
  129. throw new \Exception("您要删除的内容不存在",0);
  130. }else if($row["is_lock"] == 1){
  131. throw new \Exception("该内容您无权删除!",0);
  132. }
  133. CategoryModel::where("id",$id)->delete();
  134. return true;
  135. }
  136. /**
  137. * 更新字段值
  138. * @return CategoryModel
  139. */
  140. public static function setFields(){
  141. $data = self::getFields();
  142. return CategoryModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  143. }
  144. /**
  145. * 查找下一级分类
  146. * @param $id
  147. * @param string $field
  148. * @param array $res
  149. * @return array|mixed
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public static function getChildren($id,$field='id',$res=[]){
  155. $list = CategoryModel::where(["pid"=>$id,"status"=>0])->select()->toArray();
  156. foreach($list as $key=>$value){
  157. if(!empty($field) && isset($value[$field])){
  158. $res[] = $value[$field];
  159. $res = array_merge($res,self::getChildren($value['id'],$field));
  160. }else{
  161. $res[] = $value;
  162. $res = array_merge($res,self::getChildren($value['id'],$field));
  163. }
  164. }
  165. return $res;
  166. }
  167. /**
  168. * 查找上一级分类
  169. * @param $id
  170. * @param array $res
  171. * @return array|mixed
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public static function get_parent($id,$res=[]){
  177. $row = CategoryModel::where("id",$id)->find();
  178. if(empty($row)){
  179. return $res;
  180. }
  181. $res[] = $row;
  182. if($row["pid"] != 0){
  183. $res[] = array_merge($res,self::get_parent($res["pid"]));
  184. }
  185. return $res;
  186. }
  187. public static function getTreeData($data,$id=0) {
  188. $tree = [];
  189. while ($id > 0) {
  190. foreach ($data as $v) {
  191. if ($v['id'] == $id) {
  192. $tree[] = $v;
  193. $id = $v['pid'];
  194. break;
  195. }
  196. }
  197. }
  198. return $tree;
  199. }
  200. public static function checkTree($result, $data) {
  201. $tree = self::getTreeData($result, $data["pid"]);
  202. $flag = true;
  203. foreach ($tree as $v) {
  204. if ($v['pid'] == $data["id"]) {
  205. $flag = false;
  206. break;
  207. }
  208. }
  209. return $flag;
  210. }
  211. }