| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace app\jobs\storeSync;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\Qrcode;
- use app\models\ShareLevel;
- use app\models\StoreSyncExtLog;
- use yii\base\BaseObject;
- use yii\db\Exception;
- use yii\queue\JobInterface;
- //分销
- class ShareConfigJob extends BaseObject implements JobInterface
- {
- public array $activity_id;
- public array $to_store_id;
- public int $from_store_id;
- public array $name;
- public function execute($queue)
- {
- // TODO: Implement execute() method.
- $from_store_id = $this->from_store_id;
- $to_store_id = $this->to_store_id;
- $name = $this->name[0];
- foreach ($to_store_id as $store_id_item) {
- //同步配置 基础配置 佣金配置 推广海报
- $result = $this->shareBasicConfig($from_store_id, $store_id_item, $name);
- //同步等级
- // $levelResult = $this->shareLevel($from_store_id, $store_id_item);
- }
- }
- //同步等级
- private function shareLevel($from_store_id, $to_store_id) {
- try {
- $shareLevelList = ShareLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
- foreach ($shareLevelList as $item) {
- //判断是否为当前商城同步过此商品
- $share_level = ShareLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
- if (!$share_level) {
- $share_level = new ShareLevel();
- }
- $id = $item['id'];
- unset($item['id']);
- $share_level->attributes = $item;
- $share_level->store_id = $to_store_id;
- if (!$share_level->save()) {
- return [
- 'code' => 1,
- 'msg' => json_encode($share_level->errors, JSON_UNESCAPED_UNICODE)
- ];
- };
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $share_level->id, StoreSyncExtLog::TYPE_SHARE_CONFIG);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步配置 基础配置 佣金配置 推广海报
- private function shareBasicConfig($from_store_id, $to_store_id, $name) {
- try {
- if ($name === "share_basic_setting") {
- $share_basic_setting = Option::get('share_basic_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
- try {
- $share_basic_setting = json_decode($share_basic_setting, true);
- if (isset($share_basic_setting['share_goods_id']['value'])) {
- $ids = \explode(',', $share_basic_setting['share_goods_id']['value']);
- $arr = [];
- foreach ($ids as $id) {
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $id,
- ]);
- if ($storeSyncExtLog) {
- $arr[] = $storeSyncExtLog->to_id;
- }
- }
- $share_basic_setting['share_goods_id']['value'] = \implode(',', $arr);
- }
- $share_basic_setting = json_encode($share_basic_setting, JSON_UNESCAPED_UNICODE);
- Option::set('share_basic_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
- } catch (Exception $e) {
- }
- }
- if ($name === "share_money_setting") {
- $share_basic_setting = Option::get('share_money_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
- try {
- Option::set('share_money_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
- } catch (Exception $e) {
- }
- }
- if ($name === "share_qrcode") {
- $qrcode = Qrcode::findOne(['store_id' => $from_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- $to_qrcode = Qrcode::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- if (!$to_qrcode) {
- $to_qrcode = new Qrcode();
- }
- unset($qrcode['id']);
- $to_qrcode->attributes = $qrcode->attributes;
- $to_qrcode->store_id = $to_store_id;
- $to_qrcode->created_at = time();
- $to_qrcode->updated_at = time();
- if (!$to_qrcode->save()) {
- return [
- 'code' => 1,
- 'msg' => json_encode($to_qrcode->errors, JSON_UNESCAPED_UNICODE)
- ];
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步分销end **/
- }
|