DebugService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\QiJuhe\common\models\GoodsSkuModel;
  4. use common\enums\RabbitMqEnum;
  5. use Yii;
  6. use yii\base\BaseObject;
  7. /**
  8. * Debug
  9. * @package addons\JuheShop\common\service
  10. */
  11. class DebugService extends BaseObject
  12. {
  13. protected $hander = [
  14. 'storeCount' => 'storeCount',
  15. 'updateStore' => 'updateStore',
  16. ];
  17. /**
  18. * @var mixed
  19. */
  20. protected $result;
  21. /**
  22. * @var string
  23. */
  24. protected $errorMsg;
  25. /**
  26. * @var int
  27. */
  28. public $mall_id;
  29. public function init()
  30. {
  31. if (empty($this->mall_id)) {
  32. $this->mall_id = 0;
  33. }
  34. }
  35. protected function success($result = [])
  36. {
  37. $this->result = $result;
  38. return true;
  39. }
  40. protected function error($msg)
  41. {
  42. $this->errorMsg = $msg;
  43. return false;
  44. }
  45. public function getResult()
  46. {
  47. return $this->result;
  48. }
  49. public function getError()
  50. {
  51. return $this->errorMsg;
  52. }
  53. /**
  54. * 执行触发器
  55. *
  56. * @param string $type
  57. * @return boolean
  58. */
  59. public function hander(string $type)
  60. {
  61. if (!in_array($type, $this->hander)) {
  62. return $this->error('处理方式不存在');
  63. }
  64. if ($this->hasMethod($method = $this->hander[$type])) {
  65. return $this->$method();
  66. }
  67. }
  68. /**
  69. * 获取选品数量
  70. *
  71. * @return boolean
  72. */
  73. protected function storeCount()
  74. {
  75. $list = GoodsSkuModel::getImportedGoodsIds($this->mall_id);
  76. return $this->success(
  77. count($list)
  78. );
  79. }
  80. /**
  81. * 更新已导入选品信息
  82. *
  83. * @return boolean
  84. */
  85. protected function updateStore()
  86. {
  87. // $list = StorageModel::getListByMallIdAndImported($this->mall_id, ['id', 'goods_id']);
  88. $list = GoodsSkuModel::getImportedGoodsIds($this->mall_id);
  89. $list = static::dataGroup($list);
  90. foreach ($list as $arr) {
  91. $goods_ids = array_column($arr, 'id');
  92. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  93. 'ids' => $goods_ids,
  94. 'mall_id' => $this->mall_id,
  95. ], QiGoodsService::class, 'handleSync');
  96. }
  97. return $this->success();
  98. }
  99. private static function dataGroup(array $data, $limit = 20)
  100. {
  101. $num = ceil(count($data) / $limit);
  102. $page_data = [];
  103. for ($i = 0; $i < $num; $i++) {
  104. isset($data[$i * $limit]) && $page_data[] = array_slice($data, $i * $limit, $limit);
  105. }
  106. return $page_data;
  107. }
  108. }