Просмотр исходного кода

Merge branch 'clearimprove' into dev

shish 2 месяцев назад
Родитель
Сommit
c2dd456b92

+ 2 - 0
app-ghs/controllers/OrderClearController.php

@@ -4,6 +4,7 @@ namespace ghs\controllers;
 
 
 use biz\ghs\classes\GhsClass;
 use biz\ghs\classes\GhsClass;
 use biz\wx\classes\WxMessageClass;
 use biz\wx\classes\WxMessageClass;
+use bizGhs\clear\classes\OrderCgClearClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\order\classes\OrderClearClass;
 use bizGhs\order\classes\OrderClearClass;
 use bizGhs\shop\classes\ShopClass;
 use bizGhs\shop\classes\ShopClass;
@@ -67,6 +68,7 @@ class OrderClearController extends BaseController
                 //只要新建账单,老账单就取消掉
                 //只要新建账单,老账单就取消掉
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->save();
                 $item->save();
+                OrderCgClearClass::markExpireByClearId($item->id);
             }
             }
         }
         }
 
 

+ 2 - 0
app-hd/controllers/PurchaseClearController.php

@@ -3,6 +3,7 @@
 namespace hd\controllers;
 namespace hd\controllers;
 
 
 use biz\ghs\classes\GhsClass;
 use biz\ghs\classes\GhsClass;
+use bizHd\purchase\classes\OrderCgClearClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 use bizHd\shop\classes\ShopClass;
 use bizHd\shop\classes\ShopClass;
 use bizHd\wx\classes\WxOpenClass;
 use bizHd\wx\classes\WxOpenClass;
