StoreSyncJob.php 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\jobs;
  8. use app\constants\OptionSetting;
  9. use app\models\AboutArticle;
  10. use app\models\BookingGoodsCat;
  11. use app\models\BookingGoodsExt;
  12. use app\models\IntegralSetting;
  13. use app\models\NewIntegralCat;
  14. use app\models\NewIntegralSetting;
  15. use app\models\Option;
  16. use app\models\PostageRules;
  17. use app\models\Qrcode;
  18. use app\models\SeckillActivity;
  19. use app\models\SeckillActivityGoods;
  20. use app\models\ShareHolderLevel;
  21. use app\models\ShareLevel;
  22. use app\models\StoreCloud;
  23. use app\models\StoreSyncExtLog;
  24. use app\models\Topic;
  25. use app\models\TopicType;
  26. use app\models\VideoGoods;
  27. use app\models\VideoGoodsCat;
  28. use app\models\VideoGoodsList;
  29. use app\models\VideoGoodsSetting;
  30. use app\models\WorkerCat;
  31. use app\models\WorkerGoodsCat;
  32. use app\models\WorkerGoodsExt;
  33. use app\models\WorkerLevel;
  34. use app\models\WorkerSetting;
  35. use app\modules\admin\models\GoodsForm;
  36. use app\modules\admin\models\MerchantForm;
  37. use yii\base\BaseObject;
  38. use yii\db\Exception;
  39. use yii\queue\JobInterface;
  40. use app\models\StoreSync;
  41. use app\models\StoreSyncLog;
  42. use app\models\ActivityCutPrice;
  43. use app\models\ActivityCutPriceBanner;
  44. use app\models\ActivityCutPriceCat;
  45. use app\models\ActivityCutPriceGoods;
  46. use app\models\PtActivity;
  47. use app\models\PtActivityBanner;
  48. use app\models\PtActivityGoodsCat;
  49. use app\models\PtActivityGoods;
  50. use app\models\Goods;
  51. use app\models\AttrGroup;
  52. use app\models\Attr;
  53. use app\models\Cat;
  54. use app\models\GoodsCat;
  55. use app\models\GoodsPic;
  56. use app\models\CloudGoodsBind;
  57. use app\models\NewDiyTemplate;
  58. /**
  59. * 店铺同步
  60. */
  61. class StoreSyncJob extends BaseObject implements JobInterface
  62. {
  63. public $id;
  64. public $type;
  65. public $to_id;
  66. public function execute($queue)
  67. {
  68. try {
  69. debug_log(['id' => $this->id, 'type' => $this->type, 'to_id' => $this->to_id], 'queuePush.log');
  70. $type = $this->type;
  71. $t = \Yii::$app->db->beginTransaction();
  72. if (isset($type)) {
  73. $default = \app\models\Option::get('store_template_default', 0, 'saas', null)['value'];
  74. if(!$default) {
  75. return;
  76. }
  77. $default = json_decode($default, true);
  78. if (empty($default)) {
  79. return;
  80. }
  81. $fromStoreId = $default['store_id'];
  82. $imports = $default['type'];
  83. $toStoreIds = [$this->to_id];
  84. } else {
  85. $item = StoreSyncLog::findOne($this->id);
  86. $syncItem = StoreSync::findOne($item->sync_id);
  87. $fromStoreId = $item->from_store_id;
  88. $toStoreIds = \json_decode($item->to_store_ids, true);
  89. $imports = json_decode($syncItem->imports, true);
  90. }
  91. debug_log(['from' => $fromStoreId, 'to_store_' => $toStoreIds, 'imports' => $imports], 'queuePush.log');
  92. if (in_array('product', $imports) && !in_array('product_cat', $imports)) {
  93. array_push($imports, 'product_cat');
  94. }
  95. foreach ($toStoreIds as $toId) {
  96. //配送规则
  97. if (in_array('delivery_rules', $imports)) {
  98. list($deliveryRulesCache) = $this->syncDeliveryRules($fromStoreId, $toId);
  99. }
  100. // if (in_array('product_cat', $imports)) {
  101. // $cacheCat = $this->doGoodsCat($fromStoreId, $toId);
  102. // }
  103. if (in_array('product', $imports) && in_array('delivery_rules', $imports) && in_array('product_cat', $imports)) {
  104. //商品 分类
  105. list($goodsCache, $cacheCat) = $this->syncGoods($fromStoreId, $toId, $deliveryRulesCache, $message);
  106. // if (!empty($message)) {
  107. // throw new \Exception($message);
  108. // }
  109. $cacheCat_ = $cacheCat;
  110. if (isset($item)) {
  111. $item->goods_info = json_encode(['goods_result' => $goodsCache, 'cat_result' => $cacheCat]);
  112. }
  113. //拼团
  114. $ptCacheActivity = $ptCacheCat = $ptCache = [];
  115. if (in_array('pintuan', $imports)) {
  116. [$ptCacheActivity, $ptCacheBanner, $ptCacheCat, $ptCache, $ptCacheSetting] = $this->syncGroup($fromStoreId, $toId, $goodsCache);
  117. if (isset($item)) {
  118. $item->pintuan = json_encode(['cacheActivity' => $ptCacheActivity, 'cacheBanner' => $ptCacheBanner, 'cacheCat' => $ptCacheCat, 'cache' => $ptCache, 'cacheSetting' => $ptCacheSetting], JSON_UNESCAPED_UNICODE);
  119. }
  120. }
  121. //砍价
  122. $cutPriceCacheActivity = $cutPriceCacheCat = $cutPriceCache = [];
  123. if (in_array('activityCutPrice', $imports)) {
  124. [$cutPriceCacheActivity, $cutPriceCacheBanner, $cutPriceCacheCat, $cutPriceCache] = $this->syncBargain($fromStoreId, $toId, $goodsCache);
  125. if (isset($item)) {
  126. $item->bargain = json_encode(['cacheActivity' => $cutPriceCacheActivity, 'cacheBanner' => $cutPriceCacheBanner, 'cacheCat' => $cutPriceCacheCat, 'cache' => $cutPriceCache], JSON_UNESCAPED_UNICODE);
  127. }
  128. }
  129. //秒杀
  130. $seckillCacheActivity = $seckillCache = [];
  131. if (in_array('seckill', $imports)) {
  132. [$seckillCacheActivity, $seckillCache] = $this->seckill($fromStoreId, $toId, $goodsCache);
  133. if (isset($item)) {
  134. $item->seckill = json_encode(['cacheActivity' => $seckillCacheActivity, 'cache' => $seckillCache], JSON_UNESCAPED_UNICODE);
  135. }
  136. }
  137. //分销设置
  138. if (in_array('shareConfig', $imports)) {
  139. [$result, $levelResult] = $this->shareConfig($fromStoreId, $toId, $goodsCache);
  140. if (isset($item)) {
  141. $item->shareConfig = json_encode(['config_result' => $result, 'level_result' => $levelResult], JSON_UNESCAPED_UNICODE);
  142. }
  143. }
  144. //股东设置
  145. if (in_array('shareHolderConfig', $imports)) {
  146. [$result, $levelResult] = $this->shareHolderConfig($fromStoreId, $toId, $goodsCache);
  147. if (isset($item)) {
  148. $item->shareHolderConfig = json_encode(['config_result' => $result, 'level_result' => $levelResult], JSON_UNESCAPED_UNICODE);
  149. }
  150. }
  151. //短视频
  152. if (in_array('videoGoods', $imports)) {
  153. [$goods_result, $goods_cat_result, $goods_setting_result, $goods_list_result] = $this->videoGoods($fromStoreId, $toId, $goodsCache);
  154. if (isset($item)) {
  155. $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);
  156. }
  157. }
  158. //积分商城
  159. if (in_array('integralStore', $imports)) {
  160. [$goods_cat_result, $setting_result] = $this->integralStore($fromStoreId, $toId);
  161. if (isset($item)) {
  162. $item->integralStore = json_encode(['goods_cat_result' => $goods_cat_result, 'setting_result' => $setting_result], JSON_UNESCAPED_UNICODE);
  163. }
  164. }
  165. //上门服务
  166. $worker_goods_cat_result = $worker_goods_result = $worker_cat_result = [];
  167. if (in_array('workerConfig', $imports)) {
  168. [$worker_goods_cat_result, $worker_goods_result, $worker_cat_result, $worker_level_result, $worker_setting] = $this->workerConfig($fromStoreId, $toId, $goodsCache);
  169. if (isset($item)) {
  170. $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);
  171. }
  172. }
  173. //预约
  174. $booking_goods_cat_cache = [];
  175. if (in_array('bookingGoods', $imports)) {
  176. [$booking_goods_cat_cache, $ext_result] = $this->bookingHome($fromStoreId, $toId, $goodsCache);
  177. }
  178. //装修
  179. if (in_array('diy', $imports)) {
  180. $result = $this->syncDiy($fromStoreId, $toId, $goodsCache, $cacheCat_, [
  181. 'ptCacheActivity' => $ptCacheActivity,
  182. 'ptCacheCat' => $ptCacheCat,
  183. 'ptCache' => $ptCache,
  184. 'cutPriceCacheActivity' => $cutPriceCacheActivity,
  185. 'cutPriceCacheCat' => $cutPriceCacheCat,
  186. 'cutPriceCache' => $cutPriceCache,
  187. 'seckillCacheActivity' => $seckillCacheActivity,
  188. 'seckillCache' => $seckillCache,
  189. 'worker_goods_cat_result' => $worker_goods_cat_result,
  190. 'worker_goods_result' => $worker_goods_result,
  191. 'worker_cat_result' => $worker_cat_result,
  192. 'booking_goods_cat_cache' => $booking_goods_cat_cache
  193. ]);
  194. if (isset($item)) {
  195. $item->diy = json_encode(['diy' => $result], JSON_UNESCAPED_UNICODE);
  196. }
  197. }
  198. }
  199. if (in_array('article', $imports)) {
  200. $result = $this->articleSetting($fromStoreId, $toId);
  201. if (isset($item)) {
  202. $item->article = json_encode(['result' => $result], JSON_UNESCAPED_UNICODE);
  203. }
  204. }
  205. if (in_array('topic', $imports)) {
  206. [$type_result, $result] = $this->topicSetting($fromStoreId, $toId);
  207. if (isset($item)) {
  208. $item->topic = json_encode(['type_result' => $type_result, 'result' => $result], JSON_UNESCAPED_UNICODE);
  209. }
  210. }
  211. }
  212. if (isset($item)) {
  213. $item->status = 1;
  214. $item->result = 1;
  215. $item->save();
  216. }
  217. // $syncItem->status = 0;
  218. // $syncItem->save();
  219. $t->commit();
  220. } catch (\Throwable $e) {
  221. $logName = 'store_sync.log';
  222. debug_log('任务ID => ' . $this->id, $logName);
  223. debug_log('异常消息 => ' . $e->getMessage(), $logName);
  224. debug_log('异常文件 => ' . $e->getFile(), $logName);
  225. debug_log('异常行数 => ' . $e->getLine(), $logName);
  226. debug_log($e->getTraceAsString(), $logName);
  227. debug_log('++++++++++++++++++++++++++++', $logName);
  228. $t->rollBack();
  229. $item->status = 1;
  230. $item->result = 2;
  231. $item->save();
  232. // $syncItem->status = 0;
  233. // $syncItem->save();
  234. }
  235. }
  236. /**
  237. * 同步商城配送规则
  238. */
  239. private function syncDeliveryRules($from_store_id, $to_store_id) {
  240. //删除原来的配送规则
  241. debug_log('开始同步', 'queuePush.log');
  242. PostageRules::deleteAll(['store_id' => $to_store_id]);
  243. $PostageRulesList = PostageRules::find()->where([
  244. 'store_id' => $from_store_id,
  245. 'is_delete' => 0,
  246. 'mch_id' => 0
  247. ])->asArray()->all();
  248. $rules_id_cache = [];
  249. foreach ($PostageRulesList as $item) {
  250. //判断是否为当前商城同步过此规则
  251. $storeSyncExtLog = StoreSyncExtLog::findOne([
  252. 'from_store_id' => $from_store_id,
  253. 'to_store_id' => $to_store_id,
  254. 'type' => StoreSyncExtLog::TYPE_POSTAGE_RULES,
  255. 'from_id' => $item['id']
  256. ]);
  257. $rules_id_cache[$item['id']] = 0;
  258. $postageRules = PostageRules::findOne($storeSyncExtLog->to_id ?? 0) ?: new PostageRules();
  259. $postageRules->attributes = $item;
  260. $postageRules->store_id = $to_store_id;
  261. $postageRules->mch_id = 0;
  262. if (!$postageRules->save()) {
  263. continue;
  264. }
  265. $rules_id_cache[$item['id']] = $postageRules->id;
  266. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $item['id'], $postageRules->id, StoreSyncExtLog::TYPE_POSTAGE_RULES);
  267. }
  268. debug_log($rules_id_cache, 'queuePush.log');
  269. return $rules_id_cache;
  270. }
  271. /** 同步商品 start */
  272. // 同步商品
  273. private function syncGoods($from_store_id, $to_store_id, $deliveryRulesCache, &$message)
  274. {
  275. $goods_ids = Goods::find()->where(['store_id' => $to_store_id, 'is_delete' => 0, 'mch_id' => 0])->select('id')->column();
  276. $message = null;
  277. $cloud_store_token = get_merchant_token(0, $to_store_id, $message);
  278. $cloud_goods_id = [];
  279. foreach ($goods_ids as $param) {
  280. $goods = Goods::findOne($param);
  281. if ($goods->cloud_goods_id && !intval($goods->is_wholesale)) {
  282. if (!$cloud_store_token) {
  283. $message = $message['msg'];
  284. return [];
  285. }
  286. $cloud_goods_id[] = $goods->cloud_goods_id;
  287. }
  288. }
  289. if ($cloud_goods_id) {
  290. $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
  291. $mch_set_submit_order_data = [];
  292. $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  293. $mch_set_submit_order_data['goods_id'] = implode(',', $cloud_goods_id);
  294. $domain = (new OptionSetting)->getCloudDomainName();
  295. $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
  296. $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
  297. if($mchSetSubmitOrderInfo['code'] != 0){
  298. $message = '删除转单商品' . $mchSetSubmitOrderInfo['msg'];
  299. return [];
  300. } else {
  301. CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => $to_store_id, 'cloud_goods_id' => $cloud_goods_id, 'is_delete' => 0]);
  302. }
  303. }
  304. // 软删除商品
  305. Goods::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0, 'mch_id' => 0]);
  306. // 软删除规格组和规格
  307. $attrGroups = AttrGroup::find()->where(['store_id' => $to_store_id, 'is_delete' => 0])->all();
  308. foreach ($attrGroups as $attrGroup) {
  309. Attr::deleteAll(['attr_group_id' => $attrGroup->id, 'is_delete' => 0]);
  310. $attrGroup->is_delete = 1;
  311. $attrGroup->save();
  312. }
  313. AttrGroup::deleteAll(['store_id' => $to_store_id]);
  314. // 删除商品关联分类
  315. GoodsCat::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0]);
  316. // 删除云仓商品绑定
  317. CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => $to_store_id, 'is_delete' => 0]);
  318. // 商品分类处理
  319. $cacheCat = $this->doGoodsCat($from_store_id, $to_store_id);
  320. // 商品规格处理
  321. $goodsList = Goods::find()->where([
  322. 'store_id' => $from_store_id,
  323. 'is_delete' => 0,
  324. 'mch_id' => 0
  325. ])->andWhere(['<=', 'md_food_id', 0])->orderBy(['id' => SORT_ASC])->asArray()->all();
  326. $goodsCache = [];
  327. $store_cloud_id = StoreCloud::findOne(['store_id' => $to_store_id, 'is_delete' => 0]);
  328. foreach ($goodsList as $goods) {
  329. //判断是否为当前商城同步过此商品
  330. $storeSyncExtLog = StoreSyncExtLog::findOne([
  331. 'from_store_id' => $from_store_id,
  332. 'to_store_id' => $to_store_id,
  333. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  334. 'from_id' => $goods['id']
  335. ]);
  336. $goods_id_cache = $goods['id'];
  337. $g = Goods::findOne($storeSyncExtLog->to_id ?? 0) ?: new Goods();
  338. if (!empty($goods['attr'])) {
  339. $attrs = \json_decode($goods['attr'], true);
  340. list($attrGroupCache, $attrCache) = $this->doGoodsAttr($from_store_id, $to_store_id, $attrs);
  341. foreach ($attrs as &$attr) {
  342. if ($attr['attr_list']) {
  343. foreach ($attr['attr_list'] as &$att) {
  344. if (isset($att['attr_id']) && isset($attrCache[$att['attr_id']])) {
  345. $att['attr_id'] = $attrCache[$att['attr_id']];
  346. }
  347. }
  348. }
  349. }
  350. $goods['attr'] = \json_encode($attrs);
  351. }
  352. unset($goods['id']);
  353. $goods['store_id'] = $to_store_id;
  354. $g->setAttributes($goods, false);
  355. $g->cloud_goods_id = $goods['cloud_goods_id'];
  356. $g->is_wholesale = $goods['is_wholesale'];
  357. $g->cloud_supplier_id = $goods['cloud_supplier_id'];
  358. $g->delivery_rules_id = $deliveryRulesCache[$goods['delivery_rules_id']] ?? 0;
  359. $g->save();
  360. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $goods_id_cache, $g->id, StoreSyncExtLog::TYPE_PRODUCT);
  361. $goodsCache[$goods_id_cache] = $g->id;
  362. if ($g->cloud_goods_id > 0 && !intval($g->is_wholesale)) {
  363. try {
  364. $form = new GoodsForm();
  365. $form->id = $g->cloud_goods_id;
  366. $form->store_id = $to_store_id;
  367. $goodsInfo = $form->saveCloudGoods();
  368. $merchantForm = new MerchantForm();
  369. $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
  370. $r = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'], $goodsInfo['goods_id'], $to_store_id);
  371. } catch (\Exception $e) {
  372. }
  373. }
  374. }
  375. // 商品分类处理
  376. $goodsCats = GoodsCat::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
  377. foreach ($goodsCats as $goodsCat) {
  378. //判断是否为当前商城同步过此商品
  379. $storeSyncExtLog = StoreSyncExtLog::findOne([
  380. 'from_store_id' => $from_store_id,
  381. 'to_store_id' => $to_store_id,
  382. 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT,
  383. 'from_id' => $goodsCat['id']
  384. ]);
  385. if (isset($goodsCache[$goodsCat['goods_id']]) && isset($cacheCat[$goodsCat['cat_id']])) {
  386. $id = $goodsCat['id'];
  387. $cat = GoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsCat();
  388. $goodsCat['goods_id'] = $goodsCache[$goodsCat['goods_id']];
  389. $goodsCat['cat_id'] = $cacheCat[$goodsCat['cat_id']];
  390. $goodsCat['store_id'] = $to_store_id;
  391. unset($goodsCat['id']);
  392. $cat->setAttributes($goodsCat, false);
  393. $cat->save();
  394. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $cat->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_CAT);
  395. }
  396. }
  397. // 商品组图处理
  398. $inGoodsIds = \array_keys($goodsCache);
  399. $goodsPic = GoodsPic::find()->where(['is_delete' => 0, 'goods_id' => $inGoodsIds])->asArray()->all();
  400. foreach ($goodsPic as $pic) {
  401. //判断是否为当前商城同步过此商品
  402. $storeSyncExtLog = StoreSyncExtLog::findOne([
  403. 'from_store_id' => $from_store_id,
  404. 'to_store_id' => $to_store_id,
  405. 'type' => StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC,
  406. 'from_id' => $pic['id']
  407. ]);
  408. $newPic = GoodsPic::findOne($storeSyncExtLog->to_id ?? 0) ?: new GoodsPic();
  409. $newPic->goods_id = $goodsCache[$pic['goods_id']];
  410. $newPic->pic_url = $pic['pic_url'];
  411. $newPic->is_delete = 0;
  412. $newPic->save();
  413. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $pic['id'], $newPic->id, StoreSyncExtLog::TYPE_PRODUCT_GOODS_PIC);
  414. }
  415. // 云仓商品绑定
  416. // $cloudGoodsBinds = CloudGoodsBind::find()->where(['is_delete' => 0, 'store_id' => $from_store_id, 'goods_id' => $inGoodsIds])->asArray()->all();
  417. // foreach ($cloudGoodsBinds as $goodsBind) {
  418. // $cloudGoods = new CloudGoodsBind();
  419. // $cloudGoods->cloud_goods_id = $goodsBind['cloud_goods_id'];
  420. // $cloudGoods->cloud_supplier_id = $goodsBind['cloud_supplier_id'];
  421. // $cloudGoods->store_id = $to_store_id;
  422. // $cloudGoods->goods_id = $goodsCache[$goodsBind['goods_id']];
  423. // $cloudGoods->is_delete = 0;
  424. // $cloudGoods->created_at = time();
  425. // $cloudGoods->save();
  426. // }
  427. //重新选品
  428. return [$goodsCache, $cacheCat];
  429. }
  430. // 商品规格创建
  431. private function doGoodsAttr($from_store_id, $to_store_id, $attrs = null)
  432. {
  433. $attr_id = [];
  434. if ($attrs) {
  435. $attrs = json_decode($attrs, true);
  436. foreach ($attrs as $attr_item) {
  437. $attr_id_item = array_column($attr_item['attr_list'], 'attr_id');
  438. $attr_id = array_merge($attr_id, $attr_id_item);
  439. }
  440. }
  441. $attrGroupCache = $attrCache = [];
  442. if ($attr_id) {
  443. $attr_id = array_unique($attr_id);
  444. foreach ($attr_id as $attr_id_item) {
  445. $attrGroup = null;
  446. $old_attr = Attr::findOne($attr_id_item);
  447. if ($old_attr) {
  448. $old_attr_group = AttrGroup::findOne($old_attr->attr_group_id);
  449. //判断是否为当前商城同步过此商品
  450. $storeSyncAttrGroupExtLog = StoreSyncExtLog::findOne([
  451. 'from_store_id' => $from_store_id,
  452. 'to_store_id' => $to_store_id,
  453. 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP,
  454. 'from_id' => $old_attr_group->id
  455. ]);
  456. $attrGroup = AttrGroup::findOne($storeSyncAttrGroupExtLog->to_id ?? 0) ?: new AttrGroup();
  457. $attrGroup->store_id = $to_store_id;
  458. $attrGroup->attr_group_name = $old_attr_group->attr_group_name;
  459. $attrGroup->is_delete = $old_attr_group->is_delete;
  460. $attrGroup->save();
  461. $attrGroupCache[$old_attr_group->id] = $attrGroup->id;
  462. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr_group->id, $attrGroup->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR_GROUP);
  463. }
  464. //判断是否为当前商城同步过此商品
  465. $storeSyncAttrExtLog = StoreSyncExtLog::findOne([
  466. 'from_store_id' => $from_store_id,
  467. 'to_store_id' => $to_store_id,
  468. 'type' => StoreSyncExtLog::TYPE_PRODUCT_ATTR,
  469. 'from_id' => $attr_id_item
  470. ]);
  471. $attr = Attr::findOne($storeSyncAttrExtLog->to_id ?? 0) ?: new Attr();
  472. $attr->attr_group_id = $attrGroup->id ?? 0;
  473. $attr->attr_name = $old_attr->attr_name;
  474. $attr->is_delete = $old_attr->is_delete;
  475. $attr->is_default = 0;
  476. $attr->save();
  477. $attrCache[$old_attr->id] = $attr->id;
  478. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $old_attr->id, $attr->id, StoreSyncExtLog::TYPE_PRODUCT_ATTR);
  479. }
  480. }
  481. return [$attrGroupCache, $attrCache];
  482. }
  483. // 商品分类创建
  484. private function doGoodsCat($from_store_id, $to_store_id)
  485. {
  486. Cat::deleteAll(['store_id' => $to_store_id, 'is_delete' => 0]);
  487. $cats = Cat::find()->where([
  488. 'store_id' => $from_store_id,
  489. 'is_delete' => 0,
  490. 'parent_id' => 0,
  491. ])->asArray()->all();
  492. $catCache = [];
  493. foreach ($cats as $cat) {
  494. $catC = $cat;
  495. //判断是否为当前商城同步过此商品
  496. $storeSyncExtLog = StoreSyncExtLog::findOne([
  497. 'from_store_id' => $from_store_id,
  498. 'to_store_id' => $to_store_id,
  499. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  500. 'from_id' => $cat['id']
  501. ]);
  502. $c = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  503. unset($catC['id']);
  504. $catC['store_id'] = $to_store_id;
  505. $c->setAttributes($catC, false);
  506. $c->save();
  507. $catCache[$cat['id']] = $c->id;
  508. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat['id'], $c->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  509. $cats1 = Cat::find()->where([
  510. 'store_id' => $from_store_id,
  511. 'is_delete' => 0,
  512. 'parent_id' => $cat['id'],
  513. ])->asArray()->all();
  514. foreach ($cats1 as $cat1) {
  515. $catC1 = $cat1;
  516. //判断是否为当前商城同步过此商品
  517. $storeSyncExtLog = StoreSyncExtLog::findOne([
  518. 'from_store_id' => $from_store_id,
  519. 'to_store_id' => $to_store_id,
  520. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  521. 'from_id' => $cat1['id']
  522. ]);
  523. $c1 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  524. unset($catC1['id']);
  525. $catC1['store_id'] = $to_store_id;
  526. $catC1['parent_id'] = $c->id;
  527. $c1->setAttributes($catC1, false);
  528. $c1->save();
  529. $catCache[$cat1['id']] = $c1->id;
  530. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat1['id'], $c1->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  531. $cats2 = Cat::find()->where([
  532. 'store_id' => $from_store_id,
  533. 'is_delete' => 0,
  534. 'parent_id' => $cat1['id'],
  535. ])->asArray()->all();
  536. foreach ($cats2 as $cat2) {
  537. $catC2 = $cat2;
  538. //判断是否为当前商城同步过此商品
  539. $storeSyncExtLog = StoreSyncExtLog::findOne([
  540. 'from_store_id' => $from_store_id,
  541. 'to_store_id' => $to_store_id,
  542. 'type' => StoreSyncExtLog::TYPE_PRODUCT_CAT,
  543. 'from_id' => $cat2['id']
  544. ]);
  545. $c2 = Cat::findOne($storeSyncExtLog->to_id ?? 0) ?: new Cat();
  546. unset($catC2['id']);
  547. $catC2['store_id'] = $to_store_id;
  548. $catC2['parent_id'] = $c1->id;
  549. $c2->setAttributes($catC2, false);
  550. $c2->save();
  551. $catCache[$cat2['id']] = $c2->id;
  552. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $cat2['id'], $c2->id, StoreSyncExtLog::TYPE_PRODUCT_CAT);
  553. }
  554. }
  555. }
  556. return $catCache;
  557. }
  558. /** 同步商品 end */
  559. /** 同步拼团 start */
  560. // 同步拼团
  561. private function syncGroup($from_store_id, $to_store_id, $cacheGoods)
  562. {
  563. $cacheActivity = $this->doCopyPt($from_store_id, $to_store_id);
  564. $cacheBanner = $this->doCopyPtBanner($from_store_id, $to_store_id);
  565. $cacheCat = $this->doCopyPtCat($from_store_id, $to_store_id);
  566. $cache = $this->doCopyPtGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods);
  567. $cacheSetting = $this->doCopyPtSetting($from_store_id, $to_store_id);
  568. return [$cacheActivity, $cacheBanner, $cacheCat, $cache, $cacheSetting];
  569. }
  570. public function doCopyPtSetting($from_store_id, $to_store_id) {
  571. try {
  572. $article_ = AboutArticle::find()->where([
  573. 'is_delete' => AboutArticle::IS_DELETE_NO,
  574. 'store_id' => $from_store_id,
  575. 'type' => 1
  576. ])->select("desc")->asArray()->one();
  577. if ($article_) {
  578. $article = AboutArticle::findOne(['is_delete' => AboutArticle::IS_DELETE_NO, 'store_id' => $to_store_id, 'type' => 1]);
  579. if (!$article) {
  580. $article = new AboutArticle();
  581. }
  582. unset($article_['id']);
  583. $article->attributes = $article_;
  584. $article->store_id = $to_store_id;
  585. $article->name = $article_['name'] ?: '';
  586. $article->created_at = time();
  587. $article->updated_at = time();
  588. $article->type = 1;
  589. if (!$article->save()) {
  590. throw new \Exception(json_encode($article->errors, JSON_UNESCAPED_UNICODE));
  591. };
  592. }
  593. return [
  594. 'code' => 0,
  595. 'msg' => "同步成功"
  596. ];
  597. } catch (\Exception $e) {
  598. return [
  599. 'code' => 1,
  600. 'msg' => $e->getMessage()
  601. ];
  602. }
  603. }
  604. public function doCopyPt($from_store_id, $to_store_id) {
  605. try {
  606. PtActivity::deleteAll(['store_id' => $to_store_id]);
  607. $list = PtActivity::find()
  608. ->where([
  609. 'store_id' => $from_store_id,
  610. 'is_delete' => 0,
  611. ])->orderBy(['id' => SORT_ASC])->all();
  612. $cache = [];
  613. foreach ($list as $value) {
  614. //判断是否为当前商城同步过此商品
  615. $storeSyncExtLog = StoreSyncExtLog::findOne([
  616. 'from_store_id' => $from_store_id,
  617. 'to_store_id' => $to_store_id,
  618. 'type' => StoreSyncExtLog::TYPE_PT,
  619. 'from_id' => $value['id']
  620. ]);
  621. $id = $value['id'];
  622. $attr = $value->attributes;
  623. unset($attr['id']);
  624. $attr['store_id'] = $to_store_id;
  625. $attr['goods_ids'] = '';
  626. $model = PtActivity::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivity();
  627. $model->setAttributes($attr, false);
  628. $model->save();
  629. $cache[$id] = $model->id;
  630. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $value['id'], $model->id, StoreSyncExtLog::TYPE_PT);
  631. }
  632. return $cache;
  633. } catch(\Exception $e) {
  634. return [
  635. 'code' => 1,
  636. 'msg' => $e->getMessage()
  637. ];
  638. }
  639. }
  640. public function doCopyPtBanner($from_store_id, $to_store_id) {
  641. try {
  642. PtActivityBanner::deleteAll(['store_id' => $to_store_id]);
  643. $list = PtActivityBanner::find()
  644. ->where([
  645. 'store_id' => $from_store_id,
  646. 'is_delete' => 0,
  647. ])->orderBy(['id' => SORT_ASC])->all();
  648. $cache = [];
  649. foreach ($list as $value) {
  650. $id = $value['id'];
  651. $attr = $value->attributes;
  652. unset($attr['id']);
  653. $attr['store_id'] = $to_store_id;
  654. $model = new PtActivityBanner();
  655. $model->setAttributes($attr, false);
  656. $model->save();
  657. $cache[$id] = $model->id;
  658. }
  659. return $cache;
  660. } catch(\Exception $e) {
  661. return [
  662. 'code' => 1,
  663. 'msg' => $e->getMessage()
  664. ];
  665. }
  666. }
  667. public function doCopyPtCat($from_store_id, $to_store_id) {
  668. try {
  669. PtActivityGoodsCat::deleteAll(['store_id' => $to_store_id]);
  670. $list = PtActivityGoodsCat::find()
  671. ->where([
  672. 'store_id' => $from_store_id,
  673. 'is_delete' => 0,
  674. ])->orderBy(['id' => SORT_ASC])->all();
  675. $cache = [];
  676. foreach ($list as $value) {
  677. //判断是否为当前商城同步过此商品
  678. $storeSyncExtLog = StoreSyncExtLog::findOne([
  679. 'from_store_id' => $from_store_id,
  680. 'to_store_id' => $to_store_id,
  681. 'type' => StoreSyncExtLog::TYPE_PT_GOODS_CAT,
  682. 'from_id' => $value['id']
  683. ]);
  684. $id = $value['id'];
  685. $attr = $value->attributes;
  686. unset($attr['id']);
  687. $attr['store_id'] = $to_store_id;
  688. $model = PtActivityGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivityGoodsCat();
  689. $model->setAttributes($attr, false);
  690. $model->save();
  691. $cache[$id] = $model->id;
  692. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_PT_GOODS_CAT);
  693. }
  694. return $cache;
  695. } catch(\Exception $e) {
  696. return [
  697. 'code' => 1,
  698. 'msg' => $e->getMessage()
  699. ];
  700. }
  701. }
  702. public function doCopyPtGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods) {
  703. try {
  704. debug_log($cacheActivity,'store_sync.log');
  705. $old_activity_id = array_keys($cacheActivity);
  706. $list = PtActivityGoods::find()
  707. ->where([
  708. 'store_id' => $from_store_id,
  709. 'is_delete' => 0,
  710. 'activity_id' => $old_activity_id
  711. ])->orderBy(['id' => SORT_ASC])->all();
  712. PtActivityGoods::deleteAll(['store_id' => $to_store_id]);
  713. $cache = [];
  714. foreach ($list as $value) {
  715. $id = $value['id'];
  716. $attr = $value->attributes;
  717. if (!$cacheGoods[$attr['goods_id']]) {
  718. continue;
  719. }
  720. unset($attr['id']);
  721. $attr['store_id'] = $to_store_id;
  722. $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
  723. $cat = PtActivityGoodsCat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]);
  724. if (!$cat) {
  725. continue;
  726. }
  727. $attr['cat_id'] = $cacheCat[$attr['cat_id']];
  728. $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0, 'mch_id' => 0]);
  729. if (!$goods) {
  730. continue;
  731. }
  732. $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
  733. $agattr = json_decode($value['attr'], true);
  734. $goodsNew = Goods::findOne($attr['goods_id']);
  735. $gattrNew = json_decode($goodsNew->attr, true);
  736. $agattrNew = [];
  737. foreach($agattr as $k => $item){
  738. $attrNames = array_column($item['attr_list'], 'attr_name');
  739. // var_dump($attrNames);
  740. foreach($gattrNew as $v){
  741. $attrNamesNew = array_column($v['attr_list'], 'attr_name');
  742. // var_dump($attrNamesNew);
  743. if($attrNames === $attrNamesNew){
  744. // var_dump('$attrNames == $attrNamesNew');
  745. $item['attr_list'] = $v['attr_list'];
  746. }
  747. }
  748. $agattrNew[] = $item;
  749. }
  750. $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
  751. //判断是否为当前商城同步过此商品
  752. $storeSyncExtLog = StoreSyncExtLog::findOne([
  753. 'from_store_id' => $from_store_id,
  754. 'to_store_id' => $to_store_id,
  755. 'type' => StoreSyncExtLog::TYPE_PT_GOODS,
  756. 'from_id' => $value['id']
  757. ]);
  758. $model = PtActivityGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new PtActivityGoods();
  759. $model->setAttributes($attr, false);
  760. $model->save();
  761. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $value['id'], $model->id, StoreSyncExtLog::TYPE_PT_GOODS);
  762. $cache[$id] = $model->id;
  763. }
  764. return $cache;
  765. } catch (\Exception $e) {
  766. return [
  767. 'code' => 1,
  768. 'msg' => $e->getMessage()
  769. ];
  770. }
  771. }
  772. /** 同步拼团 end */
  773. /** 同步砍价 start */
  774. // 同步砍价
  775. private function syncBargain($from_store_id, $to_store_id, $cacheGoods)
  776. {
  777. $cacheActivity = $this->doCopyCutPrice($from_store_id, $to_store_id);
  778. $cacheBanner = $this->doCopyCutPriceBanner($from_store_id, $to_store_id);
  779. $cacheCat = $this->doCopyCutPriceCat($from_store_id, $to_store_id);
  780. $cache = $this->doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods);
  781. return [$cacheActivity, $cacheBanner, $cacheCat, $cache];
  782. }
  783. public function doCopyCutPrice($from_store_id, $to_store_id) {
  784. try {
  785. ActivityCutPrice::deleteAll(['store_id' => $to_store_id]);
  786. $list = ActivityCutPrice::find()
  787. ->where([
  788. 'store_id' => $from_store_id,
  789. 'is_delete' => 0,
  790. ])->orderBy(['id' => SORT_ASC])->all();
  791. $cache = [];
  792. foreach ($list as $value) {
  793. $id = $value['id'];
  794. $attr = $value->attributes;
  795. unset($attr['id']);
  796. $attr['store_id'] = $to_store_id;
  797. //判断是否为当前商城同步过此商品
  798. $storeSyncExtLog = StoreSyncExtLog::findOne([
  799. 'from_store_id' => $from_store_id,
  800. 'to_store_id' => $to_store_id,
  801. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE,
  802. 'from_id' => $value['id']
  803. ]);
  804. unset($attr['id']);
  805. $attr['store_id'] = $to_store_id;
  806. $attr['goods_ids'] = '';
  807. $model = ActivityCutPrice::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPrice();
  808. $model->setAttributes($attr, false);
  809. $model->save();
  810. $cache[$id] = $model->id;
  811. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE);
  812. }
  813. return $cache;
  814. } catch (\Exception $e) {
  815. return [
  816. 'code' => 1,
  817. 'msg' => $e->getMessage()
  818. ];
  819. }
  820. }
  821. public function doCopyCutPriceBanner($from_store_id, $to_store_id) {
  822. try {
  823. ActivityCutPriceBanner::deleteAll(['store_id' => $to_store_id]);
  824. $list = ActivityCutPriceBanner::find()
  825. ->where([
  826. 'store_id' => $from_store_id,
  827. 'is_delete' => 0,
  828. ])->orderBy(['id' => SORT_ASC])->all();
  829. $cache = [];
  830. foreach ($list as $value) {
  831. $id = $value['id'];
  832. $attr = $value->attributes;
  833. unset($attr['id']);
  834. $attr['store_id'] = $to_store_id;
  835. $model = new ActivityCutPriceBanner();
  836. $model->setAttributes($attr, false);
  837. $model->save();
  838. $cache[$id] = $model->id;
  839. }
  840. return $cache;
  841. } catch (\Exception $e) {
  842. return [
  843. 'code' => 1,
  844. 'msg' => $e->getMessage()
  845. ];
  846. }
  847. }
  848. public function doCopyCutPriceCat($from_store_id, $to_store_id) {
  849. try {
  850. ActivityCutPriceCat::deleteAll(['store_id' => $to_store_id]);
  851. $list = ActivityCutPriceCat::find()
  852. ->where([
  853. 'store_id' => $from_store_id,
  854. 'is_delete' => 0,
  855. ])->orderBy(['id' => SORT_ASC])->all();
  856. $cache = [];
  857. foreach ($list as $value) {
  858. //判断是否为当前商城同步过此商品
  859. $storeSyncExtLog = StoreSyncExtLog::findOne([
  860. 'from_store_id' => $from_store_id,
  861. 'to_store_id' => $to_store_id,
  862. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT,
  863. 'from_id' => $value['id']
  864. ]);
  865. $id = $value['id'];
  866. $attr = $value->attributes;
  867. unset($attr['id']);
  868. $attr['store_id'] = $to_store_id;
  869. $model = ActivityCutPriceCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceCat();
  870. $model->setAttributes($attr, false);
  871. $model->save();
  872. $cache[$id] = $model->id;
  873. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_CAT);
  874. }
  875. return $cache;
  876. } catch(\Exception $e) {
  877. return [
  878. 'code' => 1,
  879. 'msg' => $e->getMessage()
  880. ];
  881. }
  882. }
  883. public function doCopyCutPriceGoods($from_store_id, $to_store_id, $cacheActivity, $cacheCat, $cacheGoods) {
  884. try {
  885. $list = ActivityCutPriceGoods::find()
  886. ->where([
  887. 'store_id' => $from_store_id,
  888. 'is_delete' => 0,
  889. ])->orderBy(['id' => SORT_ASC])->all();
  890. $goods = [];
  891. $cache = [];
  892. foreach ($list as $value) {
  893. $id = $value['id'];
  894. $attr = $value->attributes;
  895. unset($attr['id']);
  896. $attr['store_id'] = $to_store_id;
  897. $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
  898. $cat = Cat::findOne(['id' => $attr['cat_id'], 'is_delete' => 0]);
  899. if (!$cat) {
  900. continue;
  901. }
  902. $attr['cat_id'] = $cacheCat[$attr['cat_id']];
  903. $goods = Goods::findOne(['id' => $attr['goods_id'], 'is_delete' => 0, 'mch_id' => 0]);
  904. if (!$goods) {
  905. continue;
  906. }
  907. $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
  908. $agattr = json_decode($value['attr'], true);
  909. $goodsNew = Goods::findOne($attr['goods_id']);
  910. $gattrNew = json_decode($goodsNew['attr'], true);
  911. $agattrNew = [];
  912. if ($agattr) {
  913. foreach($agattr as $k => $item){
  914. $attrNames = array_column($item['attr_list'], 'attr_name');
  915. // var_dump($attrNames);
  916. if ($gattrNew) {
  917. foreach($gattrNew as $v){
  918. $attrNamesNew = array_column($v['attr_list'], 'attr_name');
  919. // var_dump($attrNamesNew);
  920. if($attrNames === $attrNamesNew){
  921. // var_dump('$attrNames == $attrNamesNew');
  922. $item['attr_list'] = $v['attr_list'];
  923. }
  924. }
  925. }
  926. $agattrNew[] = $item;
  927. }
  928. }
  929. $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
  930. //判断是否为当前商城同步过此商品
  931. $storeSyncExtLog = StoreSyncExtLog::findOne([
  932. 'from_store_id' => $from_store_id,
  933. 'to_store_id' => $to_store_id,
  934. 'type' => StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS,
  935. 'from_id' => $value['id']
  936. ]);
  937. $model = ActivityCutPriceGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new ActivityCutPriceGoods();
  938. $model->setAttributes($attr, false);
  939. $model->save();
  940. $goods[$attr['activity_id']] = $goods[$attr['activity_id']] ?? [];
  941. array_push($goods[$attr['activity_id']], $attr['goods_id']);
  942. $cache[$id] = $model->id;
  943. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE_GOODS);
  944. }
  945. foreach($goods as $aid => $gids){
  946. $ac = ActivityCutPrice::findOne($aid);
  947. $ac->goods_ids = implode(',', $gids);
  948. $ac->save();
  949. }
  950. return $cache;
  951. } catch(\Exception $e) {
  952. return [
  953. 'code' => 1,
  954. 'msg' => $e->getMessage()
  955. ];
  956. }
  957. }
  958. /** 同步砍价 end */
  959. /** 同步DIY start */
  960. // 同步DIY
  961. private function syncDiy($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache)
  962. {
  963. try {
  964. // 首页
  965. // Todo 同步其他页面
  966. return $this->copyHome($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache);
  967. } catch (\Exception $e) {
  968. return [
  969. 'code' => 1,
  970. 'msg' => $e->getMessage()
  971. ];
  972. }
  973. }
  974. // 处理DIY首页
  975. private function copyHome($from_store_id, $to_store_id, $goodsCache, $cacheCat, $activity_cache)
  976. {
  977. try {
  978. // 取消原来的首页信息
  979. // $oldIndex = NewDiyTemplate::findOne([
  980. // 'is_delete' => 0,
  981. // 'is_index' => 1,
  982. // 'store_id' => $to_store_id,
  983. // 'name' => 'DIY',
  984. // ]);
  985. // if ($oldIndex) {
  986. // $oldIndex->is_index = 0;
  987. // $oldIndex->save();
  988. // }
  989. // $fromIndex = NewDiyTemplate::findOne([
  990. // 'is_delete' => 0,
  991. // 'is_index' => 1,
  992. // 'store_id' => $from_store_id,
  993. // 'name' => 'DIY',
  994. // ]);
  995. //20240509要同步其他diy页面
  996. $fromList = NewDiyTemplate::find()->where([
  997. 'is_delete' => 0,
  998. 'store_id' => $from_store_id,
  999. 'name' => ['DIY', 'system'],
  1000. 'is_index' => 0
  1001. ])->all();
  1002. // if (!$fromList) {
  1003. //// return;
  1004. // }
  1005. $diyCache = [];
  1006. if ($fromList) {
  1007. NewDiyTemplate::deleteAll([
  1008. 'is_delete' => 0,
  1009. 'store_id' => $to_store_id,
  1010. 'name' => ['DIY', 'system'],
  1011. 'is_index' => 0
  1012. ]);
  1013. foreach ($fromList as $fromIndex) {
  1014. //判断是否为当前商城同步过此商品
  1015. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1016. 'from_store_id' => $from_store_id,
  1017. 'to_store_id' => $to_store_id,
  1018. 'type' => StoreSyncExtLog::TYPE_DIY,
  1019. 'from_id' => $fromIndex->id
  1020. ]);
  1021. $template = \json_decode($fromIndex->template, true);
  1022. if (!$template) {
  1023. return;
  1024. }
  1025. foreach ($template['templates'] as &$item) {
  1026. $item = $this->diyHandleCommon($item, $goodsCache, $cacheCat, [], $activity_cache);
  1027. }
  1028. $toIndex = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
  1029. $toIndex->store_id = $to_store_id;
  1030. $toIndex->template = json_encode($template, JSON_UNESCAPED_UNICODE);
  1031. $toIndex->addtime = time();
  1032. $toIndex->name = $fromIndex->name;
  1033. $toIndex->is_index = $fromIndex->is_index;
  1034. $toIndex->type = $fromIndex->type;
  1035. $toIndex->is_delete = $fromIndex->is_delete;
  1036. if (!$toIndex->save()) {
  1037. throw new \Exception(json_encode($toIndex->errors, JSON_UNESCAPED_UNICODE));
  1038. }
  1039. $diyCache[$fromIndex->id] = $toIndex->id;
  1040. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $fromIndex->id, $toIndex->id, StoreSyncExtLog::TYPE_DIY);
  1041. }
  1042. }
  1043. $fromIndex_ = NewDiyTemplate::findOne([
  1044. 'is_delete' => 0,
  1045. 'is_index' => 1,
  1046. 'store_id' => $from_store_id,
  1047. 'name' => 'DIY',
  1048. ]);
  1049. $template = \json_decode($fromIndex_->template, true);
  1050. if ($template) {
  1051. NewDiyTemplate::deleteAll([
  1052. 'is_delete' => 0,
  1053. 'is_index' => 1,
  1054. 'store_id' => $to_store_id,
  1055. 'name' => 'DIY',
  1056. ]);
  1057. //判断是否为当前商城同步过此商品
  1058. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1059. 'from_store_id' => $from_store_id,
  1060. 'to_store_id' => $to_store_id,
  1061. 'type' => StoreSyncExtLog::TYPE_DIY,
  1062. 'from_id' => $fromIndex_->id
  1063. ]);
  1064. foreach ($template['templates'] as &$index_item) {
  1065. $index_item = $this->diyHandleCommon($index_item, $goodsCache, $cacheCat, $diyCache, $activity_cache);
  1066. }
  1067. $toIndex = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
  1068. $toIndex->store_id = $to_store_id;
  1069. $toIndex->template = json_encode($template, JSON_UNESCAPED_UNICODE);
  1070. $toIndex->addtime = time();
  1071. $toIndex->name = $fromIndex_->name;
  1072. $toIndex->is_index = $fromIndex_->is_index;
  1073. $toIndex->type = $fromIndex_->type;
  1074. $toIndex->is_delete = $fromIndex->is_delete;
  1075. if (!$toIndex->save()) {
  1076. throw new \Exception(json_encode($toIndex->errors, JSON_UNESCAPED_UNICODE));
  1077. }
  1078. $diyCache[$fromIndex_->id] = $toIndex->id;
  1079. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $fromIndex_->id, $toIndex->id, StoreSyncExtLog::TYPE_DIY);
  1080. }
  1081. $controls = NewDiyTemplate::findOne([
  1082. 'store_id' => $from_store_id,
  1083. 'is_delete' => 0,
  1084. 'name' => 'controls'
  1085. ]);
  1086. $template = \json_decode($controls->template, true);
  1087. if ($template) {
  1088. NewDiyTemplate::deleteAll([
  1089. 'is_delete' => 0,
  1090. 'name' => 'controls',
  1091. 'store_id' => $to_store_id,
  1092. ]);
  1093. $data = $template;
  1094. foreach ($data as &$data_item) {
  1095. if (!empty($data_item['params']['navList'])) {
  1096. foreach ($data_item['params']['navList'] as &$item) {
  1097. $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
  1098. }
  1099. debug_log($data_item['params']['navList'], 'controls.log');
  1100. }
  1101. }
  1102. //判断是否为当前商城同步过此商品
  1103. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1104. 'from_store_id' => $from_store_id,
  1105. 'to_store_id' => $to_store_id,
  1106. 'type' => StoreSyncExtLog::TYPE_DIY,
  1107. 'from_id' => $controls->id
  1108. ]);
  1109. $toControls = NewDiyTemplate::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewDiyTemplate();
  1110. $toControls->store_id = $to_store_id;
  1111. $toControls->template = json_encode($data, JSON_UNESCAPED_UNICODE);
  1112. $toControls->addtime = time();
  1113. $toControls->name = $controls->name;
  1114. $toControls->is_index = $controls->is_index;
  1115. $toControls->type = $controls->type;
  1116. $toControls->is_delete = $fromIndex->is_delete;
  1117. if (!$toControls->save()) {
  1118. throw new \Exception(json_encode($toControls->errors, JSON_UNESCAPED_UNICODE));
  1119. }
  1120. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $controls->id, $toControls->id, StoreSyncExtLog::TYPE_DIY);
  1121. }
  1122. return [
  1123. 'code' => 0,
  1124. 'msg' => '执行成功',
  1125. 'data' => $diyCache
  1126. ];
  1127. } catch (\Exception $e) {
  1128. return [
  1129. 'code' => 1,
  1130. 'msg' => $e->getMessage() . $e->getLine()
  1131. ];
  1132. }
  1133. }
  1134. //处理装修公用部分
  1135. public function diyHandleCommon($template, $goodsCache, $cacheCat, $diyCache = [], $activity_cache = [])
  1136. {
  1137. // 头部导航
  1138. if ($template['type'] == 'fastNav') {
  1139. $template = $this->replaceFastNav($template, $goodsCache, $cacheCat);
  1140. }
  1141. // 图片轮播
  1142. if ($template['type'] == 'banner') {
  1143. debug_log('1处理装修公用部分','store_sync.log');
  1144. $template = $this->replaceBanner($template, $goodsCache, $cacheCat, $diyCache);
  1145. }
  1146. // 魔方导航
  1147. if ($template['type'] == 'cubeNav') {
  1148. debug_log('2处理装修公用部分','store_sync.log');
  1149. $template = $this->replaceCubeNav($template, $goodsCache, $cacheCat, $diyCache);
  1150. }
  1151. // 魔方图片
  1152. if ($template['type'] == 'container') {
  1153. debug_log('3处理装修公用部分','store_sync.log');
  1154. $template = $this->replaceContainer($template, $goodsCache, $cacheCat, $diyCache);
  1155. }
  1156. // 产品展示
  1157. if ($template['type'] == 'productShow') {
  1158. debug_log('4处理装修公用部分','store_sync.log');
  1159. $template = $this->replaceProductShow($template, $goodsCache, $cacheCat, $diyCache);
  1160. }
  1161. //产品分类
  1162. if ($template['type'] == 'productClassify') {
  1163. debug_log('5处理装修公用部分','store_sync.log');
  1164. $template = $this->replaceProductClassify($template, $goodsCache, $cacheCat, $diyCache);
  1165. }
  1166. // 倒计时
  1167. if ($template['type'] == 'marketCountDown') {
  1168. $template = $this->replaceMarketCountDown($template, $goodsCache, $cacheCat, $diyCache);
  1169. }
  1170. //图片
  1171. if ($template['type'] == 'images') {
  1172. $template = $this->replaceImages($template, $goodsCache, $cacheCat, $diyCache);
  1173. }
  1174. // 首页推荐
  1175. if ($template['type'] == 'indexRecommend') {
  1176. // Todo 该模块数据有误,后期解决
  1177. $template = $this->replaceIndexRecommend($template, $goodsCache, $cacheCat, $diyCache);
  1178. debug_log($template, '20250328.log');
  1179. }
  1180. //秒杀
  1181. if ($template['type'] == 'productSeckill') {
  1182. $template = $this->replaceProductSeckill($template, $activity_cache['seckillCacheActivity'], $activity_cache['seckillCache'], $diyCache);
  1183. }
  1184. //上门服务
  1185. if ($template['type'] == 'serviceHome') {
  1186. $template = $this->replaceServiceHome($template, $activity_cache['worker_goods_cat_result'], $activity_cache['worker_goods_result'], $activity_cache['worker_cat_result'], $goodsCache, $diyCache);
  1187. }
  1188. //拼团
  1189. if ($template['type'] == 'ptProductShow') {
  1190. $template = $this->replacePtProductShow($template, $activity_cache['ptCacheActivity'], $activity_cache['ptCacheCat'], $activity_cache['ptCache'], $goodsCache, $diyCache);
  1191. }
  1192. //预约
  1193. if ($template['type'] == 'bookingHome') {
  1194. $template = $this->replaceBookingHome($template, $goodsCache, $activity_cache['booking_goods_cat_cache'], $diyCache);
  1195. }
  1196. return $template;
  1197. }
  1198. // 替换头部导航
  1199. private function replaceFastNav($data, $goodsCache, $cacheCat)
  1200. {
  1201. if (!empty($data['params']['list'])) {
  1202. foreach ($data['params']['list'] as &$item) {
  1203. $item = $this->replaceLink($item, $goodsCache, $cacheCat);
  1204. }
  1205. }
  1206. return $data;
  1207. }
  1208. // 替换图片轮播
  1209. private function replaceBanner($data, $goodsCache, $cacheCat, $diyCache)
  1210. {
  1211. if (!empty($data['params']['bannerList'])) {
  1212. foreach ($data['params']['bannerList'] as &$item) {
  1213. $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
  1214. }
  1215. }
  1216. return $data;
  1217. }
  1218. // 替换魔方导航
  1219. private function replaceCubeNav($data, $goodsCache, $cacheCat, $diyCache)
  1220. {
  1221. if (!empty($data['params']['list'])) {
  1222. foreach ($data['params']['list'] as &$item) {
  1223. $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
  1224. }
  1225. }
  1226. return $data;
  1227. }
  1228. // 替换魔方图片
  1229. private function replaceContainer($data, $goodsCache, $cacheCat, $diyCache)
  1230. {
  1231. debug_log('——替换魔方图片','store_sync.log');
  1232. if (!empty($data['params']['cubeCoverList'])) {
  1233. debug_log('替换魔方图片——','store_sync.log');
  1234. foreach ($data['params']['cubeCoverList'] as &$item) {
  1235. $item = $this->replaceLink($item, $goodsCache, $cacheCat, $diyCache);
  1236. }
  1237. }
  1238. return $data;
  1239. }
  1240. // 替换产品展示
  1241. private function replaceProductShow($data, $goodsCache, $cacheCat, $diyCache)
  1242. {
  1243. // 替换分类id
  1244. if (isset($data['params']['classifyData']) && isset($cacheCat[$data['params']['classifyData']])) {
  1245. $data['params']['classifyData'] = $cacheCat[$data['params']['classifyData']];
  1246. }
  1247. // 替换分类商品id
  1248. if (isset($data['params']['classifyArr'])) {
  1249. foreach ($data['params']['classifyArr'] as &$item) {
  1250. if (isset($goodsCache[$item['id']])) {
  1251. $item['id'] = $goodsCache[$item['id']];
  1252. }
  1253. }
  1254. }
  1255. // 替换商品id
  1256. if (isset($data['params']['selectData'])) {
  1257. foreach ($data['params']['selectData'] as &$select_item) {
  1258. if (isset($goodsCache[$select_item['id']])) {
  1259. $select_item['id'] = $goodsCache[$select_item['id']];
  1260. }
  1261. }
  1262. }
  1263. $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
  1264. return $data;
  1265. }
  1266. // 替换产品分类
  1267. private function replaceProductClassify($data, $goodsCache, $cacheCat, $diyCache)
  1268. {
  1269. if (!empty($data['params']['classifyList'])) {
  1270. $data['params']['classifyList'] = (array)$data['params']['classifyList'];
  1271. foreach ($data['params']['classifyList'] as &$item) {
  1272. // 替换分类
  1273. if (!empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
  1274. $item['id'] = $cacheCat[$item['classifyId']];
  1275. $item['classifyId'] = $cacheCat[$item['classifyId']];
  1276. }
  1277. if (!empty($item['data'])) {
  1278. // 替换商品id
  1279. foreach ($item['data'] as &$value) {
  1280. if (isset($goodsCache[$value['id']])) {
  1281. $value['id'] = $goodsCache[$value['id']];
  1282. }
  1283. }
  1284. }
  1285. }
  1286. }
  1287. return $data;
  1288. }
  1289. //秒杀
  1290. private function replaceProductSeckill($template, $seckillCacheActivity, $seckillCache, $diyCache) {
  1291. if (!empty($template['params'])) {
  1292. $template['params']['activeId'] = (string)$seckillCacheActivity[$template['params']['activeId']];
  1293. }
  1294. return $template;
  1295. }
  1296. //上门服务
  1297. private function replaceServiceHome($template, $worker_goods_cat_result, $worker_goods_result, $worker_cat_result, $goodsCache, $diyCache) {
  1298. if (!empty($template['params'])) {
  1299. $template['params']['classifyData'] = $worker_goods_cat_result[$template['params']['classifyData']];
  1300. if (!empty($template['params']['classifyArr'])) {
  1301. foreach ($template['params']['classifyArr'] as &$worker_goods_item) {
  1302. $worker_goods_item['id'] = $goodsCache[$worker_goods_item['id']];
  1303. }
  1304. }
  1305. }
  1306. return $template;
  1307. }
  1308. //拼团
  1309. private function replacePtProductShow($template, $ptCacheActivity, $ptCacheCat, $ptCache, $goodsCache, $diyCache) {
  1310. if (!empty($template['params'])) {
  1311. $template['params']['classifyData'] = $ptCacheCat[$template['params']['classifyData']];
  1312. if (!empty($template['params']['classifyArr'])) {
  1313. foreach ($template['params']['classifyArr'] as &$pt_classify_goods_item) {
  1314. $pt_classify_goods_item['id'] = $goodsCache[$pt_classify_goods_item['id']];
  1315. }
  1316. }
  1317. if (!empty($template['params']['selectData'])) {
  1318. foreach ($template['params']['selectData'] as &$pt_goods_item) {
  1319. $pt_goods_item['id'] = $goodsCache[$pt_goods_item['id']];
  1320. }
  1321. }
  1322. }
  1323. return $template;
  1324. }
  1325. //砍价
  1326. private function replaceCutPrice($data, $cutPriceCacheActivity, $cutPriceCacheCat, $cutPriceCache, $goodsCache, $diyCache) {
  1327. if (!empty($data['params']['classifyList'])) {
  1328. foreach ((array)$data['params']['classifyList'] as &$item) {
  1329. // 替换分类
  1330. if (!empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
  1331. $item['id'] = $cacheCat[$item['classifyId']];
  1332. $item['classifyId'] = $cacheCat[$item['classifyId']];
  1333. }
  1334. if (!empty($item['data'])) {
  1335. // 替换商品id
  1336. foreach ($item['data'] as &$value) {
  1337. if (isset($goodsCache[$value['id']])) {
  1338. $value['id'] = $goodsCache[$value['id']];
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. return $data;
  1345. }
  1346. //装修预约商品
  1347. private function replaceBookingHome($template, $goodsCache, $booking_goods_cat_cache) {
  1348. try {
  1349. if (!empty($template['params'])) {
  1350. $template['params']['classifyData'] = $booking_goods_cat_cache[$template['params']['classifyData']];
  1351. if (!empty($template['params']['selectData'])) {
  1352. foreach ($template['params']['selectData'] as &$booking_goods_item) {
  1353. $booking_goods_item['id'] = $goodsCache[$booking_goods_item['id']];
  1354. }
  1355. }
  1356. }
  1357. return $template;
  1358. } catch (\Exception $e) {
  1359. return $template;
  1360. }
  1361. }
  1362. // 替换倒计时
  1363. private function replaceMarketCountDown($data, $goodsCache, $cacheCat, $diyCache)
  1364. {
  1365. $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
  1366. return $data;
  1367. }
  1368. // 替换图片链接
  1369. private function replaceImages($data, $goodsCache, $cacheCat, $diyCache)
  1370. {
  1371. if (isset($data['params']['current'])) {
  1372. $data['params']['current'] = $this->replaceLink($data['params']['current'], $goodsCache, $cacheCat, $diyCache);
  1373. } else {
  1374. if (isset($data['params'])) {
  1375. $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat, $diyCache);
  1376. }
  1377. }
  1378. return $data;
  1379. }
  1380. //替换首页推荐
  1381. private function replaceIndexRecommend($template, $goodsCache, $cacheCat, $diyCache) {
  1382. try {
  1383. foreach ($template['params']['list'] as &$item) {
  1384. foreach ($item['pics']['link'] as &$link_item) {
  1385. if (!isset($link_item['link'])) {
  1386. continue;
  1387. }
  1388. // 处理分类id
  1389. if (
  1390. strpos($link_item['link'], '/pages/cat/cat?cat_id=') !== false &&
  1391. $link_item['params'][0]['value'] != '' &&
  1392. isset($cacheCat[$link_item['params'][0]['value']])
  1393. ) {
  1394. $link_item['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$link_item['params'][0]['value']];
  1395. $link_item['params'][0]['value'] = $cacheCat[$cacheCat[$link_item['params'][0]['value']]];
  1396. }
  1397. // 处理商品列表
  1398. if (
  1399. strpos($link_item['link'], '/other/list/list?cat_id=') !== false &&
  1400. $link_item['params'][0]['value'] != '' &&
  1401. isset($cacheCat[$link_item['params'][0]['value']])
  1402. ) {
  1403. $link_item['link'] = '/other/list/list?cat_id=' . $cacheCat[$link_item['params'][0]['value']];
  1404. $link_item['params'][0]['value'] = $cacheCat[$link_item['params'][0]['value']];
  1405. }
  1406. // 处理商品详情
  1407. if (
  1408. strpos($link_item['link'], '/goods/goods/goods?id=') !== false &&
  1409. $link_item['params'][0]['value'] != '' &&
  1410. isset($goodsCache[$link_item['params'][0]['value']])
  1411. ) {
  1412. $link_item['link'] = '/goods/goods/goods?id=' . $goodsCache[$link_item['params'][0]['value']];
  1413. $link_item['params'][0]['value'] = $goodsCache[$link_item['params'][0]['value']];
  1414. }
  1415. if (
  1416. strpos($link_item['link'], '/pages/diy/diy?id=') !== false &&
  1417. isset($diyCache[$link_item['id']])
  1418. ) {
  1419. $link_item['link'] = '/pages/diy/diy?id=' . $diyCache[$link_item['id']];
  1420. $link_item['id'] = $diyCache[$link_item['id']];
  1421. }
  1422. }
  1423. }
  1424. return $template;
  1425. } catch (\Exception $e) {
  1426. return $template;
  1427. }
  1428. }
  1429. private function replaceLink($item, $goodsCache, $cacheCat, $diyCache = [])
  1430. {
  1431. debug_log('——开始替换','store_sync.log');
  1432. if (!isset($item['link']['link'])) {
  1433. return $item;
  1434. }
  1435. debug_log('开始替换_','store_sync.log');
  1436. // 处理分类id
  1437. if (
  1438. strpos($item['link']['link'], '/pages/cat/cat?cat_id=') !== false &&
  1439. $item['link']['params'][0]['value'] != '' &&
  1440. isset($cacheCat[$item['link']['params'][0]['value']])
  1441. ) {
  1442. $item['link']['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
  1443. $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
  1444. }
  1445. // 处理商品列表
  1446. if (strpos($item['link']['link'], '/other/list/list?cat_id=') !== false) {
  1447. debug_log('处理分类替换', 'store_sync.log');
  1448. debug_log(['cacheCat' => $cacheCat], 'store_sync.log');
  1449. debug_log(['item' => $item], 'store_sync.log');
  1450. }
  1451. if (
  1452. strpos($item['link']['link'], '/other/list/list?cat_id=') !== false &&
  1453. $item['link']['params'][0]['value'] != '' &&
  1454. isset($cacheCat[$item['link']['params'][0]['value']])
  1455. ) {
  1456. $item['link']['link'] = '/other/list/list?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
  1457. $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
  1458. }
  1459. // 处理商品详情
  1460. if (
  1461. strpos($item['link']['link'], '/goods/goods/goods?id=') !== false &&
  1462. $item['link']['params'][0]['value'] != '' &&
  1463. isset($goodsCache[$item['link']['params'][0]['value']])
  1464. ) {
  1465. $item['link']['link'] = '/goods/goods/goods?id=' . $goodsCache[$item['link']['params'][0]['value']];
  1466. $item['link']['params'][0]['value'] = $goodsCache[$item['link']['params'][0]['value']];
  1467. }
  1468. debug_log('开始替换','store_sync.log');
  1469. if (
  1470. strpos($item['link']['link'], '/pages/diy/diy?id=') !== false &&
  1471. isset($diyCache[$item['link']['id']])
  1472. ) {
  1473. $item['link']['link'] = '/pages/diy/diy?id=' . $diyCache[$item['link']['id']];
  1474. $item['link']['id'] = $diyCache[$item['link']['id']];
  1475. }
  1476. debug_log($item,'store_sync.log');
  1477. debug_log('结束替换','store_sync.log');
  1478. return $item;
  1479. }
  1480. /** 同步DIY end */
  1481. /** 同步秒杀 start */
  1482. private function seckill($from_store_id, $to_store_id, $goodsCache) {
  1483. //同步活动
  1484. $cacheActivity = $this->doCopySeckill($from_store_id, $to_store_id);
  1485. //同步产品
  1486. $cache = $this->doCopySeckillGoods($from_store_id, $to_store_id, $cacheActivity, $goodsCache);
  1487. return [$cacheActivity, $cache];
  1488. }
  1489. public function doCopySeckill($from_store_id, $to_store_id) {
  1490. try {
  1491. SeckillActivity::deleteAll(['store_id' => $to_store_id]);
  1492. $list = SeckillActivity::find()
  1493. ->where([
  1494. 'store_id' => $from_store_id,
  1495. 'is_delete' => 0,
  1496. ])->orderBy(['id' => SORT_ASC])->all();
  1497. $cache = [];
  1498. foreach ($list as $value) {
  1499. $id = $value['id'];
  1500. $attr = $value->attributes;
  1501. //判断是否为当前商城同步过此商品
  1502. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1503. 'from_store_id' => $from_store_id,
  1504. 'to_store_id' => $to_store_id,
  1505. 'type' => StoreSyncExtLog::TYPE_SECKILL,
  1506. 'from_id' => $id
  1507. ]);
  1508. unset($attr['id']);
  1509. $attr['store_id'] = $to_store_id;
  1510. $model = SeckillActivity::findOne($storeSyncExtLog->to_id ?? 0) ?: new SeckillActivity();
  1511. $model->setAttributes($attr, false);
  1512. $model->save();
  1513. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_SECKILL);
  1514. $cache[$id] = $model->id;
  1515. }
  1516. return $cache;
  1517. } catch(\Exception $e) {
  1518. return [
  1519. 'code' => 1,
  1520. 'msg' => $e->getMessage()
  1521. ];
  1522. }
  1523. }
  1524. public function doCopySeckillGoods($from_store_id, $to_store_id, $cacheActivity, $cacheGoods) {
  1525. try {
  1526. $old_activity_id = array_keys($cacheActivity);
  1527. $list = SeckillActivityGoods::find()
  1528. ->where([
  1529. 'store_id' => $from_store_id,
  1530. 'is_delete' => 0,
  1531. 'activity_id' => $old_activity_id
  1532. ])->orderBy(['id' => SORT_ASC])->all();
  1533. $cache = [];
  1534. SeckillActivityGoods::deleteAll(['store_id' => $to_store_id]);
  1535. foreach ($list as $value) {
  1536. $id = $value['id'];
  1537. $attr = $value->attributes;
  1538. if (!$cacheGoods[$attr['goods_id']]) {
  1539. continue;
  1540. }
  1541. unset($attr['id']);
  1542. $attr['store_id'] = $to_store_id;
  1543. $attr['activity_id'] = $cacheActivity[$attr['activity_id']];
  1544. $attr['goods_id'] = $cacheGoods[$attr['goods_id']];
  1545. $attr['sale_num'] = 0;
  1546. $agattr = json_decode($value['attr'], true);
  1547. $goodsNew = Goods::findOne($attr['goods_id']);
  1548. $gattrNew = json_decode($goodsNew['attr'], true);
  1549. $agattrNew = [];
  1550. foreach($agattr as $k => $item){
  1551. $attrNames = array_column($item['attr_list'], 'attr_name');
  1552. // var_dump($attrNames);
  1553. if ($gattrNew) {
  1554. foreach($gattrNew as $v){
  1555. $attrNamesNew = array_column($v['attr_list'], 'attr_name');
  1556. // var_dump($attrNamesNew);
  1557. if($attrNames === $attrNamesNew){
  1558. // var_dump('$attrNames == $attrNamesNew');
  1559. $item['attr_list'] = $v['attr_list'];
  1560. }
  1561. }
  1562. }
  1563. $agattrNew[] = $item;
  1564. }
  1565. $attr['attr'] = json_encode($agattrNew, JSON_UNESCAPED_UNICODE);
  1566. //判断是否为当前商城同步过此商品
  1567. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1568. 'from_store_id' => $from_store_id,
  1569. 'to_store_id' => $to_store_id,
  1570. 'type' => StoreSyncExtLog::TYPE_SECKILL_GOODS,
  1571. 'from_id' => $id
  1572. ]);
  1573. $model = SeckillActivityGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new SeckillActivityGoods();
  1574. $model->setAttributes($attr, false);
  1575. $model->save();
  1576. $cache[$id] = $model->id;
  1577. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $model->id, StoreSyncExtLog::TYPE_SECKILL_GOODS);
  1578. }
  1579. return $cache;
  1580. } catch(\Exception $e) {
  1581. return [
  1582. 'code' => 1,
  1583. 'msg' => $e->getMessage()
  1584. ];
  1585. }
  1586. }
  1587. /** 同步秒杀 end */
  1588. /** 同步分销start **/
  1589. private function shareConfig($from_store_id, $to_store_id, $goodsCache) {
  1590. //同步配置 基础配置 佣金配置 推广海报
  1591. $result = $this->shareBasicConfig($from_store_id, $to_store_id);
  1592. //同步等级
  1593. $levelResult = $this->shareLevel($from_store_id, $to_store_id);
  1594. return [
  1595. $result, $levelResult
  1596. ];
  1597. }
  1598. //同步配置 基础配置 佣金配置 推广海报
  1599. private function shareBasicConfig($from_store_id, $to_store_id) {
  1600. try {
  1601. $share_basic_setting = Option::get('share_basic_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
  1602. try {
  1603. $share_basic_setting = json_decode($share_basic_setting, true);
  1604. if (isset($share_basic_setting['share_goods_id']['value'])) {
  1605. $ids = \explode(',', $share_basic_setting['share_goods_id']['value']);
  1606. $arr = [];
  1607. foreach ($ids as $id) {
  1608. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1609. 'from_store_id' => $from_store_id,
  1610. 'to_store_id' => $to_store_id,
  1611. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  1612. 'from_id' => $id,
  1613. ]);
  1614. if ($storeSyncExtLog) {
  1615. $arr[] = $storeSyncExtLog->to_id;
  1616. }
  1617. }
  1618. $share_basic_setting['share_goods_id']['value'] = \implode(',', $arr);
  1619. }
  1620. $share_basic_setting = json_encode($share_basic_setting, JSON_UNESCAPED_UNICODE);
  1621. Option::set('share_basic_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
  1622. } catch (Exception $e) {
  1623. return [
  1624. 'code' => 1,
  1625. 'msg' => $e->getMessage()
  1626. ];
  1627. }
  1628. $share_basic_setting = Option::get('share_money_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
  1629. try {
  1630. Option::set('share_money_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
  1631. } catch (Exception $e) {
  1632. return [
  1633. 'code' => 1,
  1634. 'msg' => $e->getMessage()
  1635. ];
  1636. }
  1637. $qrcode = Qrcode::findOne(['store_id' => $from_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
  1638. $qrcode_ = Qrcode::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
  1639. if (!$qrcode_) {
  1640. $qrcode_ = new Qrcode();
  1641. }
  1642. unset($qrcode['id']);
  1643. $qrcode_->attributes = $qrcode->attributes;
  1644. $qrcode_->store_id = $to_store_id;
  1645. $qrcode_->created_at = time();
  1646. $qrcode_->updated_at = time();
  1647. if (!$qrcode_->save()) {
  1648. return [
  1649. 'code' => 1,
  1650. 'msg' => json_encode($qrcode_->errors, JSON_UNESCAPED_UNICODE)
  1651. ];
  1652. }
  1653. return [
  1654. 'code' => 0,
  1655. 'msg' => '操作成功'
  1656. ];
  1657. } catch(\Exception $e) {
  1658. return [
  1659. 'code' => 1,
  1660. 'msg' => $e->getMessage()
  1661. ];
  1662. }
  1663. }
  1664. //同步等级
  1665. private function shareLevel($from_store_id, $to_store_id) {
  1666. try {
  1667. $shareLevelList = ShareLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
  1668. foreach ($shareLevelList as $item) {
  1669. $share_level = ShareLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
  1670. if (!$share_level) {
  1671. $share_level = new ShareLevel();
  1672. }
  1673. $id = $item['id'];
  1674. unset($item['id']);
  1675. $share_level->attributes = $item;
  1676. $share_level->store_id = $to_store_id;
  1677. if (!$share_level->save()) {
  1678. return [
  1679. 'code' => 1,
  1680. 'msg' => json_encode($share_level->errors, JSON_UNESCAPED_UNICODE)
  1681. ];
  1682. };
  1683. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $share_level->id, StoreSyncExtLog::TYPE_SHARE_CONFIG);
  1684. }
  1685. return [
  1686. 'code' => 0,
  1687. 'msg' => '同步成功'
  1688. ];
  1689. } catch (\Exception $e) {
  1690. return [
  1691. 'code' => 1,
  1692. 'msg' => $e->getMessage()
  1693. ];
  1694. }
  1695. }
  1696. /** 同步分销end **/
  1697. /** 同步股东start **/
  1698. private function shareHolderConfig($from_store_id, $to_store_id, $goodsCache) {
  1699. //页面设置 申请设置
  1700. $basic_result = $this->shareHolderBasicConfig($from_store_id, $to_store_id);
  1701. //等级同步
  1702. $level_result = $this->shareHolderLevelConfig($from_store_id, $to_store_id, $goodsCache);
  1703. return [$basic_result, $level_result];
  1704. }
  1705. //页面设置 申请设置
  1706. private function shareHolderBasicConfig($from_store_id, $to_store_id) {
  1707. try {
  1708. Option::set('cycle', Option::get('cycle', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1709. Option::set('is_open_share', Option::get('is_open_share', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1710. Option::set('is_open_range', Option::get('is_open_range', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1711. Option::set('team_num', Option::get('team_num', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1712. Option::set('qrcode', Option::get('qrcode', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1713. Option::set('range_name', Option::get('range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1714. Option::set('holder_name', Option::get('holder_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1715. Option::set('area_name', Option::get('area_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1716. Option::set('old_range_name', Option::get('old_range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1717. Option::set('direct_range_name', Option::get('direct_range_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1718. Option::set('old_add_holder_name', Option::get('old_add_holder_name', $from_store_id, 'bonus_pool', '')['value'], $to_store_id, 'bonus_pool');
  1719. Option::set('is_open_area', Option::get('is_open_area', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1720. Option::set('shareHolderProfit', Option::get('shareHolderProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1721. Option::set('shareHolderPriceMin', Option::get('shareHolderPriceMin', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1722. Option::set('shareHolderCashProfit', Option::get('shareHolderCashProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1723. Option::set('shareHolderLevel', Option::get('shareHolderLevel', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1724. Option::set('shareHolderNumber', Option::get('shareHolderNumber', $from_store_id, 'bonus_pool', '0,0')['value'], $to_store_id, 'bonus_pool');
  1725. Option::set('shareHolderHasCheck', Option::get('shareHolderHasCheck', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1726. Option::set('pay_wechat', Option::get('pay_wechat', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1727. Option::set('pay_alipay', Option::get('pay_alipay', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1728. Option::set('bank', Option::get('bank', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1729. Option::set('cash_max_day', Option::get('cash_max_day', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1730. Option::set('cash_max_single_day', Option::get('cash_max_single_day', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1731. Option::set('min_money', Option::get('min_money', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1732. Option::set('auto_money', Option::get('auto_money', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1733. Option::set('dividends_condition', Option::get('dividends_condition', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1734. Option::set('shareHolderProfitSwitch', Option::get('shareHolderProfitSwitch', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1735. Option::set('shareHolderHighProfit', Option::get('shareHolderHighProfit', $from_store_id, 'bonus_pool', 0)['value'], $to_store_id, 'bonus_pool');
  1736. //申请设置
  1737. Option::set('holder_agreement', Option::get('holder_agreement', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
  1738. Option::set('holder_banner', Option::get('holder_banner', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
  1739. Option::set('holder_audit', Option::get('holder_audit', $from_store_id, 'store', '')['value'], $to_store_id, 'store');
  1740. return [
  1741. 'code' => 0,
  1742. 'msg' => '同步成功'
  1743. ];
  1744. } catch (\Exception $e) {
  1745. return [
  1746. 'code' => 1,
  1747. 'msg' => $e->getMessage()
  1748. ];
  1749. }
  1750. }
  1751. //等级同步
  1752. private function shareHolderLevelConfig($from_store_id, $to_store_id, $goodsCache) {
  1753. try {
  1754. $shareHolderLevelList = ShareHolderLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->orderBy(['level' => 'ASC'])->asArray()->all();
  1755. $level_cache = [];
  1756. foreach ($shareHolderLevelList as $item) {
  1757. $share_holder_level = ShareHolderLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
  1758. $share_holder_level_id = $item['id'];
  1759. if (!$share_holder_level) {
  1760. $share_holder_level = new ShareHolderLevel();
  1761. }
  1762. unset($item['id']);
  1763. $share_holder_level->attributes = $item;
  1764. $share_holder_level->store_id = $to_store_id;
  1765. if ($share_holder_level->save()) {
  1766. $level_cache[$item['id']] = $share_holder_level->id;
  1767. }
  1768. $condition = json_decode($share_holder_level->condition, true);
  1769. if ($condition) {
  1770. foreach ($condition as $condition_index => $condition_item) {
  1771. //商品升级
  1772. if ($condition_index === 'goods') {
  1773. if ($condition_item['value']['id']) {
  1774. $id = [];
  1775. if ($condition_item['value']['id']) {
  1776. if (is_array($condition_item['value']['id'])) {
  1777. foreach ($condition_item['value']['id'] as $goods_id_item) {
  1778. $id[] = $goodsCache[$goods_id_item];
  1779. }
  1780. } else {
  1781. $id = [$condition_item['value']['id']];
  1782. }
  1783. }
  1784. $condition[$condition_index]['value']['id'] = $id;
  1785. }
  1786. }
  1787. if ($condition_index === 'shareholder') {
  1788. if ($condition_item['from_level_id']) {
  1789. $condition[$condition_index]['from_level_id'] = $level_cache[$condition_item['from_level_id']];
  1790. }
  1791. if ($condition_item['to_level_id']) {
  1792. $condition[$condition_index]['to_level_id'] = $level_cache[$condition_item['to_level_id']];
  1793. }
  1794. }
  1795. }
  1796. }
  1797. $share_holder_level->condition = json_encode($condition, JSON_UNESCAPED_UNICODE);
  1798. $share_holder_level->save();
  1799. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $share_holder_level_id, $share_holder_level->id, StoreSyncExtLog::TYPE_SHARE_HOLDER_CONFIG);
  1800. }
  1801. return [
  1802. 'code' => 0,
  1803. 'msg' => '同步成功',
  1804. 'data' => [
  1805. 'level_cache' => $level_cache
  1806. ]
  1807. ];
  1808. } catch (\Exception $e) {
  1809. return [
  1810. 'code' => 1,
  1811. 'msg' => $e->getMessage()
  1812. ];
  1813. }
  1814. }
  1815. /** 同步股东end **/
  1816. /** 同步短视频start **/
  1817. private function videoGoods($fromStoreId, $toId, $goodsCache) {
  1818. //同步商品
  1819. $goods_result = $this->videoGoodsCopy($fromStoreId, $toId, $goodsCache);
  1820. //同步分类
  1821. $goods_cat_result = $this->videoGoodsCatCopy($fromStoreId, $toId);
  1822. $video_cat_cache = [];
  1823. if ($goods_cat_result['code'] === 0) {
  1824. $video_cat_cache = $goods_cat_result['data']['video_cat_cache'];
  1825. }
  1826. //同步设置
  1827. $goods_setting_result = $this->videoGoodsConfigCopy($fromStoreId, $toId);
  1828. //同步视频
  1829. $goods_list_result = $this->videoCopy($fromStoreId, $toId, $goodsCache, $video_cat_cache);
  1830. return [$goods_result, $goods_cat_result, $goods_setting_result, $goods_list_result];
  1831. }
  1832. //同步商品
  1833. private function videoGoodsCopy($fromStoreId, $toId, $goodsCache) {
  1834. try {
  1835. $video_goods_list = VideoGoods::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  1836. VideoGoods::deleteAll(['store_id' => $toId]);
  1837. foreach ($video_goods_list as $video_goods_item) {
  1838. if (!$goodsCache[$video_goods_item['goods_id']]) {
  1839. continue;
  1840. }
  1841. $video_goods_id = $video_goods_item['id'];
  1842. unset($video_goods_item['id']);
  1843. //判断是否为当前商城同步过此商品
  1844. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1845. 'from_store_id' => $fromStoreId,
  1846. 'to_store_id' => $toId,
  1847. 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS,
  1848. 'from_id' => $video_goods_id
  1849. ]);
  1850. $video_goods = VideoGoods::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoods();
  1851. $video_goods->store_id = $toId;
  1852. $video_goods->status = $video_goods_item['status'];
  1853. $video_goods->goods_id = $goodsCache[$video_goods_item['goods_id']];
  1854. $video_goods->created_at = time();
  1855. $video_goods->updated_at = time();
  1856. $video_goods->save();
  1857. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $video_goods_id, $video_goods->id, StoreSyncExtLog::TYPE_VIDEO_GOODS);
  1858. }
  1859. return [
  1860. 'code' => 0,
  1861. 'msg' => '同步成功'
  1862. ];
  1863. } catch (\Exception $e) {
  1864. return [
  1865. 'code' => 1,
  1866. 'msg' => $e->getMessage()
  1867. ];
  1868. }
  1869. }
  1870. //同步短视频分类
  1871. private function videoGoodsCatCopy($fromStoreId, $toId) {
  1872. try {
  1873. $video_cat_cache = [];
  1874. $video_goods_list = VideoGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  1875. VideoGoodsCat::deleteAll(['store_id' => $toId]);
  1876. foreach ($video_goods_list as $video_goods_item) {
  1877. $id = $video_goods_item['id'];
  1878. unset($video_goods_item['id']);
  1879. //判断是否为当前商城同步过此商品
  1880. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1881. 'from_store_id' => $fromStoreId,
  1882. 'to_store_id' => $toId,
  1883. 'type' => StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT,
  1884. 'from_id' => $id
  1885. ]);
  1886. $video_goods_cat = VideoGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new VideoGoodsCat();
  1887. $video_goods_cat->attributes = $video_goods_item;
  1888. $video_goods_cat->store_id = $toId;
  1889. $video_goods_cat->created_at = time();
  1890. $video_goods_cat->updated_at = time();
  1891. if (!$video_goods_cat->save()) {
  1892. throw new \Exception(json_encode($video_goods_cat, JSON_UNESCAPED_UNICODE));
  1893. }
  1894. $video_cat_cache[$id] = $video_goods_cat->id;
  1895. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $video_goods_cat->id, StoreSyncExtLog::TYPE_VIDEO_GOODS_CAT);
  1896. }
  1897. return [
  1898. 'code' => 0,
  1899. 'msg' => '同步成功',
  1900. 'data' => [
  1901. 'video_cat_cache' => $video_cat_cache
  1902. ]
  1903. ];
  1904. } catch (\Exception $e) {
  1905. return [
  1906. 'code' => 1,
  1907. 'msg' => $e->getMessage()
  1908. ];
  1909. }
  1910. }
  1911. //同步短视频分类
  1912. private function videoGoodsConfigCopy($fromStoreId, $toId) {
  1913. try {
  1914. $video_goods_setting_ = VideoGoodsSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
  1915. unset($video_goods_setting_['id']);
  1916. $video_goods_setting = VideoGoodsSetting::findOne(['store_id' => $toId]);
  1917. if (!$video_goods_setting) {
  1918. $video_goods_setting = new VideoGoodsSetting();
  1919. }
  1920. $video_goods_setting->attributes = $video_goods_setting_;
  1921. $video_goods_setting->store_id = $toId;
  1922. $video_goods_setting->created_at = time();
  1923. $video_goods_setting->updated_at = time();
  1924. if (!$video_goods_setting->save()) {
  1925. throw new \Exception(json_encode($video_goods_setting->errors, JSON_UNESCAPED_UNICODE));
  1926. }
  1927. $video_analyze_345_ = Option::get('video_analyze_345', $fromStoreId, 'saas')['value'];
  1928. Option::set('video_analyze_345', $video_analyze_345_, $toId, 'saas');
  1929. return [
  1930. 'code' => 0,
  1931. 'msg' => '同步成功'
  1932. ];
  1933. } catch (\Exception $e) {
  1934. return [
  1935. 'code' => 1,
  1936. 'msg' => $e->getMessage()
  1937. ];
  1938. }
  1939. }
  1940. private function videoCopy($fromStoreId, $toId, $goodsCache, $video_cat_cache) {
  1941. try {
  1942. $video_goods_list = VideoGoodsList::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  1943. VideoGoodsList::deleteAll(['store_id' => $toId]);
  1944. foreach ($video_goods_list as $video_goods_item) {
  1945. $integral_cat = new VideoGoodsList();
  1946. unset($video_goods_item['id']);
  1947. $integral_cat->attributes = $video_goods_item;
  1948. $goodsList = json_decode($video_goods_item['goods_id'], true);
  1949. $ids = [];
  1950. if ($goodsList) {
  1951. foreach ($goodsList as $goods_id) {
  1952. $ids[] = $goodsCache[$goods_id];
  1953. }
  1954. }
  1955. $integral_cat->goods_id = json_encode($ids);
  1956. $integral_cat->cat_id = $video_cat_cache[$video_goods_item['cat_id']];
  1957. $integral_cat->store_id = $toId;
  1958. $integral_cat->user_id = 0;
  1959. $integral_cat->created_at = time();
  1960. $integral_cat->updated_at = time();
  1961. if (!$integral_cat->save()) {
  1962. throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
  1963. }
  1964. }
  1965. return [
  1966. 'code' => 0,
  1967. 'msg' => '同步成功'
  1968. ];
  1969. } catch (\Exception $e) {
  1970. return [
  1971. 'code' => 1,
  1972. 'msg' => $e->getMessage()
  1973. ];
  1974. }
  1975. }
  1976. /** 同步短视频end **/
  1977. /** 同步积分商城start **/
  1978. private function integralStore($fromStoreId, $toId) {
  1979. //商品分类
  1980. $goods_cat_result = $this->integralGoodsCatCopy($fromStoreId, $toId);
  1981. //设置
  1982. $setting_result = $this->integralSettingCopy($fromStoreId, $toId);
  1983. return [$goods_cat_result, $setting_result];
  1984. }
  1985. //商品分类
  1986. private function integralGoodsCatCopy($fromStoreId, $toId) {
  1987. try {
  1988. $integral_cat_list = NewIntegralCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  1989. $integral_cat_arr = [];
  1990. NewIntegralCat::deleteAll(['store_id' => $toId]);
  1991. $goods_id = Goods::find()->where(['store_id' => $toId, 'is_delete' => 0, 'product_type' => Goods::GOODS_TYPE_INTEGRAL, 'mch_id' => 0])
  1992. ->select('id')->asArray()->column();
  1993. foreach ($integral_cat_list as $integral_cat_item) {
  1994. $id = $integral_cat_item['id'];
  1995. unset($integral_cat_item['id']);
  1996. //判断是否为当前商城同步过此商品
  1997. $storeSyncExtLog = StoreSyncExtLog::findOne([
  1998. 'from_store_id' => $fromStoreId,
  1999. 'to_store_id' => $toId,
  2000. 'type' => StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT,
  2001. 'from_id' => $id
  2002. ]);
  2003. $integral_cat = NewIntegralCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new NewIntegralCat();
  2004. $integral_cat->attributes = $integral_cat_item;
  2005. $integral_cat->store_id = $toId;
  2006. $integral_cat->created_at = time();
  2007. $integral_cat->updated_at = time();
  2008. if (!$integral_cat->save()) {
  2009. throw new \Exception(json_encode($integral_cat->errors, JSON_UNESCAPED_UNICODE));
  2010. }
  2011. $integral_cat_arr[$id] = $integral_cat->id;
  2012. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $integral_cat->id, StoreSyncExtLog::TYPE_INTEGRAL_STORE_CAT);
  2013. $goods_cat_list = GoodsCat::find()->where(['goods_id' => $goods_id, 'cat_id' => $integral_cat_item['id'], 'is_delete' => 0, 'store_id' => $toId])
  2014. ->asArray()->all();
  2015. foreach ($goods_cat_list as $goods_cat_item) {
  2016. $goods_cat = GoodsCat::findOne($goods_cat_item['id']);
  2017. $goods_cat->cat_id = $integral_cat->id;
  2018. if (!$goods_cat->save()) {
  2019. throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
  2020. }
  2021. }
  2022. }
  2023. return [
  2024. 'code' => 0,
  2025. 'msg' => '同步完成',
  2026. 'data' => [
  2027. 'integral_cat_arr' => $integral_cat_arr
  2028. ]
  2029. ];
  2030. } catch (\Exception $e) {
  2031. return [
  2032. 'code' => 1,
  2033. 'msg' => $e->getMessage()
  2034. ];
  2035. }
  2036. }
  2037. private function integralSettingCopy($fromStoreId, $toId) {
  2038. try {
  2039. //签到积分设置
  2040. $integral_setting_ = IntegralSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
  2041. if ($integral_setting_) {
  2042. $integral_setting = IntegralSetting::findOne(['store_id' => $toId]);
  2043. if (!$integral_setting) {
  2044. $integral_setting = new IntegralSetting();
  2045. }
  2046. unset($integral_setting_['id']);
  2047. $integral_setting->attributes = $integral_setting_;
  2048. $integral_setting->store_id = $toId;
  2049. if (!$integral_setting->save()) {
  2050. throw new \Exception(json_encode($integral_setting->errors, JSON_UNESCAPED_UNICODE));
  2051. }
  2052. }
  2053. //积分商城设置
  2054. $new_integral_setting_ = NewIntegralSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
  2055. if ($new_integral_setting_) {
  2056. $new_integral_setting = NewIntegralSetting::findOne(['store_id' => $toId]);
  2057. if (!$new_integral_setting) {
  2058. $new_integral_setting = new NewIntegralSetting();
  2059. }
  2060. $new_integral_setting->store_id = $toId;
  2061. $new_integral_setting->polling_img = $new_integral_setting_['polling_img'];
  2062. $new_integral_setting->explain = $new_integral_setting_['explain'];
  2063. $new_integral_setting->created_at = time();
  2064. if (!$new_integral_setting->save()) {
  2065. throw new \Exception(json_encode($new_integral_setting->errors, JSON_UNESCAPED_UNICODE));
  2066. }
  2067. }
  2068. return [
  2069. 'code' => 0,
  2070. 'msg' => '同步完成'
  2071. ];
  2072. } catch (\Exception $e) {
  2073. return [
  2074. 'code' => 1,
  2075. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  2076. ];
  2077. }
  2078. }
  2079. /** 同步积分商城end **/
  2080. /** 同步上门服务start **/
  2081. private function workerConfig($fromStoreId, $toId, $goodsCache) {
  2082. //商品分类
  2083. $cat_cache = $this->workerGoodsCat($fromStoreId, $toId);
  2084. //商品列表
  2085. $goods_result = $this->workerGoods($fromStoreId, $toId, $cat_cache, $goodsCache);
  2086. //服务分类
  2087. $worker_cat_result = $this->workerCat($fromStoreId, $toId);
  2088. //服务等级
  2089. $worker_level_result = $this->workerLevel($fromStoreId, $toId);
  2090. //功能设置
  2091. $worker_setting = $this->workerSetting($fromStoreId, $toId);
  2092. return [$cat_cache, $goods_result, $worker_cat_result, $worker_level_result, $worker_setting];
  2093. }
  2094. //预约
  2095. private function bookingHome($fromStoreId, $toId, $goodsCache) {
  2096. //预约分类
  2097. $cat_cache = $this->bookingHomeGoodsCat($fromStoreId, $toId);
  2098. //预约商品与预约分类修改绑定关系
  2099. $ext_result = $this->bookingHomeGoodsCatBind($fromStoreId, $toId, $goodsCache, $cat_cache);
  2100. return [$cat_cache, $ext_result];
  2101. }
  2102. //预约商品分类
  2103. private function bookingHomeGoodsCat($fromStoreId, $toId) {
  2104. try {
  2105. $goodsCatCache = [];
  2106. $goods_cat_ = BookingGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  2107. BookingGoodsCat::deleteAll(['store_id' => $toId]);
  2108. foreach ($goods_cat_ as $goods_cat_item) {
  2109. $id = $goods_cat_item['id'];
  2110. unset($goods_cat_item['id']);
  2111. //判断是否为当前商城同步过此商品
  2112. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2113. 'from_store_id' => $fromStoreId,
  2114. 'to_store_id' => $toId,
  2115. 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT,
  2116. 'from_id' => $id
  2117. ]);
  2118. $goods_cat = BookingGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsCat();
  2119. $goods_cat->attributes = $goods_cat_item;
  2120. $goods_cat->store_id = $toId;
  2121. $goods_cat->name = $goods_cat_item['name'];
  2122. $goods_cat->pic_url = $goods_cat_item['pic_url'];
  2123. $goods_cat->sort = $goods_cat_item['sort'];
  2124. $goods_cat->is_show = $goods_cat_item['is_show'];
  2125. if (!$goods_cat->save()) {
  2126. throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
  2127. }
  2128. $goodsCatCache[$id] = $goods_cat->id;
  2129. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_BOOKING_GOODS_CAT);
  2130. }
  2131. return $goodsCatCache;
  2132. } catch (\Exception $e) {
  2133. return [];
  2134. }
  2135. }
  2136. //预约商品与预约分类修改绑定关系
  2137. private function bookingHomeGoodsCatBind($fromStoreId, $toId, $goodsCache, $cat_cache) {
  2138. try {
  2139. $goodsExt = BookingGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
  2140. BookingGoodsExt::deleteAll(['store_id' => $toId]);
  2141. foreach ($goodsExt as $ext_item) {
  2142. //判断是否为当前商城同步过此商品
  2143. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2144. 'from_store_id' => $fromStoreId,
  2145. 'to_store_id' => $toId,
  2146. 'type' => StoreSyncExtLog::TYPE_BOOKING_GOODS,
  2147. 'from_id' => $ext_item['id']
  2148. ]);
  2149. $id = $ext_item['id'];
  2150. unset($ext_item['id']);
  2151. $goods_ext = BookingGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new BookingGoodsExt();
  2152. $goods_ext->store_id = $toId;
  2153. $goods_ext->goods_id = $goodsCache[$ext_item['goods_id']];
  2154. $goods_ext->cat_id = $cat_cache[$ext_item['cat_id']];
  2155. if (!$goods_ext->save()) {
  2156. throw new \Exception(json_encode($goods_ext->errors, JSON_UNESCAPED_UNICODE));
  2157. }
  2158. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_ext->id, StoreSyncExtLog::TYPE_BOOKING_GOODS);
  2159. }
  2160. return [
  2161. 'code' => 0,
  2162. 'msg' => 'success'
  2163. ];
  2164. } catch (\Exception $e) {
  2165. return [
  2166. 'code' => 1,
  2167. 'msg' => $e->getMessage()
  2168. ];
  2169. }
  2170. }
  2171. //商品分类
  2172. private function workerGoodsCat($fromStoreId, $toId) {
  2173. try {
  2174. $cat_cache = [];
  2175. $goods_cat_ = WorkerGoodsCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  2176. WorkerGoodsCat::deleteAll(['store_id' => $toId]);
  2177. foreach ($goods_cat_ as $goods_cat_item) {
  2178. $id = $goods_cat_item['id'];
  2179. unset($goods_cat_item['id']);
  2180. //判断是否为当前商城同步过此商品
  2181. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2182. 'from_store_id' => $fromStoreId,
  2183. 'to_store_id' => $toId,
  2184. 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS_CAT,
  2185. 'from_id' => $id
  2186. ]);
  2187. $goods_cat = WorkerGoodsCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerGoodsCat();
  2188. $goods_cat->attributes = $goods_cat_item;
  2189. $goods_cat->store_id = $toId;
  2190. $goods_cat->created_at = time();
  2191. $goods_cat->updated_at = time();
  2192. $goods_cat->name = $goods_cat_item['name'];
  2193. $goods_cat->pic_url = $goods_cat_item['pic_url'];
  2194. $goods_cat->is_show = $goods_cat_item['is_show'];
  2195. $goods_cat->big_pic_url = $goods_cat_item['big_pic_url'];
  2196. $goods_cat->advert_pic = $goods_cat_item['advert_pic'];
  2197. $goods_cat->advert_url = $goods_cat_item['advert_url'];
  2198. if (!$goods_cat->save()) {
  2199. throw new \Exception(json_encode($goods_cat->errors, JSON_UNESCAPED_UNICODE));
  2200. }
  2201. $cat_cache[$id] = $goods_cat->id;
  2202. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $goods_cat->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS_CAT);
  2203. }
  2204. return $cat_cache;
  2205. } catch (\Exception $e) {
  2206. return [
  2207. 'code' => 1,
  2208. 'msg' => $e->getMessage()
  2209. ];
  2210. }
  2211. }
  2212. private function workerGoods($fromStoreId, $toId, $cat_cache, $goodsCache) {
  2213. $t = \Yii::$app->db->beginTransaction();
  2214. try {
  2215. $workGoodsCache = [];
  2216. $worker_goods_list = WorkerGoodsExt::find()->where(['store_id' => $fromStoreId])->asArray()->all();
  2217. WorkerGoodsExt::deleteAll(['store_id' => $toId]);
  2218. foreach ($worker_goods_list as $goods_item) {
  2219. $goods_id = $goodsCache[$goods_item['goods_id']];
  2220. $cat_id = $cat_cache[$goods_item['cat_id']];
  2221. if (!empty($goods_id) && !empty($cat_id)) {
  2222. $workGoodsId = $goods_item['id'];
  2223. $workGoodsCache[$workGoodsId] = 0;
  2224. unset($goods_item['id']);
  2225. //判断是否为当前商城同步过此商品
  2226. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2227. 'from_store_id' => $fromStoreId,
  2228. 'to_store_id' => $toId,
  2229. 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS,
  2230. 'from_id' => $workGoodsId
  2231. ]);
  2232. $worker_goods = WorkerGoodsExt::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerGoodsExt();
  2233. $worker_goods->attributes = $goods_item;
  2234. $worker_goods->payment_type = $goods_item['payment_type'];
  2235. $worker_goods->profit_base = $goods_item['profit_base'];
  2236. $worker_goods->profit_max = $goods_item['profit_max'];
  2237. $worker_goods->final_profit = $goods_item['profit_max'];
  2238. $worker_goods->desc = $goods_item['desc'];
  2239. $worker_goods->warn = $goods_item['warn'];
  2240. $worker_goods->bind_worker = $goods_item['bind_worker'];
  2241. $worker_goods->goods_id = $goods_id;
  2242. $worker_goods->cat_id = $cat_id;
  2243. $worker_goods->store_id = $toId;
  2244. if (!$worker_goods->save()) {
  2245. throw new \Exception(json_encode($worker_goods->errors, JSON_UNESCAPED_UNICODE));
  2246. }
  2247. $workGoodsCache[$workGoodsId] = $worker_goods->id;
  2248. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $workGoodsId, $worker_goods->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS);
  2249. }
  2250. }
  2251. $t->commit();
  2252. return $workGoodsCache;
  2253. } catch (\Exception $e) {
  2254. $t->rollBack();
  2255. return [
  2256. 'code' => 1,
  2257. 'msg' => $e->getMessage()
  2258. ];
  2259. }
  2260. }
  2261. private function workerCat($fromStoreId, $toId) {
  2262. try {
  2263. $workerCatCache = [];
  2264. $worker_cat_list = WorkerCat::find()->where(['store_id' => $fromStoreId, 'is_delete' => 0])->asArray()->all();
  2265. WorkerCat::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
  2266. foreach ($worker_cat_list as $item) {
  2267. $workerCatId = $item['id'];
  2268. $workerCatCache[$workerCatId] = 0;
  2269. unset($item['id']);
  2270. //判断是否为当前商城同步过此商品
  2271. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2272. 'from_store_id' => $fromStoreId,
  2273. 'to_store_id' => $toId,
  2274. 'type' => StoreSyncExtLog::TYPE_WORKER_CONFIG_CAT,
  2275. 'from_id' => $workerCatId
  2276. ]);
  2277. $worker_cat = WorkerCat::findOne($storeSyncExtLog->to_id ?? 0) ?: new WorkerCat();
  2278. $worker_cat->attributes = $item;
  2279. $worker_cat->store_id = $toId;
  2280. $worker_cat->created_at = time();
  2281. $worker_cat->updated_at = time();
  2282. if (!$worker_cat->save()) {
  2283. throw new \Exception(json_encode($worker_cat->errors, JSON_UNESCAPED_UNICODE));
  2284. }
  2285. $workerCatCache[$workerCatId] = $worker_cat->id;
  2286. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $workerCatId, $worker_cat->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_CAT);
  2287. }
  2288. return $workerCatCache;
  2289. } catch (\Exception $e) {
  2290. return [
  2291. 'code' => 1,
  2292. 'msg' => $e->getMessage()
  2293. ];
  2294. }
  2295. }
  2296. //同步等级
  2297. private function workerLevel($from_store_id, $to_store_id) {
  2298. try {
  2299. $workerLevelList = WorkerLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
  2300. foreach ($workerLevelList as $item) {
  2301. $workerLevelId = $item['id'];
  2302. //判断是否为当前商城同步过此商品
  2303. $worker_level = WorkerLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
  2304. if (!$worker_level) {
  2305. $worker_level = new WorkerLevel();
  2306. }
  2307. unset($item['id']);
  2308. $worker_level->attributes = $item;
  2309. $worker_level->store_id = $to_store_id;
  2310. if (!$worker_level->save()) {
  2311. return [
  2312. 'code' => 1,
  2313. 'msg' => json_encode($worker_level->errors, JSON_UNESCAPED_UNICODE)
  2314. ];
  2315. };
  2316. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $workerLevelId, $worker_level->id, StoreSyncExtLog::TYPE_WORKER_CONFIG_LEVEL);
  2317. }
  2318. return [
  2319. 'code' => 0,
  2320. 'msg' => '同步成功'
  2321. ];
  2322. } catch (\Exception $e) {
  2323. return [
  2324. 'code' => 1,
  2325. 'msg' => $e->getMessage()
  2326. ];
  2327. }
  2328. }
  2329. //同步短视频分类
  2330. private function workerSetting($fromStoreId, $toId) {
  2331. try {
  2332. $worker_setting_ = WorkerSetting::find()->where(['store_id' => $fromStoreId])->asArray()->one();
  2333. unset($worker_setting_['id']);
  2334. $worker_setting = WorkerSetting::findOne(['store_id' => $toId]);
  2335. if (!$worker_setting) {
  2336. $worker_setting = new WorkerSetting();
  2337. }
  2338. $worker_setting->store_id = $toId;
  2339. $worker_setting->order_space = $worker_setting_['order_space'] ?: 999;
  2340. $worker_setting->fee_road = $worker_setting_['fee_road'] ?? '';
  2341. $worker_setting->form_apply = $worker_setting_['form_apply'] ?? '';
  2342. $worker_setting->tag = $worker_setting_['tag'] ?? '';
  2343. $worker_setting->created_at = time();
  2344. $worker_setting->updated_at = time();
  2345. $worker_setting->starting_price = $worker_setting_['starting_price'] ?? '0.00';
  2346. $worker_setting->additional_charge = $worker_setting_['additional_charge'] ?? '0.00';
  2347. $worker_setting->banner = $worker_setting_['banner'] ?? '';
  2348. if (!$worker_setting->save()) {
  2349. throw new \Exception(json_encode($worker_setting->errors, JSON_UNESCAPED_UNICODE));
  2350. }
  2351. return [
  2352. 'code' => 0,
  2353. 'msg' => '同步成功'
  2354. ];
  2355. } catch (\Exception $e) {
  2356. return [
  2357. 'code' => 1,
  2358. 'msg' => $e->getMessage()
  2359. ];
  2360. }
  2361. }
  2362. /** 同步上门服务end **/
  2363. /** 同步文章start **/
  2364. private function articleSetting($fromStoreId, $toId) {
  2365. try {
  2366. $article_list = AboutArticle::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->asArray()->all();
  2367. debug_log(AboutArticle::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->andWhere(['<>', 'type', 1])->createCommand()->getRawSql(), 'store_sync.log');
  2368. AboutArticle::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
  2369. foreach ($article_list as $article_item) {
  2370. $id = $article_item['id'];
  2371. unset($article_item['id']);
  2372. //判断是否为当前商城同步过此商品
  2373. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2374. 'from_store_id' => $fromStoreId,
  2375. 'to_store_id' => $toId,
  2376. 'type' => StoreSyncExtLog::TYPE_ARTICLE,
  2377. 'from_id' => $id
  2378. ]);
  2379. $aboutArticle = AboutArticle::findOne($storeSyncExtLog->to_id ?? 0) ?: new AboutArticle();
  2380. $aboutArticle->attributes = $article_item;
  2381. $aboutArticle->store_id = $toId;
  2382. $aboutArticle->type = $article_item['type'];
  2383. if (!$aboutArticle->save()) {
  2384. throw new \Exception(json_encode($aboutArticle->errors, JSON_UNESCAPED_UNICODE));
  2385. };
  2386. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $aboutArticle->id, StoreSyncExtLog::TYPE_ARTICLE);
  2387. }
  2388. return [
  2389. 'code' => 0,
  2390. 'msg' => '同步成功'
  2391. ];
  2392. } catch (\Exception $e) {
  2393. return [
  2394. 'code' => 1,
  2395. 'msg' => $e->getMessage()
  2396. ];
  2397. }
  2398. }
  2399. /** 同步文章end **/
  2400. /** 同步专题start **/
  2401. private function topicSetting($fromStoreId, $toId) {
  2402. $type_result = $this->topicTypeSetting($fromStoreId, $toId);
  2403. $typeCache = [];
  2404. if ($type_result['code'] === 0) {
  2405. $typeCache = $type_result['data']['typeCache'];
  2406. }
  2407. $result = $this->topicContentSetting($fromStoreId, $toId, $typeCache);
  2408. return [$type_result, $result];
  2409. }
  2410. private function topicTypeSetting($fromStoreId, $toId) {
  2411. try {
  2412. $topic_type_list = TopicType::find()->where(['is_delete' => TopicType::IS_DELETE_NO, 'store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
  2413. TopicType::deleteAll(['store_id' => $toId, 'is_delete' => 0]);
  2414. $typeCache = [];
  2415. foreach ($topic_type_list as $topic_type_item) {
  2416. $id = $topic_type_item['id'];
  2417. unset($topic_type_item['id']);
  2418. //判断是否为当前商城同步过此商品
  2419. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2420. 'from_store_id' => $fromStoreId,
  2421. 'to_store_id' => $toId,
  2422. 'type' => StoreSyncExtLog::TYPE_TOPIC_TYPE,
  2423. 'from_id' => $id
  2424. ]);
  2425. $TopicType = TopicType::findOne($storeSyncExtLog->to_id ?? 0) ?: new TopicType();
  2426. $TopicType->attributes = $topic_type_item;
  2427. $TopicType->store_id = $toId;
  2428. if (!$TopicType->save()) {
  2429. throw new \Exception(json_encode($TopicType->errors, JSON_UNESCAPED_UNICODE));
  2430. };
  2431. $typeCache[$id] = $TopicType->id;
  2432. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $TopicType->id, StoreSyncExtLog::TYPE_TOPIC_TYPE);
  2433. }
  2434. return [
  2435. 'code' => 0,
  2436. 'msg' => '同步成功',
  2437. 'data' => [
  2438. 'typeCache' => $typeCache
  2439. ]
  2440. ];
  2441. } catch (\Exception $e) {
  2442. return [
  2443. 'code' => 1,
  2444. 'msg' => $e->getMessage()
  2445. ];
  2446. }
  2447. }
  2448. private function topicContentSetting($fromStoreId, $toId, $typeCache) {
  2449. try {
  2450. $topic_list = Topic::find()->where(['is_delete' => 0, 'store_id' => $fromStoreId])->orderBy('sort desc, id desc')->asArray()->all();
  2451. Topic::updateAll(['is_delete' => 0], ['store_id' => $toId, 'is_delete' => 0]);
  2452. foreach ($topic_list as $topic_item) {
  2453. $id = $topic_item['id'];
  2454. unset($topic_item['id']);
  2455. //判断是否为当前商城同步过此商品
  2456. $storeSyncExtLog = StoreSyncExtLog::findOne([
  2457. 'from_store_id' => $fromStoreId,
  2458. 'to_store_id' => $toId,
  2459. 'type' => StoreSyncExtLog::TYPE_TOPIC,
  2460. 'from_id' => $id
  2461. ]);
  2462. $Topic = Topic::findOne($storeSyncExtLog->to_id ?? 0) ?: new Topic();
  2463. $Topic->attributes = $topic_item;
  2464. $Topic->store_id = $toId;
  2465. $Topic->type = $typeCache[$topic_item['type']];
  2466. if (!$Topic->save()) {
  2467. throw new \Exception(json_encode($Topic->errors, JSON_UNESCAPED_UNICODE));
  2468. };
  2469. (new StoreSyncExtLog())::handleData($fromStoreId, $toId, $id, $Topic->id, StoreSyncExtLog::TYPE_TOPIC);
  2470. }
  2471. return [
  2472. 'code' => 0,
  2473. 'msg' => '同步成功'
  2474. ];
  2475. } catch (\Exception $e) {
  2476. return [
  2477. 'code' => 1,
  2478. 'msg' => $e->getMessage()
  2479. ];
  2480. }
  2481. }
  2482. /** 同步专题end **/
  2483. }