GoodsCategory.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 GoodsCategory extends BaseModel
  19. {
  20. /**
  21. * 添加商品分类
  22. * @param $data
  23. * @return \multitype
  24. */
  25. public function addCategory($data)
  26. {
  27. $category_id = model('goods_category')->add($data);
  28. Cache::tag("goods_category")->clear();
  29. return $this->success($category_id);
  30. }
  31. /**
  32. * 修改商品分类
  33. * @param $data
  34. * @return \multitype
  35. */
  36. public function editCategory($data)
  37. {
  38. $res = model('goods_category')->update($data, [ [ 'category_id', '=', $data['category_id'] ] ]);
  39. if ($res) {
  40. if (isset($data['commission_rate'])) {
  41. //修改对应商品的佣金比率
  42. model('goods')->update([ 'commission_rate' => $data['commission_rate'] ], [ [ 'category_id', '=', $data['category_id'] ] ]);
  43. model('goods_sku')->update([ 'commission_rate' => $data['commission_rate'] ], [ [ 'category_id', '=', $data['category_id'] ] ]);
  44. model('recommend_category')->update([ 'category_name' => $data['category_name'] ], [ [ 'category_id', '=', $data['category_id'] ] ]);
  45. }
  46. }
  47. Cache::tag("goods_category")->clear();
  48. Cache::tag("recommend_category")->clear();
  49. return $this->success($res);
  50. }
  51. /**
  52. * 删除分类
  53. * @param $category_id
  54. * @return \multitype
  55. */
  56. public function deleteCategory($category_id)
  57. {
  58. $goods_category_info = $this->getCategoryInfo([
  59. [ 'category_id', '=', $category_id ]
  60. ], "level");
  61. $goods_category_info = $goods_category_info['data'];
  62. $field = "category_id_" . $goods_category_info['level'];
  63. $res = model('goods_category')->delete([ [ $field, '=', $category_id ] ]);
  64. Cache::tag("goods_category")->clear();
  65. return $this->success($res);
  66. }
  67. /**
  68. * 获取商品分类信息
  69. * @param array $condition
  70. * @param string $field
  71. */
  72. public function getCategoryInfo($condition, $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,keywords,description,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,category_full_name,commission_rate,image_adv')
  73. {
  74. $data = json_encode([ $condition, $field ]);
  75. $cache = Cache::get("goods_category_getCategoryInfo_" . $data);
  76. if (!empty($cache)) {
  77. return $this->success($cache);
  78. }
  79. $res = model('goods_category')->getInfo($condition, $field);
  80. Cache::tag("goods_category")->set("goods_category_getCategoryInfo_" . $data, $res);
  81. return $this->success($res);
  82. }
  83. /**
  84. * 获取商品分类列表
  85. * @param array $condition
  86. * @param string $field
  87. * @param string $order
  88. * @param null $limit
  89. * @return \multitype
  90. */
  91. public function getCategoryList($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate,image_adv', $order = '', $limit = null)
  92. {
  93. $data = json_encode([ $condition, $field, $order, $limit ]);
  94. $cache = Cache::get("goods_category_getCategoryList_" . $data);
  95. if (!empty($cache)) {
  96. return $this->success($cache);
  97. }
  98. $list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
  99. Cache::tag("goods_category")->set("goods_category_getCategoryList_" . $data, $list);
  100. return $this->success($list);
  101. }
  102. /**
  103. * 获取商品分类树结构
  104. * @param int $level 查询等级
  105. * @param array $condition
  106. * @param string $field
  107. * @param string $order
  108. * @param null $limit
  109. * @return \multitype
  110. */
  111. public function getCategoryTree($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate', $order = 'category_id desc', $limit = null)
  112. {
  113. $data = json_encode([ $condition, $field, $order, $limit ]);
  114. $cache = Cache::get("goods_category_getCategoryTree_" . $data);
  115. if (!empty($cache)) {
  116. return $this->success($cache);
  117. }
  118. $list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
  119. $goods_category_list = [];
  120. //遍历一级商品分类
  121. foreach ($list as $k => $v) {
  122. if ($v['level'] == 1) {
  123. $goods_category_list[] = $v;
  124. unset($list[ $k ]);
  125. }
  126. }
  127. $list = array_values($list);
  128. //遍历二级商品分类
  129. foreach ($list as $k => $v) {
  130. foreach ($goods_category_list as $ck => $cv) {
  131. if ($v['level'] == 2 && $cv['category_id'] == $v['pid']) {
  132. $goods_category_list[ $ck ]['child_list'][] = $v;
  133. unset($list[ $k ]);
  134. }
  135. }
  136. }
  137. $list = array_values($list);
  138. //遍历三级商品分类
  139. foreach ($list as $k => $v) {
  140. foreach ($goods_category_list as $ck => $cv) {
  141. if (!empty($cv['child_list'])) {
  142. foreach ($cv['child_list'] as $third_k => $third_v) {
  143. if ($v['level'] == 3 && $third_v['category_id'] == $v['pid']) {
  144. $goods_category_list[ $ck ]['child_list'][ $third_k ]['child_list'][] = $v;
  145. unset($list[ $k ]);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. Cache::tag("goods_category")->set("goods_category_getCategoryTree_" . $data, $goods_category_list);
  152. return $this->success($goods_category_list);
  153. }
  154. /**
  155. * 获取商品分类分页列表
  156. * @param array $condition
  157. * @param int $page
  158. * @param int $page_size
  159. * @param string $order
  160. * @param string $field
  161. * @return \multitype
  162. */
  163. public function getCategoryPageList($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_id_3,category_full_name,commission_rate')
  164. {
  165. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  166. $cache = Cache::get("goods_category_getCategoryPageList_" . $data);
  167. if (!empty($cache)) {
  168. return $this->success($cache);
  169. }
  170. $list = model('goods_category')->pageList($condition, $field, $order, $page, $page_size);
  171. Cache::tag("goods_category")->set("goods_category_getCategoryPageList_" . $data, $list);
  172. return $this->success($list);
  173. }
  174. /**
  175. * 获取商品分类列表
  176. * @param array $condition
  177. * @param string $field
  178. * @param string $order
  179. * @param null $limit
  180. * @return \multitype
  181. */
  182. public function getCategoryByParent($condition = [], $field = 'category_id,category_name,short_name,pid,level,is_show,sort,image,attr_class_id,attr_class_name,category_id_1,category_id_2,category_id_3,commission_rate', $order = '', $limit = null)
  183. {
  184. $data = json_encode([ $condition, $field, $order, $limit ]);
  185. $cache = Cache::get("goods_category_getCategoryByParent_" . $data);
  186. if (!empty($cache)) {
  187. return $this->success($cache);
  188. }
  189. $list = model('goods_category')->getList($condition, $field, $order, '', '', '', $limit);
  190. foreach ($list as $k => $v) {
  191. $child_count = model('goods_category')->getCount([ 'pid' => $v['category_id'] ]);
  192. $list[ $k ]['child_count'] = $child_count;
  193. }
  194. Cache::tag("goods_category")->set("goods_category_getCategoryByParent_" . $data, $list);
  195. return $this->success($list);
  196. }
  197. /**
  198. * 修改排序
  199. * @param int $sort
  200. * @param int $category_id
  201. */
  202. public function modifyGoodsCategorySort($sort, $category_id)
  203. {
  204. $res = model('goods_category')->update([ 'sort' => $sort ], [ [ 'category_id', '=', $category_id ] ]);
  205. Cache::tag("goods_category")->clear();
  206. return $this->success($res);
  207. }
  208. }