| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace addons\QiJuhe\common\service;
- use addons\QiJuhe\common\models\GoodsSkuModel;
- use common\enums\RabbitMqEnum;
- use Yii;
- use yii\base\BaseObject;
- /**
- * Debug
- * @package addons\JuheShop\common\service
- */
- class DebugService extends BaseObject
- {
- protected $hander = [
- 'storeCount' => 'storeCount',
- 'updateStore' => 'updateStore',
- ];
- /**
- * @var mixed
- */
- protected $result;
- /**
- * @var string
- */
- protected $errorMsg;
- /**
- * @var int
- */
- public $mall_id;
- public function init()
- {
- if (empty($this->mall_id)) {
- $this->mall_id = 0;
- }
- }
- protected function success($result = [])
- {
- $this->result = $result;
- return true;
- }
- protected function error($msg)
- {
- $this->errorMsg = $msg;
- return false;
- }
- public function getResult()
- {
- return $this->result;
- }
- public function getError()
- {
- return $this->errorMsg;
- }
- /**
- * 执行触发器
- *
- * @param string $type
- * @return boolean
- */
- public function hander(string $type)
- {
- if (!in_array($type, $this->hander)) {
- return $this->error('处理方式不存在');
- }
-
- if ($this->hasMethod($method = $this->hander[$type])) {
- return $this->$method();
- }
- }
-
- /**
- * 获取选品数量
- *
- * @return boolean
- */
- protected function storeCount()
- {
- $list = GoodsSkuModel::getImportedGoodsIds($this->mall_id);
- return $this->success(
- count($list)
- );
- }
- /**
- * 更新已导入选品信息
- *
- * @return boolean
- */
- protected function updateStore()
- {
- // $list = StorageModel::getListByMallIdAndImported($this->mall_id, ['id', 'goods_id']);
- $list = GoodsSkuModel::getImportedGoodsIds($this->mall_id);
- $list = static::dataGroup($list);
-
- foreach ($list as $arr) {
- $goods_ids = array_column($arr, 'id');
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'ids' => $goods_ids,
- 'mall_id' => $this->mall_id,
- ], QiGoodsService::class, 'handleSync');
- }
- return $this->success();
- }
- private static function dataGroup(array $data, $limit = 20)
- {
- $num = ceil(count($data) / $limit);
- $page_data = [];
- for ($i = 0; $i < $num; $i++) {
- isset($data[$i * $limit]) && $page_data[] = array_slice($data, $i * $limit, $limit);
- }
- return $page_data;
- }
- }
|