CategoryService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace addons\QiDingDong\common\service;
  3. use addons\QiDingDong\common\enums\QiDingDongEnums;
  4. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\GoodsCategoryModel;
  5. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\GoodsCategoryRequest;
  6. use addons\QiDingDong\common\helper\RequestHelper;
  7. use addons\QiDingDong\common\models\QiDingDongCate;
  8. use common\enums\RabbitMqEnum;
  9. use common\enums\StatusEnum;
  10. use common\helpers\FormatHelper;
  11. use common\models\goods\GoodsCate;
  12. class CategoryService
  13. {
  14. /**
  15. * @param int $mall_id
  16. * @param int $parent_id
  17. * @return string|true
  18. */
  19. public function find(int $mall_id, int $parent_id = 0)
  20. {
  21. try {
  22. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
  23. [
  24. 'mall_id' => $mall_id,
  25. 'parent_id' => $parent_id
  26. ],
  27. self::class, 'async');
  28. // $this->async(['mall_id' => $mall_id, 'parent_id' => $parent_id]);
  29. } catch (\Exception $e) {
  30. return $e->getMessage();
  31. }
  32. return true;
  33. }
  34. /**
  35. * @param array $params
  36. * @return bool
  37. */
  38. public function async(array $params)
  39. {
  40. try {
  41. $mall_id = $params['mall_id'];
  42. $parent_id = $params['parent_id'];
  43. $resp = $this->req($mall_id, $parent_id);
  44. if (is_array($resp) && !empty($resp)) {
  45. $cat_ids = array_column($resp, 'cate_id');
  46. $local_list = QiDingDongCate::lists([
  47. 'where' => [
  48. ['third_cate_id' => $cat_ids],
  49. ['mall_id' => $mall_id],
  50. ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
  51. ],
  52. 'select'=> 'third_cate_id',
  53. 'index' => 'third_cate_id'
  54. ]);
  55. $inserts = [];
  56. foreach ($resp as $v) {
  57. if (!empty($local_list[$v['cate_id']])) {
  58. continue;
  59. }
  60. $v['mall_id'] = $mall_id;
  61. $v['created_at'] = time();
  62. $v['updated_at'] = time();
  63. $v['third_cate_id'] = $v['cate_id'];
  64. $v['cate_id'] = 0;
  65. $inserts[] = $v;
  66. }
  67. if (!empty($inserts)) QiDingDongCate::find()->createCommand()->batchInsert(QiDingDongCate::tableName(), array_keys($inserts[0]), $inserts)->execute();
  68. $this->writeCateTable($mall_id, $cat_ids);
  69. foreach ($resp as $val) {
  70. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
  71. [
  72. 'mall_id' => $mall_id,
  73. 'parent_id' => $val['cate_id']
  74. ],
  75. self::class, 'async');
  76. // $this->async([
  77. // 'mall_id' => $mall_id,
  78. // 'parent_id' => $val['cate_id']
  79. // ]);
  80. }
  81. }
  82. } catch (\Exception $e) {
  83. \Yii::error(FormatHelper::exception($e, __METHOD__));
  84. return false;
  85. }
  86. return true;
  87. }
  88. /**
  89. * @param int $mall_id
  90. * @param array $third_cat_ids
  91. * @return void
  92. * @throws \Exception
  93. */
  94. protected function writeCateTable(int $mall_id, array $third_cat_ids)
  95. {
  96. $l_cats = QiDingDongCate::lists([
  97. 'where' => [
  98. ['mall_id' => $mall_id],
  99. ['third_cate_id' => $third_cat_ids],
  100. ['cate_id' => StatusEnum::DISABLED]
  101. ]
  102. ], false);
  103. if (!empty($l_cats)) {
  104. /**
  105. * @var $cat QiDingDongCate
  106. */
  107. foreach ($l_cats as $cat) {
  108. if (!empty($cat->parent_id)) {
  109. $parentCat = QiDingDongCate::findOne(['mall_id' => $mall_id, 'third_cate_id' => $cat->parent_id]);
  110. }
  111. $lCat = GoodsCate::setData([
  112. 'mall_id' => $mall_id,
  113. 'mch_id' => 0,
  114. 'parent_id' => !empty($parentCat) ? $parentCat->cate_id : 0,
  115. 'name' => $cat->cate_name,
  116. 'pic' => $cat->icon
  117. ]);
  118. if (!$lCat) throw new \Exception(GoodsCate::getStaticError());
  119. $cat->cate_id = $lCat->id;
  120. if (!$cat->save()) throw new \Exception($cat->getErrorMessage());
  121. }
  122. }
  123. }
  124. /**
  125. * @param $mall_id
  126. * @param $parent_id
  127. * @return array|int|string
  128. * @throws \Exception
  129. */
  130. public function req($mall_id, $parent_id)
  131. {
  132. $contentModel = RequestHelper::contentModelInstance($mall_id);
  133. $contentModel->content->data = new GoodsCategoryModel();
  134. $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 固定值
  135. $contentModel->content->data->parent_id = $parent_id; // 固定值
  136. return RequestHelper::send(new GoodsCategoryRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id);
  137. }
  138. public static function custom(int $mall_id)
  139. {
  140. return QiDingDongCate::lists([
  141. 'where' => [
  142. ['mall_id' => $mall_id],
  143. ['level' => 1],
  144. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  145. ],
  146. 'select'=> 'cate_id, cate_name, third_cate_id'
  147. ]);
  148. }
  149. public static function firstCate(int $mall_id, int $pid, int $cid)
  150. {
  151. if (empty($cid)) return 0;
  152. $request = new CategoryService();
  153. try {
  154. /**
  155. * @var $cat QiDingDongCate
  156. */
  157. $cat = QiDingDongCate::getOne([
  158. ['mall_id' => $mall_id],
  159. ['third_cate_id' => $cid]
  160. ]);
  161. if (!empty($cat) && !empty($cat->cate_id)) return $cat->cate_id;
  162. $t = \Yii::$app->db->beginTransaction();
  163. // 没数据 就需要写入数据
  164. if (empty($cat)) {
  165. $resp = $request->req($mall_id, $pid);
  166. if (!empty($resp)) {
  167. $r_cat = null;
  168. foreach ($resp as $item) {
  169. if ($item['cate_id'] == $cid) {
  170. $r_cat = $item;
  171. break;
  172. }
  173. }
  174. if (!empty($r_cat)) {
  175. $cat = new QiDingDongCate();
  176. $cat->mall_id = $mall_id;
  177. $cat->cate_name = $r_cat['cate_name'];
  178. $cat->level = $r_cat['level'];
  179. $cat->icon = $r_cat['icon'];
  180. $cat->select_icon = $r_cat['select_icon'];
  181. $cat->parent_id = $pid;
  182. $cat->third_cate_id = $cid;
  183. if (!$cat->save()) throw new \Exception($cat->getErrorMessage());
  184. } else {
  185. $cat = null;
  186. }
  187. // if (empty($r_cat)) throw new \Exception("未找到 $pid -> $cid 类目");
  188. }
  189. }
  190. if (!empty($cat) && empty($cat->cate_id)) {
  191. $lCat = GoodsCate::setData([
  192. 'mall_id' => $mall_id,
  193. 'mch_id' => 0,
  194. 'parent_id' => !empty($cat->parent_id) ? intval(QiDingDongCate::find()->where(['mall_id' => $mall_id])
  195. ->andWhere(['third_cate_id' => $cat->parent_id])->select('cate_id')->scalar()) : 0,
  196. 'name' => $cat->cate_name,
  197. 'pic' => $cat->icon
  198. ]);
  199. if (!$lCat) throw new \Exception(GoodsCate::getStaticError());
  200. $cat->cate_id = $lCat->id;
  201. if (!$cat->save()) throw new \Exception($cat->getErrorMessage());
  202. }
  203. $t->commit();
  204. return $cat->cate_id ?? 0;
  205. } catch (\Exception $e) {
  206. if (!empty($t)) $t->rollBack();
  207. return $e->getMessage();
  208. }
  209. }
  210. }