| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace addons\CloudStock\console\migrations;
- use addons\CloudStock\common\models\CloudStockAgentOrder;
- use addons\CloudStock\common\models\CloudStockAgentOrderDetails;
- use common\helpers\FormatHelper;
- use yii\db\Migration;
- /**
- * Handles the creation of table `{{%cloud_stock_agent_order_details}}`.
- */
- class M241016065452CreateCloudStockAgentOrderDetailsTable extends Migration
- {
- /**
- * {@inheritdoc}
- */
- public function safeUp()
- {
- $sql = <<<SQL
- CREATE TABLE `qimall_addons_cloud_stock_agent_order_details` (
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
- `user_id` int(10) unsigned NOT NULL DEFAULT '0',
- `mall_id` int(10) unsigned NOT NULL DEFAULT '0',
- `goods_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商品id',
- `order_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '订单id',
- `num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '提货数量',
- `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '状态[-1:删除;0:禁用;1启用]',
- `created_at` int(10) unsigned NOT NULL DEFAULT '0',
- `updated_at` int(10) unsigned NOT NULL DEFAULT '0',
- PRIMARY KEY (`id`),
- KEY `order_id` (`order_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='提货详情表(支持批量提货)';
- SQL;
- $this->execute($sql);
- // 迁移qimall_addons_cloud_stock_agent_order到qimall_addons_cloud_stock_agent_order_details
- $select = 'id, mall_id, user_id, goods_id, num, status, created_at, updated_at';
- foreach (CloudStockAgentOrder::find()->select($select)->asArray()->batch(1000) as $batchItem) {
- $orderIds = array_column($batchItem, 'id');
- $findOrderIds = CloudStockAgentOrderDetails::find()
- ->where(['order_id' => $orderIds])
- ->select('order_id')
- ->column();
- $insertOrderIds = array_diff($orderIds, $findOrderIds);
- $batchItem = array_column($batchItem, null, 'id');
- $insertData = [];
- foreach ($insertOrderIds as $insertOrderId) {
- $order = $batchItem[$insertOrderId] ?? [];
- if (empty($order)) {
- continue;
- }
- $insertData[] = [
- 'user_id' => $order['user_id'],
- 'mall_id' => $order['mall_id'],
- 'goods_id' => $order['goods_id'],
- 'order_id' => $order['id'],
- 'num' => $order['num'],
- 'status' => $order['status'],
- 'created_at' => $order['created_at'],
- 'updated_at' => $order['updated_at'],
- ];
- }
- if ($insertData) {
- try {
- \Yii::$app->db->createCommand()->batchInsert(
- CloudStockAgentOrderDetails::tableName(),
- array_keys($insertData[0]),
- $insertData
- )->execute();
- }catch (\Exception $e) {
- \Yii::$app->custom->logs(FormatHelper::exception($e, '云库存自提订单迁移到details表失败'), $insertData);
- }
- }
- }
- }
- /**
- * {@inheritdoc}
- */
- public function safeDown()
- {
- return false;
- }
- }
|