0, 'is_fail' => 0]; $cart_select = Cart::whereIn('cart_id', $cartId)->where($where)->field('goods_id as goods,cart_num')->select()->toArray(); if (empty($cart_select)) { self::setError('购物车为空'); return false; } /** 计算价格 */ foreach ($cart_select as $k => $v) { $sell = Goods::where(['id' => $v['goods']])->value('sell'); $cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2); $cart_select[$k]['price'] = $sell; } $order = [ 'time' => time(), 'number' => static::getNewOrderId('PF'), 'total' => array_sum(array_column($cart_select, 'total')), 'pay_type' => $params['pay_type'], 'cart_id'=>explode(',',$cartId) ]; return ['order' => $order, 'cart_list' => $cart_select]; } /** * 创建新订单 * @return Object */ static public function createOrder($cartId, $addressId, $user = null, $params = []): Object { $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); $_order = $orderInfo['order']; $_order['deduction_price'] = 0; $_order['merchant'] = 0; $_order['customer'] = request()->userId; $_order['money'] = 0; $_order['user'] = request()->userId; $_order['account'] = 0; $_order['payinfo'] = ''; $_order['type'] = 0; $_order['actual'] = $_order['total']; Db::startTrans(); try { $order = Cashierclass::create($_order); $goods_list = $orderInfo['cart_list']; foreach ($goods_list as $k => $v) { $goods_list[$k]['pid'] = $order->id; $goods_list[$k]['merchant'] = 0; $goods_list[$k]['room'] = 0; $goods_list[$k]['discount'] = 0; $goods_list[$k]['warehouse'] = 0; $goods_list[$k]['nums'] = $v['cart_num']; } (new Cashierinfo())->saveAll($goods_list); Db::commit(); return $order; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 获取购货订单购物车商品信息 * @param $params * @return array */ static public function cartIdByPurchaseOrderInfo($params = []) { $where1=['paid'=>1]; d(222); $cartId=Cashierinfo::where('store_id',$params['store_id'])->where($where1)->select(); throw new \RuntimeException("Command has no defaultName"); $cartId=implode(',',$cartId); var_dump($cartId); $where = ['is_pay' => 0, 'is_fail' => 0]; $cart_select = Cart::whereIn('cart_id', $cartId)->where($where)->field('goods_id as goods,cart_num')->select()->toArray(); if (empty($cart_select)) { self::setError('购物车为空'); return false; } /** 计算价格 */ foreach ($cart_select as $k => $v) { $sell = Goods::where(['id' => $v['goods']])->value('sell'); $cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2); $cart_select[$k]['price'] = $sell; } $order = [ 'time' => time(), 'number' => static::getNewOrderId('PF'), 'total' => array_sum(array_column($cart_select, 'total')), 'pay_type' => $params['pay_type'], 'cart_id'=>explode(',',$cartId) ]; return ['order' => $order, 'cart_list' => $cart_select]; } /** * 创建购货订单 * @return Object */ static public function createPurchaseOrder($cartId, $addressId, $user = null, $params = []): Object { $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); $_order = $orderInfo['order']; $_order['deduction_price'] = 0; $_order['merchant'] = 0; $_order['customer'] = request()->userId; $_order['money'] = 0; $_order['user'] = request()->userId; $_order['account'] = 0; $_order['payinfo'] = ''; $_order['type'] = 0; $_order['actual'] = $_order['total']; Db::startTrans(); try { $order = Cashierclass::create($_order); $goods_list = $orderInfo['cart_list']; foreach ($goods_list as $k => $v) { $goods_list[$k]['pid'] = $order->id; $goods_list[$k]['merchant'] = 0; $goods_list[$k]['room'] = 0; $goods_list[$k]['discount'] = 0; $goods_list[$k]['warehouse'] = 0; $goods_list[$k]['nums'] = $v['cart_num']; } (new Cashierinfo())->saveAll($goods_list); Db::commit(); return $order; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 获取订单号 * @param $type * @return string * @author likeadmin * @date 2021/7/28 17:05 */ static public function getNewOrderId($type) { list($msec, $sec) = explode(' ', microtime()); $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)); return $orderId; } }