@@ -44,6 +45,7 @@ class PurchaseClearController extends BaseController
             foreach ($list as $item) {
             foreach ($list as $item) {
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->save();
                 $item->save();
+                OrderCgClearClass::markExpireByClearId($item->id);
 
 
                 if (!empty($ghsShop)) {
                 if (!empty($ghsShop)) {
                     $orderSn = $item->orderSn ?? '';
                     $orderSn = $item->orderSn ?? '';

+ 277 - 0
biz-ghs/clear/classes/OrderCgClearClass.php

@@ -0,0 +1,277 @@
+<?php
+
+namespace bizGhs\clear\classes;
+
+use bizGhs\base\classes\BaseClass;
+use bizGhs\order\classes\OrderClass;
+use bizHd\purchase\classes\PurchaseClass;
+use bizHd\purchase\classes\PurchaseClearClass;
+use common\components\dict;
+
+class OrderCgClearClass extends BaseClass
+{
+
+    public static $baseFile = '\bizGhs\clear\models\OrderCgClear';
+
+    const STATUS_AWAIT_PAY = 1;
+    const STATUS_HAS_PAY = 2;
+    const STATUS_EXPIRE = 3;
+
+    public static function buildPairsFromOrders($orderList)
+    {
+        if (empty($orderList)) {
+            return [];
+        }
+        $purchaseIds = [];
+        foreach ($orderList as $order) {
+            $purchaseId = is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0);
+            if (!empty($purchaseId)) {
+                $purchaseIds[] = $purchaseId;
+            }
+        }
+        $cgMap = [];
+        if (!empty($purchaseIds)) {
+            $cgList = PurchaseClass::getByIds(array_unique($purchaseIds));
+            if (!empty($cgList)) {
+                $cgMap = array_column($cgList, null, 'id');
+            }
+        }
+        $pairs = [];
+        foreach ($orderList as $order) {
+            $orderId = is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0);
+            $orderSn = is_array($order) ? ($order['orderSn'] ?? '') : ($order->orderSn ?? '');
+            $amount = is_array($order) ? ($order['remainDebtPrice'] ?? 0) : ($order->remainDebtPrice ?? 0);
+            $cgId = intval(is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0));
+            $cgSn = '';
+            if ($cgId > 0 && isset($cgMap[$cgId])) {
+                $cgSn = $cgMap[$cgId]['orderSn'] ?? '';
+            }
+            $pairs[] = [
+                'orderId' => intval($orderId),
+                'orderSn' => $orderSn,
+                'cgId' => $cgId,
+                'cgSn' => $cgSn,
+                'amount' => $amount,
+            ];
+        }
+        return $pairs;
+    }
+
+    public static function buildPairsFromPurchases($purchaseList)
+    {
+        if (empty($purchaseList)) {
+            return [];
+        }
+        $saleIds = [];
+        foreach ($purchaseList as $purchase) {
+            $saleId = is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0);
+            if (!empty($saleId)) {
+                $saleIds[] = $saleId;
+            }
+        }
+        $orderMap = [];
+        if (!empty($saleIds)) {
+            $orderList = OrderClass::getByIds(array_unique($saleIds));
+            if (!empty($orderList)) {
+                $orderMap = array_column($orderList, null, 'id');
+            }
+        }
+        $pairs = [];
+        foreach ($purchaseList as $purchase) {
+            $cgId = is_array($purchase) ? ($purchase['id'] ?? 0) : ($purchase->id ?? 0);
+            $cgSn = is_array($purchase) ? ($purchase['orderSn'] ?? '') : ($purchase->orderSn ?? '');
+            $amount = is_array($purchase) ? ($purchase['remainDebtPrice'] ?? 0) : ($purchase->remainDebtPrice ?? 0);
+            $orderId = intval(is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0));
+            $orderSn = '';
+            if ($orderId > 0 && isset($orderMap[$orderId])) {
+                $orderSn = $orderMap[$orderId]['orderSn'] ?? '';
+            }
+            $pairs[] = [
+                'orderId' => $orderId,
+                'orderSn' => $orderSn,
+                'cgId' => intval($cgId),
+                'cgSn' => $cgSn,
+                'amount' => $amount,
+            ];
+        }
+        return $pairs;
+    }
+
+    public static function bindRelations($clear, $pairs, $status = self::STATUS_AWAIT_PAY)
+    {
+        $clearId = is_object($clear) ? ($clear->id ?? 0) : ($clear['id'] ?? 0);
+        $clearSn = is_object($clear) ? ($clear->orderSn ?? '') : ($clear['orderSn'] ?? '');
+        if (empty($clearId) || empty($pairs)) {
+            return;
+        }
+        foreach ($pairs as $pair) {
+            $orderId = intval($pair['orderId'] ?? 0);
+            $cgId = intval($pair['cgId'] ?? 0);
+            if ($orderId <= 0 && $cgId <= 0) {
+                continue;
+            }
+            $exists = self::getByCondition([
+                'clearId' => $clearId,
+                'orderId' => $orderId,
+                'cgId' => $cgId,
+            ], true);
+            if (!empty($exists)) {
+                continue;
+            }
+            self::add([
+                'clearId' => $clearId,
+                'clearSn' => $clearSn,
+                'orderId' => $orderId,
+                'orderSn' => $pair['orderSn'] ?? '',
+                'cgId' => $cgId,
+                'cgSn' => $pair['cgSn'] ?? '',
+                'amount' => $pair['amount'] ?? 0,
+                'status' => $status,
+            ]);
+        }
+    }
+
+    public static function markDoneByClearId($clearId)
+    {
+        self::markStatusByClearId($clearId, self::STATUS_HAS_PAY);
+    }
+
+    public static function markExpireByClearId($clearId)
+    {
+        self::markStatusByClearId($clearId, self::STATUS_EXPIRE);
+    }
+
+    public static function markStatusByClearId($clearId, $status)
+    {
+        $clearId = intval($clearId);
+        $status = intval($status);
+        if ($clearId <= 0) {
+            return;
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
+        if (empty($list)) {
+            return;
+        }
+        foreach ($list as $item) {
+            $item->status = $status;
+            $item->save();
+        }
+    }
+
+    public static function getOrderIdsByClearId($clearId, $fallbackSaleIds = '')
+    {
+        $clearId = intval($clearId);
+        if ($clearId <= 0) {
+            return self::parseIds($fallbackSaleIds);
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId, 'orderId>' => 0], null, 'orderId', null);
+        $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'orderId')))));
+        if (!empty($ids)) {
+            return $ids;
+        }
+        return self::parseIds($fallbackSaleIds);
+    }
+
+    public static function getCgIdsByClearId($clearId, $fallbackPurchaseIds = '')
+    {
+        $clearId = intval($clearId);
+        if ($clearId <= 0) {
+            return self::parseIds($fallbackPurchaseIds);
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId, 'cgId>' => 0], null, 'cgId', null);
+        $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'cgId')))));
+        if (!empty($ids)) {
+            return $ids;
+        }
+        return self::parseIds($fallbackPurchaseIds);
+    }
+
+    public static function getPendingClearIdByOrderId($orderId, $customId)
+    {
+        $orderId = intval($orderId);
+        $customId = intval($customId);
+        if ($orderId <= 0) {
+            return 0;
+        }
+        $list = self::getAllByCondition([
+            'orderId' => $orderId,
+            'status' => self::STATUS_AWAIT_PAY,
+        ], null, 'clearId', null);
+        if (empty($list)) {
+            return 0;
+        }
+        $clearIds = array_unique(array_map('intval', array_column($list, 'clearId')));
+        $now = date("Y-m-d H:i:s");
+        foreach ($clearIds as $clearId) {
+            $clear = ClearClass::getById($clearId, true);
+            if (empty($clear)) {
+                continue;
+            }
+            if (($clear->status ?? 0) != PurchaseClearClass::STATUS_AWAIT_PAY) {
+                continue;
+            }
+            if ($customId > 0 && ($clear->customId ?? 0) != $customId) {
+                continue;
+            }
+            $deadline = $clear->deadline ?? '';
+            if (!empty($deadline) && $deadline < $now) {
+                continue;
+            }
+            return intval($clearId);
+        }
+        return 0;
+    }
+
+    public static function backfillFromClear($clear)
+    {
+        $clearId = is_object($clear) ? ($clear->id ?? 0) : ($clear['id'] ?? 0);
+        if (empty($clearId)) {
+            return 0;
+        }
+        $exists = self::getByCondition(['clearId' => $clearId], true);
+        if (!empty($exists)) {
+            return 0;
+        }
+        $clearStyle = is_object($clear) ? ($clear->clearStyle ?? 0) : ($clear['clearStyle'] ?? 0);
+        $hd2Gys = dict::getDict('clearStyle', 'hd2Gys');
+        $gys2Hd = dict::getDict('clearStyle', 'gys2Hd');
+        if (!in_array(intval($clearStyle), [intval($hd2Gys), intval($gys2Hd)])) {
+            return 0;
+        }
+        $saleIds = is_object($clear) ? ($clear->saleIds ?? '') : ($clear['saleIds'] ?? '');
+        $purchaseIds = is_object($clear) ? ($clear->purchaseIds ?? '') : ($clear['purchaseIds'] ?? '');
+        $pairs = [];
+        $saleArr = self::parseIds($saleIds);
+        if (!empty($saleArr)) {
+            $orderList = OrderClass::getByIds($saleArr);
+            if (!empty($orderList)) {
+                $pairs = self::buildPairsFromOrders($orderList);
+            }
+        } elseif (!empty(self::parseIds($purchaseIds))) {
+            $cgArr = self::parseIds($purchaseIds);
+            $purchaseList = PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null, true);
+            if (!empty($purchaseList)) {
+                $pairs = self::buildPairsFromPurchases($purchaseList);
+            }
+        }
+        if (empty($pairs)) {
+            return 0;
+        }
+        $status = intval(is_object($clear) ? ($clear->status ?? 0) : ($clear['status'] ?? 0));
+        if (!in_array($status, [self::STATUS_AWAIT_PAY, self::STATUS_HAS_PAY, self::STATUS_EXPIRE], true)) {
+            $status = self::STATUS_AWAIT_PAY;
+        }
+        self::bindRelations($clear, $pairs, $status);
+        return count($pairs);
+    }
+
+    protected static function parseIds($idStr)
+    {
+        if ($idStr === '' || $idStr === null) {
+            return [];
+        }
+        $arr = explode(',', (string)$idStr);
+        return array_values(array_unique(array_filter(array_map('intval', $arr))));
+    }
+
+}

