Category.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use think\facade\Cache;
  12. use app\model\BaseModel;
  13. use app\model\system\Config;
  14. /**
  15. * 门店分类
  16. */
  17. class Category extends BaseModel
  18. {
  19. /**
  20. * 门店分类设置
  21. * @param $data
  22. * @param $site_id
  23. * @return array
  24. */
  25. public function setCategoryConfig($data, $site_id)
  26. {
  27. return ( new Config() )->setConfig($data, '门店分类设置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'STORE_CATEGORY' ] ]);
  28. }
  29. /**
  30. * 获取门店分类设置
  31. * @param $site_id
  32. * @return array
  33. */
  34. public function getCategoryConfig($site_id)
  35. {
  36. $data = ( new Config() )->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'STORE_CATEGORY' ] ]);
  37. if (empty($data[ 'data' ][ 'value' ])) {
  38. $data[ 'data' ][ 'value' ] = [
  39. 'status' => 0
  40. ];
  41. }
  42. return $data;
  43. }
  44. /**
  45. * 添加门店分类
  46. *
  47. * @param array $data
  48. */
  49. public function addStoreCategory($data)
  50. {
  51. $res = model('store_category')->add($data);
  52. Cache::tag("store_category")->clear();
  53. return $this->success($res);
  54. }
  55. /**
  56. * 修改门店分类
  57. *
  58. * @param array $data
  59. * @param array $condition
  60. */
  61. public function editStoreCategory($data, $condition)
  62. {
  63. $check_condition = array_column($condition, 2, 0);
  64. $category_id = isset($check_condition[ 'category_id' ]) ? $check_condition[ 'category_id' ] : 0;
  65. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : 0;
  66. if (empty($category_id)) return $this->error('', 'category_id不能为空');
  67. if (empty($site_id)) return $this->error('', 'site_id不能为空');
  68. $res = model('store_category')->update($data, $condition);
  69. if (isset($data[ 'category_name' ]) && !empty($data[ 'category_name' ])) model('store')->update([ 'category_name' => $data[ 'category_name' ] ], [ [ 'category_id', '=', $category_id ], [ 'site_id', '=', $site_id ] ]);
  70. Cache::tag("store_category")->clear();
  71. return $this->success($res);
  72. }
  73. /**
  74. * 删除门店分类
  75. * @param array $condition
  76. */
  77. public function deleteStoreCategory($condition)
  78. {
  79. $check_condition = array_column($condition, 2, 0);
  80. $category_id = isset($check_condition[ 'category_id' ]) ? array_filter(explode(',', $check_condition[ 'category_id' ])) : [];
  81. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : 0;
  82. if (empty($category_id)) return $this->error('', 'category_id不能为空');
  83. if (empty($site_id)) return $this->error('', 'site_id不能为空');
  84. $res = model('store_category')->delete($condition);
  85. model('store')->update([ 'category_name' => '', 'category_id' => '' ], [ [ 'category_id', 'in', $category_id ], [ 'site_id', '=', $site_id ] ]);
  86. Cache::tag("store_category")->clear();
  87. return $this->success($res);
  88. }
  89. /**
  90. * 获取门店分类信息
  91. *
  92. * @param array $condition
  93. * @param string $field
  94. */
  95. public function getStoreCategoryInfo($condition = [], $field = '*')
  96. {
  97. $data = json_encode([ $condition, $field ]);
  98. $cache = Cache::get("store_category_getStoreCategoryInfo_" . $data);
  99. if (!empty($cache)) {
  100. return $this->success($cache);
  101. }
  102. $info = model('store_category')->getInfo($condition, $field);
  103. Cache::tag("store_category")->set("store_category_getStoreCategoryInfo_" . $data, $info);
  104. return $this->success($info);
  105. }
  106. /**
  107. * 获取门店分类列表
  108. *
  109. * @param array $condition
  110. * @param string $field
  111. * @param string $order
  112. * @param string $limit
  113. */
  114. public function getStoreCategoryList($condition = [], $field = '*', $order = 'sort asc, category_id asc', $limit = null)
  115. {
  116. $data = json_encode([ $condition, $field, $order, $limit ]);
  117. $cache = Cache::get("store_category_getStoreCategoryList_" . $data);
  118. if (!empty($cache)) {
  119. return $this->success($cache);
  120. }
  121. $list = model('store_category')->getList($condition, $field, $order, '', '', '', $limit);
  122. Cache::tag("store_category")->set("store_category_getStoreCategoryList_" . $data, $list);
  123. return $this->success($list);
  124. }
  125. /**
  126. * 获取门店分类分页列表
  127. *
  128. * @param array $condition
  129. * @param number $page
  130. * @param string $page_size
  131. * @param string $order
  132. * @param string $field
  133. */
  134. public function getStoreCategoryPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'category_id desc', $field = '*')
  135. {
  136. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  137. $cache = Cache::get("store_category_getStoreCategoryPageList_" . $data);
  138. if (!empty($cache)) {
  139. return $this->success($cache);
  140. }
  141. $list = model('store_category')->pageList($condition, $field, $order, $page, $page_size);
  142. Cache::tag("store_category")->set("store_category_getStoreCategoryPageList_" . $data, $list);
  143. return $this->success($list);
  144. }
  145. }