cache; $cache->set($cache_key, 1); if ($data['clean_data'] == 1) { // 清理会员数据、订单数据 $res = $this->user($data['mall_id']); if ($res == false) throw new Exception('清除失败:' . $this->getError()); } elseif ($data['clean_data'] == 2) { // 清理订单数据 $res = $this->order($data['mall_id']); if ($res == false) throw new Exception('清除失败:' . $this->getError()); } // 清理插件数据 if (!empty($data['clean_addons'])) { $addons_name = $data['addons_name']; if (!empty($addons_name)) { foreach ($addons_name as $name) { $res = $this->addons($data['mall_id'], $data['clean_type'] == 1 ? false : true, $name); if ($res == false) throw new Exception('清除失败:' . $this->getError()); } } } $cache->set($cache_key, 2); return true; } catch (\Exception $e) { $cache->set($cache_key, 2); var_dump($e); return true; } } /** * 会员清理 * @param $mall_id * @return int */ public function user($mall_id) { try { $part = 1; $limit = 1000; $select = "id,mall_id"; $part_where = [['=', 'mall_id', $mall_id], ['=', 'status', StatusEnum::ENABLED]]; $params = ['where' => $part_where, 'select' => $select]; while ($part_data = User::batchGetUsers($params, $part, $limit)) { // 查询user表的 $ids = implode(',', array_column($part_data, 'id')); $delsql = []; if ($ids) { $delsql[] = "DELETE FROM `qimall_user_tag_relate` WHERE `user_id` IN({$ids});"; $delsql[] = "DELETE FROM `qimall_user_partition_relationship` WHERE `user_id` IN({$ids});"; $delsql[] = "DELETE FROM `qimall_user_relationship_log` WHERE `user_id` IN({$ids});"; $delsql[] = "DELETE FROM `qimall_user_extra` WHERE `user_id` IN({$ids});"; $delsql[] = "DELETE FROM `qimall_user_address` WHERE `user_id` IN({$ids});"; foreach ($delsql as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception("按会员ID清理,第{$part}页清理失败"); } } } $part++; } // 删除token (new AccessTokenService())->delAllCacheByMallId($mall_id, 'api'); $tables = Yii::$app->db->createCommand('show tables')->queryAll(); $tables_arr = array_column($tables, 'Tables_in_qimall'); // 会员数据sql组合 $sql_arr[] = "DELETE FROM `qimall_user` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_label_relationship` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_wallet` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_info` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_member_label_relation` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_member_label` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_import_fail_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_action_record` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_invite_code` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_extra` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_label` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_bank_card` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_level_expired` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_import_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_follow` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_digital` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_bank_card_sign` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_login_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_level_upgrade_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_del_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_behavior_logs` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_1` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_2` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_3` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_4` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_5` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_6` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_7` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_8` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_9` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_10` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_1` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_2` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_3` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_4` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_5` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_6` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_7` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_8` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_9` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_10` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_capital_statistics` WHERE `mall_id` = {$mall_id};"; if (in_array('qimall_user_team_month_statistics', $tables_arr)) { $sql_arr[] = "DELETE FROM `qimall_user_team_month_statistics` WHERE `mall_id` = {$mall_id};"; } if (in_array('qimall_user_team_pay_money_statistics', $tables_arr)) { $sql_arr[] = "DELETE FROM `qimall_user_team_pay_money_statistics` WHERE `mall_id` = {$mall_id};"; } $sql_arr[] = "DELETE FROM `qimall_user_team_performance_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('会员清理失败'); } } // 订单数据清理 $this->order($mall_id); // 资产清理 $this->cleanCapital($mall_id); // 统计清理 $this->clearUserStatistics($mall_id, 1); // 缓存清理 $redis = Yii::$app->redis; $user_keys = []; $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VIEWS, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VISITORS, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER_LEVEL, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, $mall_id); foreach ($user_keys as $key) { $redis->del($key); } return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } /** * 订单数据清除 * @param $mall_id * @return int */ public function order($mall_id) { try { $this->clearOrder($mall_id); $this->clearOrderStatistics($mall_id, 2); $this->cleanGoodsSales($mall_id); return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } public function capital($mall_id) { try { $part = 1; $limit = 1000; $select = "id,mall_id"; $part_where = [['=', 'mall_id', $mall_id], ['=', 'status', StatusEnum::ENABLED]]; $params = ['where' => $part_where, 'select' => $select]; // while ($part_data = User::batchGetUsers($params, $part, $limit)) { // 查询user表的 // $ids = implode(',', array_column($part_data, 'id')); // $delsql = []; // if ($ids) { // $delsql[] = "DELETE FROM `qimall_user_tag_relate` WHERE `user_id` IN({$ids});"; // // $delsql[] = "DELETE FROM `qimall_user_partition_relationship` WHERE `user_id` IN({$ids});"; // // $delsql[] = "DELETE FROM `qimall_user_relationship_log` WHERE `user_id` IN({$ids});"; // $delsql[] = "DELETE FROM `qimall_user_extra` WHERE `user_id` IN({$ids});"; // $delsql[] = "DELETE FROM `qimall_user_address` WHERE `user_id` IN({$ids});"; // foreach ($delsql as $sql) { // Yii::error(sprintf('执行数据清除语句: %s', $sql)); // $res = Yii::$app->db->createCommand($sql)->execute(); // if ($res === false) { // throw new \Exception("按会员ID清理,第{$part}页清理失败"); // } // } // } // $part++; // } //获取所有表格 $tables = Yii::$app->db->createCommand('show tables')->queryAll(); $tables_arr = array_column($tables, 'Tables_in_qimall'); // 删除token // (new AccessTokenService())->delAllCacheByMallId($mall_id, 'api'); // 会员数据sql组合 // $sql_arr[] = "DELETE FROM `qimall_user` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_label_relationship` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_wallet` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_info` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "UPDATE `qimall_user_wallet` SET `score`=0.00 , `frozen_score`=0.00 , `total_score`=0.00 , `total_use_score`=0.00 , `balance`=0.00 , `frozen_balance`=0.00 , `total_balance`=0.00 , `total_use_balance`=0.00 , `commission`=0.00 , `frozen_commission`=0.00 , `total_commission`=0.00 , `total_use_commission`=0.00 , `balance_withdrawal`=0.00 , `commission_withdrawal`=0.00 WHERE `mall_id` = {$mall_id}"; // $sql_arr[] = "DELETE FROM `qimall_user_member_label_relation` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_member_label` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_import_fail_log` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_action_record` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_invite_code` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_extra` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_label` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_bank_card` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_level_expired` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_import_log` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_follow` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_digital` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_bank_card_sign` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_login_log` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_user_level_upgrade_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_del_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_behavior_logs` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_achievement_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_achievement_statistics_total` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_1` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_2` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_3` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_4` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_5` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_6` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_7` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_8` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_9` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_10` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_1` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_2` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_3` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_4` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_5` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_6` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_7` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_8` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_9` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_10` WHERE `mall_id` = {$mall_id};"; if (in_array('qimall_user_team_month_statistics', $tables_arr)) { $sql_arr[] = "DELETE FROM `qimall_user_team_month_statistics` WHERE `mall_id` = {$mall_id};"; } if (in_array('qimall_user_team_pay_money_statistics', $tables_arr)) { $sql_arr[] = "DELETE FROM `qimall_user_team_pay_money_statistics` WHERE `mall_id` = {$mall_id};"; } $sql_arr[] = "DELETE FROM `qimall_user_team_performance_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('会员清理失败'); } } // 订单数据清理 $this->clearOrder($mall_id); $this->clearOrderStatistics($mall_id, 3); // 资产清理 $this->cleanCapital($mall_id); // 统计清理 $this->clearUserStatistics($mall_id, 3); // 缓存清理 $redis = Yii::$app->redis; $user_keys = []; $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VIEWS, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VISITORS, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER_LEVEL, $mall_id); $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, $mall_id); foreach ($user_keys as $key) { $redis->del($key); } return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } /** * @param $mall_id * @param $isMycat * @param $addons_name * @return bool * @throws \yii\db\Exception */ public function addons($mall_id, $isMycat = false, $addons_name = '') { try { // 获取所有表 $sql = ""; $tables = Yii::$app->db->createCommand('show tables')->queryAll(); $list = []; $addons_name_c = $addons_name; $string = 'Tables_in_qimall'; if (!empty($addons_name)) { $addons_name = '_' . StringHelper::toUnderScore($addons_name); } $addons_name = str_replace('-', '_', $addons_name); $key = $isMycat === false ? $string : str_replace('_', ' ', $string); // 所有表 foreach ($tables as $item) { $list[] = $item[$key]; } // 筛选出不可以被清空数据的插件表 $dont_clean_tables = [ 'qimall_addons_distribution_level', 'qimall_addons_agent_level', 'qimall_addons_area_community', 'qimall_addons_store_coupon_cat_relate', 'qimall_addons_store_coupon_goods_relate', 'qimall_addons_agent_distribution_setting', 'qimall_addons_agent_goods_setting', 'qimall_addons_agent_goods_repurchase', 'qimall_addons_agent_setting', 'qimall_addons_boss_bonus_level_upgrade_condition', 'qimall_addons_business_bonus_upgrade_condition', 'qimall_addons_setting', 'qimall_addons_order', 'qimall_addons_marketing_setting', 'qimall_addons_links', 'qimall_addons_event', 'qimall_addons_combo', 'qimall_addons_binding', 'qimall_addons', 'qimall_addons_invoker', 'qimall_addons_cloud_stock_upgrade_bag', 'qimall_addons_cloud_stock_goods', 'qimall_addons_cloud_stock_sms', 'qimall_addons_physical_stock_upgrade_bag', 'qimall_addons_physical_stock_goods', 'qimall_addons_physical_stock_sms', // 星链供应链 'qimall_addons_star_chain_category', 'qimall_addons_star_chain_express', 'qimall_addons_star_chain_goods', 'qimall_addons_star_chain_region', 'qimall_addons_star_chain_storage', 'qimall_addons_star_chain_goods_attr', // 七件事供应链 'qimall_addons_qijuhe_category', 'qimall_addons_qijuhe_express', 'qimall_addons_qijuhe_goods', 'qimall_addons_qijuhe_storage', 'qimall_addons_qijuhe_supplys', 'qimall_addons_qijuhe_goods_attr', // 聚合供应链 'qimall_addons_juhe_shop_category', 'qimall_addons_juhe_shop_express', 'qimall_addons_juhe_shop_goods', 'qimall_addons_juhe_shop_goods_attr', 'qimall_addons_juhe_shop_storage', 'qimall_addons_album', // 电子券 'qimall_addons_electron_coupon', 'qimall_addons_sudoku', 'qimall_addons_score_mall_goods', 'qimall_addons_score_mall_goods_attr', // 幸运拼团 'qimall_addons_lucky_group_activity', // 活动列表 'qimall_addons_lucky_group_goods', // 活动关联商品列表 'qimall_addons_lucky_group_level', // 权重等级列表 'qimall_addons_lucky_group_category', // 活动分类列表 ]; // 循环清除选择的插件表 foreach ($list as $item) { if (strpos($item, 'qimall_addons' . $addons_name) !== false && !in_array($item, $dont_clean_tables)) { // 插件等级跟配置不删除 if (strpos($item, 'level') !== false) continue; if (strpos($item, 'item_setting') !== false) continue; // 设置相关跳过 if (strpos($item, '_setting') !== false) continue; // 支付宝合规配置不删除 if (strpos($item, 'qimall_addons_tax_config') !== false) continue; if (strpos($item, 'qimall_addons_tax_account') !== false) continue; if (strpos($item, 'qimall_addons_tax_account_book') !== false) continue; // 1002 【清除数据】不要清除优惠券设置,还有推三返一的活动创建的设置 if (in_array($item, ['qimall_addons_coupon', 'qimall_addons_rebate'])) continue; $sql = "DELETE FROM `{$item}` WHERE `mall_id` = {$mall_id};"; try { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $e) { Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage())); } } } // 额外清除 $del_tables = []; // 签到 if (empty($addons_name_c) || $addons_name_c == 'SignIn') { $del_tables[] = 'qimall_addons_user_sign_in_award'; $del_tables[] = 'qimall_addons_user_sign_in_record'; $del_tables[] = 'qimall_addons_user_sign_in_award_record'; } // 渠道分红 if (empty($addons_name_c) || $addons_name_c == 'Agent') { $del_tables[] = 'qimall_agent_commission_details'; } // 二二复制 if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') { $del_tables[] = 'qimall_addons_double_copy_pool_level'; } // 股东分红 if (empty($addons_name_c) || $addons_name_c == 'Boss') { $del_tables[] = 'qimall_addons_boss_reward_log'; } // HiGo if (empty($addons_name_c) || $addons_name_c == 'HiGo') { $del_tables[] = 'qimall_addons_higo_hibei_log'; $del_tables[] = 'qimall_addons_higo_mall'; $del_tables[] = 'qimall_addons_higo_merchant_reward_log'; $del_tables[] = 'qimall_addons_higo_recharge_orders'; $del_tables[] = 'qimall_addons_higo_wallet'; $del_tables[] = 'qimall_addons_higo_wallet_freeze_log'; $del_tables[] = 'qimall_addons_higo_wallet_frozen_log'; $del_tables[] = 'qimall_addons_higo_wallet_log'; $del_tables[] = 'qimall_addons_higo_withdraw_log'; $del_tables[] = 'qimall_addons_higo_store_goods_mode'; } foreach ($del_tables as $table) { // if (in_array($table, $tables)) { $sql = "DELETE FROM `{$table}` WHERE `mall_id` = {$mall_id};"; try { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $e) { Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage())); } // } } // O2O专项清除 if (empty($addons_name_c) || $addons_name_c == 'Store') { $this->claerStoreRedis($mall_id); } // 多商户清除 if (empty($addons_name_c) || $addons_name_c == 'Mch') { // 清除门店商品 $sql = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id} AND `mch_id` > 0"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } // 服务商分红 if (empty($addons_name_c) || $addons_name_c == 'ServeBonus') { $sql = "UPDATE `qimall_addons_servebonus_level` SET `serve_bonus_number`=0 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } // 多商户清除 if (empty($addons_name_c) || $addons_name_c == 'PublicLine') { // 清除门店商品 $sql = "DELETE FROM `qimall_commission_log` WHERE `mall_id` = {$mall_id} AND `from_type` = 'PublicLine'"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } // 删除商品关联插件 $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id}"; if ($addons_name_c) { $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id} AND `marketing_type` = '{$addons_name_c}'"; } if (!empty($addons_name_c) && $addons_name_c == 'ScoreMall') $sql = '';// 积分商城商品绑定了主商城商品所以不删除 if ($sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } //数据清除插件调用清除插件数据 public function clearDataAddons($mall_id, $isMycat = false, $addons_name = '') { try { // 获取所有表 $sql = ""; $tables = Yii::$app->db->createCommand('show tables')->queryAll(); $list = []; $addons_name_c = $addons_name; $string = 'Tables_in_qimall'; if (!empty($addons_name)) { if ($addons_name == 'HuifuPay') { $addons_name = '_' . strtolower($addons_name); } else { $addons_name = '_' . StringHelper::toUnderScore($addons_name); } } $addons_name = str_replace('-', '_', $addons_name); $key = $isMycat === false ? $string : str_replace('_', ' ', $string); // 所有表 foreach ($tables as $item) { $list[] = $item[$key]; } // 筛选出不可以被清空数据的插件表 $dont_clean_tables = [ //业绩奖励 'qimall_addons_achievement_user', 'qimall_addons_achievement_commission_item_setting', 'qimall_addons_achievement_counting_goods', //分销商 'qimall_addons_distribution', 'qimall_addons_distribution_level', 'qimall_addons_distribution_commission_level_setting', 'qimall_addons_distribution_commission_item_setting', 'qimall_addons_distribution_commission_item_setting_detail', 'qimall_addons_distribution_goods_setting', //渠道分红 'qimall_addons_agent_level','qimall_addons_agent', 'qimall_addons_agent_distribution_setting', 'qimall_addons_agent_goods_setting', 'qimall_addons_agent_goods_repurchase', 'qimall_addons_agent_setting', 'qimall_addons_agent_commission_item_setting', 'qimall_addons_agent_commission_item_setting_detail', 'qimall_addons_agent_commission_level_setting', //社区团购 'qimall_addons_community_head', 'qimall_addons_community_head_apply', 'qimall_addons_community_goods_mode_setting', 'qimall_addons_community_goods_setting', //二二复制 'qimall_addons_double_copy_user', 'qimall_addons_double_copy_level', 'qimall_addons_double_copy_level_setting', 'qimall_addons_double_copy_pool_user', 'qimall_addons_double_copy_pool_level', 'qimall_addons_double_copy_relation', 'qimall_addons_double_copy_commission_item_setting', 'qimall_addons_double_copy_commission_item_setting_detail', 'qimall_addons_double_copy_commitment_award_goods_setting', //汇付支付 'qimall_addons_huifupay_user_bankcard', 'qimall_addons_logistics_express', 'qimall_addons_area_agent','qimall_addons_cloud_stock_agent', 'qimall_addons_area_community','qimall_addons_area_regional', 'qimall_addons_store_coupon_cat_relate', 'qimall_addons_store_coupon_goods_relate', 'qimall_addons_boss_bonus_level_upgrade_condition', 'qimall_addons_business_bonus_upgrade_condition', 'qimall_addons_setting', 'qimall_addons_order', 'qimall_addons_marketing_setting', 'qimall_addons_links', 'qimall_addons_event', 'qimall_addons_combo', 'qimall_addons_binding', 'qimall_addons', 'qimall_addons_invoker', 'qimall_addons_cloud_stock_upgrade_bag', 'qimall_addons_cloud_stock_goods', 'qimall_addons_cloud_stock_sms', 'qimall_addons_physical_stock_upgrade_bag', 'qimall_addons_physical_stock_goods', 'qimall_addons_physical_stock_sms', // 星链供应链 'qimall_addons_star_chain_category', 'qimall_addons_star_chain_express', 'qimall_addons_star_chain_goods', 'qimall_addons_star_chain_region', 'qimall_addons_star_chain_storage', 'qimall_addons_star_chain_goods_attr', // 七件事供应链 'qimall_addons_qijuhe_category', 'qimall_addons_qijuhe_express', 'qimall_addons_qijuhe_goods', 'qimall_addons_qijuhe_storage', 'qimall_addons_qijuhe_supplys', 'qimall_addons_qijuhe_goods_attr', // 聚合供应链 'qimall_addons_juhe_shop_category', 'qimall_addons_juhe_shop_express', 'qimall_addons_juhe_shop_goods', 'qimall_addons_juhe_shop_goods_attr', 'qimall_addons_juhe_shop_storage', 'qimall_addons_album', // 电子券 'qimall_addons_electron_coupon', 'qimall_addons_sudoku', 'qimall_addons_score_mall_goods', 'qimall_addons_score_mall_goods_attr','qimall_addons_redpack_wallet', // 幸运拼团 'qimall_addons_lucky_group_activity', // 活动列表 'qimall_addons_lucky_group_goods', // 活动关联商品列表 'qimall_addons_lucky_group_level', // 权重等级列表 'qimall_addons_lucky_group_category', // 活动分类列表 ]; // 循环清除选择的插件表 foreach ($list as $item) { if (strpos($item, 'qimall_addons' . $addons_name) !== false && !in_array($item, $dont_clean_tables)) { // 插件等级跟配置不删除 if (strpos($item, 'level') !== false) continue; if (strpos($item, 'item_setting') !== false) continue; // 设置相关跳过 if (strpos($item, '_setting') !== false) continue; // 支付宝合规配置不删除 if (strpos($item, 'qimall_addons_tax_config') !== false) continue; if (strpos($item, 'qimall_addons_tax_account') !== false) continue; if (strpos($item, 'qimall_addons_tax_account_book') !== false) continue; // 1002 【清除数据】不要清除优惠券设置,还有推三返一的活动创建的设置 if (in_array($item, ['qimall_addons_rebate'])) continue; $sql = "DELETE FROM `{$item}` WHERE `mall_id` = {$mall_id};"; // echo $sql . "\r\n"; try { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $e) { Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage())); } } } // exit; // 额外清除 $del_tables = []; // 签到 if (empty($addons_name_c) || $addons_name_c == 'SignIn') { $del_tables[] = 'qimall_addons_user_sign_in_award'; $del_tables[] = 'qimall_addons_user_sign_in_record'; $del_tables[] = 'qimall_addons_user_sign_in_award_record'; } // 渠道分红 if (empty($addons_name_c) || $addons_name_c == 'Agent') { $del_tables[] = 'qimall_agent_commission_details'; } // 二二复制 // if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') { // $del_tables[] = 'qimall_addons_double_copy_pool_level'; // } // 股东分红 if (empty($addons_name_c) || $addons_name_c == 'Boss') { $del_tables[] = 'qimall_addons_boss_reward_log'; } // HiGo if (empty($addons_name_c) || $addons_name_c == 'HiGo') { $del_tables[] = 'qimall_addons_higo_hibei_log'; $del_tables[] = 'qimall_addons_higo_mall'; $del_tables[] = 'qimall_addons_higo_merchant_reward_log'; $del_tables[] = 'qimall_addons_higo_recharge_orders'; $del_tables[] = 'qimall_addons_higo_wallet'; $del_tables[] = 'qimall_addons_higo_wallet_freeze_log'; $del_tables[] = 'qimall_addons_higo_wallet_frozen_log'; $del_tables[] = 'qimall_addons_higo_wallet_log'; $del_tables[] = 'qimall_addons_higo_withdraw_log'; $del_tables[] = 'qimall_addons_higo_store_goods_mode'; } foreach ($del_tables as $table) { // if (in_array($table, $tables)) { $sql = "DELETE FROM `{$table}` WHERE `mall_id` = {$mall_id};"; try { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $e) { Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage())); } // } } // O2O专项清除 if (empty($addons_name_c) || $addons_name_c == 'Store') { $this->claerStoreRedis($mall_id); } //渠道管理 if (empty($addons_name_c) || $addons_name_c == 'Agent') { $sql = "UPDATE `qimall_addons_agent` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //分销商 if (empty($addons_name_c) || $addons_name_c == 'Distribution') { $sql = "UPDATE `qimall_addons_distribution` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //区域管理 if (empty($addons_name_c) || $addons_name_c == 'Area') { $sql = "UPDATE `qimall_addons_area_agent` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //云库存 if (empty($addons_name_c) || $addons_name_c == 'CloudStock') { $sql = "UPDATE `qimall_addons_cloud_stock_agent` SET `total_price`=0.00 , `price`=0.00, `frozen_price` = 0.00, `stock` = 0 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); $sql = "DELETE FROM `qimall_addons_cloud_stock_agent_goods` WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //红包拓客 if (empty($addons_name_c) || $addons_name_c == 'Redpack') { $sql = "UPDATE `qimall_addons_redpack_wallet` SET `redpack`=0.00 , `frozen_redpack`=0.00 , `total_redpack`=0.00, `total_frozen_redpack` = 0.00, `total_use_redpack` = 0.00, `total_use_frozen_redpack` = 0.00 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } // 多商户清除 // if (empty($addons_name_c) || $addons_name_c == 'Mch') { // // 清除门店商品 // $sql = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id} AND `mch_id` > 0"; // Yii::error(sprintf('执行数据清除语句: %s', $sql)); // Yii::$app->db->createCommand($sql)->execute(); // } // 服务商分红 if (empty($addons_name_c) || $addons_name_c == 'ServeBonus') { $sql = "UPDATE `qimall_addons_servebonus_level` SET `serve_bonus_number`=0 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //业绩奖励 if (empty($addons_name_c) || $addons_name_c == 'Achievement') { $sql = "UPDATE `qimall_addons_achievement_user` SET `total_commission`=0 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } //二二复制 if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') { $sql = "UPDATE `qimall_addons_double_copy_user` SET `commission`=0, `frozen_commission`=0, `frozen_fund`=0, `help_num`=0, `thaw_percent`=0, `achievement`=0, `score`=0, `frozen_score`=0, `frozen_fund_score`=0 WHERE `mall_id` = {$mall_id}"; Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } // 删除商品关联插件 // $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id}"; // if ($addons_name_c) { // $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id} AND `marketing_type` = '{$addons_name_c}'"; // } if (!empty($addons_name_c) && $addons_name_c == 'ScoreMall') $sql = '';// 积分商城商品绑定了主商城商品所以不删除 if ($sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); Yii::$app->db->createCommand($sql)->execute(); } return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } /** * 删除门店数据概览redis缓存 * * @param integer $mall_id * @return void */ protected function claerStoreRedis(int $mall_id) { $keys = [ StoreEnum::STORE_NOT_SETTLE_AMOUNTS, StoreEnum::STORE_SETTLE_AMOUNTS, StoreEnum::STORE_PAY_PEOPLE_NUMBERS, StoreEnum::STORE_PAY_ORDER_NUMBERS, StoreEnum::STORE_REAL_PAY_AMOUNTS, StoreEnum::STORE_WAITING_AUDIT_STORE_NUMBERS, ]; $redis = Yii::$app->redis; foreach ($keys as $key) { $redis->del($key . ':' . $mall_id); } } public function clearUserStatistics($mall_id, $clear_data = 1) { if ($clear_data == 3) { $sql_arr[] = "UPDATE `qimall_user_team_statistics` SET `user_team_order_total`=0, `user_direct_order_total`=0, `user_indirect_order_total`=0, `team_finish_order_total` = 0 WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "UPDATE `qimall_mall_statistics` SET `order_total` = 0, `order_price_total` = 0 WHERE `mall_id` = {$mall_id};"; } else { $sql_arr[] = "DELETE FROM `qimall_user_team_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_mall_statistics` WHERE `mall_id` = {$mall_id};"; } $sql_arr[] = "DELETE FROM `qimall_user_statistics_total` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_behaviour_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_global_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_capital_statistics` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_census_mall_view` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_user_source` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_user_level_single_day` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_user_level` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_goods` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_capital` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_census_mall` WHERE `mall_id` = {$mall_id};"; // $sql_arr[] = "DELETE FROM `qimall_census_mall_single_day` WHERE `mall_id` = {$mall_id};"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('会员统计清理失败'); } } $this->clearOrderStatistics($mall_id, $clear_data); } /** * 资产清理 * @param $mall_id * @return void * @throws \yii\db\Exception */ public function cleanCapital($mall_id) { $sql_arr[] = "DELETE FROM `qimall_commission_years_billing` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_commission_month_billing` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_commission_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_commission_frozen_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_balance_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_balance_frozen_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_score_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_score_frozen_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_digital_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};"; // 提现数据清理 foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('资产清理失败'); } } } /** * 订单清理 * @param $mall_id * @return void * @throws \yii\db\Exception */ public function clearOrder($mall_id) { $part = 1; $limit = 1000; $select = "id,mall_id"; while ($part_data = Order::find()->select($select)->where(['mall_id' => $mall_id])->orderBy('id asc') ->offset(($part - 1) * $limit)->limit($limit)->asArray()->all()) { // 查询user表的 $order_ids = implode(',', array_column($part_data, 'id')); $delsql = []; if ($order_ids) { // 订单详情ID $details = OrderDetail::find()->select('id,order_id,mall_id')->where(['mall_id' => $mall_id, 'order_id' => $order_ids])->asArray()->all(); $detail_ids = implode(',', array_column($details, 'id')); $delsql[] = "DELETE FROM `qimall_order_marketing` WHERE `order_detail_id` IN({$detail_ids});"; $delsql[] = "DELETE FROM `qimall_order_express` WHERE `order_id` IN({$order_ids});"; $delsql[] = "DELETE FROM `qimall_order_express_log` WHERE `order_id` IN({$order_ids});"; $delsql[] = "DELETE FROM `qimall_order_action` WHERE `order_id` IN({$order_ids});"; foreach ($delsql as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception("按订单ID清理,第{$part}页清理失败"); } } } $part++; } $refund_part = 1; $refund_limit = 1000; $refund_select = "id,mall_id"; while ($refund_data = OrderRefund::find()->select($refund_select)->where(['mall_id' => $mall_id])->orderBy('id asc') ->offset(($refund_part - 1) * $refund_limit)->limit($refund_limit)->asArray()->all()) { // 查询user表的 $refund_ids = implode(',', array_column($refund_data, 'id')); if ($refund_ids) { $refundsql = "DELETE FROM `qimall_order_refund_step` WHERE `order_refund_id` IN({$refund_ids});"; Yii::error(sprintf('执行数据清除语句: %s', $refundsql)); $res = Yii::$app->db->createCommand($refundsql)->execute(); if ($res === false) { throw new \Exception("按售后订单ID清理,第{$part}页清理失败"); } } $refund_part++; } $sql_arr[] = "DELETE FROM `qimall_order_user_handle` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_unread_msg` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_refund` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_invoice` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_express_log` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_detail` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_comments` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_batch_delivery_error` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order_batch_delivery` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_order` WHERE `mall_id` = {$mall_id};"; //支付单 $sql_arr[] = "DELETE FROM `qimall_payment_refund` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_trade` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_trade_item` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_trade_item_refund` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_transfer` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_details` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_logs` WHERE `mall_id` = {$mall_id};"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('clearOrder:订单清理失败'); } } } /** * 订单统计清理 * @param integer $mall_id * @param integer $type 类型[1=清理会员,2=清理订单] * @return void * @throws \yii\db\Exception */ public function clearOrderStatistics($mall_id, $type = 1) { $sql_arr[] = "DELETE FROM `qimall_user_statistics_total` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_user_goods_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_region_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_user` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_single_month` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_single_hour` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_rm_analysis` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_rf_analysis` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_region` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_purchasing_power` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_payment_single_month` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_analysis` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_logs` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_details` WHERE `mall_id` = {$mall_id};"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('clearOrderStatistics:订单清理失败'); } } // 清理缓存 $redis = Yii::$app->redis; $data_keys = [ RedisKeyEnum::CENSUS_MALL_SINGLE_DATE, RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH, RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH, ]; foreach ($data_keys as $key_enum) { $keys = $this->getKey($key_enum, $mall_id) . "*"; $keys_arr = $redis->keys($keys); foreach ($keys_arr as $item) { $redis->del($item); } } $order_keys = []; $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_REGION, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_PAY_MONEY, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_CARD_NUMBERS, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, $mall_id); $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_BUY_POWER, $mall_id); // $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH, $mall_id); // $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH, $mall_id); foreach ($order_keys as $key) { $redis->del($key); } if ($type > 1) { $this->setOrderStatistics($mall_id); } } /** * 订单统计重置 * @param $mall_id * @return void * @throws \yii\db\Exception */ public function setOrderStatistics($mall_id) { $sql_arr[] = "UPDATE `qimall_user_team_statistics` SET `user_team_order_total`=0, `user_direct_order_total`=0, `user_indirect_order_total`=0, `user_team_order_price_total`=0,`user_direct_order_price_total`=0,`user_indirect_order_price_total`=0,`team_finish_order_total`=0,`team_finish_order_price_total`=0 WHERE `mall_id` = {$mall_id}"; $sql_arr[] = "UPDATE `qimall_census_mall_single_day` SET `total_order_money`=0,`total_pay_money`=0,`add_orders`=0,`add_pay_orders`=0,`add_pay_nums`=0,`add_pay_people_nums`=0,`add_refund_orders`=0,`add_users`=0,`total_views`=0,`total_visitors`=0 WHERE `mall_id` = {$mall_id}"; $sql_arr[] = "UPDATE `qimall_census_mall` SET `total_orders`=0,`total_pay_orders`=0,`total_pay_people`=0,`number_of_orders`=0,`repurchase_people`=0,`new_customers`=0,`regular_customers`=0,`new_customers_pay_peoples`=0,`regular_customers_pay_peoples`=0,`total_order_money`=0,`total_pay_money`=0,`repurchase_money`=0,`new_customers_pay_money`=0,`regular_customers_pay_money`=0,`total_users`=0,`total_visitors`=0,`share_people`=0,`total_views`=0 WHERE `mall_id` = {$mall_id}"; $sql_arr[] = "UPDATE `qimall_census_mall_goods` SET `total_sales_volume`=0,`total_pay_money`=0,`add_card_numbers`=0 WHERE `mall_id` = {$mall_id}"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行数据清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('setOrderStatistics:订单统计修改失败'); } } } private function getKey($key, $mall_id) { return RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id; } /** * 商品清理-要求清理商品时可使用 * @Author:lun * @Date:2023-11-27 10:46 * @Copyright:copyright(c)2023 广东七件事集团 * @param $mall_id * @return void */ public function cleanGoods($mall_id) { $part = 1; $limit = 1000; $select = "id,mall_id"; while ($part_data = Goods::find()->select($select)->where(['mall_id' => $mall_id])->orderBy('id asc') ->offset(($part - 1) * $limit)->limit($limit)->asArray()->all()) { // 查询goods表的 $goods_ids = implode(',', array_column($part_data, 'id')); $delsql = []; if ($goods_ids) { // 商品规格ID $delsql[] = "DELETE FROM `qimall_goods_attr` WHERE `goods_id` IN({$goods_ids});"; $delsql[] = "DELETE FROM `qimall_goods_cate_relate` WHERE `goods_id` IN({$goods_ids});"; $delsql[] = "DELETE FROM `qimall_goods_service_relate` WHERE `goods_id` IN({$goods_ids});"; foreach ($delsql as $sql) { Yii::error(sprintf('执行商品规格清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception("按商品规格ID清理,第{$part}页清理失败"); } } } $part++; } $sql_arr[] = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_cate` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_collect` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_decoration_template` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_decoration_template_content` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_extra` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_label` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_service` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_specs` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_statistics` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_goods_template_relation` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "DELETE FROM `qimall_census_mall_goods` WHERE `mall_id` = {$mall_id};"; $sql_arr[] = "UPDATE `qimall_census_mall` SET `goods_listings`=0,`goods_total_visitors`=0,`goods_total_views`=0,`add_goods`=0 WHERE `mall_id` = {$mall_id}"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行商品清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('cleanGoods:商品清理失败'); } } } /** * 清理商品销量 * * @param integer $mall_id * @return void */ public function cleanGoodsSales(int $mall_id) { // 销量 $sql_arr[] = "UPDATE `qimall_goods` SET `sales_num` = 0 WHERE `mall_id` = {$mall_id}"; foreach ($sql_arr as $sql) { Yii::error(sprintf('执行商品销量清除语句: %s', $sql)); $res = Yii::$app->db->createCommand($sql)->execute(); if ($res === false) { throw new \Exception('cleanGoods:商品清理失败'); } } } }