| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace app\jobs\storeSync;
- use app\models\ShareHolderLevel;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //股东
- class ShareHolderConfigJob extends BaseObject implements JobInterface
- {
- public array $activity_id;
- public array $to_store_id;
- public int $from_store_id;
- public function execute($queue)
- {
- // TODO: Implement execute() method.
- $from_store_id = $this->from_store_id;
- $to_store_id = $this->to_store_id;
- foreach ($to_store_id as $store_id_item) {
- //等级同步
- $level_result = $this->shareHolderLevelConfig($from_store_id, $store_id_item);
- }
- }
- //等级同步
- private function shareHolderLevelConfig($from_store_id, $to_store_id) {
- try {
- $shareHolderLevelList = ShareHolderLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->orderBy(['level' => 'ASC'])->asArray()->all();
- $level_cache = [];
- foreach ($shareHolderLevelList as $item) {
- $share_holder_level = ShareHolderLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
- if (!$share_holder_level) {
- $share_holder_level = new ShareHolderLevel();
- }
- $share_holder_level_id = $item['id'];
- unset($item['id']);
- $share_holder_level->attributes = $item;
- $share_holder_level->store_id = $to_store_id;
- if ($share_holder_level->save()) {
- $level_cache[$item['id']] = $share_holder_level->id;
- }
- $condition = json_decode($share_holder_level->condition, true);
- if ($condition) {
- foreach ($condition as $condition_index => $condition_item) {
- //商品升级
- if ($condition_index === 'goods') {
- if ($condition_item['value']['id']) {
- $id = [];
- if ($condition_item['value']['id']) {
- if (is_array($condition_item['value']['id'])) {
- foreach ($condition_item['value']['id'] as $goods_id_item) {
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $goods_id_item
- ]);
- if ($storeSyncExtLog && !$storeSyncExtLog->to_id) {
- continue;
- }
- $id[] = $storeSyncExtLog->to_id;
- }
- } else {
- $id = [$condition_item['value']['id']];
- }
- }
- $condition[$condition_index]['value']['id'] = $id;
- }
- }
- if ($condition_index === 'shareholder') {
- if ($condition_item['from_level_id']) {
- $condition[$condition_index]['from_level_id'] = $level_cache[$condition_item['from_level_id']];
- }
- if ($condition_item['to_level_id']) {
- $condition[$condition_index]['to_level_id'] = $level_cache[$condition_item['to_level_id']];
- }
- }
- }
- }
- $share_holder_level->condition = json_encode($condition, JSON_UNESCAPED_UNICODE);
- $share_holder_level->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $share_holder_level_id, $share_holder_level->id, StoreSyncExtLog::TYPE_SHARE_HOLDER_CONFIG);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功',
- 'data' => [
- 'level_cache' => $level_cache
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|