$this->mall_id, 'is_delete' => 0]); if (empty($base_setting)) { $this->error = '请先保存基础设置'; return false; } $model = SupplysModel::findOne(['mall_id' => $this->mall_id, 'id' => $id]); empty($model) && $model = new SupplysModel(); // 旧配置 $old = json_encode([$model->sell, $model->cost, $model->original]); foreach ($data as $k => $v) { is_array($v) && $data[$k] = Json::encode($v); } $array['mall_id'] = $this->mall_id; $array['updated_at'] = time(); empty($model->id) && $array['created_at'] = $array['updated_at']; $data = array_merge($data, $array); $model->load($data, ''); // 新配置 $new = json_encode([$model->sell, $model->cost, $model->original]); if ($old != $new) { $this->addChangeDelay($model->id); } return $model->save(); } /** * 获取中台列表 * * @param array $params 查询条件 * @return array */ public function getSupplysList($params) { $limit = $params['limit'] ?? 30; $page = $params['page'] ?? 1; $model = SupplysModel::find(); $model->where(['mall_id' => $this->mall_id]); !empty($params['name']) && $model->andWhere(['like', 'app_name', $params['name']]); $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]); $model->limit($pagination->limit)->offset($pagination->offset); $list = $model->orderBy('id desc') ->asArray() ->all(); $service = new SettingService(['mall_id' => $this->mall_id]); foreach ($list as $k => $v) { $list[$k]['callback_url'] = $service->getCallbackUrl() . "?supplys=" . $v['id']; } $data['list'] = $list; $data['page_count'] = $pagination->pageCount; $data['current_page'] = $page + 1; return $data; } /** * 获取实时详情 * * @param integer $id * @return array */ public function getInfo($id) { $model = SupplysModel::find(); $info = $model ->where(['id' => $id, 'mall_id' => $this->mall_id]) ->asArray() ->one(); isset($info['original']) && $info['original'] = Json::decode($info['original']); isset($info['sell']) && $info['sell'] = Json::decode($info['sell']); isset($info['cost']) && $info['cost'] = Json::decode($info['cost']); return $info; } /** * 获取供应链销售策略 * * @param integer $mall_id * @param integer $supply_id * @return array */ public static function getStrategySet($mall_id, $supply_id) { $where = ['id' => $supply_id, 'mall_id' => $mall_id]; $supply_set = SupplysModel::find()->where($where)->select('sell, cost, original')->asArray()->one(); isset($supply_set['original']) && $supply_set['original'] = Json::decode($supply_set['original']); isset($supply_set['sell']) && $supply_set['sell'] = Json::decode($supply_set['sell']); isset($supply_set['cost']) && $supply_set['cost'] = Json::decode($supply_set['cost']); $set = []; foreach ($supply_set as $setting) { $set = array_merge($set, $setting); } // 销售价 $set['sell_value'] = 100; if (isset($set['sell_strategy'])) { switch ($set['sell_strategy']) { // 按指导价 case '1': $set['sell_value'] = $set['sell_guide']; break; // 按协议价 case '2': $set['sell_value'] = $set['sell_agreement']; break; // 按营销价 case '3': $set['sell_value'] = $set['sell_sale']; break; // 默认100% default: $set['sell_value'] = 100; break; } } // 成本价 $set['cost_value'] = 100; if (isset($set['cost_strategy'])) { switch ($set['cost_strategy']) { // 按指导价 case '1': $set['cost_value'] = $set['cost_guide']; break; // 按协议价 case '2': $set['cost_value'] = $set['cost_agreement']; break; // 按营销价 case '3': $set['cost_value'] = $set['cost_sale']; break; // 默认100% default: $set['cost_value'] = 100; break; } } // 原价 $set['original_value'] = 100; if (isset($set['original_strategy'])) { switch ($set['original_strategy']) { // 按指导价 case '1': $set['original_value'] = $set['original_guide']; break; // 按协议价 case '2': $set['original_value'] = $set['original_agreement']; break; // 按营销价 case '3': $set['original_value'] = $set['original_sale']; break; // 默认100% default: $set['original_value'] = 100; break; } } return $set; } /** * 获取设置 * * @param integer $mall_id * @param integer $supply_id * @return array */ public static function getSet($mall_id, $supply_id) { $base_setting = SettingService::getSet($mall_id); $set = self::getStrategySet($mall_id, $supply_id); return array_merge($base_setting, $set); } /** * 添加修改延迟任务 * * @param int $supply_id * @return void */ public function addChangeDelay($supply_id = null) { // batch /* $batch = StorageModel::find() ->select('id, mall_id, goods_id') ->where(['mall_id' => $this->mall_id, 'supply_id' => $supply_id, 'is_import' => 2, 'is_delete' => 0]); foreach ($batch->batch(20) as $items) { // ID $ids = array_column($items, 'id'); // 添加延迟任务 Yii::$app->services->rabbitMq->delay(5, [ 'mall_id' => $this->mall_id, 'ids' => $ids, ], self::class, 'addChangeTask'); } */ Yii::$app->services->rabbitMq->delay(5, [ 'mall_id' => $this->mall_id, 'supply_id' => $supply_id, 'page' => 0, ], self::class, 'addDelayTask'); } /** * 添加延迟队列 * * @param array $params * @return void */ public static function addDelayTask(array $params) { $page = $params['page'] ?? 0; $limit = 10000; $offset = bcmul($limit, $page); $batch = StorageModel::find() ->select('id, mall_id, goods_id') ->where(['mall_id' => $params['mall_id'], 'supply_id' => $params['supply_id'], 'is_import' => 2, 'is_delete' => 0]) ->orderBy('id asc') ->limit($limit) ->offset($offset); $count = 0; foreach ($batch->batch(20) as $items) { // ID $ids = array_column($items, 'id'); // 添加延迟任务 Yii::$app->services->rabbitMq->delay(5, [ 'mall_id' => $params['mall_id'], 'ids' => $ids, ], self::class, 'addChangeTask'); $count = bcadd($count, count($items)); } Yii::error('当前数量: ' . $count); if ($count == $limit) { $page++; Yii::$app->services->rabbitMq->delay(5, array_merge($params, ['page' => $page]), self::class, 'addDelayTask'); } } /** * 添加修改任务 * * @param array $params * @return void */ public static function addChangeTask(array $params) { QiGoodsService::handleSync($params); } }