db->createCommand($sql)->execute(); $sql = "ALTER TABLE xhAdminRole ADD shopId INT NOT NULL DEFAULT 0 COMMENT '门店id' AFTER `sjId`;"; Yii::$app->db->createCommand($sql)->execute(); $list = SjClass::getAllByCondition(['id>' => 0], null, '*', null, true); if (!empty($list)) { foreach ($list as $sj) { $sjId = $sj->id ?? 0; if (empty($sjId)) { continue; } $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true); if (empty($shopList)) { continue; } //补花材 $classList = ItemClassClass::getAllByCondition(['sjId' => $sjId], null, '*'); if (!empty($classList)) { foreach ($shopList as $shop) { $shopId = $shop->id ?? 0; foreach ($classList as $class) { $classId = $class['id'] ?? 0; $name = $class['name'] ?? ''; $inTurn = $class['inTurn'] ?? ''; $isDefault = $class['isDefault'] ?? 0; $data = [ 'shopId' => $shopId, 'sjId' => $sjId, 'name' => $name, 'inTurn' => $inTurn, 'isDefault' => $isDefault, ]; $r = ItemClassClass::add($data, true); $newId = $r->id ?? 0; ItemClass::updateByCondition(['shopId' => $shopId, 'classId' => $classId], ['classId' => $newId]); } } } //补角色 $roleList = AdminRoleClass::getAllByCondition(['sjId' => $sjId], null, '*'); if (!empty($roleList)) { foreach ($shopList as $shop) { $shopId = $shop->id ?? 0; foreach ($roleList as $role) { $roleName = $role['roleName'] ?? ''; $roleId = $role['id'] ?? 0; $roleData = [ 'shopId' => $shopId, 'sjId' => $sjId, 'roleName' => $roleName, ]; $new = AdminRoleClass::add($roleData, true); $newId = $new->id ?? 0; ShopAdminClass::updateByCondition(['shopId' => $shopId, 'roleId' => $roleId], ['roleId' => $newId]); } } } } } } //初始化 ./yii init/main public function actionMain() { //基地批发商 $jdShopList = ShopClass::getAllByCondition(['ptStyle' => dict::getDict('ptStyle', 'kmGhs')], 'default DESC', '*', null, true); if (!empty($jdShopList)) { foreach ($jdShopList as $jdShop) { $main = MainClass::add(['id' => null], true); $mainId = $main->id ?? 0; if (empty($mainId)) { echo "mainId没有生成 \n"; exit(); } $jdShop->mainId = $mainId; $jdShop->save(); } } //批发商 $pfShopList = ShopClass::getAllByCondition(['ptStyle' => dict::getDict('ptStyle', 'ghs')], 'default DESC', '*', null, true); if (!empty($pfShopList)) { foreach ($pfShopList as $pfShop) { $pfShopId = $pfShop->id ?? 0; $main = MainClass::add(['id' => null], true); $mainId = $main->id ?? 0; if (empty($mainId)) { echo "mainId没有生成 \n"; exit(); } $pfShop->mainId = $mainId; $pfShop->save(); //将shop资产信息迁移到main self::migration($main, $pfShop); //员工数据迁移到currentGhsShopId,删除零售和基地供应商的管理员信息 $staffList = ShopAdminClass::getAllByCondition(['shopId' => $pfShopId], null, '*'); if (!empty($staffList)) { foreach ($staffList as $staff) { $pfAdminId = $staff['adminId'] ?? 0; $pfAdmin = AdminClass::getById($pfAdminId, true); if (!empty($pfAdmin)) { if (empty($pfAdmin->currentGhsShopId)) { $pfAdmin->currentGhsShopId = $pfShopId; $pfAdmin->ghsMiniOpenId = $pfAdmin->miniOpenId ?? ''; $pfAdmin->miniOpenId = ''; $pfAdmin->currentShopId = 0; $pfAdmin->save(); } } else { echo "没有找到shopId:{$pfShopId}批发店的员工\n"; } $staffMobile = $staff['mobile'] ?? ''; //删除和批发店用一样手机号的 零售和基地供应商 员工 if (!empty($staffMobile)) { $h = AdminClass::getAllByCondition(['mobile' => $staffMobile, 'style' => dict::getDict('ptStyle', 'hd')], null, '*', null, true); if (!empty($h)) { foreach ($h as $he) { $currentAdminId = $he->id ?? 0; ShopAdminClass::deleteByCondition(['adminId' => $currentAdminId]); $he->delete(); } } $j = AdminClass::getAllByCondition(['mobile' => $staffMobile, 'style' => dict::getDict('ptStyle', 'kmGhs')], null, '*', null, true); if (!empty($j)) { foreach ($j as $je) { $currentAdminId = $je->id ?? 0; ShopAdminClass::deleteByCondition(['adminId' => $currentAdminId]); $je->delete(); } } } } } //花材和分类,零售和批发都有,单独拎出来 ItemClass::updateByCondition(['shopId' => $pfShopId], ['mainId' => $mainId]); ItemClassClass::updateByCondition(['shopId' => $pfShopId], ['mainId' => $mainId]); self::setCommon($pfShopId, $mainId); self::setGhs($pfShopId, $mainId); } } //零售 $lsShopList = ShopClass::getAllByCondition(['ptStyle' => dict::getDict('ptStyle', 'hd')], 'default DESC', '*', null, true); if (!empty($lsShopList)) { foreach ($lsShopList as $lsShop) { $main = MainClass::add(['id' => null], true); $mainId = $main->id ?? 0; if (empty($mainId)) { echo "mainId没有生成 \n"; exit(); } $lsShop->mainId = $mainId; $lsShop->save(); $lsShopId = $lsShop->id ?? 0; $lsSjId = $lsShop->sjId ?? 0; //将shop资产信息迁移到main self::migration($main, $lsShop); $staffList = ShopAdminClass::getAllByCondition(['shopId' => $lsShopId], null, '*'); if (!empty($staffList)) { foreach ($staffList as $staff) { $adminId = $staff['adminId'] ?? 0; $admin = AdminClass::getById($adminId, true); if (!empty($admin)) { if (empty($admin->currentShopId)) { $admin->currentShopId = $lsShopId; $admin->save(); } } } } //花材和分类,零售和批发都有,单独拎出来 ItemClass::updateByCondition(['shopId' => $lsShopId], ['mainId' => $mainId]); ItemClassClass::updateByCondition(['shopId' => $lsShopId], ['mainId' => $mainId]); self::setCommon($lsShopId, $mainId); self::setHd($lsSjId, $lsShopId, $mainId); } } //清除零售商城相关的数据 $sql = "TRUNCATE xhUser;TRUNCATE xhCustom;truncate xhUserAsset;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "UPDATE xhOrder SET customId=0,customName='',alipayId=0;"; Yii::$app->db->createCommand($sql)->execute(); //补已经存在花店的默认客户 $lsShopList = ShopClass::getAllByCondition(['ptStyle' => dict::getDict('ptStyle', 'hd')], null, '*', null, true); if (!empty($lsShopList)) { foreach ($lsShopList as $lsShop) { $mobile = $lsShop->mobile ?? 0; if (empty($mobile) || stringUtil::isMobile($mobile) == false) { continue; } $user = UserClass::replaceUser(['mobile' => $mobile]); $custom = \bizHd\custom\classes\CustomClass::replaceCustom($lsShop, $user); $customId = $custom->id ?? 0; $lsShop->defaultCustomId = $customId; $lsShop->save(); } } //欠款字段迁移 $sql = 'UPDATE xhGhsOrder SET debtPrice=actPrice,remainDebtPrice=actPrice WHERE debt=1'; Yii::$app->db->createCommand($sql)->execute(); $sql = 'UPDATE xhCg SET debtPrice=actPrice,remainDebtPrice=actPrice WHERE debt=1;'; Yii::$app->db->createCommand($sql)->execute(); self::migrationDel(); //客户和供应商的中央化 self::ghsCustomMain(); } //客户和供应商的中央化 ssh 20220506 public static function ghsCustomMain() { $map = []; $allShop = ShopClass::getAllByCondition(['id>' => 0], null, '*', null, true); if (!empty($allShop)) { foreach ($allShop as $single) { $mainId = $single->mainId ?? 0; $shopId = $single->id ?? 0; $map[$shopId] = $mainId; } } $customList = CustomClass::getAllByCondition(['id>' => 0], null, '*', null, true); if (!empty($customList)) { foreach ($customList as $custom) { $shopId = $custom->shopId ?? 0; $mainId = $map[$shopId] ?? 0; $custom->mainId = $mainId; $ownShopId = $custom->ownShopId ?? 0; $ownMainId = $map[$ownShopId] ?? 0; $custom->ownMainId = $ownMainId; $custom->save(); } } $ghsList = GhsClass::getAllByCondition(['id>' => 0], null, '*', null, true); foreach ($ghsList as $ghs) { $shopId = $ghs->shopId ?? 0; $mainId = $map[$shopId] ?? 0; $ghs->mainId = $mainId; $ownShopId = $ghs->ownShopId ?? 0; $ownMainId = $map[$ownShopId] ?? 0; $ghs->ownMainId = $ownMainId; $ghs->save(); } } public static function setCommon($shopId, $mainId) { ShopAdminClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); CashClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); AdminRoleClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); CheckOrderClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatPdAddClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatPdAddMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatPdSubClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatPdSubMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); } public static function setGhs($shopId, $mainId) { StatSaleClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatSaleMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatUserClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatUserMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatVisitClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatVisitMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatOutClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatOutMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); PtYeChangeClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); ShopYeChangeClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); RechargeClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatYjClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatYjMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatOrderClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatOrderMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatOrderCountClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); RefundOrderClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); //采购统计 StatCgClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatCgMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatCgGhsClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatCgGhsMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); CustomClass::updateByCondition(['ownShopId' => $shopId], ['ownMainId' => $mainId]); StatStockOutClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatStockOutMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatStockInClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatStockInMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatRefundClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); StatRefundMonthClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); CgClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); XjClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); //报损中央化 StockWastageOrderClass::updateByCondition(['shopId' => $shopId], ['mainId' => $mainId]); $wastageList = StockWastageOrderClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true); if (!empty($wastageList)) { foreach ($wastageList as $wastage) { $wastageOrderSn = $wastage->orderSn ?? 0; StockWastageOrderItemClass::updateByCondition(['orderSn' => $wastageOrderSn], ['mainId' => $mainId]); } } } public static function setHd($sjId, $shopId, $mainId) { GoodsClass::updateByCondition(['sjId' => $sjId], ['mainId' => $mainId]); GoodsCategoryClass::updateByCondition(['sjId' => $sjId], ['mainId' => $mainId]); CategoryClass::updateByCondition(['sjId' => $sjId], ['mainId' => $mainId]); GoodsSettingClass::updateByCondition(['sjId' => $sjId], ['mainId' => $mainId]); FestRiseClass::updateByCondition(['sjId' => $sjId], ['mainId' => $mainId]); GhsClass::updateByCondition(['ownShopId' => $shopId], ['ownMainId' => $mainId]); } public static function migration($main, $shop) { //shop数据迁移到main,保存之后请在migrationDel()增加对应字段!!!!!! $main->balance = $shop->balance ?? 0; $main->freezeBalance = $shop->freezeBalance ?? 0; $main->txBalance = $shop->txBalance ?? 0; $main->fixBalance = $shop->fixBalance ?? 0; $main->totalRecharge = $shop->totalRecharge ?? 0; $main->totalIncome = $shop->totalIncome ?? 0; $main->totalExpend = $shop->totalExpend ?? 0; $main->totalPurchase = $shop->totalPurchase ?? 0; $main->totalItem = $shop->totalItem ?? 0; $main->totalSale = $shop->totalSale ?? 0; $main->totalStockIn = $shop->totalStockIn ?? 0; $main->totalStockOut = $shop->totalStockOut ?? 0; $main->totalRefund = $shop->totalRefund ?? 0; $main->totalWast = $shop->totalWast ?? 0; $main->totalPdSub = $shop->totalPdSub ?? 0; $main->totalPdAdd = $shop->totalPdAdd ?? 0; $main->totalPurchaseOrder = $shop->totalPurchaseOrder ?? 0; $main->totalUser = $shop->totalUser ?? 0; $main->totalVisit = $shop->totalVisit ?? 0; $main->totalView = $shop->totalView ?? 0; $main->cashName = $shop->cashName ?? ''; $main->cashAccount = $shop->cashAccount ?? ''; $main->cashBank = $shop->cashBank ?? ''; $main->cgUnPay = $shop->cgUnPay ?? 0; $main->cgUnSend = $shop->cgUnSend ?? 0; $main->cgSending = $shop->cgSending ?? 0; $main->cgFinish = $shop->cgFinish ?? 0; $main->cgCancel = $shop->cgCancel ?? 0; $main->mayGatheringNum = $shop->mayGatheringNum ?? 0; $main->mayGathering = $shop->mayGathering ?? 0; $main->mayPay = $shop->mayPay ?? 0; $main->mayPayNum = $shop->mayPayNum ?? 0; $main->totalOrder = $shop->totalOrder ?? 0; $main->totalOrderNum = $shop->totalOrderNum ?? 0; $main->finishOrder = $shop->finishOrder ?? 0; $main->unPayOrder = $shop->unPayOrder ?? 0; $main->unSendOrder = $shop->unSendOrder ?? 0; $main->sendingOrder = $shop->sendingOrder ?? 0; $main->cancelOrder = $shop->cancelOrder ?? 0; $main->save(); } //迁移完成之后删除xhShop对应字段 public static function migrationDel() { $sql = "ALTER TABLE `xhShop` DROP COLUMN `balance`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `freezeBalance`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `txBalance`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `fixBalance`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalRecharge`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalIncome`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalExpend`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalPurchase`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalItem`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalSale`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalStockIn`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalStockOut`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalRefund`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalWast`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalPdSub`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalPdAdd`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalPurchaseOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalUser`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalVisit`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalView`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cashName`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cashAccount`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cashBank`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cgUnPay`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cgUnSend`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cgSending`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cgFinish`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cgCancel`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `mayGathering`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `mayGatheringNum`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `mayPay`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `mayPayNum`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `totalOrderNum`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `finishOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `unPayOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `unSendOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `sendingOrder`;"; Yii::$app->db->createCommand($sql)->execute(); $sql = "ALTER TABLE `xhShop` DROP COLUMN `cancelOrder`;"; Yii::$app->db->createCommand($sql)->execute(); } }