Category.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\diy;
  13. use app\model\BaseModel;
  14. use think\facade\Cache;
  15. /**
  16. * 自定义模板分类
  17. */
  18. class Category extends BaseModel
  19. {
  20. /**
  21. * 添加自定义模板分类
  22. * @param $data
  23. * @return array
  24. */
  25. public function addCategory($data)
  26. {
  27. $count = model("diy_template_category")->getCount([ [ 'name', '=', $data[ 'name' ] ], [ 'pid', '=', $data[ 'pid' ] ] ]);
  28. if ($count > 0) {
  29. return $this->error('', '模板分类名称已存在');
  30. }
  31. $data[ 'create_time' ] = time();
  32. $data[ 'level' ] = $data[ 'pid' ] ? 2 : 1;
  33. $res = model("diy_template_category")->add($data);
  34. return $this->success($res);
  35. }
  36. /**
  37. * 编辑自定义模板分类
  38. * @param $data
  39. * @param $condition
  40. * @return array
  41. */
  42. public function editCategory($data, $condition)
  43. {
  44. $data[ 'modify_time' ] = time();
  45. $res = model("diy_template_category")->update($data, $condition);
  46. return $this->success($res);
  47. }
  48. /**
  49. * 删除自定义模板分类
  50. * @param $condition
  51. * @return array
  52. */
  53. public function deleteCategory($condition)
  54. {
  55. $id = model('diy_template_category')->getValue($condition, 'category_id');
  56. $child_id = model('diy_template_category')->getValue([ [ 'pid', '=', $id ] ], 'category_id');
  57. if (!empty($child_id)) return $this->error('', '当前模板分类存在子模板分类,不可删除');
  58. $info = model('diy_template_goods')->getInfo([ [ 'category_id', '=', $id ] ], 'category_id');
  59. if (!empty($info)) return $this->error('', '当前模板分类正在使用,不可删除');
  60. $res = model("diy_template_category")->delete($condition);
  61. return $this->success($res);
  62. }
  63. /**
  64. * 获取自定义模板分类信息
  65. * @param $condition
  66. * @param string $field
  67. * @return array
  68. */
  69. public function getCategoryInfo($condition, $field = '*')
  70. {
  71. $info = model("diy_template_category")->getInfo($condition, $field);
  72. return $this->success($info);
  73. }
  74. /**
  75. * 自定义模板分类列表
  76. * @param array $condition
  77. * @param int $page
  78. * @param int $page_size
  79. * @param string $order
  80. * @param string $field
  81. * @return array
  82. */
  83. public function getCategoryTree($condition = [], $order = 'create_time desc', $field = 'category_id, name, pid, level, state')
  84. {
  85. $list = model("diy_template_category")->getList($condition, $field, $order);
  86. $category_list = [];
  87. //遍历一级商品分类
  88. foreach ($list as $k => $v) {
  89. if ($v[ 'level' ] == 1) {
  90. $category_list[] = $v;
  91. unset($list[ $k ]);
  92. }
  93. }
  94. $list = array_values($list);
  95. //遍历二级商品分类
  96. foreach ($list as $k => $v) {
  97. foreach ($category_list as $ck => $cv) {
  98. if ($v[ 'level' ] == 2 && $cv[ 'category_id' ] == $v[ 'pid' ]) {
  99. $category_list[ $ck ][ 'child_list' ][] = $v;
  100. unset($list[ $k ]);
  101. }
  102. }
  103. }
  104. return $this->success($category_list);
  105. }
  106. /**
  107. * 自定义模板分类列表
  108. * @param array $condition
  109. * @param string $field
  110. * @param string $order
  111. * @return array
  112. */
  113. public function getCategoryList($condition = [], $field = '*', $order = '')
  114. {
  115. $list = model("diy_template_category")->getList($condition, $field, $order);
  116. return $this->success($list);
  117. }
  118. /**
  119. * 自定义模板分类列表
  120. * @param array $condition
  121. * @param int $page
  122. * @param int $page_size
  123. * @param string $field
  124. * @param string $order
  125. * @param string $alias
  126. * @param array $join
  127. * @return array
  128. */
  129. public function getCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = '', $alias = 'a', $join = [])
  130. {
  131. $list = model("diy_template_category")->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  132. return $this->success($list);
  133. }
  134. }