MallCateService.php 4.9 KB

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