+ 15 - 0
biz-ghs/clear/models/OrderCgClear.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace bizGhs\clear\models;
+
+use bizGhs\base\models\Base;
+
+class OrderCgClear extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhOrderCgClear';
+    }
+
+}

+ 5 - 0
biz-ghs/order/classes/OrderClearClass.php

@@ -12,6 +12,7 @@ use common\components\noticeUtil;
 use common\components\orderSn;
 use common\components\orderSn;
 use common\components\util;
 use common\components\util;
 use bizGhs\base\classes\BaseClass;
 use bizGhs\base\classes\BaseClass;
+use bizGhs\clear\classes\OrderCgClearClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 
 
@@ -82,6 +83,7 @@ class OrderClearClass extends BaseClass
                 //只要新建账单,老账单就取消掉
                 //只要新建账单,老账单就取消掉
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->status = PurchaseClearClass::STATUS_EXPIRE;
                 $item->save();
                 $item->save();
+                OrderCgClearClass::markExpireByClearId($item->id);
             }
             }
         }
         }
 
 
@@ -225,6 +227,8 @@ class OrderClearClass extends BaseClass
             util::fail('结账金额大于总欠款金额');
             util::fail('结账金额大于总欠款金额');
         }
         }
         $return = PurchaseClearClass::add($clearData, true);
         $return = PurchaseClearClass::add($clearData, true);
