CategoryService.php 8.6 KB

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