| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?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 = self::resolveDebtAmount($order);
- $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 = self::resolveDebtAmount($purchase);
- $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);
- $newAmount = self::normalizeAmount($pair['amount'] ?? 0);
- if (!empty($exists)) {
- self::patchAmountIfEmpty($exists, $newAmount);
- continue;
- }
- self::add([
- 'clearId' => $clearId,
- 'clearSn' => $clearSn,
- 'orderId' => $orderId,
- 'orderSn' => $pair['orderSn'] ?? '',
- 'cgId' => $cgId,
- 'cgSn' => $pair['cgSn'] ?? '',
- 'amount' => $newAmount,
- 'status' => $status,
- ]);
- }
- }
- public static function markDoneByClearId($clearId)
- {
- self::markStatusByClearId($clearId, self::STATUS_HAS_PAY);
- }
- /**
- * 更新关联表中的结账单号 (clearSn)
- * 用途:当微信/支付宝支付由于拉卡拉规则需要生成新的结账单号时,同步更新关联表中的单号,保持数据一致性
- * 谁用:PurchaseClearController 微信/支付宝支付接口
- */
- public static function updateClearSnByClearId($clearId, $newClearSn)
- {
- $clearId = intval($clearId);
- if ($clearId <= 0 || empty($newClearSn)) {
- return;
- }
- $list = self::getAllByCondition(['clearId' => $clearId], null, '*', null, true);
- if (empty($list)) {
- return;
- }
- foreach ($list as $item) {
- $item->clearSn = $newClearSn;
- $item->save();
- }
- }
- 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)
- {
- $clearId = intval($clearId);
- if ($clearId <= 0) {
- return [];
- }
- $list = self::getAllByCondition(['clearId' => $clearId, 'orderId>' => 0], null, 'orderId', null);
- return array_values(array_unique(array_filter(array_map('intval', array_column($list, 'orderId')))));
- }
- public static function getCgIdsByClearId($clearId)
- {
- $clearId = intval($clearId);
- if ($clearId <= 0) {
- return [];
- }
- $list = self::getAllByCondition(['clearId' => $clearId, 'cgId>' => 0], null, 'cgId', null);
- return array_values(array_unique(array_filter(array_map('intval', array_column($list, 'cgId')))));
- }
- 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 resolveDebtAmount($entity)
- {
- return self::normalizeAmount(self::readField($entity, 'actPrice'));
- }
- protected static function readField($entity, $key)
- {
- return is_array($entity) ? ($entity[$key] ?? 0) : ($entity->$key ?? 0);
- }
- protected static function normalizeAmount($amount)
- {
- if ($amount === '' || $amount === null) {
- return '0.00';
- }
- return bcadd((string)$amount, '0', 2);
- }
- protected static function patchAmountIfEmpty($row, $amount)
- {
- $amount = self::normalizeAmount($amount);
- if (bccomp($amount, '0', 2) <= 0) {
- return false;
- }
- $current = self::normalizeAmount(is_object($row) ? ($row->amount ?? 0) : ($row['amount'] ?? 0));
- if (bccomp($current, '0', 2) > 0) {
- return false;
- }
- if (is_object($row)) {
- $row->amount = $amount;
- return $row->save(false);
- }
- return false;
- }
- }
|