'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成']; */ public $order_status = [ '-1' => '全部', self::Ordered => '已下单', self::Paid => '已付款', self::Transferred => '已转让', self::Shipped => '提货', self::Cancelled => '已取消', self::Closure => '关闭', self::Freeze => '质押', ]; //抢购下单:未选择地区购买 public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId):bool { for ($i = 0; $i < $num; $i++) { $order_arr['price'] = $price; $order_arr['product_id']= $productId; $order_arr['area_id'] = 0; self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price); } return true; } //抢购下单:选择地区购买 public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId):bool { $result = true; foreach ($areaArr as $item) { // 生成订单 $order_arr['price'] = $price; $order_arr['product_id']= $productId; $order_arr['area_id'] = $item; self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price); } //修改区域状态 $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop); if($count != count($areaArr)) $result = false; return $result; } //合成订单 public static function setSynthesisOrder($synthesis, $productId, $num, $uid):bool { $result = true; foreach ($productId as $key => $item) { //获取需求数量 if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));; //判断持有数量足够 $total_num = $synthesis[$key.'_num'] * $num; if($total_num > self::getUserOrderNum($uid, $item)) throw new Exception(__("材料不足"));; //关闭持有订单 $result = self::where('user_id', $uid)->where('product_id', $item)->where('status', self::Paid)->limit($total_num)->setField('status', self::Closure); } return $result; } /** * @param int $orderId 订单id * @param array $orderInfo 订单详情 * @param int $typeId 订单类型 * @param int $userId 用户id * @param int $fromUser 来源id * @param float $fees 手续费 * @param float $fees 抢购价 */ public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object { return self::create([ 'order_id' => $orderId, 'product_id' => $orderInfo['product_id'], 'type_id' => $typeId, 'status' => self::Paid, 'area_id' => $orderInfo['area_id'], 'order_no' => $orderNo, 'user_id' => $userId, 'from_user' => $fromUser, 'price' => $orderInfo['price'], 'popular_price'=> $price, 'fees' => $fees, 'num' => 1 ]); } //获取持有产品数量 public static function getByOrderProductNum($productId, $num, $uid) { if(empty($productId)) return (object)[]; foreach ($productId as &$item) { $item['num'] = $num; $count = self::getUserOrderNum($uid, $item['id']); if(empty($count)) $count = 0; $item['hold_num'] = $count; } return $productId; } //获取持有产品数量 private static function getUserOrderNum(int $uid, int $product_id): int { return self::where('user_id', $uid)->where('product_id', $product_id)->where('status', '=', self::Paid)->sum('num'); } // 获取订单状态 public static function getProductOrder($orderId, $status, string $field){ return self::alias('a')->where('a.id', $orderId) ->join("product_list b", "a.product_id = b.id", "left") ->field('a.*,'.$field) ->where('a.status', $status) ->find(); } // 获取新人福利领取订单记录 public static function getUserWelfare($uid, $typeId){ return self::where('user_id', $uid) ->where('type_id', $typeId) ->order('create_time desc') ->find(); } public static function getStatusList() { return [self::Ordered => __('已下单'), self::Paid => __('已付款'), self::Transferred => __('已转让'), self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭'), self::Freeze => __('质押')]; } //全部类型: public static function getStatusAll() { return [ ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')], ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')], ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')], ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')], ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')], ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')], ['type_id'=>self::Popular, 'status' => self::Freeze, 'text' => __('质押')], ]; } //产品 public function products() { return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0); } //用户 user_id public function users() { return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0); } //区域 public function areas() { return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0); } //提货地址 public function address() { return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0); } public function getCreateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUpdateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } protected function setCreateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } protected function setUpdateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } }