Browse Source

线上商品清洗功能和迁移数据功能

master
liangyuyan 5 years ago
parent
commit
c5ff8db836
  1. 3
      app/Admin/Actions/Grid/GoodsCopy.php
  2. 36
      app/Admin/Actions/Grid/GoodsNewCopy.php
  3. 11
      app/Admin/Controllers/v3/GoodsNewController.php
  4. 105
      app/Admin/Forms/GoodsNewCopyForm.php
  5. 112
      app/Console/Commands/MigrateGoods.php
  6. 93
      app/Console/Commands/MigrateShoppingCart.php
  7. 21
      app/Console/Commands/MigrateStore.php
  8. 98
      app/Console/Commands/MigrateStoreWithdrawal.php
  9. 98
      app/Console/Commands/MigrateUser.php
  10. 90
      app/Console/Commands/MigrateUserCollection.php
  11. 8
      app/Models/v3/GoodsNew.php
  12. 4
      resources/lang/zh-CN/goods-new.php

3
app/Admin/Actions/Grid/GoodsCopy.php

@ -4,9 +4,6 @@ namespace App\Admin\Actions\Grid;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Widgets\Modal;
use App\Models\v3\Store as StoreModel;
use Dcat\Admin\Form;
use App\Models\v3\Goods as GoodsModel;
use App\Admin\Forms\GoodsCopyForm;
class GoodsCopy extends RowAction

36
app/Admin/Actions/Grid/GoodsNewCopy.php

@ -0,0 +1,36 @@
<?php
namespace App\Admin\Actions\Grid;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Forms\GoodsNewCopyForm;
class GoodsNewCopy extends RowAction
{
/**
* @return string
*/
protected $title = '复制';
public function render()
{
$id = $this->getKey();
$modal = Modal::make()
->xl()
->title($this->title)
->body(GoodsNewCopyForm::make()->setKey($id))
->button($this->title);
return $modal;
}
public function parameters()
{
return [
];
}
}

11
app/Admin/Controllers/v3/GoodsNewController.php

@ -2,7 +2,7 @@
namespace App\Admin\Controllers\v3;
use App\Admin\Actions\Grid\GoodsCopy;
use App\Admin\Actions\Grid\GoodsNewCopy;
use App\Admin\Repositories\v3\GoodsNew;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
@ -13,7 +13,6 @@ use App\Models\v3\StoreNew as StoreModel;
use Dcat\Admin\Form\NestedForm;
use App\Models\v3\GoodsNew as GoodsModel;
use App\Models\v3\Market as MarketModel;
use Dcat\Admin\Grid\Displayers\Actions;
use App\Models\v3\GoodsCategory as GoodsCategoryModel;
class GoodsNewController extends AdminController
@ -51,7 +50,7 @@ class GoodsNewController extends AdminController
$grid->sort->sortable();
$grid->on_sale->switch();
$grid->actions([new GoodsCopy()]);
$grid->actions([new GoodsNewCopy()]);
$grid->filter(function (Grid\Filter $filter) {
$filter->like('name');
});
@ -144,7 +143,7 @@ class GoodsNewController extends AdminController
*/
protected function form()
{
$builder = GoodsModel::with(['ImageBanners','VideoBanners']);
$builder = GoodsModel::with(['ImageNewBanners','VideoNewBanners']);
return Form::make($builder, function (Form $form) {
$form->hidden('id');
@ -178,13 +177,13 @@ class GoodsNewController extends AdminController
$form->number('start_num')->attribute('min', 1)->default(1);
});
$form->column(12, function (Form $form) {
$form->hasMany('image_banners', function (Form\NestedForm $form) {
$form->hasMany('image_new_banners', function (Form\NestedForm $form) {
$form->image('path')->width(2);
$form->number('sort')->width(2)->default(0);
$form->hidden('type')->value(1)->default(1);
})->required();
$form->divider();
$form->hasMany('video_banners', function (Form\NestedForm $form) {
$form->hasMany('video_new_banners', function (Form\NestedForm $form) {
$form->file('path')->width(2);
$form->number('sort')->width(2)->default(0);
$form->hidden('type')->value(2)->default(2);

105
app/Admin/Forms/GoodsNewCopyForm.php

@ -0,0 +1,105 @@
<?php
namespace App\Admin\Forms;
use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use App\Models\v3\StoreNew as StoreModel;
use App\Models\v3\GoodsNew as GoodsModel;
use App\Models\v3\GoodsNewBanners as GoodsBannerModel;
use Illuminate\Support\Facades\DB;
class GoodsNewCopyForm extends Form
{
/**
* Handle the form request.
*
* @param array $input
*
* @return Response
*/
public function handle(array $input)
{
// 获取外部传递参数
$goodsId = $input['goods_id'];
$storeIds= $input['store_ids'];
$goods = GoodsModel::find($goodsId);
$goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get();
$markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id');
foreach($storeIds as $key =>$value){
$storeId = $value;
$model = new GoodsModel();
$model->store_id = $storeId;
$model->market_id = $markets[$storeId];
$model->category_id = $goods->category_id;
$model->name = $goods->name;
$model->cover_img = $goods->cover_img;
$model->goods_unit = $goods->goods_unit;
$model->tags = $goods->tags;
$model->spec = $goods->spec;
$model->details_imgs = $goods->details_imgs;
$model->content = $goods->content;
$model->details = $goods->details;
$model->price = $goods->price;
$model->original_price = $goods->original_price;
$model->vip_price = $goods->vip_price;
$model->start_num = $goods->start_num;
$model->restrict_num = $goods->restrict_num;
$model->is_infinite = $goods->is_infinite;
$model->inventory = $goods->inventory;
$model->on_sale = $goods->on_sale;
$model->sort = $goods->sort;
$model->remark = $goods->remark;
if($model->save() && !empty($goodsBanners)){
$goodsId = $model->getKey();
$banners = [];
foreach($goodsBanners as $kb => $vb){
$banners[] = [
'goods_id' => $goodsId,
'type' => $vb->type,
'path' => $vb->path,
'sort' => $vb->sort,
'created_at' => time(),
'updated_at' => time(),
];
GoodsBannerModel::insert($banners);
}
};
}
return $this->success('修改成功', '/goods');
}
/**
* Build a form here.
*/
public function form()
{
$id = $this->getKey();
$goods = GoodsModel::select('name')->find($id);
$this->hidden('goods_id')->value($id);
$this->display('name')->value($goods->name);
$stores = StoreModel::getStoreArray();
$this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!');
}
/**
* The data of the form.
*
* @return array
*/
public function default()
{
return [];
}
}

112
app/Console/Commands/MigrateGoods.php

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateGoods extends Command
{
@ -38,41 +39,52 @@ class MigrateGoods extends Command
*/
public function handle()
{
// 如果表不存在则创建
// if(!Schema::hasTable('lanzu_store_users')){
// Schema::create('lanzu_store_users',function (Blueprint $table){
// $table->id();
// });
// }
$specData = [
'克' => '净含量',
'斤' => '净含量',
'两' => '净含量',
'只' => '净含量',
'份' => '净含量',
'g' => '净含量',
'l' => '净含量',
'ml' => '净含量',
'ML' => '净含量',
'L' => '净含量',
'个' => '净含量',
'条' => '净含量',
'公分' => '净含量',
'边' => '净含量',
'包' => '净含量',
'辣' => '口味',
'馅' => '馅料',
'mm' => '尺寸'
];
$oldData = DB::table('ims_cjdc_goods_new')->get();
$oldTableName = 'ims_cjdc_goods_new';
$oldSpecTableName = 'ims_cjdc_spec_combination_new';
$oldStoreTableName = 'ims_cjdc_store_new';
$newTableName = 'lanzu_goods_new';
$newBannerTableName = 'lanzu_goods_banners_new';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧商品表不存在');
return 0;
}
if(!Schema::hasTable($oldSpecTableName)){
var_dump('旧规格表不存在');
return 0;
}
if(!Schema::hasTable($oldStoreTableName)){
var_dump('旧的店铺表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新商品表不存在');
return 0;
}
if(!Schema::hasTable($newBannerTableName)){
var_dump('新商品的banners表不存在');
return 0;
}
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
$oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$startTime = time();
$total = 0;
$error = [];
foreach ($oldData as $value){
$goodsId = $value->id;
// 判断店铺是否存在,如果商品的店铺不在,则不存这个商品
$storeExist = DB::table($oldStoreTableName)->where('id',$value->store_id)->exists();
if(!$storeExist){
continue;
}
$goodsData =[
// 'id'=>$value->id,
'market_id'=>$value->market_id,
'market_id' => $oldStoreData[$value->store_id],
'store_id'=>$value->store_id,
'on_sale'=> $value->is_show == 1 ? 1 : 0,
'sort'=>$value->num,
@ -88,13 +100,11 @@ class MigrateGoods extends Command
'cover_img'=>$value->logo,
'goods_unit'=>$value->good_unit,
'spec'=>$value->spec,
'content'=>$value->content,
'details'=>$value->details,
'content'=> strip_tags($value->content),
'details'=> strip_tags($value->details),
];
$specs = DB::table('ims_cjdc_spec_combination_new')->where('good_id',$value->id)->get();
if(!empty($specs)){
$specs = DB::table($oldSpecTableName)->where('good_id',$goodsId)->get();
if(count($specs) > 0){
foreach($specs as $vs){
$spec = ['spec_key'=>'净含量','spec_value'=>$vs->combination];
if(strstr($vs->combination,'辣')){
@ -104,7 +114,9 @@ class MigrateGoods extends Command
}else if(strstr($vs->combination,'mm')){
$spec['spec_key'] = '尺寸';
}
$goodsData['spec'] = json_encode($spec);
$goodsData['spec'] = json_encode([$spec]);
$newData = $goodsData;
$newBanner = [
'type' =>1,
@ -112,32 +124,34 @@ class MigrateGoods extends Command
'created_at' =>time(),
'updated_at' =>time(),
];
$id = DB::table('lanzu_goods_new')->insertGetId($newData);
$newBanner['goods_id'] = $id;
DB::table('lanzu_goods_banners_new')->insertGetId($newBanner);
$total++;
if($total == 2){
$id = DB::table($newTableName)->insertGetId($newData);
if(!$id){
$error[] = ['id'=>$goodsId];
break;
}
$newBanner['goods_id'] = $id;
DB::table($newBannerTableName)->insertGetId($newBanner);
$total++;
}
}else{
$newData = $goodsData;
$id = DB::table('lanzu_goods_new')->insertGetId($newData);
$newBanner['goods_id'] = $id;
DB::table('lanzu_goods_banners_new')->insertGetId($newBanner);
$total++;
if($total == 2){
$res= $id = DB::table($newTableName)->insertGetId($newData);
if(!$res){
$error[] = ['id'=>$goodsId];
break;
}
$newBanner['goods_id'] = $id;
DB::table($newBannerTableName)->insertGetId($newBanner);
$total++;
}
$bar->advance();
}
$bar->finish();
echo '\r\n ';
echo time()-$startTime;
echo '\r\n '.$total;
var_dump([time()-$startTime]);
var_dump($total);
var_dump($error);
return 0;
}
}

93
app/Console/Commands/MigrateShoppingCart.php

@ -0,0 +1,93 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateShoppingCart extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrateData:shoppingCart';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command 迁移购物车数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$oldTableName = 'ims_cjdc_shopcar';
$oldStoreTableName = 'ims_cjdc_store';
$newTableName = 'lanzu_shopping_cart';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
$oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$startTime = time();
$error = [];
foreach ($oldData as $key => $value){
$cartId = $value->id;
// 判断在新表是否存在
$exist = DB::table($newTableName)->where('id',$cartId)->exists();
if($exist){
continue;
}
$newData =[
'id'=>$cartId,
'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0,
'store_id'=>$value->store_id,
'goods_id'=>$value->good_id,
'user_id'=>$value->user_id,
'num'=>$value->num,
'activity_type'=>1,
'created_at' => time(),
'updated_at' => time(),
];
$res = DB::table($newTableName)->insert($newData);
if(!$res){
$error[] = ['id'=>$cartId];
break;
}
$bar->advance();
}
$bar->finish();
var_dump([time()-$startTime]);
var_dump($error);
return 0;
}
}

21
app/Console/Commands/MigrateStore.php

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateStore extends Command
{
@ -38,14 +39,19 @@ class MigrateStore extends Command
*/
public function handle()
{
// 如果表不存在则创建
// if(!Schema::hasTable('lanzu_store_users')){
// Schema::create('lanzu_store_users',function (Blueprint $table){
// $table->id();
// });
// }
$oldTableName = 'ims_cjdc_store_new';
$newTableName = 'lanzu_store_new';
$storeImgPath = 'QR_code/code_store_img/wx_store_';/* 商户小程序码 */
$storePayPath = 'QR_code/code_pay_img/wx_pay_';/* 收银二维码 */
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$oldData = DB::table($oldTableName)->orderBy('id','desc')->get();
$bar = $this->output->createProgressBar(count($oldData));
@ -100,6 +106,9 @@ class MigrateStore extends Command
'introduction'=> strip_tags($value->details),
'store_applet_img' => $storeImgPath.$storeId.'.jpg',
'cash_code_img' => $storePayPath.$storeId.'.jpg',
'created_at' => time(),
'updated_at' => time(),
];

98
app/Console/Commands/MigrateStoreWithdrawal.php

@ -0,0 +1,98 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateStoreWithdrawal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrateData:storeWithdrawal';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command 迁移店铺提现记录数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$oldTableName = 'ims_cjdc_withdrawal';
$newTableName = 'lanzu_store_withdrawal';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$typeData = [2 => 1,3 => 3, 1 => 4];
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$startTime = time();
$error = [];
foreach ($oldData as $key => $value){
$withdrawalId = $value->id;
// 判断记录在新表是否存在
$exist = DB::table($newTableName)->where('id',$withdrawalId)->exists();
if($exist){
continue;
}
$newData =[
'id'=>$withdrawalId,
'store_id'=>$value->store_id,
'name'=>$value->name,
'tel'=>$value->tel,
'type'=> isset($typeData[$value->type])?$typeData[$value->type]:0,
'check_time'=>strtotime($value->sh_time),
'state'=>$value->state,
'apply_cash'=>$value->tx_cost,
'real_cash'=>$value->sj_cost,
'bank_card'=>$value->yhk_num,
'bank_info'=>$value->yh_info,
'created_at' => strtotime($value->time),
'updated_at' => strtotime($value->sh_time),
];
$res = DB::table($newTableName)->insert($newData);
if(!$res){
$error[] = ['id'=>$withdrawalId];
break;
}
$bar->advance();
}
$bar->finish();
var_dump([time()-$startTime]);
var_dump($error);
return 0;
}
}

98
app/Console/Commands/MigrateUser.php

@ -0,0 +1,98 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrateData:user';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command 迁移微信用户数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$oldTableName = 'ims_cjdc_user';
$newTableName = 'lanzu_user';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$typeData = [2 => 1,3 => 3, 1 => 4];
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$startTime = time();
$error = [];
foreach ($oldData as $key => $value){
$userId = $value->id;
// 判断记录在新表是否存在
$exist = DB::table($newTableName)->where('id',$userId)->exists();
if($exist){
continue;
}
$newData =[
'id'=>$userId,
'nick_name'=>$value->name,
'avatar'=>$value->img,
'openid'=>$value->openid,
// 'type'=> isset($typeData[$value->type])?$typeData[$value->type]:0,
'total_score'=>$value->total_score,
'wallet'=>$value->wallet,
'real_name'=>$value->real_name,
'tel'=>$value->tel,
'unionid'=>$value->unionid,
'status'=>$value->status,
'created_at' => strtotime($value->join_time),
'updated_at' => strtotime($value->sh_time),
];
$res = DB::table($newTableName)->insert($newData);
if(!$res){
$error[] = ['id'=>$userId];
break;
}
$bar->advance();
}
$bar->finish();
var_dump([time()-$startTime]);
var_dump($error);
return 0;
}
}

90
app/Console/Commands/MigrateUserCollection.php

@ -0,0 +1,90 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MigrateUserCollection extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrateData:userCollection';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command 迁移用户收藏店铺数据';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$oldTableName = 'ims_cjdc_collection';
$oldStoreTableName = 'ims_cjdc_store';
$newTableName = 'lanzu_user_collection';
// 判断表是否存在
if(!Schema::hasTable($oldTableName)){
var_dump('旧表不存在');
return 0;
}
if(!Schema::hasTable($newTableName)){
var_dump('新表不存在');
return 0;
}
$oldData = DB::table($oldTableName)->orderBy('id','asc')->get();
$oldStoreData = DB::table($oldStoreTableName)->pluck('market_id','id');
$bar = $this->output->createProgressBar(count($oldData));
$bar->start();
$startTime = time();
$error = [];
foreach ($oldData as $key => $value){
$collectionId = $value->id;
// 判断记录在新表是否存在
$exist = DB::table($newTableName)->where('id',$collectionId)->exists();
if($exist){
continue;
}
$newData =[
'id'=>$collectionId,
'store_id'=>$value->store_id,
'user_id'=>$value->user_id,
'market_id'=> isset($oldStoreData[$value->store_id])?$oldStoreData[$value->store_id]:0,
'created_at' => time(),
'updated_at' => time(),
];
$res = DB::table($newTableName)->insert($newData);
if(!$res){
$error[] = ['id'=>$collectionId];
break;
}
$bar->advance();
}
$bar->finish();
var_dump([time()-$startTime]);
var_dump($error);
return 0;
}
}

8
app/Models/v3/GoodsNew.php

@ -45,12 +45,12 @@ class GoodsNew extends Model
$value = $value ? $value : $this->is_infinite;
return isset(self::$_isInfinite[$value]) ? self::$_isInfinite[$value] : '';
}
public function ImageBanners(){
return $this->hasMany('\App\Models\v3\GoodsBanners','goods_id','id')->where('type',1);
public function ImageNewBanners(){
return $this->hasMany('\App\Models\v3\GoodsNewBanners','goods_id','id')->where('type',1);
}
public function VideoBanners(){
return $this->hasMany('\App\Models\v3\GoodsBanners','goods_id','id')->where('type',2);
public function VideoNewBanners(){
return $this->hasMany('\App\Models\v3\GoodsNewBanners','goods_id','id')->where('type',2);
}

4
resources/lang/zh-CN/goods-new.php

@ -34,8 +34,8 @@ return [
'spec_key' =>'规格名称',
'spec_value' => '规格参数',
'market_id' => '市场',
'image_banners' => '轮播图片',
'video_banners' => '轮播视频'
'image_new_banners' => '轮播图片',
'video_new_banners' => '轮播视频'
],
'options' => [
],

Loading…
Cancel
Save