ClearController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsBackClass;
  4. use bizGhs\clear\classes\OrderCgClearClass;
  5. use bizGhs\clear\models\Clear;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\custom\classes\CustomBalanceChangeClass;
  8. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  9. use bizGhs\custom\services\GhsRechargeSettleService;
  10. use bizGhs\ghs\classes\GhsClass;
  11. use bizGhs\ghs\models\Ghs;
  12. use bizGhs\order\classes\OrderClass;
  13. use bizGhs\order\classes\PurchaseOrderClass;
  14. use bizHd\purchase\classes\PurchaseClass;
  15. use yii\console\Controller;
  16. use Yii;
  17. class ClearController extends Controller
  18. {
  19. //补回 xhOrderCgClear
  20. public function actionBc()
  21. {
  22. ini_set('memory_limit', '2045M');
  23. set_time_limit(0);
  24. if (getenv('YII_ENV') == 'production') {
  25. $autoId = 817342;
  26. } else {
  27. $autoId = 1426;
  28. }
  29. $query = new \yii\db\Query();
  30. $query->from(Clear::tableName());
  31. $query->where(['>', 'id', $autoId]);
  32. foreach ($query->batch(500) as $changeList) {
  33. $ids = array_column($changeList, 'id');
  34. $relate = OrderCgClearClass::getAllByCondition(['clearId' => ['in', $ids]], null, '*', 'clearId');
  35. $connection = Yii::$app->db;
  36. $transaction = $connection->beginTransaction();
  37. try {
  38. foreach ($changeList as $clear) {
  39. $clearId = $clear['id'] ?? 0;
  40. $clearSn = $clear['orderSn'] ?? '';
  41. $status = $clear['status'] ?? 1;
  42. $clearAmount = $clear['actPrice'] ?? 0;
  43. $purchaseIds = $clear['purchaseIds'] ?? '';
  44. $saleIds = $clear['saleIds'] ?? '';
  45. $clearStyle = $clear['clearStyle'] ?? 1;
  46. if (!isset($relate[$clearId])) {
  47. if ($clearStyle == 3) {
  48. if (!empty($purchaseIds)) {
  49. $purchaseIdList = explode(',', $purchaseIds);
  50. $purchaseInfo = PurchaseOrderClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
  51. foreach ($purchaseInfo as $purchase) {
  52. $purchaseId = $purchase['id'] ?? 0;
  53. $purchaseOrderSn = $purchase['orderSn'] ?? 0;
  54. $addData = ['orderId' => 0, 'orderSn' => '', 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
  55. 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
  56. OrderCgClearClass::add($addData);
  57. echo "ghs cg clearId {$clearId} OK ### \n";
  58. }
  59. } else {
  60. echo "供货商采购 clearId {$clearId} 有缺数据 \n";
  61. }
  62. } else {
  63. if (!empty($saleIds) && !empty($purchaseIds)) {
  64. $saleIdList = explode(',', $saleIds);
  65. $purchaseIdList = explode(',', $purchaseIds);
  66. $saleInfo = OrderClass::getAllByCondition(['id' => ['in', $saleIdList]], null, 'id,orderSn,purchaseId');
  67. $purchaseInfo = PurchaseClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
  68. if (!empty($saleInfo)) {
  69. foreach ($saleInfo as $sale) {
  70. $saleId = $sale['id'] ?? 0;
  71. $purchaseId = $sale['purchaseId'] ?? 0;
  72. $saleOrderSn = $sale['orderSn'] ?? '';
  73. $purchase = $purchaseInfo[$purchaseId] ?? [];
  74. if (empty($purchase)) {
  75. echo "purchase {$purchaseId} 没有找到 \n";
  76. exit();
  77. }
  78. $purchaseOrderSn = $purchase['orderSn'] ?? '';
  79. $addData = ['orderId' => $saleId, 'orderSn' => $saleOrderSn, 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
  80. 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
  81. OrderCgClearClass::add($addData);
  82. }
  83. echo "hd cg clearId {$clearId} OK *** \n";
  84. }
  85. } else {
  86. echo "花店采购 clearId {$clearId} 有缺数据 \n";
  87. }
  88. }
  89. } else {
  90. echo "有记录 {$clearId} \n";
  91. }
  92. }
  93. $transaction->commit();
  94. } catch (\Exception $e) {
  95. $transaction->rollBack();
  96. $msg = $e->getMessage();
  97. echo "报错了:" . $msg . "\n";
  98. }
  99. }
  100. }
  101. public function actionBalance()
  102. {
  103. ini_set('memory_limit', '2045M');
  104. set_time_limit(0);
  105. //$sql = "delete from xhGhsBack";
  106. //Yii::$app->db->createCommand($sql)->execute();
  107. $query = new \yii\db\Query();
  108. $query->from(Ghs::tableName());
  109. $query->where(['ownPtStyle' => 1]);
  110. $query->andWhere(['>', 'balance', 0]);
  111. foreach ($query->batch(500) as $ghsList) {
  112. $connection = Yii::$app->db;
  113. $transaction = $connection->beginTransaction();
  114. try {
  115. foreach ($ghsList as $ghs) {
  116. GhsBackClass::add($ghs);
  117. }
  118. $transaction->commit();
  119. } catch (\Exception $e) {
  120. $transaction->rollBack();
  121. $msg = $e->getMessage();
  122. echo "报错了:" . $msg . "\n";
  123. }
  124. }
  125. }
  126. public function actionBalanceBack()
  127. {
  128. $list = GhsBackClass::getAllByCondition(['id>' => 0], null, '*', 'id', true);
  129. if (!empty($list)) {
  130. $connection = Yii::$app->db;
  131. $transaction = $connection->beginTransaction();
  132. try {
  133. foreach ($list as $ghs) {
  134. $ownPtStyle = $ghs['ownPtStyle'] ?? 1;
  135. $balance = $ghs['balance'] ?? 0;
  136. $customId = $ghs['customId'] ?? 0;
  137. if($customId == 16440 || $customId == 46425){
  138. if ($ownPtStyle == 1) {
  139. $custom = CustomClass::getById($customId, true);
  140. if (!empty($custom)) {
  141. $shopId = $ghs['shopId'] ?? 0;
  142. $shop = \bizGhs\shop\classes\ShopClass::getById($shopId, true);
  143. if (!empty($shop)) {
  144. $staff = new \stdClass();
  145. $staff->id = 0;
  146. $staff->name = '';
  147. $params = [
  148. 'remark' => '系统升级恢复',
  149. 'rechargeType' => 0, // 0 代表正常充值
  150. ];
  151. // 模拟商家帮充并自动销挂账,payWay 设为 0 (线下微信)
  152. GhsRechargeSettleService::merchantRechargeWithAutoClear($custom, $balance, $shop, $staff, 0, $params);
  153. echo "customId: {$customId} 充值成功 {$balance} 元\n";
  154. } else {
  155. echo "customId: {$customId} 找不到对应的 shop\n";
  156. }
  157. } else {
  158. echo "customId: {$customId} 找不到对应的 custom\n";
  159. }
  160. }
  161. }
  162. }
  163. $transaction->commit();
  164. } catch (\Exception $e) {
  165. $transaction->rollBack();
  166. echo "执行失败,整体回滚: " . $e->getMessage() . "\n";
  167. }
  168. }
  169. }
  170. /**
  171. * 安全回滚最新一次“系统升级恢复”产生的多余充值和销账数据
  172. * 支持传参单个 customId 测试:php yii clear/rollback-latest-upgrade 25695
  173. * 批量跑全部:php yii clear/rollback-latest-upgrade
  174. */
  175. public function actionRollbackLatestUpgrade($customId = 0)
  176. {
  177. $connection = Yii::$app->db;
  178. $customId = intval($customId);
  179. // 1. 查找所有备注为 "系统升级恢复" 的充值流水 (按 ID 倒序,最新产生的在最前面)
  180. $query = (new \yii\db\Query())
  181. ->from('xhCustomRecharge')
  182. ->where(['remark' => '系统升级恢复']);
  183. if ($customId > 0) {
  184. $query->andWhere(['customId' => $customId]);
  185. }
  186. $recharges = $query->orderBy('id DESC')->all();
  187. if (empty($recharges)) {
  188. echo "没有找到 '系统升级恢复' 相关的充值记录,无需回滚。\n";
  189. return;
  190. }
  191. // 2. 按客户分组,找到每个客户绝对最新(最后一次运行)的那一笔充值
  192. $latestRecharges = [];
  193. foreach ($recharges as $r) {
  194. $cid = $r['customId'];
  195. if (!isset($latestRecharges[$cid])) {
  196. $latestRecharges[$cid] = $r;
  197. }
  198. }
  199. if ($customId > 0) {
  200. echo "正在回滚指定客户 ID: {$customId} 的最新一笔升级恢复数据...\n";
  201. } else {
  202. echo "共找到 " . count($latestRecharges) . " 个客户的最新充值记录,准备执行批量安全回滚...\n";
  203. }
  204. $transaction = $connection->beginTransaction();
  205. try {
  206. foreach ($latestRecharges as $cr) {
  207. $cId = intval($cr['customId']);
  208. $amount = $cr['amount'];
  209. $clearId = intval($cr['clearId']);
  210. $crId = intval($cr['id']);
  211. $ghsRechargeId = intval($cr['ghsRechargeId'] ?? 0);
  212. // 1) 备注说明:根据用户指令,xhGhs (供货商) 与 xhGhsCustom (客户) 的 balance 余额无需变动还原,因此这里不执行余额扣减。
  213. // 2) 如果这笔充值关联了自动销账(clearId > 0),精准回退订单待结和结账单状态
  214. if ($clearId > 0) {
  215. $cgClears = \bizGhs\clear\classes\OrderCgClearClass::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
  216. foreach ($cgClears as $cgClear) {
  217. $orderId = intval($cgClear->orderId);
  218. $cgId = intval($cgClear->cgId);
  219. $clearedAmount = $cgClear->amount;
  220. // a. 恢复批发端的订单未结挂账金额和状态 (xhOrder)
  221. if ($orderId > 0) {
  222. $order = \bizGhs\order\classes\OrderClass::getLockById($orderId);
  223. if (!empty($order)) {
  224. $order->remainDebtPrice = bcadd($order->remainDebtPrice, $clearedAmount, 2);
  225. $order->debt = 1; // 重新恢复为挂账状态
  226. if ($order->clearId == $clearId) {
  227. $order->clearId = 0;
  228. $order->clearTime = '0000-00-00 00:00:00';
  229. }
  230. $order->save(false);
  231. }
  232. }
  233. // b. 恢复零售花店端的采购单未结挂账金额和状态 (xhPurchase)
  234. $purchase = null;
  235. if ($cgId > 0) {
  236. $purchase = \bizHd\purchase\classes\PurchaseClass::getLockById($cgId);
  237. } elseif ($orderId > 0) {
  238. $purchase = \bizHd\purchase\classes\PurchaseClass::getByCondition(['saleId' => $orderId], true);
  239. if (!empty($purchase)) {
  240. $purchase = \bizHd\purchase\classes\PurchaseClass::getLockById($purchase->id);
  241. }
  242. }
  243. if (!empty($purchase)) {
  244. $purchase->remainDebtPrice = bcadd($purchase->remainDebtPrice, $clearedAmount, 2);
  245. $purchase->debt = 1; // 恢复挂账状态
  246. if ($purchase->clearId == $clearId) {
  247. $purchase->clearId = 0;
  248. $purchase->clearTime = '0000-00-00 00:00:00';
  249. }
  250. $purchase->save(false);
  251. }
  252. // 删除销账明细纪录
  253. $cgClear->delete();
  254. }
  255. // 将结账单(xhClear)置为已作废状态 (status = 3)
  256. $clearObj = \bizGhs\order\classes\OrderClearClass::getLockById($clearId);
  257. if (!empty($clearObj)) {
  258. $clearObj->status = 3; // 3 代表已作废/已取消
  259. $clearObj->save(false);
  260. }
  261. }
  262. // 3) 删除本次充值产生的余额变动明细纪录
  263. CustomBalanceChangeClass::deleteByCondition(['relateId' => $crId]);
  264. if ($ghsRechargeId > 0) {
  265. GhsBalanceChangeClass::deleteByCondition(['relateId' => $ghsRechargeId]);
  266. }
  267. // 4) 删除充值记录本身
  268. $connection->createCommand()->delete('xhCustomRecharge', ['id' => $crId])->execute();
  269. if ($ghsRechargeId > 0) {
  270. $connection->createCommand()->delete('xhGhsRecharge', ['id' => $ghsRechargeId])->execute();
  271. }
  272. echo "【成功】客户 ID: {$cId} 多余的充值及 {$amount} 元的销账订单已完美退回并删除!\n";
  273. }
  274. $transaction->commit();
  275. echo "【全部完成】数据已成功安全回滚!\n";
  276. } catch (\Exception $e) {
  277. $transaction->rollBack();
  278. echo "【错误】回滚失败,已安全整体撤销。报错原因: " . $e->getMessage() . "\n";
  279. }
  280. }
  281. public function actionGhsGhs()
  282. {
  283. ini_set('memory_limit', '2045M');
  284. set_time_limit(0);
  285. $query = new \yii\db\Query();
  286. $query->from(Ghs::tableName());
  287. $query->where(['ownPtStyle' => 2]);
  288. foreach ($query->batch(500) as $ghsList) {
  289. $connection = Yii::$app->db;
  290. $transaction = $connection->beginTransaction();
  291. try {
  292. foreach ($ghsList as $ghs) {
  293. $ghsId = $ghs['id'] ?? 0;
  294. $balance = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => 1], 'actPrice');
  295. $debtNum = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => 1]);
  296. GhsClass::updateById($ghsId, ['balance' => -$balance, 'debtNum' => $debtNum]);
  297. $customId = $ghs['customId'] ?? 0;
  298. CustomClass::updateById($customId, ['balance' => -$balance, 'debtNum' => $debtNum]);
  299. }
  300. $transaction->commit();
  301. } catch (\Exception $e) {
  302. $transaction->rollBack();
  303. $msg = $e->getMessage();
  304. echo "报错了:" . $msg . "\n";
  305. }
  306. }
  307. }
  308. }