MallCateService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\StarChain\common\services;
  3. use addons\StarChain\common\models\Category;
  4. use addons\StarChain\common\models\MallCate;
  5. use common\enums\StatusEnum;
  6. use Exception;
  7. /**
  8. * 分类服务
  9. */
  10. class MallCateService extends BaseService
  11. {
  12. protected $category_models = [];
  13. protected $first_category = true;
  14. protected $category_end = false;
  15. /**
  16. * 分类级别
  17. *
  18. * @var integer
  19. */
  20. protected $category_level = 1;
  21. /**
  22. * 获取分类ID 不存在创建
  23. *
  24. * @param array $category_1
  25. * @param array $category_2
  26. * @param array $category_3
  27. * @return integer
  28. */
  29. public function getCats($category_1, $category_2, $category_3)
  30. {
  31. // 使用ID
  32. // $category_arr = $this->explodeCategory($info['category_id']);
  33. // 使用名字
  34. $category_arr = $this->getCategoryName($category_1, $category_2, $category_3);
  35. // 倒转
  36. $category_arr = array_reverse($category_arr);
  37. foreach ($category_arr as $category) {
  38. $category_id = $this->checkCategory($category);
  39. // 如果返回分类ID 不继续
  40. if ($category_id) {
  41. return $category_id;
  42. }
  43. // 查询到父类 结束
  44. if ($this->category_end) {
  45. break;
  46. }
  47. }
  48. // 没有返回ID, 需要创建
  49. return $this->createCategory();
  50. }
  51. /**
  52. * 使用名称
  53. *
  54. * @param string $category
  55. * @return void
  56. */
  57. private function checkCategory($category)
  58. {
  59. // 只查询第一个分类,第一个没有则创建全部
  60. // if ($this->first_category) {
  61. $params = [
  62. 'mall_id' => $this->mall_id,
  63. // 'is_delete' => 0,
  64. 'mch_id' => 0,
  65. 'status' => 1,
  66. ];
  67. $category_model = MallCate::find()->where($params)->andWhere(['like', 'name', $category['name']])->one();
  68. // 查询到并且是子分类返回
  69. if (!empty($category_model) && $this->first_category) {
  70. return $category_model->id;
  71. }
  72. $this->first_category = false;
  73. // }
  74. // 父级分类
  75. $pid = !empty($category_model) ? $category_model->id : 0;
  76. $this->setCategoryForm($category['name'], $pid);
  77. }
  78. /**
  79. * 创建分类并返回分类ID
  80. *
  81. * @return int
  82. */
  83. public function createCategory()
  84. {
  85. // 倒序, 从子分类开始加
  86. $models = array_reverse($this->category_models);
  87. $pid = 0;
  88. foreach ($models as $model) {
  89. if (!$model->parent_id) {
  90. $model->parent_id = $pid;
  91. //查找上级数据
  92. $goods_cate = MallCate::find()->where(['mall_id' => $this->mall_id, 'parent_id' => $pid])->andWhere(['<>', 'status', StatusEnum::DELETE])->asArray()->one();
  93. if (!empty($goods_cate)) $model->level = $goods_cate['level'] + 1;
  94. } else {
  95. $model->level = 1;
  96. }
  97. // 保存
  98. $model->save();
  99. $pid = $model->id;
  100. }
  101. return $pid;
  102. }
  103. /**
  104. * 创建分类model
  105. *
  106. * @param string $title
  107. * @return void
  108. */
  109. private function setCategoryForm($title, $pid)
  110. {
  111. if ($pid) {
  112. $level = $this->category_level - 1;
  113. $this->category_models[$level]->parent_id = $pid;
  114. $this->category_end = true;
  115. return;
  116. }
  117. $model = new MallCate();
  118. $model->mall_id = $this->mall_id;
  119. $model->mch_id = 0;
  120. // $model->store_id = 0;
  121. $model->name = $title;
  122. $model->sort = 100;
  123. $model->pic = '';
  124. $model->big_pic = '';
  125. $model->advert_pic = '';
  126. $model->advert_url = '';
  127. $model->status = 1;
  128. $model->is_show = 1;
  129. $model->advert_open_type = '';
  130. $model->advert_params = '';
  131. $model->parent_id = $pid;
  132. $model->level = 0;
  133. $this->category_models[$this->category_level] = $model;
  134. $this->category_level++;
  135. }
  136. /**
  137. * 获取分类
  138. *
  139. * @param string $str
  140. * @return array
  141. */
  142. private function getCategoryName($category_1, $category_2, $category_3)
  143. {
  144. !empty($category_1) && $category_arr[] = ['name' => Category::getCategoryName($category_1)];
  145. !empty($category_2) && $category_arr[] = ['name' => Category::getCategoryName($category_2)];
  146. !empty($category_3) && $category_arr[] = ['name' => Category::getCategoryName($category_3)];
  147. if (empty($category_arr)) {
  148. throw new Exception('供应链返回分类为空');
  149. }
  150. return $category_arr;
  151. }
  152. }