ClearController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\services\GhsRechargeSettleService;
  8. use bizGhs\ghs\classes\GhsClass;
  9. use bizGhs\ghs\models\Ghs;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use yii\console\Controller;
  14. use Yii;
  15. class ClearController extends Controller
  16. {
  17. //补回 xhOrderCgClear
  18. public function actionBc()
  19. {
  20. ini_set('memory_limit', '2045M');
  21. set_time_limit(0);
  22. if (getenv('YII_ENV') == 'production') {
  23. $autoId = 817342;
  24. } else {
  25. $autoId = 1426;
  26. }
  27. $query = new \yii\db\Query();
  28. $query->from(Clear::tableName());
  29. $query->where(['>', 'id', $autoId]);
  30. foreach ($query->batch(500) as $changeList) {
  31. $ids = array_column($changeList, 'id');
  32. $relate = OrderCgClearClass::getAllByCondition(['clearId' => ['in', $ids]], null, '*', 'clearId');
  33. $connection = Yii::$app->db;
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. foreach ($changeList as $clear) {
  37. $clearId = $clear['id'] ?? 0;
  38. $clearSn = $clear['orderSn'] ?? '';
  39. $status = $clear['status'] ?? 1;
  40. $clearAmount = $clear['actPrice'] ?? 0;
  41. $purchaseIds = $clear['purchaseIds'] ?? '';
  42. $saleIds = $clear['saleIds'] ?? '';
  43. $clearStyle = $clear['clearStyle'] ?? 1;
  44. if (!isset($relate[$clearId])) {
  45. if ($clearStyle == 3) {
  46. if (!empty($purchaseIds)) {
  47. $purchaseIdList = explode(',', $purchaseIds);
  48. $purchaseInfo = PurchaseOrderClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
  49. foreach ($purchaseInfo as $purchase) {
  50. $purchaseId = $purchase['id'] ?? 0;
  51. $purchaseOrderSn = $purchase['orderSn'] ?? 0;
  52. $addData = ['orderId' => 0, 'orderSn' => '', 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
  53. 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
  54. OrderCgClearClass::add($addData);
  55. echo "ghs cg clearId {$clearId} OK ### \n";
  56. }
  57. } else {
  58. echo "供货商采购 clearId {$clearId} 有缺数据 \n";
  59. }
  60. } else {
  61. if (!empty($saleIds) && !empty($purchaseIds)) {
  62. $saleIdList = explode(',', $saleIds);
  63. $purchaseIdList = explode(',', $purchaseIds);
  64. $saleInfo = OrderClass::getAllByCondition(['id' => ['in', $saleIdList]], null, 'id,orderSn,purchaseId');
  65. $purchaseInfo = PurchaseClass::getAllByCondition(['id' => ['in', $purchaseIdList]], null, 'id,orderSn', 'id');
  66. if (!empty($saleInfo)) {
  67. foreach ($saleInfo as $sale) {
  68. $saleId = $sale['id'] ?? 0;
  69. $purchaseId = $sale['purchaseId'] ?? 0;
  70. $saleOrderSn = $sale['orderSn'] ?? '';
  71. $purchase = $purchaseInfo[$purchaseId] ?? [];
  72. if (empty($purchase)) {
  73. echo "purchase {$purchaseId} 没有找到 \n";
  74. exit();
  75. }
  76. $purchaseOrderSn = $purchase['orderSn'] ?? '';
  77. $addData = ['orderId' => $saleId, 'orderSn' => $saleOrderSn, 'cgId' => $purchaseId, 'cgSn' => $purchaseOrderSn,
  78. 'clearId' => $clearId, 'clearSn' => $clearSn, 'amount' => $clearAmount, 'status' => $status];
  79. OrderCgClearClass::add($addData);
  80. }
  81. echo "hd cg clearId {$clearId} OK *** \n";
  82. }
  83. } else {
  84. echo "花店采购 clearId {$clearId} 有缺数据 \n";
  85. }
  86. }
  87. } else {
  88. echo "有记录 {$clearId} \n";
  89. }
  90. }
  91. $transaction->commit();
  92. } catch (\Exception $e) {
  93. $transaction->rollBack();
  94. $msg = $e->getMessage();
  95. echo "报错了:" . $msg . "\n";
  96. }
  97. }
  98. }
  99. public function actionBalance()
  100. {
  101. ini_set('memory_limit', '2045M');
  102. set_time_limit(0);
  103. //$sql = "delete from xhGhsBack";
  104. //Yii::$app->db->createCommand($sql)->execute();
  105. $query = new \yii\db\Query();
  106. $query->from(Ghs::tableName());
  107. $query->where(['ownPtStyle' => 1]);
  108. $query->andWhere(['>', 'balance', 0]);
  109. foreach ($query->batch(500) as $ghsList) {
  110. $connection = Yii::$app->db;
  111. $transaction = $connection->beginTransaction();
  112. try {
  113. foreach ($ghsList as $ghs) {
  114. GhsBackClass::add($ghs);
  115. }
  116. $transaction->commit();
  117. } catch (\Exception $e) {
  118. $transaction->rollBack();
  119. $msg = $e->getMessage();
  120. echo "报错了:" . $msg . "\n";
  121. }
  122. }
  123. }
  124. public function actionBalanceBack()
  125. {
  126. $list = GhsBackClass::getAllByCondition(['id>' => 0], null, '*', 'id', true);
  127. if (!empty($list)) {
  128. foreach ($list as $ghs) {
  129. $ownPtStyle = $ghs['ownPtStyle'] ?? 1;
  130. $balance = $ghs['balance'] ?? 0;
  131. $customId = $ghs['customId'] ?? 0;
  132. if ($ownPtStyle == 1) {
  133. $custom = CustomClass::getById($customId, true);
  134. if (!empty($custom)) {
  135. $shopId = $ghs['shopId'] ?? 0;
  136. $shop = \bizGhs\shop\classes\ShopClass::getById($shopId, true);
  137. if (!empty($shop)) {
  138. $connection = Yii::$app->db;
  139. $transaction = $connection->beginTransaction();
  140. try {
  141. $staff = new \stdClass();
  142. $staff->id = 0;
  143. $staff->name = '';
  144. $params = [
  145. 'remark' => '系统升级恢复',
  146. 'rechargeType' => 0, // 0 代表正常充值
  147. ];
  148. // 模拟商家帮充并自动销挂账,payWay 设为 0 (线下微信)
  149. GhsRechargeSettleService::merchantRechargeWithAutoClear($custom, $balance, $shop, $staff, 0, $params);
  150. $transaction->commit();
  151. echo "customId: {$customId} 充值成功 {$balance} 元\n";
  152. } catch (\Exception $e) {
  153. $transaction->rollBack();
  154. echo "customId: {$customId} 充值失败: " . $e->getMessage() . "\n";
  155. }
  156. } else {
  157. echo "customId: {$customId} 找不到对应的 shop\n";
  158. }
  159. } else {
  160. echo "customId: {$customId} 找不到对应的 custom\n";
  161. }
  162. }
  163. }
  164. }
  165. }
  166. public function actionGhsGhs()
  167. {
  168. ini_set('memory_limit', '2045M');
  169. set_time_limit(0);
  170. $query = new \yii\db\Query();
  171. $query->from(Ghs::tableName());
  172. $query->where(['ownPtStyle' => 2]);
  173. foreach ($query->batch(500) as $ghsList) {
  174. $connection = Yii::$app->db;
  175. $transaction = $connection->beginTransaction();
  176. try {
  177. foreach ($ghsList as $ghs) {
  178. $ghsId = $ghs['id'] ?? 0;
  179. $balance = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => 1], 'actPrice');
  180. $debtNum = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => 1]);
  181. GhsClass::updateById($ghsId, ['balance' => -$balance, 'debtNum' => $debtNum]);
  182. $customId = $ghs['customId'] ?? 0;
  183. CustomClass::updateById($customId, ['balance' => -$balance, 'debtNum' => $debtNum]);
  184. }
  185. $transaction->commit();
  186. } catch (\Exception $e) {
  187. $transaction->rollBack();
  188. $msg = $e->getMessage();
  189. echo "报错了:" . $msg . "\n";
  190. }
  191. }
  192. }
  193. }