+        $pairs = OrderCgClearClass::buildPairsFromOrders($list);
+        OrderCgClearClass::bindRelations($return, $pairs);
         $complete = $post['complete'] ?? 0;
         $complete = $post['complete'] ?? 0;
         if ($complete == 1) {
         if ($complete == 1) {
             self::confirmClear($return, $payWay);
             self::confirmClear($return, $payWay);
@@ -316,6 +320,7 @@ class OrderClearClass extends BaseClass
             ];
             ];
             ShopMoneyChangeClass::addData($change);
             ShopMoneyChangeClass::addData($change);
         }
         }
+        OrderCgClearClass::markDoneByClearId($clearId);
     }
     }
 
 
 }
 }

+ 233 - 0
biz-hd/purchase/classes/OrderCgClearClass.php

@@ -0,0 +1,233 @@
+<?php
+
+namespace bizHd\purchase\classes;
+
+use bizGhs\clear\classes\ClearClass;
+use bizGhs\order\classes\OrderClass;
+use bizHd\base\classes\BaseClass;
+use common\components\dict;
+
+class OrderCgClearClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\purchase\models\OrderCgClear';
+
+    const STATUS_AWAIT_PAY = 1;
+    const STATUS_HAS_PAY = 2;
+    const STATUS_EXPIRE = 3;
+
+    public static function buildPairsFromOrders($orderList)
+    {
+        if (empty($orderList)) {
+            return [];
+        }
+        $purchaseIds = [];
+        foreach ($orderList as $order) {
+            $purchaseId = is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0);
+            if (!empty($purchaseId)) {
+                $purchaseIds[] = $purchaseId;
+            }
+        }
+        $cgMap = [];
+        if (!empty($purchaseIds)) {
+            $cgList = PurchaseClass::getByIds(array_unique($purchaseIds));
+            if (!empty($cgList)) {
+                $cgMap = array_column($cgList, null, 'id');
+            }
+        }
+        $pairs = [];
+        foreach ($orderList as $order) {
+            $orderId = is_array($order) ? ($order['id'] ?? 0) : ($order->id ?? 0);
+            $orderSn = is_array($order) ? ($order['orderSn'] ?? '') : ($order->orderSn ?? '');
+            $amount = is_array($order) ? ($order['remainDebtPrice'] ?? 0) : ($order->remainDebtPrice ?? 0);
+            $cgId = intval(is_array($order) ? ($order['purchaseId'] ?? 0) : ($order->purchaseId ?? 0));
+            $cgSn = '';
+            if ($cgId > 0 && isset($cgMap[$cgId])) {
+                $cgSn = $cgMap[$cgId]['orderSn'] ?? '';
+            }
+            $pairs[] = [
+                'orderId' => intval($orderId),
+                'orderSn' => $orderSn,
+                'cgId' => $cgId,
+                'cgSn' => $cgSn,
+                'amount' => $amount,
+            ];
+        }
+        return $pairs;
+    }
+
+    public static function buildPairsFromPurchases($purchaseList)
+    {
+        if (empty($purchaseList)) {
+            return [];
+        }
+        $saleIds = [];
+        foreach ($purchaseList as $purchase) {
+            $saleId = is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0);
+            if (!empty($saleId)) {
+                $saleIds[] = $saleId;
+            }
+        }
+        $orderMap = [];
+        if (!empty($saleIds)) {
+            $orderList = OrderClass::getByIds(array_unique($saleIds));
+            if (!empty($orderList)) {
+                $orderMap = array_column($orderList, null, 'id');
+            }
+        }
+        $pairs = [];
+        foreach ($purchaseList as $purchase) {
+            $cgId = is_array($purchase) ? ($purchase['id'] ?? 0) : ($purchase->id ?? 0);
+            $cgSn = is_array($purchase) ? ($purchase['orderSn'] ?? '') : ($purchase->orderSn ?? '');
+            $amount = is_array($purchase) ? ($purchase['remainDebtPrice'] ?? 0) : ($purchase->remainDebtPrice ?? 0);
+            $orderId = intval(is_array($purchase) ? ($purchase['saleId'] ?? 0) : ($purchase->saleId ?? 0));
+            $orderSn = '';
+            if ($orderId > 0 && isset($orderMap[$orderId])) {
+                $orderSn = $orderMap[$orderId]['orderSn'] ?? '';
+            }
+            $pairs[] = [
+                'orderId' => $orderId,
+                'orderSn' => $orderSn,
+                'cgId' => intval($cgId),
+                'cgSn' => $cgSn,
+                'amount' => $amount,
+            ];
+        }
+        return $pairs;
+    }
+
+    public static function bindRelations($clear, $pairs, $status = self::STATUS_AWAIT_PAY)
+    {
+        $clearId = is_object($clear) ? ($clear->id ?? 0) : ($clear['id'] ?? 0);
+        $clearSn = is_object($clear) ? ($clear->orderSn ?? '') : ($clear['orderSn'] ?? '');
+        if (empty($clearId) || empty($pairs)) {
+            return;
+        }
+        foreach ($pairs as $pair) {
+            $orderId = intval($pair['orderId'] ?? 0);
+            $cgId = intval($pair['cgId'] ?? 0);
+            if ($orderId <= 0 && $cgId <= 0) {
+                continue;
+            }
+            $exists = self::getByCondition([
+                'clearId' => $clearId,
+                'orderId' => $orderId,
+                'cgId' => $cgId,
+            ], true);
+            if (!empty($exists)) {
+                continue;
+            }
+            self::add([
+                'clearId' => $clearId,
+                'clearSn' => $clearSn,
+                'orderId' => $orderId,
+                'orderSn' => $pair['orderSn'] ?? '',
+                'cgId' => $cgId,
+                'cgSn' => $pair['cgSn'] ?? '',
+                'amount' => $pair['amount'] ?? 0,
+                'status' => $status,
+            ]);
+        }
+    }
+
+    public static function markDoneByClearId($clearId)
+    {
+        self::markStatusByClearId($clearId, self::STATUS_HAS_PAY);
+    }
+
+    public static function markExpireByClearId($clearId)
+    {
+        self::markStatusByClearId($clearId, self::STATUS_EXPIRE);
+    }
+
+    public static function markStatusByClearId($clearId, $status)
+    {
+        $clearId = intval($clearId);
+        $status = intval($status);
+        if ($clearId <= 0) {
+            return;
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
+        if (empty($list)) {
+            return;
+        }
+        foreach ($list as $item) {
+            $item->status = $status;
+            $item->save();
+        }
+    }
+
+    public static function getOrderIdsByClearId($clearId, $fallbackSaleIds = '')
+    {
+        $clearId = intval($clearId);
+        if ($clearId <= 0) {
+            return self::parseIds($fallbackSaleIds);
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId, 'orderId>' => 0], null, 'orderId', null);
+        $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'orderId')))));
+        if (!empty($ids)) {
+            return $ids;
+        }
+        return self::parseIds($fallbackSaleIds);
+    }
+
+    public static function getCgIdsByClearId($clearId, $fallbackPurchaseIds = '')
+    {
+        $clearId = intval($clearId);
+        if ($clearId <= 0) {
+            return self::parseIds($fallbackPurchaseIds);
+        }
+        $list = self::getAllByCondition(['clearId' => $clearId, 'cgId>' => 0], null, 'cgId', null);
+        $ids = array_values(array_unique(array_filter(array_map('intval', array_column($list, 'cgId')))));
+        if (!empty($ids)) {
+            return $ids;
+        }
+        return self::parseIds($fallbackPurchaseIds);
+    }
+
+    public static function getPendingClearIdByOrderId($orderId, $customId)
+    {
+        $orderId = intval($orderId);
+        $customId = intval($customId);
+        if ($orderId <= 0) {
+            return 0;
+        }
+        $list = self::getAllByCondition([
+            'orderId' => $orderId,
+            'status' => self::STATUS_AWAIT_PAY,
+        ], null, 'clearId', null);
+        if (empty($list)) {
+            return 0;
+        }
+        $clearIds = array_unique(array_map('intval', array_column($list, 'clearId')));
+        $now = date("Y-m-d H:i:s");
+        foreach ($clearIds as $clearId) {
+            $clear = ClearClass::getById($clearId, true);
+            if (empty($clear)) {
+                continue;
+            }
+            if (($clear->status ?? 0) != PurchaseClearClass::STATUS_AWAIT_PAY) {
+                continue;
+            }
+            if ($customId > 0 && ($clear->customId ?? 0) != $customId) {
+                continue;
+            }
+            $deadline = $clear->deadline ?? '';
+            if (!empty($deadline) && $deadline < $now) {
+                continue;
+            }
+            return intval($clearId);
+        }
+        return 0;
+    }
+
+    protected static function parseIds($idStr)
+    {
+        if ($idStr === '' || $idStr === null) {
+            return [];
+        }
+        $arr = explode(',', (string)$idStr);
+        return array_values(array_unique(array_filter(array_map('intval', $arr))));
+    }
+
+}

