| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\jobs;
- use app\constants\OptionSetting;
- use app\models\AboutArticle;
- use app\models\BookingGoodsCat;
- use app\models\BookingGoodsExt;
- use app\models\IntegralSetting;
- use app\models\NewIntegralCat;
- use app\models\NewIntegralSetting;
- use app\models\Option;
- use app\models\PostageRules;
- use app\models\Qrcode;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\ShareHolderLevel;
- use app\models\ShareLevel;
- use app\models\StoreCloud;
- use app\models\StoreSyncExtLog;
- use app\models\Topic;
- use app\models\TopicType;
- use app\models\VideoGoods;
- use app\models\VideoGoodsCat;
- use app\models\VideoGoodsList;
- use app\models\VideoGoodsSetting;
- use app\models\WorkerCat;
- use app\models\WorkerGoodsCat;
- use app\models\WorkerGoodsExt;
- use app\models\WorkerLevel;
- use app\models\WorkerSetting;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MerchantForm;
- use yii\base\BaseObject;
- use yii\db\Exception;
- use yii\queue\JobInterface;
- use app\models\StoreSync;
- use app\models\StoreSyncLog;
- use app\models\ActivityCutPrice;
- use app\models\ActivityCutPriceBanner;
- use app\models\ActivityCutPriceCat;
- use app\models\ActivityCutPriceGoods;
- use app\models\PtActivity;
- use app\models\PtActivityBanner;
- use app\models\PtActivityGoodsCat;
- use app\models\PtActivityGoods;
- use app\models\Goods;
- use app\models\AttrGroup;
- use app\models\Attr;
- use app\models\Cat;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\CloudGoodsBind;
- use app\models\NewDiyTemplate;
- /**
- * 店铺同步
- */
- class StoreSyncJob extends BaseObject implements JobInterface
- {
- public $id;
- public $type;
- public $to_id;
- public function execute($queue)
- {
- try {
- debug_log(['id' => $this->id, 'type' => $this->type, 'to_id' => $this->to_id], 'queuePush.log');
- $type = $this->type;
- $t = \Yii::$app->db->beginTransaction();
- if (isset($type)) {
- $default = \app\models\Option::get('store_template_default', 0, 'saas', null)['value'];
- if(!$default) {
- return;
- }
- $default = json_decode($default, true);
- if (empty($default)) {
- return;
- }
- $fromStoreId = $default['store_id'];
- $imports = $default['type'];
- $toStoreIds = [$this->to_id];
- } else {
- $item = StoreSyncLog::findOne($this->id);
- $syncItem = StoreSync::findOne($item->sync_id);
- $fromStoreId = $item->from_store_id;
- $toStoreIds = \json_decode($item->to_store_ids, true);
- $imports = json_decode($syncItem->imports, true);
- }
- debug_log(['from' => $fromStoreId, 'to_store_' => $toStoreIds, 'imports' => $imports], 'queuePush.log');
- if (in_array('product', $imports) && !in_array('product_cat', $imports)) {
- array_push($imports, 'product_cat');
- }
- foreach ($toStoreIds as $toId) {
- //配送规则
- if (in_array('delivery_rules', $imports)) {
- list($deliveryRulesCache) = $this->syncDeliveryRules($fromStoreId, $toId);
- }
- // if (in_array('product_cat', $imports)) {
- // $cacheCat = $this->doGoodsCat($fromStoreId, $toId);
- // }
- if (in_array('product', $imports) && in_array('delivery_rules', $imports) && in_array('product_cat', $imports)) {
- //商品 分类
- list($goodsCache, $cacheCat) = $this->syncGoods($fromStoreId, $toId, $deliveryRulesCache, $message);
- // if (!empty($message)) {
- // throw new \Exception($message);
- // }
- $cacheCat_ = $cacheCat;
- if (isset($item)) {
- $item->goods_info = json_encode(['goods_result' => $goodsCache, 'cat_result' => $cacheCat]);
- }
- //拼团
- $ptCacheActivity = $ptCacheCat = $ptCache = [];
- if (in_array('pintuan', $imports)) {
- [$ptCacheActivity, $ptCacheBanner, $ptCacheCat, $ptCache, $ptCacheSetting] = $this->syncGroup($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->pintuan = json_encode(['cacheActivity' => $ptCacheActivity, 'cacheBanner' => $ptCacheBanner, 'cacheCat' => $ptCacheCat, 'cache' => $ptCache, 'cacheSetting' => $ptCacheSetting], JSON_UNESCAPED_UNICODE);
- }
- }
- //砍价
- $cutPriceCacheActivity = $cutPriceCacheCat = $cutPriceCache = [];
- if (in_array('activityCutPrice', $imports)) {
- [$cutPriceCacheActivity, $cutPriceCacheBanner, $cutPriceCacheCat, $cutPriceCache] = $this->syncBargain($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->bargain = json_encode(['cacheActivity' => $cutPriceCacheActivity, 'cacheBanner' => $cutPriceCacheBanner, 'cacheCat' => $cutPriceCacheCat, 'cache' => $cutPriceCache], JSON_UNESCAPED_UNICODE);
- }
- }
- //秒杀
- $seckillCacheActivity = $seckillCache = [];
- if (in_array('seckill', $imports)) {
- [$seckillCacheActivity, $seckillCache] = $this->seckill($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->seckill = json_encode(['cacheActivity' => $seckillCacheActivity, 'cache' => $seckillCache], JSON_UNESCAPED_UNICODE);
- }
- }
- //分销设置
- if (in_array('shareConfig', $imports)) {
- [$result, $levelResult] = $this->shareConfig($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->shareConfig = json_encode(['config_result' => $result, 'level_result' => $levelResult], JSON_UNESCAPED_UNICODE);
- }
- }
- //股东设置
- if (in_array('shareHolderConfig', $imports)) {
- [$result, $levelResult] = $this->shareHolderConfig($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->shareHolderConfig = json_encode(['config_result' => $result, 'level_result' => $levelResult], JSON_UNESCAPED_UNICODE);
- }
- }
- //短视频
- if (in_array('videoGoods', $imports)) {
- [$goods_result, $goods_cat_result, $goods_setting_result, $goods_list_result] = $this->videoGoods($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->videoGoods = json_encode(['goods_result' => $goods_result, 'goods_cat_result' => $goods_cat_result, 'goods_setting_result' => $goods_setting_result, 'goods_list_result' => $goods_list_result], JSON_UNESCAPED_UNICODE);
- }
- }
- //积分商城
- if (in_array('integralStore', $imports)) {
- [$goods_cat_result, $setting_result] = $this->integralStore($fromStoreId, $toId);
- if (isset($item)) {
- $item->integralStore = json_encode(['goods_cat_result' => $goods_cat_result, 'setting_result' => $setting_result], JSON_UNESCAPED_UNICODE);
- }
- }
- //上门服务
- $worker_goods_cat_result = $worker_goods_result = $worker_cat_result = [];
- if (in_array('workerConfig', $imports)) {
- [$worker_goods_cat_result, $worker_goods_result, $worker_cat_result, $worker_level_result, $worker_setting] = $this->workerConfig($fromStoreId, $toId, $goodsCache);
- if (isset($item)) {
- $item->workerConfig = json_encode(['cat_result' => $worker_goods_cat_result, 'goods_result' => $worker_goods_result, 'worker_cat_result' => $worker_cat_result, 'worker_level_result' => $worker_level_result, 'worker_setting' => $worker_setting], JSON_UNESCAPED_UNICODE);
- }
- }
- //预约
- $booking_goods_cat_cache = [];
- if (in_array('bookingGoods', $imports)) {
- [$booking_goods_cat_cache, $ext_result] = $this->bookingHome($fromStoreId, $toId, $goodsCache);
- }
- //装修
- if (in_array('diy', $imports)) {
- $result = $this->syncDiy($fromStoreId, $toId, $goodsCache, $cacheCat_, [
- 'ptCacheActivity' => $ptCacheActivity,
- 'ptCacheCat' => $ptCacheCat,
- 'ptCache' => $ptCache,
- 'cutPriceCacheActivity' => $cutPriceCacheActivity,
- 'cutPriceCacheCat' => $cutPriceCacheCat,
- 'cutPriceCache' => $cutPriceCache,
- 'seckillCacheActivity' => $seckillCacheActivity,
- 'seckillCache' => $seckillCache,
- 'worker_goods_cat_result' => $worker_goods_cat_result,
- 'worker_goods_result' => $worker_goods_result,
- 'worker_cat_result' => $worker_cat_result,
- 'booking_goods_cat_cache' => $booking_goods_cat_cache
- ]);
- if (isset($item)) {
- $item->diy = json_encode(['diy' => $result], JSON_UNESCAPED_UNICODE);
- }
- }
- }
- if (in_array('article', $imports)) {
- $result = $this->articleSetting($fromStoreId, $toId);
- if (isset($item)) {
- $item->article = json_encode(['result' => $result], JSON_UNESCAPED_UNICODE);
- }
- }
- if (in_array('topic', $imports)) {
- [$type_result, $result] = $this->topicSetting($fromStoreId, $toId);
- if (isset($item)) {
- $item->topic = json_encode(['type_result' => $type_result, 'result' => $result], JSON_UNESCAPED_UNICODE);
- }
- }
- }
- if (isset($item)) {
- $item->status = 1;
- $item->result = 1;
- $item->save();
- }
- // $syncItem->status = 0;
- // $syncItem->save();
- $t->commit();
- } catch (\Throwable $e) {
- $logName = 'store_sync.log';
- debug_log('任务ID => ' . $this->id, $logName);
- debug_log('异常消息 => ' . $e->getMessage(), $logName);
- debug_log('异常文件 => ' . $e->getFile(), $logName);
- debug_log('异常行数 => ' . $e->getLine(), $logName);
- debug_log($e->getTraceAsString(), $logName);
- debug_log('++++++++++++++++++++++++++++', $logName);
- $t->rollBack();
- $item->status = 1;
- $item->result = 2;
- $item->save();
- // $syncItem->status = 0;
- // $syncItem->save();
- }
- }
- /**
- * 同步商城配送规则
- */
- private function syncDeliveryRules($from_store_id, $to_store_id) {
- //删除原来的配送规则
- debug_log('开始同步', 'queuePush.log');
- PostageRules::deleteAll(['store_id' => $to_store_id]);
- $PostageRulesList = PostageRules::find()->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'mch_id' => 0
- ])->asArray()->all();
- $rules_id_cache = [];
- foreach ($PostageRulesList as $item) {
- //判断是否为当前商城同步过此规则
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_POSTAGE_RULES,
- 'from_id' => $item['id']
- ]);
- $rules_id_cache[$item['id']] = 0;
- $postageRules = PostageRules::findOne($storeSyncExtLog->to_id ?? 0) ?: new PostageRules();
- $postageRules->attributes = $item;
- $postageRules->store_id = $to_store_id;
- $postageRules->mch_id = 0;
- if (!$postageRules->save()) {
- continue;
- }
- $rules_id_cache[$item['id']] = $postageRules->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $item['id'], $postageRules->id, StoreSyncExtLog::TYPE_POSTAGE_RULES);
- }
- debug_log($rules_id_cache, 'queuePush.log');
- return $rules_id_cache;
- }
- /** 同步商品 start */
- // 同步商品
- private function syncGoods($from_store_id, $to_store_id, $deliveryRulesCache, &$message)
- {
- $goods_ids = Goods::find()->where(['store_id' => $to_store_id, 'is_delete' => 0, 'mch_id' => 0])->select('id')->column();
- $message = null;
- $cloud_store_token = get_merchant_token(0, $to_store_id, $message);
- $cloud_goods_id = [];
- foreach ($goods_ids as $param) {
- $goods = Goods::findOne($param);
- if ($goods->cloud_goods_id && !intval($goods->is_wholesale)) {
- if (!$cloud_store_token) {
- $message = $message['msg'];
- return [];
- }
- $cloud_goods_id[] = $goods->cloud_goods_id;
- }
- }
- if ($cloud_goods_id) {
- $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
- $mch_set_submit_order_data = [];
- $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_submit_order_data['goods_id'] = implode(',', $cloud_goods_id);
- $domain = (new OptionSetting)->getCloudDomainName();
- $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
- $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
- if($mchSetSubmitOrderInfo['code'] != 0){
- $message = '删除转单商品' . $mchSetSubmitOrderInfo['msg'];
- return [];
- } else {
- CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => $to_store_id, 'cloud_goods_id' => $cloud_goods_id, 'is_delete' => 0]);
- }
- }
- // 软删除商品
- Goods::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0, 'mch_id' => 0]);
- // 软删除规格组和规格
- $attrGroups = AttrGroup::find()->where(['store_id' => $to_store_id, 'is_delete' => 0])->all();
- foreach ($attrGroups as $attrGroup) {
- Attr::deleteAll(['attr_group_id' => $attrGroup->id, 'is_delete' => 0]);
- $attrGroup->is_delete = 1;
- $attrGroup->save();
- }
- AttrGroup::deleteAll(['store_id' => $to_store_id]);
- // 删除商品关联分类
- GoodsCat::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0]);
- // 删除云仓商品绑定
- CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => $to_store_id, 'is_delete' => 0]);
- // 商品分类处理
- $cacheCat = $this->doGoodsCat($from_store_id, $to_store_id);
- // 商品规格处理
- $goodsList = Goods::find()->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'mch_id' => 0
- ])->andWhere(['<=', 'md_food_id', 0])->orderBy(['id' => SORT_ASC])->asArray()->all();
- $goodsCache = [];
- $store_cloud_id = StoreCloud::findOne(['store_id' => $to_store_id, 'is_delete' => 0]);
- foreach ($goodsList as $goods) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT,
- 'from_id' => $goods['id']
- ]);
- $goods_id_cache = $goods['id'];
- $g = Goods::findOne($storeSyncExtLog->to_id ?? 0) ?: new Goods();
- if (!empty($goods['attr'])) {
- $attrs = \json_decode($goods['attr'], true);
- list($attrGroupCache, $attrCache) = $this->doGoodsAttr($from_store_id, $to_store_id, $attrs);
- foreach ($attrs as &$attr) {
- if ($attr['attr_list']) {
- foreach ($attr['attr_list'] as &$att) {
- if (isset($att['attr_id']) && isset($attrCache[$att['attr_id']])) {
- $att['attr_id'] = $attrCache[$att['attr_id']];
- }
- }
- }
- }
- $goods['attr'] = \json_encode($attrs);
- }
- unset($goods['id']);
- $goods['store_id'] = $to_store_id;
- $g->setAttributes($goods, false);
- $g->cloud_goods_id = $goods['cloud_goods_id'];
- $g->is_wholesale = $goods['is_wholesale'];
- $g->cloud_supplier_id = $goods['cloud_supplier_id'];
- $g->delivery_rules_id = $deliveryRulesCache[$goods['delivery_rules_id']] ?? 0;
- $g->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $goods_id_cache, $g->id, StoreSyncExtLog::TYPE_PRODUCT);
- $goodsCache[$goods_id_cache] = $g->id;
- if ($g->cloud_goods_id > 0 && !intval($g->is_wholesale)) {
- try {
- $form = new GoodsForm();
- $form->id = $g->cloud_goods_id;
- $form->store_id = $to_store_id;
- $goodsInfo = $form->saveCloudGoods();
- $merchantForm = new MerchantForm();
- $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
- $r = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'], $goodsInfo['goods_id'], $to_store_id);
- } catch (\Exception $e) {
- }
- }
- }
- // 商品分类处理
- $goodsCats = GoodsCat::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
- foreach ($goodsCats as $goodsCat) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT,
- 'from_id' => $goodsCat['id']
- ]);
- if (isset($goodsCache[$goodsCat['goods_id']]) && isset($cacheCat[$goodsCat['cat_id']])) {
- $id = $goodsCat['id'];
- $cat = GoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsCat();
- $goodsCat['goods_id'] = $goodsCache[$goodsCat['goods_id']];
- $goodsCat['cat_id'] = $cacheCat[$goodsCat['cat_id']];
- $goodsCat['store_id'] = $to_store_id;
- unset($goodsCat['id']);
- $cat->setAttributes($goodsCat, false);
- $cat->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $cat->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT);
- }
- }
- // 商品组图处理
- $inGoodsIds = \array_keys($goodsCache);
- $goodsPic = GoodsPic::find()->where(['is_delete' => 0, 'goods_id' => $inGoodsIds])->asArray()->all();
- foreach ($goodsPic as $pic) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC,
- 'from_id' => $pic['id']
- ]);
- $newPic = GoodsPic::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsPic();
- $newPic->goods_id = $goodsCache[$pic['goods_id']];
- $newPic->pic_url = $pic['pic_url'];
- $newPic->is_delete = 0;
- $newPic->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $pic['id'], $newPic->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC);
- }
- // 云仓商品绑定
- // $cloudGoodsBinds = CloudGoodsBind::find()->where(['is_delete' => 0, 'store_id' => $from_store_id, 'goods_id' => $inGoodsIds])->asArray()->all();
- // foreach ($cloudGoodsBinds as $goodsBind) {
- // $cloudGoods = new CloudGoodsBind();
- // $cloudGoods->cloud_goods_id = $goodsBind['cloud_goods_id'];
- // $cloudGoods->cloud_supplier_id = $goodsBind['cloud_supplier_id'];
- // $cloudGoods->store_id = $to_store_id;
- // $cloudGoods->goods_id = $goodsCache[$goodsBind['goods_id']];
- // $cloudGoods->is_delete = 0;
- // $cloudGoods->created_at = time();
- // $cloudGoods->save();
- // }
- //重新选品
- return [$goodsCache, $cacheCat];
- }
- // 商品规格创建
- private function doGoodsAttr($from_store_id, $to_store_id, $attrs = null)
- {
- $attr_id = [];
- if ($attrs) {
- $attrs = json_decode($attrs, true);
- foreach ($attrs as $attr_item) {
- $attr_id_item = array_column($attr_item['attr_list'], 'attr_id');
- $attr_id = array_merge($attr_id, $attr_id_item);
- }
- }
- $attrGroupCache = $attrCache = [];
- if ($attr_id) {
- $attr_id = array_unique($attr_id);
- foreach ($attr_id as $attr_id_item) {
- $attrGroup = null;
- $old_attr = Attr::findOne($attr_id_item);
- if ($old_attr) {
- $old_attr_group = AttrGroup::findOne($old_attr->attr_group_id);
- //判断是否为当前商城同步过此商品
- $storeSyncAttrGroupExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP,
- 'from_id' => $old_attr_group->id
- ]);
- $attrGroup = AttrGroup::findOne($storeSyncAttrGroupExtLog->to_id ?? 0) ?: new AttrGroup();
- $attrGroup->store_id = $to_store_id;
- $attrGroup->attr_group_name = $old_attr_group->attr_group_name;
- $attrGroup->is_delete = $old_attr_group->is_delete;
- $attrGroup->save();
- $attrGroupCache[$old_attr_group->id] = $attrGroup->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr_group->id, $attrGroup->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP);
- }
- //判断是否为当前商城同步过此商品
- $storeSyncAttrExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR,
- 'from_id' => $attr_id_item
- ]);
- $attr = Attr::findOne($storeSyncAttrExtLog->to_id ?? 0) ?: new Attr();
- $attr->attr_group_id = $attrGroup->id ?? 0;
- $attr->attr_name = $old_attr->attr_name;
- $attr->is_delete = $old_attr->is_delete;
- $attr->is_default = 0;
- $attr->save();
- $attrCache[$old_attr->id] = $attr->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr->id, $attr->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR);
- }
- }
- return [$attrGroupCache, $attrCache];
- }
- // 商品分类创建
- private function doGoodsCat($from_store_id, $to_store_id)
- {
- Cat::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0]);
- $cats = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'parent_id' => 0,
- ])->asArray()->all();
- $catCache = [];
- foreach ($cats as $cat) {
- $catC = $cat;
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat['id']
- ]);
- $c = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- unset($catC['id']);
- $catC['store_id'] = $to_store_id;
- $c->setAttributes($catC, false);
- $c->save();
- $catCache[$cat['id']] = $c->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat['id'], $c->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- $cats1 = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'parent_id' => $cat['id'],
- ])->asArray()->all();
- foreach ($cats1 as $cat1) {
- $catC1 = $cat1;
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat1['id']
- ]);
- $c1 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- unset($catC1['id']);
- $catC1['store_id'] = $to_store_id;
- $catC1['parent_id'] = $c->id;
- $c1->setAttributes($catC1, false);
- $c1->save();
- $catCache[$cat1['id']] = $c1->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat1['id'], $c1->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- $cats2 = Cat::find()->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'parent_id' => $cat1['id'],
- ])->asArray()->all();
- foreach ($cats2 as $cat2) {
- $catC2 = $cat2;
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
- 'from_id' => $cat2['id']
- ]);
- $c2 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
- unset($catC2['id']);
- $catC2['store_id'] = $to_store_id;
- $catC2['parent_id'] = $c1->id;
- $c2->setAttributes($catC2, false);
- $c2->save();
- $catCache[$cat2['id']] = $c2->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat2['id'], $c2->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
- }
- }
- }
- return $catCache;
- }
- /** 同步商品 end */
- /** 同步拼团 start */
- // 同步拼团
- private function syncGroup($from_store_id, $to_store_id, $cacheGoods)
- {
- $cacheActivity = $this->doCopyPt($from_store_id, $to_store_id);
- $cacheBanner = $this->doCopyPtBanner($from_store_id, $to_store_id);
- $cacheCat = $this->doCopyPtCat($from_store_id, $to_store_id);
- $cache = $this->doCopyPtGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods);
- $cacheSetting = $this->doCopyPtSetting($from_store_id, $to_store_id);
- return [$cacheActivity, $cacheBanner, $cacheCat, $cache, $cacheSetting];
- }
- public function doCopyPtSetting($from_store_id, $to_store_id) {
- try {
- $article_ = AboutArticle::find()->where([
- 'is_delete' => AboutArticle::IS_DELETE_NO,
- 'store_id' => $from_store_id,
- 'type' => 1
- ])->select("desc")->asArray()->one();
- if ($article_) {
- $article = AboutArticle::findOne(['is_delete' => AboutArticle::IS_DELETE_NO, 'store_id' => $to_store_id, 'type' => 1]);
- if (!$article) {
- $article = new AboutArticle();
- }
- unset($article_['id']);
- $article->attributes = $article_;
- $article->store_id = $to_store_id;
- $article->name = $article_['name'] ?: '';
- $article->created_at = time();
- $article->updated_at = time();
- $article->type = 1;
- if (!$article->save()) {
- throw new \Exception(json_encode($article->errors, JSON_UNESCAPED_UNICODE));
- };
- }
- return [
- 'code' => 0,
- 'msg' => "同步成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyPt($from_store_id, $to_store_id) {
- try {
- PtActivity::deleteAll(['store_id' => $to_store_id]);
- $list = PtActivity::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PT,
- 'from_id' => $value['id']
- ]);
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $attr['goods_ids'] = '';
- $model = PtActivity::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivity();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $value['id'], $model->id, StoreSyncExtLog::TYPE_PT);
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyPtBanner($from_store_id, $to_store_id) {
- try {
- PtActivityBanner::deleteAll(['store_id' => $to_store_id]);
- $list = PtActivityBanner::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $model = new PtActivityBanner();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyPtCat($from_store_id, $to_store_id) {
- try {
- PtActivityGoodsCat::deleteAll(['store_id' => $to_store_id]);
- $list = PtActivityGoodsCat::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PT_GOODS_CAT,
- 'from_id' => $value['id']
- ]);
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $model = PtActivityGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivityGoodsCat();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_PT_GOODS_CAT);
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyPtGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods) {
- try {
- debug_log($cacheActivity,'store_sync.log');
- $old_activity_id = array_keys($cacheActivity);
- $list = PtActivityGoods::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'activity_id' => $old_activity_id
- ])->orderBy(['id' => SORT_ASC])->all();
- PtActivityGoods::deleteAll(['store_id' => $to_store_id]);
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- if (!$cacheGoods[$attr['goods_id']]) {
- continue;
- }
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
- $cat = PtActivityGoodsCat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]);
- if (!$cat) {
- continue;
- }
- $attr['cat_id'] = $cacheCat[$attr['cat_id']];
- $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0, 'mch_id' => 0]);
- if (!$goods) {
- continue;
- }
- $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
- $agattr = json_decode($value['attr'], true);
- $goodsNew = Goods::findOne($attr['goods_id']);
- $gattrNew = json_decode($goodsNew->attr, true);
- $agattrNew = [];
- foreach($agattr as $k => $item){
- $attrNames = array_column($item['attr_list'], 'attr_name');
- // var_dump($attrNames);
- foreach($gattrNew as $v){
- $attrNamesNew = array_column($v['attr_list'], 'attr_name');
- // var_dump($attrNamesNew);
- if($attrNames === $attrNamesNew){
- // var_dump('$attrNames == $attrNamesNew');
- $item['attr_list'] = $v['attr_list'];
- }
- }
- $agattrNew[] = $item;
- }
- $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_PT_GOODS,
- 'from_id' => $value['id']
- ]);
- $model = PtActivityGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivityGoods();
- $model->setAttributes($attr, false);
- $model->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $value['id'], $model->id, StoreSyncExtLog::TYPE_PT_GOODS);
- $cache[$id] = $model->id;
- }
- return $cache;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步拼团 end */
- /** 同步砍价 start */
- // 同步砍价
- private function syncBargain($from_store_id, $to_store_id, $cacheGoods)
- {
- $cacheActivity = $this->doCopyCutPrice($from_store_id, $to_store_id);
- $cacheBanner = $this->doCopyCutPriceBanner($from_store_id, $to_store_id);
- $cacheCat = $this->doCopyCutPriceCat($from_store_id, $to_store_id);
- $cache = $this->doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods);
- return [$cacheActivity, $cacheBanner, $cacheCat, $cache];
- }
- public function doCopyCutPrice($from_store_id, $to_store_id) {
- try {
- ActivityCutPrice::deleteAll(['store_id' => $to_store_id]);
- $list = ActivityCutPrice::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE,
- 'from_id' => $value['id']
- ]);
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $attr['goods_ids'] = '';
- $model = ActivityCutPrice::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPrice();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE);
- }
- return $cache;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyCutPriceBanner($from_store_id, $to_store_id) {
- try {
- ActivityCutPriceBanner::deleteAll(['store_id' => $to_store_id]);
- $list = ActivityCutPriceBanner::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $model = new ActivityCutPriceBanner();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- }
- return $cache;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyCutPriceCat($from_store_id, $to_store_id) {
- try {
- ActivityCutPriceCat::deleteAll(['store_id' => $to_store_id]);
- $list = ActivityCutPriceCat::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT,
- 'from_id' => $value['id']
- ]);
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $model = ActivityCutPriceCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceCat();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT);
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods) {
- try {
- $list = ActivityCutPriceGoods::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $goods = [];
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
- $cat = Cat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]);
- if (!$cat) {
- continue;
- }
- $attr['cat_id'] = $cacheCat[$attr['cat_id']];
- $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0, 'mch_id' => 0]);
- if (!$goods) {
- continue;
- }
- $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
- $agattr = json_decode($value['attr'], true);
- $goodsNew = Goods::findOne($attr['goods_id']);
- $gattrNew = json_decode($goodsNew['attr'], true);
- $agattrNew = [];
- if ($agattr) {
- foreach($agattr as $k => $item){
- $attrNames = array_column($item['attr_list'], 'attr_name');
- // var_dump($attrNames);
- if ($gattrNew) {
- foreach($gattrNew as $v){
- $attrNamesNew = array_column($v['attr_list'], 'attr_name');
- // var_dump($attrNamesNew);
- if($attrNames === $attrNamesNew){
- // var_dump('$attrNames == $attrNamesNew');
- $item['attr_list'] = $v['attr_list'];
- }
- }
- }
- $agattrNew[] = $item;
- }
- }
- $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS,
- 'from_id' => $value['id']
- ]);
- $model = ActivityCutPriceGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceGoods();
- $model->setAttributes($attr, false);
- $model->save();
- $goods[$attr['activity_id']] = $goods[$attr['activity_id']] ?? [];
- array_push($goods[$attr['activity_id']], $attr['goods_id']);
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS);
- }
- foreach($goods as $aid => $gids){
- $ac = ActivityCutPrice::findOne($aid);
- $ac->goods_ids = implode(',', $gids);
- $ac->save();
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步砍价 end */
- /** 同步DIY start */
- // 同步DIY
- private function syncDiy($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache)
- {
- try {
- // 首页
- // Todo 同步其他页面
- return $this->copyHome($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- // 处理DIY首页
- private function copyHome($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache)
- {
- try {
- // 取消原来的首页信息
- // $oldIndex = NewDiyTemplate::findOne([
- // 'is_delete' => 0,
- // 'is_index' => 1,
- // 'store_id' => $to_store_id,
- // 'name' => 'DIY',
- // ]);
- // if ($oldIndex) {
- // $oldIndex->is_index = 0;
- // $oldIndex->save();
- // }
- // $fromIndex = NewDiyTemplate::findOne([
- // 'is_delete' => 0,
- // 'is_index' => 1,
- // 'store_id' => $from_store_id,
- // 'name' => 'DIY',
- // ]);
- //20240509要同步其他diy页面
- $fromList = NewDiyTemplate::find()->where([
- 'is_delete' => 0,
- 'store_id' => $from_store_id,
- 'name' => ['DIY', 'system'],
- 'is_index' => 0
- ])->all();
- // if (!$fromList) {
- //// return;
- // }
- $diyCache = [];
- if ($fromList) {
- NewDiyTemplate::deleteAll([
- 'is_delete' => 0,
- 'store_id' => $to_store_id,
- 'name' => ['DIY', 'system'],
- 'is_index' => 0
- ]);
- foreach ($fromList as $fromIndex) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_DIY,
- 'from_id' => $fromIndex->id
- ]);
- $template = \json_decode($fromIndex->template, true);
- if (!$template) {
- return;
- }
- foreach ($template['templates'] as &$item) {
- $item = $this->diyHandleCommon($item, $goodsCache, $cacheCat, [], $activity_cache);
- }
- $toIndex = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
- $toIndex->store_id = $to_store_id;
- $toIndex->template = json_encode($template, JSON_UNESCAPED_UNICODE);
- $toIndex->addtime = time();
- $toIndex->name = $fromIndex->name;
- $toIndex->is_index = $fromIndex->is_index;
- $toIndex->type = $fromIndex->type;
- $toIndex->is_delete = $fromIndex->is_delete;
- if (!$toIndex->save()) {
- throw new \Exception(json_encode($toIndex->errors, JSON_UNESCAPED_UNICODE));
- }
- $diyCache[$fromIndex->id] = $toIndex->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $fromIndex->id, $toIndex->id, StoreSyncExtLog::TYPE_DIY);
- }
- }
- $fromIndex_ = NewDiyTemplate::findOne([
- 'is_delete' => 0,
- 'is_index' => 1,
- 'store_id' => $from_store_id,
- 'name' => 'DIY',
- ]);
- $template = \json_decode($fromIndex_->template, true);
- if ($template) {
- NewDiyTemplate::deleteAll([
- 'is_delete' => 0,
- 'is_index' => 1,
- 'store_id' => $to_store_id,
- 'name' => 'DIY',
- ]);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_DIY,
- 'from_id' => $fromIndex_->id
- ]);
- foreach ($template['templates'] as &$index_item) {
- $index_item = $this->diyHandleCommon($index_item, $goodsCache, $cacheCat, $diyCache, $activity_cache);
- }
- $toIndex = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
- $toIndex->store_id = $to_store_id;
- $toIndex->template = json_encode($template, JSON_UNESCAPED_UNICODE);
- $toIndex->addtime = time();
- $toIndex->name = $fromIndex_->name;
- $toIndex->is_index = $fromIndex_->is_index;
- $toIndex->type = $fromIndex_->type;
- $toIndex->is_delete = $fromIndex->is_delete;
- if (!$toIndex->save()) {
- throw new \Exception(json_encode($toIndex->errors, JSON_UNESCAPED_UNICODE));
- }
- $diyCache[$fromIndex_->id] = $toIndex->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $fromIndex_->id, $toIndex->id, StoreSyncExtLog::TYPE_DIY);
- }
- $controls = NewDiyTemplate::findOne([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'name' => 'controls'
- ]);
- $template = \json_decode($controls->template, true);
- if ($template) {
- NewDiyTemplate::deleteAll([
- 'is_delete' => 0,
- 'name' => 'controls',
- 'store_id' => $to_store_id,
- ]);
- $data = $template;
- foreach ($data as &$data_item) {
- if (!empty($data_item['params']['navList'])) {
- foreach ($data_item['params']['navList'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
- }
- debug_log($data_item['params']['navList'], 'controls.log');
- }
- }
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_DIY,
- 'from_id' => $controls->id
- ]);
- $toControls = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
- $toControls->store_id = $to_store_id;
- $toControls->template = json_encode($data, JSON_UNESCAPED_UNICODE);
- $toControls->addtime = time();
- $toControls->name = $controls->name;
- $toControls->is_index = $controls->is_index;
- $toControls->type = $controls->type;
- $toControls->is_delete = $fromIndex->is_delete;
- if (!$toControls->save()) {
- throw new \Exception(json_encode($toControls->errors, JSON_UNESCAPED_UNICODE));
- }
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $controls->id, $toControls->id, StoreSyncExtLog::TYPE_DIY);
- }
- return [
- 'code' => 0,
- 'msg' => '执行成功',
- 'data' => $diyCache
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- //处理装修公用部分
- public function diyHandleCommon($template, $goodsCache, $cacheCat, $diyCache = [], $activity_cache = [])
- {
- // 头部导航
- if ($template['type'] == 'fastNav') {
- $template = $this->replaceFastNav($template, $goodsCache, $cacheCat);
- }
- // 图片轮播
- if ($template['type'] == 'banner') {
- debug_log('1处理装修公用部分','store_sync.log');
- $template = $this->replaceBanner($template, $goodsCache, $cacheCat, $diyCache);
- }
- // 魔方导航
- if ($template['type'] == 'cubeNav') {
- debug_log('2处理装修公用部分','store_sync.log');
- $template = $this->replaceCubeNav($template, $goodsCache, $cacheCat, $diyCache);
- }
- // 魔方图片
- if ($template['type'] == 'container') {
- debug_log('3处理装修公用部分','store_sync.log');
- $template = $this->replaceContainer($template, $goodsCache, $cacheCat, $diyCache);
- }
- // 产品展示
- if ($template['type'] == 'productShow') {
- debug_log('4处理装修公用部分','store_sync.log');
- $template = $this->replaceProductShow($template, $goodsCache, $cacheCat, $diyCache);
- }
- //产品分类
- if ($template['type'] == 'productClassify') {
- debug_log('5处理装修公用部分','store_sync.log');
- $template = $this->replaceProductClassify($template, $goodsCache, $cacheCat, $diyCache);
- }
- // 倒计时
- if ($template['type'] == 'marketCountDown') {
- $template = $this->replaceMarketCountDown($template, $goodsCache, $cacheCat, $diyCache);
- }
- //图片
- if ($template['type'] == 'images') {
- $template = $this->replaceImages($template, $goodsCache, $cacheCat, $diyCache);
- }
- // 首页推荐
- if ($template['type'] == 'indexRecommend') {
- // Todo 该模块数据有误,后期解决
- $template = $this->replaceIndexRecommend($template, $goodsCache, $cacheCat, $diyCache);
- debug_log($template, '20250328.log');
- }
- //秒杀
- if ($template['type'] == 'productSeckill') {
- $template = $this->replaceProductSeckill($template, $activity_cache['seckillCacheActivity'], $activity_cache['seckillCache'], $diyCache);
- }
- //上门服务
- if ($template['type'] == 'serviceHome') {
- $template = $this->replaceServiceHome($template, $activity_cache['worker_goods_cat_result'], $activity_cache['worker_goods_result'], $activity_cache['worker_cat_result'], $goodsCache, $diyCache);
- }
- //拼团
- if ($template['type'] == 'ptProductShow') {
- $template = $this->replacePtProductShow($template, $activity_cache['ptCacheActivity'], $activity_cache['ptCacheCat'], $activity_cache['ptCache'], $goodsCache, $diyCache);
- }
- //预约
- if ($template['type'] == 'bookingHome') {
- $template = $this->replaceBookingHome($template, $goodsCache, $activity_cache['booking_goods_cat_cache'], $diyCache);
- }
- return $template;
- }
- // 替换头部导航
- private function replaceFastNav($data, $goodsCache, $cacheCat)
- {
- if (!empty($data['params']['list'])) {
- foreach ($data['params']['list'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat);
- }
- }
- return $data;
- }
- // 替换图片轮播
- private function replaceBanner($data, $goodsCache, $cacheCat, $diyCache)
- {
- if (!empty($data['params']['bannerList'])) {
- foreach ($data['params']['bannerList'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
- }
- }
- return $data;
- }
- // 替换魔方导航
- private function replaceCubeNav($data, $goodsCache, $cacheCat, $diyCache)
- {
- if (!empty($data['params']['list'])) {
- foreach ($data['params']['list'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
- }
- }
- return $data;
- }
- // 替换魔方图片
- private function replaceContainer($data, $goodsCache, $cacheCat, $diyCache)
- {
- debug_log('——替换魔方图片','store_sync.log');
- if (!empty($data['params']['cubeCoverList'])) {
- debug_log('替换魔方图片——','store_sync.log');
- foreach ($data['params']['cubeCoverList'] as &$item) {
- $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
- }
- }
- return $data;
- }
- // 替换产品展示
- private function replaceProductShow($data, $goodsCache, $cacheCat, $diyCache)
- {
- // 替换分类id
- if (isset($data['params']['classifyData']) && isset($cacheCat[$data['params']['classifyData']])) {
- $data['params']['classifyData'] = $cacheCat[$data['params']['classifyData']];
- }
- // 替换分类商品id
- if (isset($data['params']['classifyArr'])) {
- foreach ($data['params']['classifyArr'] as &$item) {
- if (isset($goodsCache[$item['id']])) {
- $item['id'] = $goodsCache[$item['id']];
- }
- }
- }
- // 替换商品id
- if (isset($data['params']['selectData'])) {
- foreach ($data['params']['selectData'] as &$select_item) {
- if (isset($goodsCache[$select_item['id']])) {
- $select_item['id'] = $goodsCache[$select_item['id']];
- }
- }
- }
- $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
- return $data;
- }
- // 替换产品分类
- private function replaceProductClassify($data, $goodsCache, $cacheCat, $diyCache)
- {
- if (!empty($data['params']['classifyList'])) {
- $data['params']['classifyList'] = (array)$data['params']['classifyList'];
- foreach ($data['params']['classifyList'] as &$item) {
- // 替换分类
- if (!empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
- $item['id'] = $cacheCat[$item['classifyId']];
- $item['classifyId'] = $cacheCat[$item['classifyId']];
- }
- if (!empty($item['data'])) {
- // 替换商品id
- foreach ($item['data'] as &$value) {
- if (isset($goodsCache[$value['id']])) {
- $value['id'] = $goodsCache[$value['id']];
- }
- }
- }
- }
- }
- return $data;
- }
- //秒杀
- private function replaceProductSeckill($template, $seckillCacheActivity, $seckillCache, $diyCache) {
- if (!empty($template['params'])) {
- $template['params']['activeId'] = (string)$seckillCacheActivity[$template['params']['activeId']];
- }
- return $template;
- }
- //上门服务
- private function replaceServiceHome($template, $worker_goods_cat_result, $worker_goods_result, $worker_cat_result, $goodsCache, $diyCache) {
- if (!empty($template['params'])) {
- $template['params']['classifyData'] = $worker_goods_cat_result[$template['params']['classifyData']];
- if (!empty($template['params']['classifyArr'])) {
- foreach ($template['params']['classifyArr'] as &$worker_goods_item) {
- $worker_goods_item['id'] = $goodsCache[$worker_goods_item['id']];
- }
- }
- }
- return $template;
- }
- //拼团
- private function replacePtProductShow($template, $ptCacheActivity, $ptCacheCat, $ptCache, $goodsCache, $diyCache) {
- if (!empty($template['params'])) {
- $template['params']['classifyData'] = $ptCacheCat[$template['params']['classifyData']];
- if (!empty($template['params']['classifyArr'])) {
- foreach ($template['params']['classifyArr'] as &$pt_classify_goods_item) {
- $pt_classify_goods_item['id'] = $goodsCache[$pt_classify_goods_item['id']];
- }
- }
- if (!empty($template['params']['selectData'])) {
- foreach ($template['params']['selectData'] as &$pt_goods_item) {
- $pt_goods_item['id'] = $goodsCache[$pt_goods_item['id']];
- }
- }
- }
- return $template;
- }
- //砍价
- private function replaceCutPrice($data, $cutPriceCacheActivity, $cutPriceCacheCat, $cutPriceCache, $goodsCache, $diyCache) {
- if (!empty($data['params']['classifyList'])) {
- foreach ((array)$data['params']['classifyList'] as &$item) {
- // 替换分类
- if (!empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
- $item['id'] = $cacheCat[$item['classifyId']];
- $item['classifyId'] = $cacheCat[$item['classifyId']];
- }
- if (!empty($item['data'])) {
- // 替换商品id
- foreach ($item['data'] as &$value) {
- if (isset($goodsCache[$value['id']])) {
- $value['id'] = $goodsCache[$value['id']];
- }
- }
- }
- }
- }
- return $data;
- }
- //装修预约商品
- private function replaceBookingHome($template, $goodsCache, $booking_goods_cat_cache) {
- try {
- if (!empty($template['params'])) {
- $template['params']['classifyData'] = $booking_goods_cat_cache[$template['params']['classifyData']];
- if (!empty($template['params']['selectData'])) {
- foreach ($template['params']['selectData'] as &$booking_goods_item) {
- $booking_goods_item['id'] = $goodsCache[$booking_goods_item['id']];
- }
- }
- }
- return $template;
- } catch (\Exception $e) {
- return $template;
- }
- }
- // 替换倒计时
- private function replaceMarketCountDown($data, $goodsCache, $cacheCat, $diyCache)
- {
- $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
- return $data;
- }
- // 替换图片链接
- private function replaceImages($data, $goodsCache, $cacheCat, $diyCache)
- {
- if (isset($data['params']['current'])) {
- $data['params']['current'] = $this->replaceLink($data['params']['current'], $goodsCache, $cacheCat, $diyCache);
- } else {
- if (isset($data['params'])) {
- $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
- }
- }
- return $data;
- }
- //替换首页推荐
- private function replaceIndexRecommend($template, $goodsCache, $cacheCat, $diyCache) {
- try {
- foreach ($template['params']['list'] as &$item) {
- foreach ($item['pics']['link'] as &$link_item) {
- if (!isset($link_item['link'])) {
- continue;
- }
- // 处理分类id
- if (
- strpos($link_item['link'], '/pages/cat/cat?cat_id=') !== false &&
- $link_item['params'][0]['value'] != '' &&
- isset($cacheCat[$link_item['params'][0]['value']])
- ) {
- $link_item['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$link_item['params'][0]['value']];
- $link_item['params'][0]['value'] = $cacheCat[$cacheCat[$link_item['params'][0]['value']]];
- }
- // 处理商品列表
- if (
- strpos($link_item['link'], '/other/list/list?cat_id=') !== false &&
- $link_item['params'][0]['value'] != '' &&
- isset($cacheCat[$link_item['params'][0]['value']])
- ) {
- $link_item['link'] = '/other/list/list?cat_id=' . $cacheCat[$link_item['params'][0]['value']];
- $link_item['params'][0]['value'] = $cacheCat[$link_item['params'][0]['value']];
- }
- // 处理商品详情
- if (
- strpos($link_item['link'], '/goods/goods/goods?id=') !== false &&
- $link_item['params'][0]['value'] != '' &&
- isset($goodsCache[$link_item['params'][0]['value']])
- ) {
- $link_item['link'] = '/goods/goods/goods?id=' . $goodsCache[$link_item['params'][0]['value']];
- $link_item['params'][0]['value'] = $goodsCache[$link_item['params'][0]['value']];
- }
- if (
- strpos($link_item['link'], '/pages/diy/diy?id=') !== false &&
- isset($diyCache[$link_item['id']])
- ) {
- $link_item['link'] = '/pages/diy/diy?id=' . $diyCache[$link_item['id']];
- $link_item['id'] = $diyCache[$link_item['id']];
- }
- }
- }
- return $template;
- } catch (\Exception $e) {
- return $template;
- }
- }
- private function replaceLink($item, $goodsCache, $cacheCat, $diyCache = [])
- {
- debug_log('——开始替换','store_sync.log');
- if (!isset($item['link']['link'])) {
- return $item;
- }
- debug_log('开始替换_','store_sync.log');
- // 处理分类id
- if (
- strpos($item['link']['link'], '/pages/cat/cat?cat_id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($cacheCat[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
- }
- // 处理商品列表
- if (strpos($item['link']['link'], '/other/list/list?cat_id=') !== false) {
- debug_log('处理分类替换', 'store_sync.log');
- debug_log(['cacheCat' => $cacheCat], 'store_sync.log');
- debug_log(['item' => $item], 'store_sync.log');
- }
- if (
- strpos($item['link']['link'], '/other/list/list?cat_id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($cacheCat[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/other/list/list?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
- }
- // 处理商品详情
- if (
- strpos($item['link']['link'], '/goods/goods/goods?id=') !== false &&
- $item['link']['params'][0]['value'] != '' &&
- isset($goodsCache[$item['link']['params'][0]['value']])
- ) {
- $item['link']['link'] = '/goods/goods/goods?id=' . $goodsCache[$item['link']['params'][0]['value']];
- $item['link']['params'][0]['value'] = $goodsCache[$item['link']['params'][0]['value']];
- }
- debug_log('开始替换','store_sync.log');
- if (
- strpos($item['link']['link'], '/pages/diy/diy?id=') !== false &&
- isset($diyCache[$item['link']['id']])
- ) {
- $item['link']['link'] = '/pages/diy/diy?id=' . $diyCache[$item['link']['id']];
- $item['link']['id'] = $diyCache[$item['link']['id']];
- }
- debug_log($item,'store_sync.log');
- debug_log('结束替换','store_sync.log');
- return $item;
- }
- /** 同步DIY end */
- /** 同步秒杀 start */
- private function seckill($from_store_id, $to_store_id, $goodsCache) {
- //同步活动
- $cacheActivity = $this->doCopySeckill($from_store_id, $to_store_id);
- //同步产品
- $cache = $this->doCopySeckillGoods($from_store_id, $to_store_id, $cacheActivity, $goodsCache);
- return [$cacheActivity, $cache];
- }
- public function doCopySeckill($from_store_id, $to_store_id) {
- try {
- SeckillActivity::deleteAll(['store_id' => $to_store_id]);
- $list = SeckillActivity::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_SECKILL,
- 'from_id' => $id
- ]);
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $model = SeckillActivity::findOne($storeSyncExtLog->to_id ?? 0) ?: new SeckillActivity();
- $model->setAttributes($attr, false);
- $model->save();
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_SECKILL);
- $cache[$id] = $model->id;
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function doCopySeckillGoods($from_store_id, $to_store_id, $cacheActivity, $cacheGoods) {
- try {
- $old_activity_id = array_keys($cacheActivity);
- $list = SeckillActivityGoods::find()
- ->where([
- 'store_id' => $from_store_id,
- 'is_delete' => 0,
- 'activity_id' => $old_activity_id
- ])->orderBy(['id' => SORT_ASC])->all();
- $cache = [];
- SeckillActivityGoods::deleteAll(['store_id' => $to_store_id]);
- foreach ($list as $value) {
- $id = $value['id'];
- $attr = $value->attributes;
- if (!$cacheGoods[$attr['goods_id']]) {
- continue;
- }
- unset($attr['id']);
- $attr['store_id'] = $to_store_id;
- $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
- $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
- $attr['sale_num'] = 0;
- $agattr = json_decode($value['attr'], true);
- $goodsNew = Goods::findOne($attr['goods_id']);
- $gattrNew = json_decode($goodsNew['attr'], true);
- $agattrNew = [];
- foreach($agattr as $k => $item){
- $attrNames = array_column($item['attr_list'], 'attr_name');
- // var_dump($attrNames);
- if ($gattrNew) {
- foreach($gattrNew as $v){
- $attrNamesNew = array_column($v['attr_list'], 'attr_name');
- // var_dump($attrNamesNew);
- if($attrNames === $attrNamesNew){
- // var_dump('$attrNames == $attrNamesNew');
- $item['attr_list'] = $v['attr_list'];
- }
- }
- }
- $agattrNew[] = $item;
- }
- $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $from_store_id,
- 'to_store_id' => $to_store_id,
- 'type' => StoreSyncExtLog::TYPE_SECKILL_GOODS,
- 'from_id' => $id
- ]);
- $model = SeckillActivityGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new SeckillActivityGoods();
- $model->setAttributes($attr, false);
- $model->save();
- $cache[$id] = $model->id;
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_SECKILL_GOODS);
- }
- return $cache;
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步秒杀 end */
- /** 同步分销start **/
- private function shareConfig($from_store_id, $to_store_id, $goodsCache) {
- //同步配置 基础配置 佣金配置 推广海报
- $result = $this->shareBasicConfig($from_store_id, $to_store_id);
- //同步等级
- $levelResult = $this->shareLevel($from_store_id, $to_store_id);
- return [
- $result, $levelResult
- ];
- }
- //同步配置 基础配置 佣金配置 推广海报
- private function shareBasicConfig($from_store_id, $to_store_id) {
- try {
- $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) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $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) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $qrcode = Qrcode::findOne(['store_id' => $from_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- $qrcode_ = Qrcode::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
- if (!$qrcode_) {
- $qrcode_ = new Qrcode();
- }
- unset($qrcode['id']);
- $qrcode_->attributes = $qrcode->attributes;
- $qrcode_->store_id = $to_store_id;
- $qrcode_->created_at = time();
- $qrcode_->updated_at = time();
- if (!$qrcode_->save()) {
- return [
- 'code' => 1,
- 'msg' => json_encode($qrcode_->errors, JSON_UNESCAPED_UNICODE)
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch(\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步等级
- 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()
- ];
- }
- }
- /** 同步分销end **/
- /** 同步股东start **/
- private function shareHolderConfig($from_store_id, $to_store_id, $goodsCache) {
- //页面设置 申请设置
- $basic_result = $this->shareHolderBasicConfig($from_store_id, $to_store_id);
- //等级同步
- $level_result = $this->shareHolderLevelConfig($from_store_id, $to_store_id, $goodsCache);
- return [$basic_result, $level_result];
- }
- //页面设置 申请设置
- private function shareHolderBasicConfig($from_store_id, $to_store_id) {
- try {
- Option::set('cycle', Option::get('cycle', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('is_open_share', Option::get('is_open_share', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('is_open_range', Option::get('is_open_range', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('team_num', Option::get('team_num', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('qrcode', Option::get('qrcode', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('range_name', Option::get('range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('holder_name', Option::get('holder_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('area_name', Option::get('area_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('old_range_name', Option::get('old_range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('direct_range_name', Option::get('direct_range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('old_add_holder_name', Option::get('old_add_holder_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
- Option::set('is_open_area', Option::get('is_open_area', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderProfit', Option::get('shareHolderProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderPriceMin', Option::get('shareHolderPriceMin', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderCashProfit', Option::get('shareHolderCashProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderLevel', Option::get('shareHolderLevel', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderNumber', Option::get('shareHolderNumber', $from_store_id, 'bonus_pool', '0,0')['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderHasCheck', Option::get('shareHolderHasCheck', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('pay_wechat', Option::get('pay_wechat', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('pay_alipay', Option::get('pay_alipay', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('bank', Option::get('bank', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('cash_max_day', Option::get('cash_max_day', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('cash_max_single_day', Option::get('cash_max_single_day', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('min_money', Option::get('min_money', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('auto_money', Option::get('auto_money', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('dividends_condition', Option::get('dividends_condition', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderProfitSwitch', Option::get('shareHolderProfitSwitch', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- Option::set('shareHolderHighProfit', Option::get('shareHolderHighProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
- //申请设置
- Option::set('holder_agreement', Option::get('holder_agreement', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
- Option::set('holder_banner', Option::get('holder_banner', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
- Option::set('holder_audit', Option::get('holder_audit', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //等级同步
- private function shareHolderLevelConfig($from_store_id, $to_store_id, $goodsCache) {
- 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']]);
- $share_holder_level_id = $item['id'];
- if (!$share_holder_level) {
- $share_holder_level = new ShareHolderLevel();
- }
- 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) {
- $id[] = $goodsCache[$goods_id_item];
- }
- } 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()
- ];
- }
- }
- /** 同步股东end **/
- /** 同步短视频start **/
- private function videoGoods($fromStoreId, $toId, $goodsCache) {
- //同步商品
- $goods_result = $this->videoGoodsCopy($fromStoreId, $toId, $goodsCache);
- //同步分类
- $goods_cat_result = $this->videoGoodsCatCopy($fromStoreId, $toId);
- $video_cat_cache = [];
- if ($goods_cat_result['code'] === 0) {
- $video_cat_cache = $goods_cat_result['data']['video_cat_cache'];
- }
- //同步设置
- $goods_setting_result = $this->videoGoodsConfigCopy($fromStoreId, $toId);
- //同步视频
- $goods_list_result = $this->videoCopy($fromStoreId, $toId, $goodsCache, $video_cat_cache);
- return [$goods_result, $goods_cat_result, $goods_setting_result, $goods_list_result];
- }
- //同步商品
- private function videoGoodsCopy($fromStoreId, $toId, $goodsCache) {
- try {
- $video_goods_list = VideoGoods::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- VideoGoods::deleteAll(['store_id' => $toId]);
- foreach ($video_goods_list as $video_goods_item) {
- if (!$goodsCache[$video_goods_item['goods_id']]) {
- continue;
- }
- $video_goods_id = $video_goods_item['id'];
- unset($video_goods_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS,
- 'from_id' => $video_goods_id
- ]);
- $video_goods = VideoGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoods();
- $video_goods->store_id = $toId;
- $video_goods->status = $video_goods_item['status'];
- $video_goods->goods_id = $goodsCache[$video_goods_item['goods_id']];
- $video_goods->created_at = time();
- $video_goods->updated_at = time();
- $video_goods->save();
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $video_goods_id, $video_goods->id, StoreSyncExtLog::TYPE_VIDEO_GOODS);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步短视频分类
- private function videoGoodsCatCopy($fromStoreId, $toId) {
- try {
- $video_cat_cache = [];
- $video_goods_list = VideoGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- VideoGoodsCat::deleteAll(['store_id' => $toId]);
- foreach ($video_goods_list as $video_goods_item) {
- $id = $video_goods_item['id'];
- unset($video_goods_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT,
- 'from_id' => $id
- ]);
- $video_goods_cat = VideoGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoodsCat();
- $video_goods_cat->attributes = $video_goods_item;
- $video_goods_cat->store_id = $toId;
- $video_goods_cat->created_at = time();
- $video_goods_cat->updated_at = time();
- if (!$video_goods_cat->save()) {
- throw new \Exception(json_encode($video_goods_cat, JSON_UNESCAPED_UNICODE));
- }
- $video_cat_cache[$id] = $video_goods_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $video_goods_cat->id, StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功',
- 'data' => [
- 'video_cat_cache' => $video_cat_cache
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步短视频分类
- private function videoGoodsConfigCopy($fromStoreId, $toId) {
- try {
- $video_goods_setting_ = VideoGoodsSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
- unset($video_goods_setting_['id']);
- $video_goods_setting = VideoGoodsSetting::findOne(['store_id' => $toId]);
- if (!$video_goods_setting) {
- $video_goods_setting = new VideoGoodsSetting();
- }
- $video_goods_setting->attributes = $video_goods_setting_;
- $video_goods_setting->store_id = $toId;
- $video_goods_setting->created_at = time();
- $video_goods_setting->updated_at = time();
- if (!$video_goods_setting->save()) {
- throw new \Exception(json_encode($video_goods_setting->errors, JSON_UNESCAPED_UNICODE));
- }
- $video_analyze_345_ = Option::get('video_analyze_345', $fromStoreId, 'saas')['value'];
- Option::set('video_analyze_345', $video_analyze_345_, $toId, 'saas');
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function videoCopy($fromStoreId, $toId, $goodsCache, $video_cat_cache) {
- try {
- $video_goods_list = VideoGoodsList::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- VideoGoodsList::deleteAll(['store_id' => $toId]);
- foreach ($video_goods_list as $video_goods_item) {
- $integral_cat = new VideoGoodsList();
- unset($video_goods_item['id']);
- $integral_cat->attributes = $video_goods_item;
- $goodsList = json_decode($video_goods_item['goods_id'], true);
- $ids = [];
- if ($goodsList) {
- foreach ($goodsList as $goods_id) {
- $ids[] = $goodsCache[$goods_id];
- }
- }
- $integral_cat->goods_id = json_encode($ids);
- $integral_cat->cat_id = $video_cat_cache[$video_goods_item['cat_id']];
- $integral_cat->store_id = $toId;
- $integral_cat->user_id = 0;
- $integral_cat->created_at = time();
- $integral_cat->updated_at = time();
- if (!$integral_cat->save()) {
- throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步短视频end **/
- /** 同步积分商城start **/
- private function integralStore($fromStoreId, $toId) {
- //商品分类
- $goods_cat_result = $this->integralGoodsCatCopy($fromStoreId, $toId);
- //设置
- $setting_result = $this->integralSettingCopy($fromStoreId, $toId);
- return [$goods_cat_result, $setting_result];
- }
- //商品分类
- private function integralGoodsCatCopy($fromStoreId, $toId) {
- try {
- $integral_cat_list = NewIntegralCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- $integral_cat_arr = [];
- NewIntegralCat::deleteAll(['store_id' => $toId]);
- $goods_id = Goods::find()->where(['store_id' => $toId, 'is_delete' => 0, 'product_type' => Goods::GOODS_TYPE_INTEGRAL, 'mch_id' => 0])
- ->select('id')->asArray()->column();
- foreach ($integral_cat_list as $integral_cat_item) {
- $id = $integral_cat_item['id'];
- unset($integral_cat_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT,
- 'from_id' => $id
- ]);
- $integral_cat = NewIntegralCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewIntegralCat();
- $integral_cat->attributes = $integral_cat_item;
- $integral_cat->store_id = $toId;
- $integral_cat->created_at = time();
- $integral_cat->updated_at = time();
- if (!$integral_cat->save()) {
- throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $integral_cat_arr[$id] = $integral_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $integral_cat->id, StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT);
- $goods_cat_list = GoodsCat::find()->where(['goods_id' => $goods_id, 'cat_id' => $integral_cat_item['id'], 'is_delete' => 0, 'store_id' => $toId])
- ->asArray()->all();
- foreach ($goods_cat_list as $goods_cat_item) {
- $goods_cat = GoodsCat::findOne($goods_cat_item['id']);
- $goods_cat->cat_id = $integral_cat->id;
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '同步完成',
- 'data' => [
- 'integral_cat_arr' => $integral_cat_arr
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function integralSettingCopy($fromStoreId, $toId) {
- try {
- //签到积分设置
- $integral_setting_ = IntegralSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
- if ($integral_setting_) {
- $integral_setting = IntegralSetting::findOne(['store_id' => $toId]);
- if (!$integral_setting) {
- $integral_setting = new IntegralSetting();
- }
- unset($integral_setting_['id']);
- $integral_setting->attributes = $integral_setting_;
- $integral_setting->store_id = $toId;
- if (!$integral_setting->save()) {
- throw new \Exception(json_encode($integral_setting->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- //积分商城设置
- $new_integral_setting_ = NewIntegralSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
- if ($new_integral_setting_) {
- $new_integral_setting = NewIntegralSetting::findOne(['store_id' => $toId]);
- if (!$new_integral_setting) {
- $new_integral_setting = new NewIntegralSetting();
- }
- $new_integral_setting->store_id = $toId;
- $new_integral_setting->polling_img = $new_integral_setting_['polling_img'];
- $new_integral_setting->explain = $new_integral_setting_['explain'];
- $new_integral_setting->created_at = time();
- if (!$new_integral_setting->save()) {
- throw new \Exception(json_encode($new_integral_setting->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '同步完成'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
- ];
- }
- }
- /** 同步积分商城end **/
- /** 同步上门服务start **/
- private function workerConfig($fromStoreId, $toId, $goodsCache) {
- //商品分类
- $cat_cache = $this->workerGoodsCat($fromStoreId, $toId);
- //商品列表
- $goods_result = $this->workerGoods($fromStoreId, $toId, $cat_cache, $goodsCache);
- //服务分类
- $worker_cat_result = $this->workerCat($fromStoreId, $toId);
- //服务等级
- $worker_level_result = $this->workerLevel($fromStoreId, $toId);
- //功能设置
- $worker_setting = $this->workerSetting($fromStoreId, $toId);
- return [$cat_cache, $goods_result, $worker_cat_result, $worker_level_result, $worker_setting];
- }
- //预约
- private function bookingHome($fromStoreId, $toId, $goodsCache) {
- //预约分类
- $cat_cache = $this->bookingHomeGoodsCat($fromStoreId, $toId);
- //预约商品与预约分类修改绑定关系
- $ext_result = $this->bookingHomeGoodsCatBind($fromStoreId, $toId, $goodsCache, $cat_cache);
- return [$cat_cache, $ext_result];
- }
- //预约商品分类
- private function bookingHomeGoodsCat($fromStoreId, $toId) {
- try {
- $goodsCatCache = [];
- $goods_cat_ = BookingGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- BookingGoodsCat::deleteAll(['store_id' => $toId]);
- foreach ($goods_cat_ as $goods_cat_item) {
- $id = $goods_cat_item['id'];
- unset($goods_cat_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT,
- 'from_id' => $id
- ]);
- $goods_cat = BookingGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsCat();
- $goods_cat->attributes = $goods_cat_item;
- $goods_cat->store_id = $toId;
- $goods_cat->name = $goods_cat_item['name'];
- $goods_cat->pic_url = $goods_cat_item['pic_url'];
- $goods_cat->sort = $goods_cat_item['sort'];
- $goods_cat->is_show = $goods_cat_item['is_show'];
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $goodsCatCache[$id] = $goods_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT);
- }
- return $goodsCatCache;
- } catch (\Exception $e) {
- return [];
- }
- }
- //预约商品与预约分类修改绑定关系
- private function bookingHomeGoodsCatBind($fromStoreId, $toId, $goodsCache, $cat_cache) {
- try {
- $goodsExt = BookingGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
- BookingGoodsExt::deleteAll(['store_id' => $toId]);
- foreach ($goodsExt as $ext_item) {
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS,
- 'from_id' => $ext_item['id']
- ]);
- $id = $ext_item['id'];
- unset($ext_item['id']);
- $goods_ext = BookingGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsExt();
- $goods_ext->store_id = $toId;
- $goods_ext->goods_id = $goodsCache[$ext_item['goods_id']];
- $goods_ext->cat_id = $cat_cache[$ext_item['cat_id']];
- if (!$goods_ext->save()) {
- throw new \Exception(json_encode($goods_ext->errors, JSON_UNESCAPED_UNICODE));
- }
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_ext->id, StoreSyncExtLog::TYPE_BOOKING_GOODS);
- }
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //商品分类
- private function workerGoodsCat($fromStoreId, $toId) {
- try {
- $cat_cache = [];
- $goods_cat_ = WorkerGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- WorkerGoodsCat::deleteAll(['store_id' => $toId]);
- foreach ($goods_cat_ as $goods_cat_item) {
- $id = $goods_cat_item['id'];
- unset($goods_cat_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS_CAT,
- 'from_id' => $id
- ]);
- $goods_cat = WorkerGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerGoodsCat();
- $goods_cat->attributes = $goods_cat_item;
- $goods_cat->store_id = $toId;
- $goods_cat->created_at = time();
- $goods_cat->updated_at = time();
- $goods_cat->name = $goods_cat_item['name'];
- $goods_cat->pic_url = $goods_cat_item['pic_url'];
- $goods_cat->is_show = $goods_cat_item['is_show'];
- $goods_cat->big_pic_url = $goods_cat_item['big_pic_url'];
- $goods_cat->advert_pic = $goods_cat_item['advert_pic'];
- $goods_cat->advert_url = $goods_cat_item['advert_url'];
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $cat_cache[$id] = $goods_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS_CAT);
- }
- return $cat_cache;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function workerGoods($fromStoreId, $toId, $cat_cache, $goodsCache) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $workGoodsCache = [];
- $worker_goods_list = WorkerGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
- WorkerGoodsExt::deleteAll(['store_id' => $toId]);
- foreach ($worker_goods_list as $goods_item) {
- $goods_id = $goodsCache[$goods_item['goods_id']];
- $cat_id = $cat_cache[$goods_item['cat_id']];
- if (!empty($goods_id) && !empty($cat_id)) {
- $workGoodsId = $goods_item['id'];
- $workGoodsCache[$workGoodsId] = 0;
- unset($goods_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS,
- 'from_id' => $workGoodsId
- ]);
- $worker_goods = WorkerGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerGoodsExt();
- $worker_goods->attributes = $goods_item;
- $worker_goods->payment_type = $goods_item['payment_type'];
- $worker_goods->profit_base = $goods_item['profit_base'];
- $worker_goods->profit_max = $goods_item['profit_max'];
- $worker_goods->final_profit = $goods_item['profit_max'];
- $worker_goods->desc = $goods_item['desc'];
- $worker_goods->warn = $goods_item['warn'];
- $worker_goods->bind_worker = $goods_item['bind_worker'];
- $worker_goods->goods_id = $goods_id;
- $worker_goods->cat_id = $cat_id;
- $worker_goods->store_id = $toId;
- if (!$worker_goods->save()) {
- throw new \Exception(json_encode($worker_goods->errors, JSON_UNESCAPED_UNICODE));
- }
- $workGoodsCache[$workGoodsId] = $worker_goods->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $workGoodsId, $worker_goods->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS);
- }
- }
- $t->commit();
- return $workGoodsCache;
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function workerCat($fromStoreId, $toId) {
- try {
- $workerCatCache = [];
- $worker_cat_list = WorkerCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
- WorkerCat::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
- foreach ($worker_cat_list as $item) {
- $workerCatId = $item['id'];
- $workerCatCache[$workerCatId] = 0;
- unset($item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_CAT,
- 'from_id' => $workerCatId
- ]);
- $worker_cat = WorkerCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerCat();
- $worker_cat->attributes = $item;
- $worker_cat->store_id = $toId;
- $worker_cat->created_at = time();
- $worker_cat->updated_at = time();
- if (!$worker_cat->save()) {
- throw new \Exception(json_encode($worker_cat->errors, JSON_UNESCAPED_UNICODE));
- }
- $workerCatCache[$workerCatId] = $worker_cat->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $workerCatId, $worker_cat->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_CAT);
- }
- return $workerCatCache;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步等级
- private function workerLevel($from_store_id, $to_store_id) {
- try {
- $workerLevelList = WorkerLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
- foreach ($workerLevelList as $item) {
- $workerLevelId = $item['id'];
- //判断是否为当前商城同步过此商品
- $worker_level = WorkerLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
- if (!$worker_level) {
- $worker_level = new WorkerLevel();
- }
- unset($item['id']);
- $worker_level->attributes = $item;
- $worker_level->store_id = $to_store_id;
- if (!$worker_level->save()) {
- return [
- 'code' => 1,
- 'msg' => json_encode($worker_level->errors, JSON_UNESCAPED_UNICODE)
- ];
- };
- (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $workerLevelId, $worker_level->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_LEVEL);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步短视频分类
- private function workerSetting($fromStoreId, $toId) {
- try {
- $worker_setting_ = WorkerSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
- unset($worker_setting_['id']);
- $worker_setting = WorkerSetting::findOne(['store_id' => $toId]);
- if (!$worker_setting) {
- $worker_setting = new WorkerSetting();
- }
- $worker_setting->store_id = $toId;
- $worker_setting->order_space = $worker_setting_['order_space'] ?: 999;
- $worker_setting->fee_road = $worker_setting_['fee_road'] ?? '';
- $worker_setting->form_apply = $worker_setting_['form_apply'] ?? '';
- $worker_setting->tag = $worker_setting_['tag'] ?? '';
- $worker_setting->created_at = time();
- $worker_setting->updated_at = time();
- $worker_setting->starting_price = $worker_setting_['starting_price'] ?? '0.00';
- $worker_setting->additional_charge = $worker_setting_['additional_charge'] ?? '0.00';
- $worker_setting->banner = $worker_setting_['banner'] ?? '';
- if (!$worker_setting->save()) {
- throw new \Exception(json_encode($worker_setting->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步上门服务end **/
- /** 同步文章start **/
- private function articleSetting($fromStoreId, $toId) {
- try {
- $article_list = AboutArticle::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->asArray()->all();
- debug_log(AboutArticle::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->createCommand()->getRawSql(), 'store_sync.log');
- AboutArticle::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
- foreach ($article_list as $article_item) {
- $id = $article_item['id'];
- unset($article_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_ARTICLE,
- 'from_id' => $id
- ]);
- $aboutArticle = AboutArticle::findOne($storeSyncExtLog->to_id ?? 0) ?: new AboutArticle();
- $aboutArticle->attributes = $article_item;
- $aboutArticle->store_id = $toId;
- $aboutArticle->type = $article_item['type'];
- if (!$aboutArticle->save()) {
- throw new \Exception(json_encode($aboutArticle->errors, JSON_UNESCAPED_UNICODE));
- };
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $aboutArticle->id, StoreSyncExtLog::TYPE_ARTICLE);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步文章end **/
- /** 同步专题start **/
- private function topicSetting($fromStoreId, $toId) {
- $type_result = $this->topicTypeSetting($fromStoreId, $toId);
- $typeCache = [];
- if ($type_result['code'] === 0) {
- $typeCache = $type_result['data']['typeCache'];
- }
- $result = $this->topicContentSetting($fromStoreId, $toId, $typeCache);
- return [$type_result, $result];
- }
- private function topicTypeSetting($fromStoreId, $toId) {
- try {
- $topic_type_list = TopicType::find()->where(['is_delete' => TopicType::IS_DELETE_NO, 'store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
- TopicType::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
- $typeCache = [];
- foreach ($topic_type_list as $topic_type_item) {
- $id = $topic_type_item['id'];
- unset($topic_type_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_TOPIC_TYPE,
- 'from_id' => $id
- ]);
- $TopicType = TopicType::findOne($storeSyncExtLog->to_id ?? 0) ?: new TopicType();
- $TopicType->attributes = $topic_type_item;
- $TopicType->store_id = $toId;
- if (!$TopicType->save()) {
- throw new \Exception(json_encode($TopicType->errors, JSON_UNESCAPED_UNICODE));
- };
- $typeCache[$id] = $TopicType->id;
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $TopicType->id, StoreSyncExtLog::TYPE_TOPIC_TYPE);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功',
- 'data' => [
- 'typeCache' => $typeCache
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function topicContentSetting($fromStoreId, $toId, $typeCache) {
- try {
- $topic_list = Topic::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
- Topic::updateAll(['is_delete' => 0], ['store_id' => $toId, 'is_delete' => 0]);
- foreach ($topic_list as $topic_item) {
- $id = $topic_item['id'];
- unset($topic_item['id']);
- //判断是否为当前商城同步过此商品
- $storeSyncExtLog = StoreSyncExtLog::findOne([
- 'from_store_id' => $fromStoreId,
- 'to_store_id' => $toId,
- 'type' => StoreSyncExtLog::TYPE_TOPIC,
- 'from_id' => $id
- ]);
- $Topic = Topic::findOne($storeSyncExtLog->to_id ?? 0) ?: new Topic();
- $Topic->attributes = $topic_item;
- $Topic->store_id = $toId;
- $Topic->type = $typeCache[$topic_item['type']];
- if (!$Topic->save()) {
- throw new \Exception(json_encode($Topic->errors, JSON_UNESCAPED_UNICODE));
- };
- (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $Topic->id, StoreSyncExtLog::TYPE_TOPIC);
- }
- return [
- 'code' => 0,
- 'msg' => '同步成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /** 同步专题end **/
- }
|