OrderGoodsSeeder.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. use Illuminate\Database\Seeder;
  3. class OrderGoodsSeeder extends Seeder
  4. {
  5. protected $sourceTable = 'sz_yi_order_goods';
  6. protected $table = 'yz_order_goods';
  7. protected $sourceGoodsTable = 'sz_yi_goods';
  8. protected $orderTable = 'yz_order';
  9. // protected $mappingOrderTable = 'yz_mapping_orders';
  10. // protected $mappingGoodTable = 'yz_mapping_goods'; //todo 等待"商品重构组"生成新旧goods_id的对应表
  11. /**
  12. * Run the database seeds.
  13. *
  14. * @return void
  15. */
  16. public function run()
  17. {
  18. return;
  19. if (!Schema::hasTable($this->sourceTable)) {
  20. echo $this->sourceTable." 不存在 跳过\n";
  21. return;
  22. }
  23. if (!Schema::hasTable($this->sourceGoodsTable)) {
  24. echo $this->sourceGoodsTable." 不存在 跳过\n";
  25. return;
  26. }
  27. //检测新的数据表是否有数据
  28. // $newList = DB::table($this->table)->first();
  29. // if($newList){
  30. // echo $this->table."数据表已经有数据, 请检查.\n";
  31. // return;
  32. // }
  33. // print_r($_SERVER);
  34. $sourceList = \Illuminate\Support\Facades\DB::table($this->sourceTable)->chunk(100, function($records){
  35. foreach ($records as $record){
  36. //如果旧表字段没有值,则设为0
  37. foreach ($record as $k => $v){
  38. if(!$v){
  39. if ($k != 'total'){
  40. $record[$k] = 0;
  41. } else {
  42. $record[$k] = 1; //新表中 total 字段默认为 1
  43. }
  44. }
  45. }
  46. //获取"商品名称"和"商品图片URL"
  47. $goodInfo = \Illuminate\Support\Facades\DB::table($this->sourceGoodsTable)->select('title','thumb')
  48. ->where('id','=',$record['goodsid'])->first();
  49. if ($goodInfo){
  50. $record['title'] = $goodInfo['title'];
  51. if(preg_match('/^http.*/',$goodInfo['thumb'])){
  52. $record['thumb'] = $goodInfo['thumb']; //todo 最后的url取决于迁徙后放在哪个路径
  53. } else if (preg_match('/^images.*/',$goodInfo['thumb'])){
  54. $record['thumb'] = 'http://demo.yunzshop.com'.'/attachment/'.$goodInfo['thumb']; //todo 最后的url取决于迁徙后放在哪个路径
  55. //无法通过全局变量$_SERVER['SERVER_NAME']获取网站域名
  56. }
  57. } else {
  58. // echo '在旧的goods表中找不到该商品信息';
  59. // return;
  60. $record['title'] = 0; //todo 调试用
  61. $record['thumb'] = 0; //todo 调试用
  62. }
  63. //获取orderid和member_id
  64. // $orderMapping = DB::table($this->mappingOrderTable)->select('new_order_id','new_member_id')
  65. // ->where('old_order_id','=',$record['orderid'])->first();
  66. // if(!$orderMapping){
  67. // echo $this->mappingOrderTable.'表中没有找到新旧orderid的对应关系,旧orderid为'.$record['orderid'];
  68. // return;
  69. // }
  70. // $uid = $orderMapping['new_member_id'];
  71. // $orderId = $orderMapping['new_order_id'];
  72. $uid = 1; //todo 调试用
  73. $orderId = $record['orderid']; //todo 调试用
  74. //获取goods_id
  75. // $goodsId = DB::table($this->mappingGoodTable)->select('new_good_id')
  76. // ->where('old_good_id','=',$record['goodsid'])->first();
  77. // if(!goodsId){
  78. // echo $this->mappingGoodTable.'表中没有找到新旧goods_id的对应关系,旧goodsid为'.$record['goodsid'];
  79. // return;
  80. // }
  81. $goodsId = $record['goodsid']; //todo 调试用
  82. \Illuminate\Support\Facades\DB::table($this->table)->insert(
  83. [
  84. 'uniacid' => $record['uniacid'], //公众号ID
  85. 'order_id' => $orderId, //订单ID
  86. 'member_id' => $uid, //会员身份标识, mc_member表的uid
  87. 'goods_id' => $goodsId, //商品ID
  88. 'goods_sn' => $record['goodssn'], //商品编码 //todo productsn?
  89. 'goods_price' => $record['price'] * 100, //商品快照价格 (单位为"分")
  90. 'total' => $record['total'], //订单商品件数
  91. 'price' => $record['realprice'] * 100, //真实价格 (单位为"分")
  92. 'title' => $record['title'], //商品名称
  93. 'thumb' => $record['thumb'], //商品图片
  94. 'create_time' => $record['createtime'], //生成记录的时间
  95. ]
  96. );
  97. }
  98. });
  99. }
  100. }