+ 5 - 1
biz-hd/purchase/classes/PurchaseClearClass.php

@@ -92,7 +92,10 @@ class PurchaseClearClass extends BaseClass
             'ghsShopName' => $ghsShopName,
             'ghsShopName' => $ghsShopName,
         ];
         ];
         $data = array_merge($data, $params);
         $data = array_merge($data, $params);
-        return self::add($data);
+        $clear = self::add($data);
+        $pairs = OrderCgClearClass::buildPairsFromPurchases($purchaseList);
+        OrderCgClearClass::bindRelations($clear, $pairs);
+        return $clear;
     }
     }
 
 
     //第三方支付回调 ssh 2021.4.28
     //第三方支付回调 ssh 2021.4.28
@@ -217,6 +220,7 @@ class PurchaseClearClass extends BaseClass
 
 
         //客户付供货商款减少
         //客户付供货商款减少
         CustomClass::clearDebtAmountReduce($custom, $ghs, $order->actPrice, $order);
         CustomClass::clearDebtAmountReduce($custom, $ghs, $order->actPrice, $order);
+        OrderCgClearClass::markDoneByClearId($clearId);
     }
     }
 
 
 }
 }

+ 15 - 0
biz-hd/purchase/models/OrderCgClear.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace bizHd\purchase\models;
+
+use bizHd\base\models\Base;
+
+class OrderCgClear extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhOrderCgClear';
+    }
+
+}

