CleanService.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. <?php
  2. namespace backend\modules\mall\services;
  3. use addons\Store\common\enums\StoreEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\helpers\StringHelper;
  7. use common\models\goods\Goods;
  8. use common\models\order\Order;
  9. use common\models\order\OrderDetail;
  10. use common\models\order\OrderRefund;
  11. use common\models\user\User;
  12. use common\traits\ErrorTrait;
  13. use Exception;
  14. use services\api\AccessTokenService;
  15. use Yii;
  16. /**
  17. * 清理数据
  18. * SpecsService class
  19. * @package backend\modules\mall\services
  20. */
  21. class CleanService extends BaseService
  22. {
  23. use ErrorTrait;
  24. public function batchHandle($data)
  25. {
  26. // $clean = new CleanService();
  27. try {
  28. $cache_key = 'clear_data_'.$data['mall_id'];
  29. $cache = \Yii::$app->cache;
  30. $cache->set($cache_key, 1);
  31. if ($data['clean_data'] == 1) {
  32. // 清理会员数据、订单数据
  33. $res = $this->user($data['mall_id']);
  34. if ($res == false) throw new Exception('清除失败:' . $this->getError());
  35. } elseif ($data['clean_data'] == 2) {
  36. // 清理订单数据
  37. $res = $this->order($data['mall_id']);
  38. if ($res == false) throw new Exception('清除失败:' . $this->getError());
  39. }
  40. // 清理插件数据
  41. if (!empty($data['clean_addons'])) {
  42. $addons_name = $data['addons_name'];
  43. if (!empty($addons_name)) {
  44. foreach ($addons_name as $name) {
  45. $res = $this->addons($data['mall_id'], $data['clean_type'] == 1 ? false : true, $name);
  46. if ($res == false) throw new Exception('清除失败:' . $this->getError());
  47. }
  48. }
  49. }
  50. $cache->set($cache_key, 2);
  51. return true;
  52. } catch (\Exception $e) {
  53. $cache->set($cache_key, 2);
  54. var_dump($e);
  55. return true;
  56. }
  57. }
  58. /**
  59. * 会员清理
  60. * @param $mall_id
  61. * @return int
  62. */
  63. public function user($mall_id)
  64. {
  65. try {
  66. $part = 1;
  67. $limit = 1000;
  68. $select = "id,mall_id";
  69. $part_where = [['=', 'mall_id', $mall_id], ['=', 'status', StatusEnum::ENABLED]];
  70. $params = ['where' => $part_where, 'select' => $select];
  71. while ($part_data = User::batchGetUsers($params, $part, $limit)) { // 查询user表的
  72. $ids = implode(',', array_column($part_data, 'id'));
  73. $delsql = [];
  74. if ($ids) {
  75. $delsql[] = "DELETE FROM `qimall_user_tag_relate` WHERE `user_id` IN({$ids});";
  76. $delsql[] = "DELETE FROM `qimall_user_partition_relationship` WHERE `user_id` IN({$ids});";
  77. $delsql[] = "DELETE FROM `qimall_user_relationship_log` WHERE `user_id` IN({$ids});";
  78. $delsql[] = "DELETE FROM `qimall_user_extra` WHERE `user_id` IN({$ids});";
  79. $delsql[] = "DELETE FROM `qimall_user_address` WHERE `user_id` IN({$ids});";
  80. foreach ($delsql as $sql) {
  81. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  82. $res = Yii::$app->db->createCommand($sql)->execute();
  83. if ($res === false) {
  84. throw new \Exception("按会员ID清理,第{$part}页清理失败");
  85. }
  86. }
  87. }
  88. $part++;
  89. }
  90. // 删除token
  91. (new AccessTokenService())->delAllCacheByMallId($mall_id, 'api');
  92. $tables = Yii::$app->db->createCommand('show tables')->queryAll();
  93. $tables_arr = array_column($tables, 'Tables_in_qimall');
  94. // 会员数据sql组合
  95. $sql_arr[] = "DELETE FROM `qimall_user` WHERE `mall_id` = {$mall_id};";
  96. $sql_arr[] = "DELETE FROM `qimall_user_label_relationship` WHERE `mall_id` = {$mall_id};";
  97. $sql_arr[] = "DELETE FROM `qimall_user_wallet` WHERE `mall_id` = {$mall_id};";
  98. $sql_arr[] = "DELETE FROM `qimall_user_info` WHERE `mall_id` = {$mall_id};";
  99. // $sql_arr[] = "DELETE FROM `qimall_user_member_label_relation` WHERE `mall_id` = {$mall_id};";
  100. $sql_arr[] = "DELETE FROM `qimall_user_member_label` WHERE `mall_id` = {$mall_id};";
  101. $sql_arr[] = "DELETE FROM `qimall_user_import_fail_log` WHERE `mall_id` = {$mall_id};";
  102. $sql_arr[] = "DELETE FROM `qimall_user_action_record` WHERE `mall_id` = {$mall_id};";
  103. $sql_arr[] = "DELETE FROM `qimall_user_invite_code` WHERE `mall_id` = {$mall_id};";
  104. // $sql_arr[] = "DELETE FROM `qimall_user_extra` WHERE `mall_id` = {$mall_id};";
  105. $sql_arr[] = "DELETE FROM `qimall_user_label` WHERE `mall_id` = {$mall_id};";
  106. $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};";
  107. $sql_arr[] = "DELETE FROM `qimall_user_bank_card` WHERE `mall_id` = {$mall_id};";
  108. $sql_arr[] = "DELETE FROM `qimall_user_level_expired` WHERE `mall_id` = {$mall_id};";
  109. $sql_arr[] = "DELETE FROM `qimall_user_import_log` WHERE `mall_id` = {$mall_id};";
  110. $sql_arr[] = "DELETE FROM `qimall_user_follow` WHERE `mall_id` = {$mall_id};";
  111. $sql_arr[] = "DELETE FROM `qimall_user_digital` WHERE `mall_id` = {$mall_id};";
  112. $sql_arr[] = "DELETE FROM `qimall_user_bank_card_sign` WHERE `mall_id` = {$mall_id};";
  113. $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};";
  114. $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};";
  115. $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};";
  116. $sql_arr[] = "DELETE FROM `qimall_user_login_log` WHERE `mall_id` = {$mall_id};";
  117. $sql_arr[] = "DELETE FROM `qimall_user_level_upgrade_log` WHERE `mall_id` = {$mall_id};";
  118. $sql_arr[] = "DELETE FROM `qimall_user_del_log` WHERE `mall_id` = {$mall_id};";
  119. $sql_arr[] = "DELETE FROM `qimall_user_behavior_logs` WHERE `mall_id` = {$mall_id};";
  120. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_1` WHERE `mall_id` = {$mall_id};";
  121. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_2` WHERE `mall_id` = {$mall_id};";
  122. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_3` WHERE `mall_id` = {$mall_id};";
  123. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_4` WHERE `mall_id` = {$mall_id};";
  124. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_5` WHERE `mall_id` = {$mall_id};";
  125. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_6` WHERE `mall_id` = {$mall_id};";
  126. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_7` WHERE `mall_id` = {$mall_id};";
  127. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_8` WHERE `mall_id` = {$mall_id};";
  128. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_9` WHERE `mall_id` = {$mall_id};";
  129. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_10` WHERE `mall_id` = {$mall_id};";
  130. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_statistics` WHERE `mall_id` = {$mall_id};";
  131. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_1` WHERE `mall_id` = {$mall_id};";
  132. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_2` WHERE `mall_id` = {$mall_id};";
  133. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_3` WHERE `mall_id` = {$mall_id};";
  134. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_4` WHERE `mall_id` = {$mall_id};";
  135. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_5` WHERE `mall_id` = {$mall_id};";
  136. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_6` WHERE `mall_id` = {$mall_id};";
  137. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_7` WHERE `mall_id` = {$mall_id};";
  138. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_8` WHERE `mall_id` = {$mall_id};";
  139. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_9` WHERE `mall_id` = {$mall_id};";
  140. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_10` WHERE `mall_id` = {$mall_id};";
  141. $sql_arr[] = "DELETE FROM `qimall_census_mall_capital_statistics` WHERE `mall_id` = {$mall_id};";
  142. if (in_array('qimall_user_team_month_statistics', $tables_arr)) {
  143. $sql_arr[] = "DELETE FROM `qimall_user_team_month_statistics` WHERE `mall_id` = {$mall_id};";
  144. }
  145. if (in_array('qimall_user_team_pay_money_statistics', $tables_arr)) {
  146. $sql_arr[] = "DELETE FROM `qimall_user_team_pay_money_statistics` WHERE `mall_id` = {$mall_id};";
  147. }
  148. $sql_arr[] = "DELETE FROM `qimall_user_team_performance_statistics` WHERE `mall_id` = {$mall_id};";
  149. $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};";
  150. foreach ($sql_arr as $sql) {
  151. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  152. $res = Yii::$app->db->createCommand($sql)->execute();
  153. if ($res === false) {
  154. throw new \Exception('会员清理失败');
  155. }
  156. }
  157. // 订单数据清理
  158. $this->order($mall_id);
  159. // 资产清理
  160. $this->cleanCapital($mall_id);
  161. // 统计清理
  162. $this->clearUserStatistics($mall_id, 1);
  163. // 缓存清理
  164. $redis = Yii::$app->redis;
  165. $user_keys = [];
  166. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VIEWS, $mall_id);
  167. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VISITORS, $mall_id);
  168. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER, $mall_id);
  169. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER_LEVEL, $mall_id);
  170. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, $mall_id);
  171. foreach ($user_keys as $key) {
  172. $redis->del($key);
  173. }
  174. return true;
  175. } catch (\Exception $e) {
  176. $this->setError($e->getMessage());
  177. return false;
  178. }
  179. }
  180. /**
  181. * 订单数据清除
  182. * @param $mall_id
  183. * @return int
  184. */
  185. public function order($mall_id)
  186. {
  187. try {
  188. $this->clearOrder($mall_id);
  189. $this->clearOrderStatistics($mall_id, 2);
  190. $this->cleanGoodsSales($mall_id);
  191. return true;
  192. } catch (\Exception $e) {
  193. $this->setError($e->getMessage());
  194. return false;
  195. }
  196. }
  197. public function capital($mall_id) {
  198. try {
  199. $part = 1;
  200. $limit = 1000;
  201. $select = "id,mall_id";
  202. $part_where = [['=', 'mall_id', $mall_id], ['=', 'status', StatusEnum::ENABLED]];
  203. $params = ['where' => $part_where, 'select' => $select];
  204. // while ($part_data = User::batchGetUsers($params, $part, $limit)) { // 查询user表的
  205. // $ids = implode(',', array_column($part_data, 'id'));
  206. // $delsql = [];
  207. // if ($ids) {
  208. // $delsql[] = "DELETE FROM `qimall_user_tag_relate` WHERE `user_id` IN({$ids});";
  209. // // $delsql[] = "DELETE FROM `qimall_user_partition_relationship` WHERE `user_id` IN({$ids});";
  210. // // $delsql[] = "DELETE FROM `qimall_user_relationship_log` WHERE `user_id` IN({$ids});";
  211. // $delsql[] = "DELETE FROM `qimall_user_extra` WHERE `user_id` IN({$ids});";
  212. // $delsql[] = "DELETE FROM `qimall_user_address` WHERE `user_id` IN({$ids});";
  213. // foreach ($delsql as $sql) {
  214. // Yii::error(sprintf('执行数据清除语句: %s', $sql));
  215. // $res = Yii::$app->db->createCommand($sql)->execute();
  216. // if ($res === false) {
  217. // throw new \Exception("按会员ID清理,第{$part}页清理失败");
  218. // }
  219. // }
  220. // }
  221. // $part++;
  222. // }
  223. //获取所有表格
  224. $tables = Yii::$app->db->createCommand('show tables')->queryAll();
  225. $tables_arr = array_column($tables, 'Tables_in_qimall');
  226. // 删除token
  227. // (new AccessTokenService())->delAllCacheByMallId($mall_id, 'api');
  228. // 会员数据sql组合
  229. // $sql_arr[] = "DELETE FROM `qimall_user` WHERE `mall_id` = {$mall_id};";
  230. // $sql_arr[] = "DELETE FROM `qimall_user_label_relationship` WHERE `mall_id` = {$mall_id};";
  231. // $sql_arr[] = "DELETE FROM `qimall_user_wallet` WHERE `mall_id` = {$mall_id};";
  232. // $sql_arr[] = "DELETE FROM `qimall_user_info` WHERE `mall_id` = {$mall_id};";
  233. $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}";
  234. // $sql_arr[] = "DELETE FROM `qimall_user_member_label_relation` WHERE `mall_id` = {$mall_id};";
  235. // $sql_arr[] = "DELETE FROM `qimall_user_member_label` WHERE `mall_id` = {$mall_id};";
  236. $sql_arr[] = "DELETE FROM `qimall_user_import_fail_log` WHERE `mall_id` = {$mall_id};";
  237. // $sql_arr[] = "DELETE FROM `qimall_user_action_record` WHERE `mall_id` = {$mall_id};";
  238. // $sql_arr[] = "DELETE FROM `qimall_user_invite_code` WHERE `mall_id` = {$mall_id};";
  239. // $sql_arr[] = "DELETE FROM `qimall_user_extra` WHERE `mall_id` = {$mall_id};";
  240. // $sql_arr[] = "DELETE FROM `qimall_user_label` WHERE `mall_id` = {$mall_id};";
  241. $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};";
  242. // $sql_arr[] = "DELETE FROM `qimall_user_bank_card` WHERE `mall_id` = {$mall_id};";
  243. // $sql_arr[] = "DELETE FROM `qimall_user_level_expired` WHERE `mall_id` = {$mall_id};";
  244. $sql_arr[] = "DELETE FROM `qimall_user_import_log` WHERE `mall_id` = {$mall_id};";
  245. // $sql_arr[] = "DELETE FROM `qimall_user_follow` WHERE `mall_id` = {$mall_id};";
  246. $sql_arr[] = "DELETE FROM `qimall_user_digital` WHERE `mall_id` = {$mall_id};";
  247. // $sql_arr[] = "DELETE FROM `qimall_user_bank_card_sign` WHERE `mall_id` = {$mall_id};";
  248. // $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};";
  249. $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};";
  250. // $sql_arr[] = "DELETE FROM `qimall_user_ali_account` WHERE `mall_id` = {$mall_id};";
  251. // $sql_arr[] = "DELETE FROM `qimall_user_login_log` WHERE `mall_id` = {$mall_id};";
  252. // $sql_arr[] = "DELETE FROM `qimall_user_level_upgrade_log` WHERE `mall_id` = {$mall_id};";
  253. $sql_arr[] = "DELETE FROM `qimall_user_del_log` WHERE `mall_id` = {$mall_id};";
  254. $sql_arr[] = "DELETE FROM `qimall_user_behavior_logs` WHERE `mall_id` = {$mall_id};";
  255. $sql_arr[] = "DELETE FROM `qimall_user_achievement_statistics` WHERE `mall_id` = {$mall_id};";
  256. $sql_arr[] = "DELETE FROM `qimall_user_achievement_statistics_total` WHERE `mall_id` = {$mall_id};";
  257. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_1` WHERE `mall_id` = {$mall_id};";
  258. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_2` WHERE `mall_id` = {$mall_id};";
  259. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_3` WHERE `mall_id` = {$mall_id};";
  260. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_4` WHERE `mall_id` = {$mall_id};";
  261. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_5` WHERE `mall_id` = {$mall_id};";
  262. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_6` WHERE `mall_id` = {$mall_id};";
  263. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_7` WHERE `mall_id` = {$mall_id};";
  264. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_8` WHERE `mall_id` = {$mall_id};";
  265. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_9` WHERE `mall_id` = {$mall_id};";
  266. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_order_10` WHERE `mall_id` = {$mall_id};";
  267. $sql_arr[] = "DELETE FROM `qimall_user_team_achievement_statistics` WHERE `mall_id` = {$mall_id};";
  268. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_1` WHERE `mall_id` = {$mall_id};";
  269. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_2` WHERE `mall_id` = {$mall_id};";
  270. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_3` WHERE `mall_id` = {$mall_id};";
  271. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_4` WHERE `mall_id` = {$mall_id};";
  272. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_5` WHERE `mall_id` = {$mall_id};";
  273. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_6` WHERE `mall_id` = {$mall_id};";
  274. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_7` WHERE `mall_id` = {$mall_id};";
  275. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_8` WHERE `mall_id` = {$mall_id};";
  276. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_9` WHERE `mall_id` = {$mall_id};";
  277. $sql_arr[] = "DELETE FROM `qimall_user_team_date_achievement_statistics_10` WHERE `mall_id` = {$mall_id};";
  278. if (in_array('qimall_user_team_month_statistics', $tables_arr)) {
  279. $sql_arr[] = "DELETE FROM `qimall_user_team_month_statistics` WHERE `mall_id` = {$mall_id};";
  280. }
  281. if (in_array('qimall_user_team_pay_money_statistics', $tables_arr)) {
  282. $sql_arr[] = "DELETE FROM `qimall_user_team_pay_money_statistics` WHERE `mall_id` = {$mall_id};";
  283. }
  284. $sql_arr[] = "DELETE FROM `qimall_user_team_performance_statistics` WHERE `mall_id` = {$mall_id};";
  285. $sql_arr[] = "DELETE FROM `qimall_user_third_party_address` WHERE `mall_id` = {$mall_id};";
  286. foreach ($sql_arr as $sql) {
  287. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  288. $res = Yii::$app->db->createCommand($sql)->execute();
  289. if ($res === false) {
  290. throw new \Exception('会员清理失败');
  291. }
  292. }
  293. // 订单数据清理
  294. $this->clearOrder($mall_id);
  295. $this->clearOrderStatistics($mall_id, 3);
  296. // 资产清理
  297. $this->cleanCapital($mall_id);
  298. // 统计清理
  299. $this->clearUserStatistics($mall_id, 3);
  300. // 缓存清理
  301. $redis = Yii::$app->redis;
  302. $user_keys = [];
  303. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VIEWS, $mall_id);
  304. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_VISITORS, $mall_id);
  305. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER, $mall_id);
  306. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_USER_LEVEL, $mall_id);
  307. $user_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PLATFORM_USER, $mall_id);
  308. foreach ($user_keys as $key) {
  309. $redis->del($key);
  310. }
  311. return true;
  312. } catch (\Exception $e) {
  313. $this->setError($e->getMessage());
  314. return false;
  315. }
  316. }
  317. /**
  318. * @param $mall_id
  319. * @param $isMycat
  320. * @param $addons_name
  321. * @return bool
  322. * @throws \yii\db\Exception
  323. */
  324. public function addons($mall_id, $isMycat = false, $addons_name = '')
  325. {
  326. try {
  327. // 获取所有表
  328. $sql = "";
  329. $tables = Yii::$app->db->createCommand('show tables')->queryAll();
  330. $list = [];
  331. $addons_name_c = $addons_name;
  332. $string = 'Tables_in_qimall';
  333. if (!empty($addons_name)) {
  334. $addons_name = '_' . StringHelper::toUnderScore($addons_name);
  335. }
  336. $addons_name = str_replace('-', '_', $addons_name);
  337. $key = $isMycat === false ? $string : str_replace('_', ' ', $string);
  338. // 所有表
  339. foreach ($tables as $item) {
  340. $list[] = $item[$key];
  341. }
  342. // 筛选出不可以被清空数据的插件表
  343. $dont_clean_tables = [
  344. 'qimall_addons_distribution_level', 'qimall_addons_agent_level',
  345. 'qimall_addons_area_community', 'qimall_addons_store_coupon_cat_relate', 'qimall_addons_store_coupon_goods_relate',
  346. 'qimall_addons_agent_distribution_setting', 'qimall_addons_agent_goods_setting', 'qimall_addons_agent_goods_repurchase',
  347. 'qimall_addons_agent_setting', 'qimall_addons_boss_bonus_level_upgrade_condition', 'qimall_addons_business_bonus_upgrade_condition',
  348. 'qimall_addons_setting', 'qimall_addons_order', 'qimall_addons_marketing_setting', 'qimall_addons_links',
  349. 'qimall_addons_event', 'qimall_addons_combo', 'qimall_addons_binding', 'qimall_addons', 'qimall_addons_invoker',
  350. 'qimall_addons_cloud_stock_upgrade_bag', 'qimall_addons_cloud_stock_goods', 'qimall_addons_cloud_stock_sms',
  351. 'qimall_addons_physical_stock_upgrade_bag', 'qimall_addons_physical_stock_goods', 'qimall_addons_physical_stock_sms',
  352. // 星链供应链
  353. 'qimall_addons_star_chain_category', 'qimall_addons_star_chain_express', 'qimall_addons_star_chain_goods',
  354. 'qimall_addons_star_chain_region', 'qimall_addons_star_chain_storage', 'qimall_addons_star_chain_goods_attr',
  355. // 七件事供应链
  356. 'qimall_addons_qijuhe_category', 'qimall_addons_qijuhe_express', 'qimall_addons_qijuhe_goods',
  357. 'qimall_addons_qijuhe_storage', 'qimall_addons_qijuhe_supplys', 'qimall_addons_qijuhe_goods_attr',
  358. // 聚合供应链
  359. 'qimall_addons_juhe_shop_category', 'qimall_addons_juhe_shop_express', 'qimall_addons_juhe_shop_goods',
  360. 'qimall_addons_juhe_shop_goods_attr', 'qimall_addons_juhe_shop_storage',
  361. 'qimall_addons_album',
  362. // 电子券
  363. 'qimall_addons_electron_coupon', 'qimall_addons_sudoku', 'qimall_addons_score_mall_goods',
  364. 'qimall_addons_score_mall_goods_attr',
  365. // 幸运拼团
  366. 'qimall_addons_lucky_group_activity', // 活动列表
  367. 'qimall_addons_lucky_group_goods', // 活动关联商品列表
  368. 'qimall_addons_lucky_group_level', // 权重等级列表
  369. 'qimall_addons_lucky_group_category', // 活动分类列表
  370. ];
  371. // 循环清除选择的插件表
  372. foreach ($list as $item) {
  373. if (strpos($item, 'qimall_addons' . $addons_name) !== false && !in_array($item, $dont_clean_tables)) {
  374. // 插件等级跟配置不删除
  375. if (strpos($item, 'level') !== false) continue;
  376. if (strpos($item, 'item_setting') !== false) continue;
  377. // 设置相关跳过
  378. if (strpos($item, '_setting') !== false) continue;
  379. // 支付宝合规配置不删除
  380. if (strpos($item, 'qimall_addons_tax_config') !== false) continue;
  381. if (strpos($item, 'qimall_addons_tax_account') !== false) continue;
  382. if (strpos($item, 'qimall_addons_tax_account_book') !== false) continue;
  383. // 1002 【清除数据】不要清除优惠券设置,还有推三返一的活动创建的设置
  384. if (in_array($item, ['qimall_addons_coupon', 'qimall_addons_rebate'])) continue;
  385. $sql = "DELETE FROM `{$item}` WHERE `mall_id` = {$mall_id};";
  386. try {
  387. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  388. $res = Yii::$app->db->createCommand($sql)->execute();
  389. } catch (\Exception $e) {
  390. Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage()));
  391. }
  392. }
  393. }
  394. // 额外清除
  395. $del_tables = [];
  396. // 签到
  397. if (empty($addons_name_c) || $addons_name_c == 'SignIn') {
  398. $del_tables[] = 'qimall_addons_user_sign_in_award';
  399. $del_tables[] = 'qimall_addons_user_sign_in_record';
  400. $del_tables[] = 'qimall_addons_user_sign_in_award_record';
  401. }
  402. // 渠道分红
  403. if (empty($addons_name_c) || $addons_name_c == 'Agent') {
  404. $del_tables[] = 'qimall_agent_commission_details';
  405. }
  406. // 二二复制
  407. if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') {
  408. $del_tables[] = 'qimall_addons_double_copy_pool_level';
  409. }
  410. // 股东分红
  411. if (empty($addons_name_c) || $addons_name_c == 'Boss') {
  412. $del_tables[] = 'qimall_addons_boss_reward_log';
  413. }
  414. // HiGo
  415. if (empty($addons_name_c) || $addons_name_c == 'HiGo') {
  416. $del_tables[] = 'qimall_addons_higo_hibei_log';
  417. $del_tables[] = 'qimall_addons_higo_mall';
  418. $del_tables[] = 'qimall_addons_higo_merchant_reward_log';
  419. $del_tables[] = 'qimall_addons_higo_recharge_orders';
  420. $del_tables[] = 'qimall_addons_higo_wallet';
  421. $del_tables[] = 'qimall_addons_higo_wallet_freeze_log';
  422. $del_tables[] = 'qimall_addons_higo_wallet_frozen_log';
  423. $del_tables[] = 'qimall_addons_higo_wallet_log';
  424. $del_tables[] = 'qimall_addons_higo_withdraw_log';
  425. $del_tables[] = 'qimall_addons_higo_store_goods_mode';
  426. }
  427. foreach ($del_tables as $table) {
  428. // if (in_array($table, $tables)) {
  429. $sql = "DELETE FROM `{$table}` WHERE `mall_id` = {$mall_id};";
  430. try {
  431. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  432. $res = Yii::$app->db->createCommand($sql)->execute();
  433. } catch (\Exception $e) {
  434. Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage()));
  435. }
  436. // }
  437. }
  438. // O2O专项清除
  439. if (empty($addons_name_c) || $addons_name_c == 'Store') {
  440. $this->claerStoreRedis($mall_id);
  441. }
  442. // 多商户清除
  443. if (empty($addons_name_c) || $addons_name_c == 'Mch') {
  444. // 清除门店商品
  445. $sql = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id} AND `mch_id` > 0";
  446. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  447. Yii::$app->db->createCommand($sql)->execute();
  448. }
  449. // 服务商分红
  450. if (empty($addons_name_c) || $addons_name_c == 'ServeBonus') {
  451. $sql = "UPDATE `qimall_addons_servebonus_level` SET `serve_bonus_number`=0 WHERE `mall_id` = {$mall_id}";
  452. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  453. Yii::$app->db->createCommand($sql)->execute();
  454. }
  455. // 多商户清除
  456. if (empty($addons_name_c) || $addons_name_c == 'PublicLine') {
  457. // 清除门店商品
  458. $sql = "DELETE FROM `qimall_commission_log` WHERE `mall_id` = {$mall_id} AND `from_type` = 'PublicLine'";
  459. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  460. Yii::$app->db->createCommand($sql)->execute();
  461. }
  462. // 删除商品关联插件
  463. $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id}";
  464. if ($addons_name_c) {
  465. $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id} AND `marketing_type` = '{$addons_name_c}'";
  466. }
  467. if (!empty($addons_name_c) && $addons_name_c == 'ScoreMall') $sql = '';// 积分商城商品绑定了主商城商品所以不删除
  468. if ($sql) {
  469. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  470. Yii::$app->db->createCommand($sql)->execute();
  471. }
  472. return true;
  473. } catch (\Exception $e) {
  474. $this->setError($e->getMessage());
  475. return false;
  476. }
  477. }
  478. //数据清除插件调用清除插件数据
  479. public function clearDataAddons($mall_id, $isMycat = false, $addons_name = '')
  480. {
  481. try {
  482. // 获取所有表
  483. $sql = "";
  484. $tables = Yii::$app->db->createCommand('show tables')->queryAll();
  485. $list = [];
  486. $addons_name_c = $addons_name;
  487. $string = 'Tables_in_qimall';
  488. if (!empty($addons_name)) {
  489. if ($addons_name == 'HuifuPay') {
  490. $addons_name = '_' . strtolower($addons_name);
  491. } else {
  492. $addons_name = '_' . StringHelper::toUnderScore($addons_name);
  493. }
  494. }
  495. $addons_name = str_replace('-', '_', $addons_name);
  496. $key = $isMycat === false ? $string : str_replace('_', ' ', $string);
  497. // 所有表
  498. foreach ($tables as $item) {
  499. $list[] = $item[$key];
  500. }
  501. // 筛选出不可以被清空数据的插件表
  502. $dont_clean_tables = [
  503. //业绩奖励
  504. 'qimall_addons_achievement_user', 'qimall_addons_achievement_commission_item_setting', 'qimall_addons_achievement_counting_goods',
  505. //分销商
  506. 'qimall_addons_distribution', 'qimall_addons_distribution_level', 'qimall_addons_distribution_commission_level_setting', 'qimall_addons_distribution_commission_item_setting',
  507. 'qimall_addons_distribution_commission_item_setting_detail', 'qimall_addons_distribution_goods_setting',
  508. //渠道分红
  509. 'qimall_addons_agent_level','qimall_addons_agent', 'qimall_addons_agent_distribution_setting', 'qimall_addons_agent_goods_setting', 'qimall_addons_agent_goods_repurchase',
  510. 'qimall_addons_agent_setting', 'qimall_addons_agent_commission_item_setting', 'qimall_addons_agent_commission_item_setting_detail', 'qimall_addons_agent_commission_level_setting',
  511. //社区团购
  512. 'qimall_addons_community_head', 'qimall_addons_community_head_apply', 'qimall_addons_community_goods_mode_setting', 'qimall_addons_community_goods_setting',
  513. //二二复制
  514. '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',
  515. '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',
  516. //汇付支付
  517. 'qimall_addons_huifupay_user_bankcard',
  518. 'qimall_addons_logistics_express',
  519. 'qimall_addons_area_agent','qimall_addons_cloud_stock_agent',
  520. 'qimall_addons_area_community','qimall_addons_area_regional',
  521. 'qimall_addons_store_coupon_cat_relate', 'qimall_addons_store_coupon_goods_relate',
  522. 'qimall_addons_boss_bonus_level_upgrade_condition', 'qimall_addons_business_bonus_upgrade_condition',
  523. 'qimall_addons_setting', 'qimall_addons_order', 'qimall_addons_marketing_setting', 'qimall_addons_links',
  524. 'qimall_addons_event', 'qimall_addons_combo', 'qimall_addons_binding', 'qimall_addons', 'qimall_addons_invoker',
  525. 'qimall_addons_cloud_stock_upgrade_bag', 'qimall_addons_cloud_stock_goods', 'qimall_addons_cloud_stock_sms',
  526. 'qimall_addons_physical_stock_upgrade_bag', 'qimall_addons_physical_stock_goods', 'qimall_addons_physical_stock_sms',
  527. // 星链供应链
  528. 'qimall_addons_star_chain_category', 'qimall_addons_star_chain_express', 'qimall_addons_star_chain_goods',
  529. 'qimall_addons_star_chain_region', 'qimall_addons_star_chain_storage', 'qimall_addons_star_chain_goods_attr',
  530. // 七件事供应链
  531. 'qimall_addons_qijuhe_category', 'qimall_addons_qijuhe_express', 'qimall_addons_qijuhe_goods',
  532. 'qimall_addons_qijuhe_storage', 'qimall_addons_qijuhe_supplys', 'qimall_addons_qijuhe_goods_attr',
  533. // 聚合供应链
  534. 'qimall_addons_juhe_shop_category', 'qimall_addons_juhe_shop_express', 'qimall_addons_juhe_shop_goods',
  535. 'qimall_addons_juhe_shop_goods_attr', 'qimall_addons_juhe_shop_storage',
  536. 'qimall_addons_album',
  537. // 电子券
  538. 'qimall_addons_electron_coupon', 'qimall_addons_sudoku', 'qimall_addons_score_mall_goods',
  539. 'qimall_addons_score_mall_goods_attr','qimall_addons_redpack_wallet',
  540. // 幸运拼团
  541. 'qimall_addons_lucky_group_activity', // 活动列表
  542. 'qimall_addons_lucky_group_goods', // 活动关联商品列表
  543. 'qimall_addons_lucky_group_level', // 权重等级列表
  544. 'qimall_addons_lucky_group_category', // 活动分类列表
  545. ];
  546. // 循环清除选择的插件表
  547. foreach ($list as $item) {
  548. if (strpos($item, 'qimall_addons' . $addons_name) !== false && !in_array($item, $dont_clean_tables)) {
  549. // 插件等级跟配置不删除
  550. if (strpos($item, 'level') !== false) continue;
  551. if (strpos($item, 'item_setting') !== false) continue;
  552. // 设置相关跳过
  553. if (strpos($item, '_setting') !== false) continue;
  554. // 支付宝合规配置不删除
  555. if (strpos($item, 'qimall_addons_tax_config') !== false) continue;
  556. if (strpos($item, 'qimall_addons_tax_account') !== false) continue;
  557. if (strpos($item, 'qimall_addons_tax_account_book') !== false) continue;
  558. // 1002 【清除数据】不要清除优惠券设置,还有推三返一的活动创建的设置
  559. if (in_array($item, ['qimall_addons_rebate'])) continue;
  560. $sql = "DELETE FROM `{$item}` WHERE `mall_id` = {$mall_id};";
  561. // echo $sql . "\r\n";
  562. try {
  563. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  564. $res = Yii::$app->db->createCommand($sql)->execute();
  565. } catch (\Exception $e) {
  566. Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage()));
  567. }
  568. }
  569. }
  570. // exit;
  571. // 额外清除
  572. $del_tables = [];
  573. // 签到
  574. if (empty($addons_name_c) || $addons_name_c == 'SignIn') {
  575. $del_tables[] = 'qimall_addons_user_sign_in_award';
  576. $del_tables[] = 'qimall_addons_user_sign_in_record';
  577. $del_tables[] = 'qimall_addons_user_sign_in_award_record';
  578. }
  579. // 渠道分红
  580. if (empty($addons_name_c) || $addons_name_c == 'Agent') {
  581. $del_tables[] = 'qimall_agent_commission_details';
  582. }
  583. // 二二复制
  584. // if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') {
  585. // $del_tables[] = 'qimall_addons_double_copy_pool_level';
  586. // }
  587. // 股东分红
  588. if (empty($addons_name_c) || $addons_name_c == 'Boss') {
  589. $del_tables[] = 'qimall_addons_boss_reward_log';
  590. }
  591. // HiGo
  592. if (empty($addons_name_c) || $addons_name_c == 'HiGo') {
  593. $del_tables[] = 'qimall_addons_higo_hibei_log';
  594. $del_tables[] = 'qimall_addons_higo_mall';
  595. $del_tables[] = 'qimall_addons_higo_merchant_reward_log';
  596. $del_tables[] = 'qimall_addons_higo_recharge_orders';
  597. $del_tables[] = 'qimall_addons_higo_wallet';
  598. $del_tables[] = 'qimall_addons_higo_wallet_freeze_log';
  599. $del_tables[] = 'qimall_addons_higo_wallet_frozen_log';
  600. $del_tables[] = 'qimall_addons_higo_wallet_log';
  601. $del_tables[] = 'qimall_addons_higo_withdraw_log';
  602. $del_tables[] = 'qimall_addons_higo_store_goods_mode';
  603. }
  604. foreach ($del_tables as $table) {
  605. // if (in_array($table, $tables)) {
  606. $sql = "DELETE FROM `{$table}` WHERE `mall_id` = {$mall_id};";
  607. try {
  608. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  609. $res = Yii::$app->db->createCommand($sql)->execute();
  610. } catch (\Exception $e) {
  611. Yii::error(sprintf('%s插件数据清理失败: %s', $addons_name, $e->getMessage()));
  612. }
  613. // }
  614. }
  615. // O2O专项清除
  616. if (empty($addons_name_c) || $addons_name_c == 'Store') {
  617. $this->claerStoreRedis($mall_id);
  618. }
  619. //渠道管理
  620. if (empty($addons_name_c) || $addons_name_c == 'Agent') {
  621. $sql = "UPDATE `qimall_addons_agent` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}";
  622. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  623. Yii::$app->db->createCommand($sql)->execute();
  624. }
  625. //分销商
  626. if (empty($addons_name_c) || $addons_name_c == 'Distribution') {
  627. $sql = "UPDATE `qimall_addons_distribution` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}";
  628. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  629. Yii::$app->db->createCommand($sql)->execute();
  630. }
  631. //区域管理
  632. if (empty($addons_name_c) || $addons_name_c == 'Area') {
  633. $sql = "UPDATE `qimall_addons_area_agent` SET `total_commission`=0.00 , `total_score`=0.00 , `total_digital`=0.00 WHERE `mall_id` = {$mall_id}";
  634. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  635. Yii::$app->db->createCommand($sql)->execute();
  636. }
  637. //云库存
  638. if (empty($addons_name_c) || $addons_name_c == 'CloudStock') {
  639. $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}";
  640. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  641. Yii::$app->db->createCommand($sql)->execute();
  642. $sql = "DELETE FROM `qimall_addons_cloud_stock_agent_goods` WHERE `mall_id` = {$mall_id}";
  643. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  644. Yii::$app->db->createCommand($sql)->execute();
  645. }
  646. //红包拓客
  647. if (empty($addons_name_c) || $addons_name_c == 'Redpack') {
  648. $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}";
  649. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  650. Yii::$app->db->createCommand($sql)->execute();
  651. }
  652. // 多商户清除
  653. // if (empty($addons_name_c) || $addons_name_c == 'Mch') {
  654. // // 清除门店商品
  655. // $sql = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id} AND `mch_id` > 0";
  656. // Yii::error(sprintf('执行数据清除语句: %s', $sql));
  657. // Yii::$app->db->createCommand($sql)->execute();
  658. // }
  659. // 服务商分红
  660. if (empty($addons_name_c) || $addons_name_c == 'ServeBonus') {
  661. $sql = "UPDATE `qimall_addons_servebonus_level` SET `serve_bonus_number`=0 WHERE `mall_id` = {$mall_id}";
  662. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  663. Yii::$app->db->createCommand($sql)->execute();
  664. }
  665. //业绩奖励
  666. if (empty($addons_name_c) || $addons_name_c == 'Achievement') {
  667. $sql = "UPDATE `qimall_addons_achievement_user` SET `total_commission`=0 WHERE `mall_id` = {$mall_id}";
  668. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  669. Yii::$app->db->createCommand($sql)->execute();
  670. }
  671. //二二复制
  672. if (empty($addons_name_c) || $addons_name_c == 'DoubleCopy') {
  673. $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}";
  674. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  675. Yii::$app->db->createCommand($sql)->execute();
  676. }
  677. // 删除商品关联插件
  678. // $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id}";
  679. // if ($addons_name_c) {
  680. // $sql = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id} AND `marketing_type` = '{$addons_name_c}'";
  681. // }
  682. if (!empty($addons_name_c) && $addons_name_c == 'ScoreMall') $sql = '';// 积分商城商品绑定了主商城商品所以不删除
  683. if ($sql) {
  684. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  685. Yii::$app->db->createCommand($sql)->execute();
  686. }
  687. return true;
  688. } catch (\Exception $e) {
  689. $this->setError($e->getMessage());
  690. return false;
  691. }
  692. }
  693. /**
  694. * 删除门店数据概览redis缓存
  695. *
  696. * @param integer $mall_id
  697. * @return void
  698. */
  699. protected function claerStoreRedis(int $mall_id)
  700. {
  701. $keys = [
  702. StoreEnum::STORE_NOT_SETTLE_AMOUNTS,
  703. StoreEnum::STORE_SETTLE_AMOUNTS,
  704. StoreEnum::STORE_PAY_PEOPLE_NUMBERS,
  705. StoreEnum::STORE_PAY_ORDER_NUMBERS,
  706. StoreEnum::STORE_REAL_PAY_AMOUNTS,
  707. StoreEnum::STORE_WAITING_AUDIT_STORE_NUMBERS,
  708. ];
  709. $redis = Yii::$app->redis;
  710. foreach ($keys as $key) {
  711. $redis->del($key . ':' . $mall_id);
  712. }
  713. }
  714. public function clearUserStatistics($mall_id, $clear_data = 1)
  715. {
  716. if ($clear_data == 3) {
  717. $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};";
  718. $sql_arr[] = "UPDATE `qimall_mall_statistics` SET `order_total` = 0, `order_price_total` = 0 WHERE `mall_id` = {$mall_id};";
  719. } else {
  720. $sql_arr[] = "DELETE FROM `qimall_user_team_statistics` WHERE `mall_id` = {$mall_id};";
  721. $sql_arr[] = "DELETE FROM `qimall_mall_statistics` WHERE `mall_id` = {$mall_id};";
  722. }
  723. $sql_arr[] = "DELETE FROM `qimall_user_statistics_total` WHERE `mall_id` = {$mall_id};";
  724. $sql_arr[] = "DELETE FROM `qimall_user_statistics` WHERE `mall_id` = {$mall_id};";
  725. $sql_arr[] = "DELETE FROM `qimall_user_behaviour_statistics` WHERE `mall_id` = {$mall_id};";
  726. $sql_arr[] = "DELETE FROM `qimall_global_statistics` WHERE `mall_id` = {$mall_id};";
  727. $sql_arr[] = "DELETE FROM `qimall_capital_statistics` WHERE `mall_id` = {$mall_id};";
  728. // $sql_arr[] = "DELETE FROM `qimall_census_mall_view` WHERE `mall_id` = {$mall_id};";
  729. $sql_arr[] = "DELETE FROM `qimall_census_mall_user_source` WHERE `mall_id` = {$mall_id};";
  730. $sql_arr[] = "DELETE FROM `qimall_census_mall_user_level_single_day` WHERE `mall_id` = {$mall_id};";
  731. $sql_arr[] = "DELETE FROM `qimall_census_mall_user_level` WHERE `mall_id` = {$mall_id};";
  732. $sql_arr[] = "DELETE FROM `qimall_census_mall_goods` WHERE `mall_id` = {$mall_id};";
  733. $sql_arr[] = "DELETE FROM `qimall_census_mall_capital` WHERE `mall_id` = {$mall_id};";
  734. // $sql_arr[] = "DELETE FROM `qimall_census_mall` WHERE `mall_id` = {$mall_id};";
  735. // $sql_arr[] = "DELETE FROM `qimall_census_mall_single_day` WHERE `mall_id` = {$mall_id};";
  736. foreach ($sql_arr as $sql) {
  737. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  738. $res = Yii::$app->db->createCommand($sql)->execute();
  739. if ($res === false) {
  740. throw new \Exception('会员统计清理失败');
  741. }
  742. }
  743. $this->clearOrderStatistics($mall_id, $clear_data);
  744. }
  745. /**
  746. * 资产清理
  747. * @param $mall_id
  748. * @return void
  749. * @throws \yii\db\Exception
  750. */
  751. public function cleanCapital($mall_id)
  752. {
  753. $sql_arr[] = "DELETE FROM `qimall_commission_years_billing` WHERE `mall_id` = {$mall_id};";
  754. $sql_arr[] = "DELETE FROM `qimall_commission_month_billing` WHERE `mall_id` = {$mall_id};";
  755. $sql_arr[] = "DELETE FROM `qimall_commission_log` WHERE `mall_id` = {$mall_id};";
  756. $sql_arr[] = "DELETE FROM `qimall_commission_frozen_log` WHERE `mall_id` = {$mall_id};";
  757. $sql_arr[] = "DELETE FROM `qimall_balance_log` WHERE `mall_id` = {$mall_id};";
  758. $sql_arr[] = "DELETE FROM `qimall_balance_frozen_log` WHERE `mall_id` = {$mall_id};";
  759. $sql_arr[] = "DELETE FROM `qimall_score_log` WHERE `mall_id` = {$mall_id};";
  760. $sql_arr[] = "DELETE FROM `qimall_score_frozen_log` WHERE `mall_id` = {$mall_id};";
  761. $sql_arr[] = "DELETE FROM `qimall_digital_log` WHERE `mall_id` = {$mall_id};";
  762. $sql_arr[] = "DELETE FROM `qimall_user_withdraw_log` WHERE `mall_id` = {$mall_id};"; // 提现数据清理
  763. foreach ($sql_arr as $sql) {
  764. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  765. $res = Yii::$app->db->createCommand($sql)->execute();
  766. if ($res === false) {
  767. throw new \Exception('资产清理失败');
  768. }
  769. }
  770. }
  771. /**
  772. * 订单清理
  773. * @param $mall_id
  774. * @return void
  775. * @throws \yii\db\Exception
  776. */
  777. public function clearOrder($mall_id)
  778. {
  779. $part = 1;
  780. $limit = 1000;
  781. $select = "id,mall_id";
  782. while ($part_data = Order::find()->select($select)->where(['mall_id' => $mall_id])->orderBy('id asc')
  783. ->offset(($part - 1) * $limit)->limit($limit)->asArray()->all()) { // 查询user表的
  784. $order_ids = implode(',', array_column($part_data, 'id'));
  785. $delsql = [];
  786. if ($order_ids) {
  787. // 订单详情ID
  788. $details = OrderDetail::find()->select('id,order_id,mall_id')->where(['mall_id' => $mall_id, 'order_id' => $order_ids])->asArray()->all();
  789. $detail_ids = implode(',', array_column($details, 'id'));
  790. $delsql[] = "DELETE FROM `qimall_order_marketing` WHERE `order_detail_id` IN({$detail_ids});";
  791. $delsql[] = "DELETE FROM `qimall_order_express` WHERE `order_id` IN({$order_ids});";
  792. $delsql[] = "DELETE FROM `qimall_order_express_log` WHERE `order_id` IN({$order_ids});";
  793. $delsql[] = "DELETE FROM `qimall_order_action` WHERE `order_id` IN({$order_ids});";
  794. foreach ($delsql as $sql) {
  795. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  796. $res = Yii::$app->db->createCommand($sql)->execute();
  797. if ($res === false) {
  798. throw new \Exception("按订单ID清理,第{$part}页清理失败");
  799. }
  800. }
  801. }
  802. $part++;
  803. }
  804. $refund_part = 1;
  805. $refund_limit = 1000;
  806. $refund_select = "id,mall_id";
  807. while ($refund_data = OrderRefund::find()->select($refund_select)->where(['mall_id' => $mall_id])->orderBy('id asc')
  808. ->offset(($refund_part - 1) * $refund_limit)->limit($refund_limit)->asArray()->all()) { // 查询user表的
  809. $refund_ids = implode(',', array_column($refund_data, 'id'));
  810. if ($refund_ids) {
  811. $refundsql = "DELETE FROM `qimall_order_refund_step` WHERE `order_refund_id` IN({$refund_ids});";
  812. Yii::error(sprintf('执行数据清除语句: %s', $refundsql));
  813. $res = Yii::$app->db->createCommand($refundsql)->execute();
  814. if ($res === false) {
  815. throw new \Exception("按售后订单ID清理,第{$part}页清理失败");
  816. }
  817. }
  818. $refund_part++;
  819. }
  820. $sql_arr[] = "DELETE FROM `qimall_order_user_handle` WHERE `mall_id` = {$mall_id};";
  821. $sql_arr[] = "DELETE FROM `qimall_order_unread_msg` WHERE `mall_id` = {$mall_id};";
  822. $sql_arr[] = "DELETE FROM `qimall_order_refund` WHERE `mall_id` = {$mall_id};";
  823. $sql_arr[] = "DELETE FROM `qimall_order_invoice` WHERE `mall_id` = {$mall_id};";
  824. $sql_arr[] = "DELETE FROM `qimall_order_express_log` WHERE `mall_id` = {$mall_id};";
  825. $sql_arr[] = "DELETE FROM `qimall_order_detail` WHERE `mall_id` = {$mall_id};";
  826. $sql_arr[] = "DELETE FROM `qimall_order_comments` WHERE `mall_id` = {$mall_id};";
  827. $sql_arr[] = "DELETE FROM `qimall_order_batch_delivery_error` WHERE `mall_id` = {$mall_id};";
  828. $sql_arr[] = "DELETE FROM `qimall_order_batch_delivery` WHERE `mall_id` = {$mall_id};";
  829. $sql_arr[] = "DELETE FROM `qimall_order` WHERE `mall_id` = {$mall_id};";
  830. //支付单
  831. $sql_arr[] = "DELETE FROM `qimall_payment_refund` WHERE `mall_id` = {$mall_id};";
  832. $sql_arr[] = "DELETE FROM `qimall_payment_trade` WHERE `mall_id` = {$mall_id};";
  833. $sql_arr[] = "DELETE FROM `qimall_payment_trade_item` WHERE `mall_id` = {$mall_id};";
  834. $sql_arr[] = "DELETE FROM `qimall_payment_trade_item_refund` WHERE `mall_id` = {$mall_id};";
  835. $sql_arr[] = "DELETE FROM `qimall_payment_transfer` WHERE `mall_id` = {$mall_id};";
  836. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics` WHERE `mall_id` = {$mall_id};";
  837. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_details` WHERE `mall_id` = {$mall_id};";
  838. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_logs` WHERE `mall_id` = {$mall_id};";
  839. foreach ($sql_arr as $sql) {
  840. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  841. $res = Yii::$app->db->createCommand($sql)->execute();
  842. if ($res === false) {
  843. throw new \Exception('clearOrder:订单清理失败');
  844. }
  845. }
  846. }
  847. /**
  848. * 订单统计清理
  849. * @param integer $mall_id
  850. * @param integer $type 类型[1=清理会员,2=清理订单]
  851. * @return void
  852. * @throws \yii\db\Exception
  853. */
  854. public function clearOrderStatistics($mall_id, $type = 1)
  855. {
  856. $sql_arr[] = "DELETE FROM `qimall_user_statistics_total` WHERE `mall_id` = {$mall_id};";
  857. $sql_arr[] = "DELETE FROM `qimall_user_statistics` WHERE `mall_id` = {$mall_id};";
  858. $sql_arr[] = "DELETE FROM `qimall_user_goods_statistics` WHERE `mall_id` = {$mall_id};";
  859. $sql_arr[] = "DELETE FROM `qimall_goods_statistics` WHERE `mall_id` = {$mall_id};";
  860. $sql_arr[] = "DELETE FROM `qimall_region_statistics` WHERE `mall_id` = {$mall_id};";
  861. $sql_arr[] = "DELETE FROM `qimall_census_mall_user` WHERE `mall_id` = {$mall_id};";
  862. $sql_arr[] = "DELETE FROM `qimall_census_mall_single_month` WHERE `mall_id` = {$mall_id};";
  863. $sql_arr[] = "DELETE FROM `qimall_census_mall_single_hour` WHERE `mall_id` = {$mall_id};";
  864. $sql_arr[] = "DELETE FROM `qimall_census_mall_rm_analysis` WHERE `mall_id` = {$mall_id};";
  865. $sql_arr[] = "DELETE FROM `qimall_census_mall_rf_analysis` WHERE `mall_id` = {$mall_id};";
  866. $sql_arr[] = "DELETE FROM `qimall_census_mall_region` WHERE `mall_id` = {$mall_id};";
  867. $sql_arr[] = "DELETE FROM `qimall_census_mall_purchasing_power` WHERE `mall_id` = {$mall_id};";
  868. $sql_arr[] = "DELETE FROM `qimall_census_mall_payment_single_month` WHERE `mall_id` = {$mall_id};";
  869. $sql_arr[] = "DELETE FROM `qimall_census_mall_analysis` WHERE `mall_id` = {$mall_id};";
  870. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics` WHERE `mall_id` = {$mall_id};";
  871. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_logs` WHERE `mall_id` = {$mall_id};";
  872. $sql_arr[] = "DELETE FROM `qimall_payment_type_refund_statistics_details` WHERE `mall_id` = {$mall_id};";
  873. foreach ($sql_arr as $sql) {
  874. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  875. $res = Yii::$app->db->createCommand($sql)->execute();
  876. if ($res === false) {
  877. throw new \Exception('clearOrderStatistics:订单清理失败');
  878. }
  879. }
  880. // 清理缓存
  881. $redis = Yii::$app->redis;
  882. $data_keys = [
  883. RedisKeyEnum::CENSUS_MALL_SINGLE_DATE,
  884. RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH,
  885. RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH,
  886. ];
  887. foreach ($data_keys as $key_enum) {
  888. $keys = $this->getKey($key_enum, $mall_id) . "*";
  889. $keys_arr = $redis->keys($keys);
  890. foreach ($keys_arr as $item) {
  891. $redis->del($item);
  892. }
  893. }
  894. $order_keys = [];
  895. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL, $mall_id);
  896. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_REGION, $mall_id);
  897. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_SALES_VOLUME, $mall_id);
  898. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_PAY_MONEY, $mall_id);
  899. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_GOODS_CARD_NUMBERS, $mall_id);
  900. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_SINGLE_HOUR, $mall_id);
  901. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_RF_ANALYSIS, $mall_id);
  902. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_RM_ANALYSIS, $mall_id);
  903. $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_BUY_POWER, $mall_id);
  904. // $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH, $mall_id);
  905. // $order_keys[] = $this->getKey(RedisKeyEnum::CENSUS_MALL_PAYMENT_SINGLE_MONTH, $mall_id);
  906. foreach ($order_keys as $key) {
  907. $redis->del($key);
  908. }
  909. if ($type > 1) {
  910. $this->setOrderStatistics($mall_id);
  911. }
  912. }
  913. /**
  914. * 订单统计重置
  915. * @param $mall_id
  916. * @return void
  917. * @throws \yii\db\Exception
  918. */
  919. public function setOrderStatistics($mall_id)
  920. {
  921. $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}";
  922. $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}";
  923. $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}";
  924. $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}";
  925. foreach ($sql_arr as $sql) {
  926. Yii::error(sprintf('执行数据清除语句: %s', $sql));
  927. $res = Yii::$app->db->createCommand($sql)->execute();
  928. if ($res === false) {
  929. throw new \Exception('setOrderStatistics:订单统计修改失败');
  930. }
  931. }
  932. }
  933. private function getKey($key, $mall_id)
  934. {
  935. return RedisKeyEnum::suffix($key, '', true) . ':' . $mall_id;
  936. }
  937. /**
  938. * 商品清理-要求清理商品时可使用
  939. * @Author:lun
  940. * @Date:2023-11-27 10:46
  941. * @Copyright:copyright(c)2023 广东七件事集团
  942. * @param $mall_id
  943. * @return void
  944. */
  945. public function cleanGoods($mall_id)
  946. {
  947. $part = 1;
  948. $limit = 1000;
  949. $select = "id,mall_id";
  950. while ($part_data = Goods::find()->select($select)->where(['mall_id' => $mall_id])->orderBy('id asc')
  951. ->offset(($part - 1) * $limit)->limit($limit)->asArray()->all()) { // 查询goods表的
  952. $goods_ids = implode(',', array_column($part_data, 'id'));
  953. $delsql = [];
  954. if ($goods_ids) {
  955. // 商品规格ID
  956. $delsql[] = "DELETE FROM `qimall_goods_attr` WHERE `goods_id` IN({$goods_ids});";
  957. $delsql[] = "DELETE FROM `qimall_goods_cate_relate` WHERE `goods_id` IN({$goods_ids});";
  958. $delsql[] = "DELETE FROM `qimall_goods_service_relate` WHERE `goods_id` IN({$goods_ids});";
  959. foreach ($delsql as $sql) {
  960. Yii::error(sprintf('执行商品规格清除语句: %s', $sql));
  961. $res = Yii::$app->db->createCommand($sql)->execute();
  962. if ($res === false) {
  963. throw new \Exception("按商品规格ID清理,第{$part}页清理失败");
  964. }
  965. }
  966. }
  967. $part++;
  968. }
  969. $sql_arr[] = "DELETE FROM `qimall_goods` WHERE `mall_id` = {$mall_id};";
  970. $sql_arr[] = "DELETE FROM `qimall_goods_cate` WHERE `mall_id` = {$mall_id};";
  971. $sql_arr[] = "DELETE FROM `qimall_goods_collect` WHERE `mall_id` = {$mall_id};";
  972. $sql_arr[] = "DELETE FROM `qimall_goods_decoration_template` WHERE `mall_id` = {$mall_id};";
  973. $sql_arr[] = "DELETE FROM `qimall_goods_decoration_template_content` WHERE `mall_id` = {$mall_id};";
  974. $sql_arr[] = "DELETE FROM `qimall_goods_extra` WHERE `mall_id` = {$mall_id};";
  975. $sql_arr[] = "DELETE FROM `qimall_goods_label` WHERE `mall_id` = {$mall_id};";
  976. $sql_arr[] = "DELETE FROM `qimall_goods_marketing` WHERE `mall_id` = {$mall_id};";
  977. $sql_arr[] = "DELETE FROM `qimall_goods_service` WHERE `mall_id` = {$mall_id};";
  978. $sql_arr[] = "DELETE FROM `qimall_goods_specs` WHERE `mall_id` = {$mall_id};";
  979. $sql_arr[] = "DELETE FROM `qimall_goods_statistics` WHERE `mall_id` = {$mall_id};";
  980. $sql_arr[] = "DELETE FROM `qimall_goods_template_relation` WHERE `mall_id` = {$mall_id};";
  981. $sql_arr[] = "DELETE FROM `qimall_census_mall_goods` WHERE `mall_id` = {$mall_id};";
  982. $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}";
  983. foreach ($sql_arr as $sql) {
  984. Yii::error(sprintf('执行商品清除语句: %s', $sql));
  985. $res = Yii::$app->db->createCommand($sql)->execute();
  986. if ($res === false) {
  987. throw new \Exception('cleanGoods:商品清理失败');
  988. }
  989. }
  990. }
  991. /**
  992. * 清理商品销量
  993. *
  994. * @param integer $mall_id
  995. * @return void
  996. */
  997. public function cleanGoodsSales(int $mall_id)
  998. {
  999. // 销量
  1000. $sql_arr[] = "UPDATE `qimall_goods` SET `sales_num` = 0 WHERE `mall_id` = {$mall_id}";
  1001. foreach ($sql_arr as $sql) {
  1002. Yii::error(sprintf('执行商品销量清除语句: %s', $sql));
  1003. $res = Yii::$app->db->createCommand($sql)->execute();
  1004. if ($res === false) {
  1005. throw new \Exception('cleanGoods:商品清理失败');
  1006. }
  1007. }
  1008. }
  1009. }