field('user_id,actual_amount as pay_amount,sell_order_id') ->whereTime('createtime', 'today') ->where('warehouse_id', $warehouse_id) ->whereIn('status', [0,1]) ->whereIn('order_type', [0]) ->select(); if (!$buyer_order) { return false; } $seller_order = Db::name('order') ->field('user_id,actual_amount as pay_amount,sell_order_id') ->whereIn('id', array_column($buyer_order, "sell_order_id")) ->select(); if (!$seller_order) { return false; } $buyer_amount = abs(array_sum(array_column($buyer_order, "pay_amount"))); $seller_amount = abs(array_sum(array_column($seller_order, "pay_amount"))); // var_dump($buyer_amount); // var_dump($seller_amount); // exit(); if ($buyer_amount != $seller_amount) { return false; } $order = array_merge($buyer_order, $seller_order); $order_new = []; foreach ($order as $value) { if (!isset($order_new[$value['user_id']])) { $order_new[$value['user_id']] = $value; } else { $order_new[$value['user_id']]['pay_amount'] += $value['pay_amount']; } } $buyer_order = []; $seller_order = []; foreach ($order_new as $key => $value) { if ($value['pay_amount'] > 0) { $seller_order[] = $value; } elseif ($value['pay_amount'] < 0) { $buyer_order[] = $value; } } // echo "
";
		// //print_r($order);
		// print_r($buyer_order);
		// print_r($seller_order);
		// exit();
		$pay_record = [];
		foreach ($seller_order as $seq => &$seller) {
			if ($seller['pay_amount'] <= 0) {
				unset($seller_order[$seq]);
                continue;
            }
            foreach ($buyer_order as $key => &$buyer) {
            	if ($buyer['pay_amount'] == 0) {
            		unset($buyer_order[$key]);
	                continue;
	            }

            	if ($seller['pay_amount'] >= abs($buyer['pay_amount'])) {
	                $pay_record[] = [
	                       'payer_id' => $buyer['user_id'],
	                       'payee_id' => $seller['user_id'],
	                       'amount' => abs($buyer['pay_amount']),
	                       'createtime' => $time,
	                       'warehouse_id' => $warehouse_id,
	                       'date' => date("Y-m-d", $time),
	               ];
	              	$seller['pay_amount'] = $seller['pay_amount'] + $buyer['pay_amount'];
	                unset($buyer_order[$key]);
	          	} else {
	                $pay_record[] = [
	                      'payer_id' => $buyer['user_id'],
	                       'payee_id' => $seller['user_id'],
	                       'amount' => $seller['pay_amount'],
	                       'createtime' => $time,
	                       'warehouse_id' => $warehouse_id,
	                       'date' => date("Y-m-d", $time),
	               ];
	               $buyer['pay_amount'] = $buyer['pay_amount'] + $seller['pay_amount'];
	               unset($seller_order[$seq]);
	               $seller['pay_amount'] = 0;
	               break;
	            }
	            // echo "
";
				// print_r($buyer);
            }
		}
		unset($seller);
		// foreach ($pay_record as $key => $value) {
		// 	if ($value['amount'] == 0) {
		// 		unset($pay_record[$key]);
		// 	}
		// }
		// echo "
";
		// print_r($pay_record);
		// exit();
		
		$ret = Db::name('income_expend')->insertAll($pay_record);
		return $ret;

	}
	/**
	 * 取消订单
	 * [cancelOrder description]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function cancelOrder($id){
		$order = OrderModel::where('id', $id)->find();
        if (!$order) {
            $this->error("订单不存在");
        }
        $info = OrderModel::where('id', $id)->update(['status' => -1,'cancel_time' => time()]);
        if ($info === false) {
            $this->error("取消失败");
        }
        $detail = Detail::where("order_id", $id)->find();
        //上架商品
        Goods::where('id', $detail['goods_id'])->update(['status' => 'normal', "updatetime" => time()]);
        //分佣给买家上级
        $site = Config::get("site");
        $commission = bcmul($order['order_amount'], $site['primary_distribution'] * 0.01, 2);
        $obj = new UserService();
        $obj->userCommission(1, $this->auth->getUserinfo(), $order['order_sn'], -$commission);

        return true;
	}
	
}