+ 102 - 3
console/controllers/ClearController.php

@@ -2,15 +2,36 @@
 
 
 namespace console\controllers;
 namespace console\controllers;
 
 
-use bizGhs\order\classes\OrderClass;
+use bizGhs\clear\classes\OrderCgClearClass;
 use bizGhs\order\classes\OrderClearClass;
 use bizGhs\order\classes\OrderClearClass;
+use bizGhs\order\classes\OrderClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\purchase\classes\PurchaseClass;
+use bizHd\purchase\classes\PurchaseClearClass;
+use common\components\dict;
 use yii\console\Controller;
 use yii\console\Controller;
-use Yii;
+use yii\console\ExitCode;
 
 
 class ClearController extends Controller
 class ClearController extends Controller
 {
 {
 
 
+    /** @var int 是否仅预览,1=是 */
+    public $dryRun = 0;
+
+    /** @var int 每批处理条数 */
+    public $batchSize = 200;
+
+    /** @var int 指定结账单 ID,0=全部 */
+    public $clearId = 0;
+
+    public function options($actionID)
+    {
+        return array_merge(parent::options($actionID), [
+            'dryRun',
+            'batchSize',
+            'clearId',
+        ]);
+    }
+
     //更新 ssh 2023503
     //更新 ssh 2023503
     public function actionUpdate()
     public function actionUpdate()
     {
     {
@@ -38,4 +59,82 @@ class ClearController extends Controller
 
 
     }
     }
 
 
-}
+    /**
+     * 历史 xhClear 回填 xhOrderCgClear
+     *
+     * 用法:
+     *   php yii clear/backfill-order-cg-clear
+     *   php yii clear/backfill-order-cg-clear --dryRun=1
+     *   php yii clear/backfill-order-cg-clear --clearId=123
+     */
+    public function actionBackfillOrderCgClear()
+    {
+        ini_set('memory_limit', '2045M');
+        set_time_limit(0);
+
+        if ($this->batchSize < 1) {
+            $this->stderr("batchSize 必须大于 0\n");
+            return ExitCode::UNSPECIFIED_ERROR;
+        }
+
+        $hd2Gys = dict::getDict('clearStyle', 'hd2Gys');
+        $gys2Hd = dict::getDict('clearStyle', 'gys2Hd');
+        $where = [
+            'clearStyle' => ['in', [intval($hd2Gys), intval($gys2Hd)]],
+        ];
+        if ($this->clearId > 0) {
+            $where['id'] = intval($this->clearId);
+        }
+
+        $lastId = 0;
+        $total = 0;
+        $inserted = 0;
+        $skipped = 0;
+
+        $this->stdout(sprintf(
+            "开始回填 xhOrderCgClear,dryRun=%d,batchSize=%d,clearId=%d\n",
+            (int)$this->dryRun,
+            $this->batchSize,
+            (int)$this->clearId
+        ));
+
+        while (true) {
+            $batchWhere = $where;
+            if ($lastId > 0) {
+                $batchWhere['id>'] = $lastId;
+            }
+            $list = PurchaseClearClass::getLimitList('*', $batchWhere, $this->batchSize, 'id asc');
+            if (empty($list)) {
+                break;
+            }
+            foreach ($list as $clear) {
+                $total++;
+                $lastId = intval($clear['id'] ?? 0);
+                $saleIds = trim($clear['saleIds'] ?? '');
+                $purchaseIds = trim($clear['purchaseIds'] ?? '');
+                if ($saleIds === '' && $purchaseIds === '') {
+                    $skipped++;
+                    continue;
+                }
+                if ($this->dryRun) {
+                    $this->stdout("dryRun clearId={$lastId} saleIds={$saleIds} purchaseIds={$purchaseIds}\n");
+                    continue;
+                }
+                $count = OrderCgClearClass::backfillFromClear($clear);
+                if ($count > 0) {
+                    $inserted += $count;
+                    $this->stdout("clearId={$lastId} 写入 {$count} 行\n");
+                } else {
+                    $skipped++;
+                }
+            }
+            if (count($list) < $this->batchSize) {
+                break;
+            }
+        }
+
+        $this->stdout("完成:扫描 {$total} 条,写入 {$inserted} 行关系,跳过 {$skipped} 条\n");
+        return ExitCode::OK;
+    }
+
+}