leftJoin('order og','cm.order_id=og.order_id') ->where('cm.order_id',$orderId) ->where('cm.clearing_status',0) ->where('og.commission_settlement_time','>',0) ->where('og.commission_settlement_time','<',time()) ->where('og.commission_settlement_status',0) ->field('cm.id,cm.order_id,cm.user_id,cm.clearing_money,cm.commission_percent,og.order_no') ->select(); if ($commissionDetails->isEmpty()){ return true; } $data = []; $infos = []; try { foreach ($commissionDetails as $detail){ if(self::endCommissionDetail($detail,$data) === false){ $infos[] = '分佣失败commissionId::'.$detail->id; } } if (count($data)){ Order::whereIn('order_id',$data)->update(['commission_settlement_status'=>1]); } }catch (Exception $e){ $msg = __METHOD__.",orderId::".$orderId.'结算分佣失败'.$e->getMessage(); log_record($msg,'error'); Message::wxRobot($msg); return false; } if (count($infos)){ $infoStr = json_encode($infos); log_record('info::'.$infoStr,'error'); Message::wxRobot($infoStr); } return true; } /** * 处理单条待结算分佣记录 * @param $detail * @param $data * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function endCommissionDetail($detail,&$data): bool { $orderGoods = OrderGoods::field('order_goods_id,total_num,total_pay_price,rice_card_money,total_pay_price,frozen_status') ->where('order_id',$detail->order_id)->select(); $frozenS = $orderGoods->column('frozen_status'); $s = array_search(1,$frozenS);//查找是否有冻结中的订单商品 if (!($s === false)){ log_record(__METHOD__.':orderGoods frozen orderId:'.$detail->order_id,'error'); return false; } $saleVolume = '0'; $refundCnt = 0; foreach ($orderGoods as $og){ $rate = 1; $refund = OrderRefund::where('order_goods_id',$og->order_goods_id) ->where('finance_refund',10)->find(); $ogSale = bcadd($og['total_pay_price'],$og['rice_card_money'],4); if ($refund){ $refundCnt += 1; $rate = bcdiv(strval($og['total_num'] - $refund['goods_num']),strval($og['total_num']),4); } if ($rate <= 1 && $rate >= 0){ $saleVolume = bcadd(bcmul($ogSale,strval($rate),4),strval($saleVolume),4); } } $clearing_money = bcmul($saleVolume,strval($detail->commission_percent/100),4); $can_withdraw_money = WithdrawMoneyLog::addNewLog($detail->user_id,50,$clearing_money,'',$detail->order_id); if ($can_withdraw_money === false){ log_record(__METHOD__.':failed orderId:'.$detail->order_id,'error'); return false; } $remark = $refundCnt>0?'订单退部分扣除部分佣金':''; // 结算金额累计 CommissionsDetail::where('id', $detail->id) ->update(['clearing_money_amount'=>$can_withdraw_money, 'clearing_money'=>$clearing_money, 'order_sale_volume'=>$saleVolume, 'clearing_status'=>1, 'remark'=>$remark, 'update_time'=>time()]); if (!in_array($detail->order_id,$data)){ $data[] = $detail->order_id; } return true; } }