GoodsShopCategory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\goods;
  13. use think\facade\Cache;
  14. use app\model\BaseModel;
  15. /**
  16. * 商品店内分类
  17. */
  18. class GoodsShopCategory extends BaseModel
  19. {
  20. /**
  21. * 添加商品店内分类
  22. * @param $data
  23. * @return \multitype
  24. */
  25. public function addShopCategory($data)
  26. {
  27. $category_id = model('goods_shop_category')->add($data);
  28. Cache::tag("goods_shop_category")->clear();
  29. return $this->success($category_id);
  30. }
  31. /**
  32. * 修改商品店内分类
  33. * @param $data
  34. * @return \multitype
  35. */
  36. public function editShopCategory($data)
  37. {
  38. $res = model('goods_shop_category')->update($data, [ [ 'category_id', '=', $data['category_id'] ] ]);
  39. Cache::tag("goods_shop_category")->clear();
  40. return $this->success($res);
  41. }
  42. /**
  43. * 删除分类
  44. * @param $category_id
  45. * @return \multitype
  46. */
  47. public function deleteShopCategory($category_id)
  48. {
  49. $goods_shop_category_info = $this->getShopCategoryInfo([
  50. [ 'category_id', '=', $category_id ]
  51. ], "level");
  52. $goods_shop_category_info = $goods_shop_category_info['data'];
  53. $field = "category_id_" . $goods_shop_category_info['level'];
  54. $res = model('goods_shop_category')->delete([ [ $field, '=', $category_id ] ]);
  55. Cache::tag("goods_shop_category")->clear();
  56. return $this->success($res);
  57. }
  58. /**
  59. * 获取商品店内分类信息
  60. * @param array $condition
  61. * @param string $field
  62. */
  63. public function getShopCategoryInfo($condition, $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,keywords,description,category_id_1,category_id_2,category_full_name')
  64. {
  65. $data = json_encode([ $condition, $field ]);
  66. $cache = Cache::get("goods_shop_category_getShopCategoryInfo_" . $data);
  67. if (!empty($cache)) {
  68. return $this->success($cache);
  69. }
  70. $res = model('goods_shop_category')->getInfo($condition, $field);
  71. Cache::tag("goods_shop_category")->set("goods_shop_category_getShopCategoryInfo_" . $data, $res);
  72. return $this->success($res);
  73. }
  74. /**
  75. * 获取商品店内分类列表
  76. * @param array $condition
  77. * @param string $field
  78. * @param string $order
  79. * @param null $limit
  80. * @return \multitype
  81. */
  82. public function getShopCategoryList($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2', $order = '', $limit = null)
  83. {
  84. $data = json_encode([ $condition, $field, $order, $limit ]);
  85. $cache = Cache::get("goods_shop_category_getShopCategoryList_" . $data);
  86. if (!empty($cache)) {
  87. return $this->success($cache);
  88. }
  89. $list = model('goods_shop_category')->getList($condition, $field, $order, '', '', '', $limit);
  90. Cache::tag("goods_shop_category")->set("goods_shop_category_getShopCategoryList_" . $data, $list);
  91. return $this->success($list);
  92. }
  93. /**
  94. * 获取商品店内分类树结构
  95. * @param array $condition
  96. * @param string $field
  97. * @param string $order
  98. * @param null $limit
  99. * @return \multitype
  100. */
  101. public function getShopCategoryTree($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2', $order = '', $limit = null)
  102. {
  103. $data = json_encode([ $condition, $field, $order, $limit ]);
  104. $cache = Cache::get("goods_shop_category_getShopCategoryTree_" . $data);
  105. if (!empty($cache)) {
  106. return $this->success($cache);
  107. }
  108. $list = model('goods_shop_category')->getList($condition, $field, $order, '', '', '', $limit);
  109. $goods_shop_category_list = [];
  110. //遍历一级商品店内分类
  111. foreach ($list as $k => $v) {
  112. if ($v['level'] == 1) {
  113. $goods_shop_category_list[] = $v;
  114. unset($list[ $k ]);
  115. }
  116. }
  117. $list = array_values($list);
  118. //遍历二级商品店内分类
  119. foreach ($list as $k => $v) {
  120. foreach ($goods_shop_category_list as $ck => $cv) {
  121. if ($v['level'] == 2 && $cv['category_id'] == $v['pid']) {
  122. $goods_shop_category_list[ $ck ]['child_list'][] = $v;
  123. unset($list[ $k ]);
  124. }
  125. }
  126. }
  127. Cache::tag("goods_shop_category")->set("goods_shop_category_getShopCategoryTree_" . $data, $list);
  128. return $this->success($goods_shop_category_list);
  129. }
  130. /**
  131. * 获取商品店内分类分页列表
  132. * @param array $condition
  133. * @param int $page
  134. * @param int $page_size
  135. * @param string $order
  136. * @param string $field
  137. * @return \multitype
  138. */
  139. public function getShopCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,category_id_1,category_id_2,category_full_name')
  140. {
  141. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  142. $cache = Cache::get("goods_shop_category_getShopCategoryPageList_" . $data);
  143. if (!empty($cache)) {
  144. return $this->success($cache);
  145. }
  146. $list = model('goods_shop_category')->pageList($condition, $field, $order, $page, $page_size);
  147. Cache::tag("goods_shop_category")->set("goods_shop_category_getShopCategoryPageList_" . $data, $list);
  148. return $this->success($list);
  149. }
  150. }