hasMany('app\\common\\model\\order\\OrderProduct', 'order_id', 'order_id')->hidden(['content']); } /** * 关联订单收货地址表 */ public function address() { return $this->hasOne('app\\common\\model\\order\\OrderAddress'); } /** * 关联自提订单联系方式 */ public function extract() { return $this->hasOne('app\\common\\model\\order\\OrderExtract'); } /** * 关联物流公司表 */ public function express() { return $this->belongsTo('app\\common\\model\\settings\\Express', 'express_id', 'express_id'); } /** * 关联自提门店表 */ public function extractStore() { return $this->belongsTo('app\\common\\model\\store\\Store', 'extract_store_id', 'store_id'); } /** * 关联门店店员表 */ public function extractClerk() { return $this->belongsTo('app\\common\\model\\store\\Clerk', 'extract_clerk_id'); } /** * 关联用户表 */ public function user() { return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id'); } /** * 关联用户表 */ public function room() { return $this->belongsTo('app\\common\\model\\plus\\live\\Room', 'room_id', 'room_id'); } /** * 关联供应商表 */ public function supplier() { return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id')->field(['shop_supplier_id', 'name','user_id']); } /** * 订单状态文字描述 * @param $value * @param $data * @return string */ public function getStateTextAttr($value, $data) { // 订单状态 if (in_array($data['order_status'], [20, 30])) { $orderStatus = [20 => '已取消', 30 => '已完成']; return $orderStatus[$data['order_status']]; } // 付款状态 if ($data['pay_status'] == 10) { return '待付款'; } // 拼团状态 if($data['order_source'] == OrderSourceEnum::ASSEMBLE){ $assemble_status = $this->getAssembleStatus($data); if($assemble_status != ''){ return $assemble_status; } } // 发货状态 if ($data['delivery_status'] == 10) { return '已付款,待发货'; } if ($data['receipt_status'] == 10) { return '已发货,待收货'; } return $value; } /** * 拼团订单状态 */ private function getAssembleStatus($data){ // 发货状态 if ($data['assemble_status'] == 10) { return '已付款,未成团'; } if ($data['assemble_status'] == 20 && $data['delivery_status'] == 10) { return '拼团成功,待发货'; } if ($data['assemble_status'] == 30) { return '拼团失败'; } return ''; } /** * 付款状态 * @param $value * @return array */ public function getPayTypeAttr($value) { return ['text' => OrderPayTypeEnum::data()[$value]['name'], 'value' => $value]; } /** * 订单来源 * @param $value * @return array */ public function getOrderSourceTextAttr($value, $data) { return OrderSourceEnum::data()[$data['order_source']]['name']; } /** * 付款状态 * @param $value * @return array */ public function getPayStatusAttr($value) { return ['text' => OrderPayStatusEnum::data()[$value]['name'], 'value' => $value]; } /** * 改价金额(差价) * @param $value * @return array */ public function getUpdatePriceAttr($value) { return [ 'symbol' => $value < 0 ? '-' : '+', 'value' => sprintf('%.2f', abs($value)) ]; } /** * 发货状态 * @param $value * @return array */ public function getDeliveryStatusAttr($value) { $status = [10 => '待发货', 20 => '已发货']; return ['text' => $status[$value], 'value' => $value]; } /** * 收货状态 * @param $value * @return array */ public function getReceiptStatusAttr($value) { $status = [10 => '待收货', 20 => '已收货']; return ['text' => $status[$value], 'value' => $value]; } /** * 收货状态 * @param $value * @return array */ public function getOrderStatusAttr($value) { $status = [10 => '进行中', 20 => '已取消', 21 => '待取消', 30 => '已完成']; return ['text' => $status[$value], 'value' => $value]; } /** * 配送方式 * @param $value * @return array */ public function getDeliveryTypeAttr($value) { return ['text' => DeliveryTypeEnum::data()[$value]['name'], 'value' => $value]; } /** * 订单详情 * @param $where * @param string[] $with * @return array|\think\Model|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function detail($where, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk', 'supplier']) { is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where; return (new static())->with($with)->where($filter)->find(); } /** * 订单详情 */ public static function detailByNo($order_no, $with = ['user', 'address', 'product' => ['image', 'refund'], 'extract', 'express', 'extractStore.logo', 'extractClerk', 'supplier']) { return (new static())->with($with)->where('order_no', '=', $order_no)->find(); } /** * 批量获取订单列表 */ public function getListByIds($orderIds, $with = []) { $data = $this->getListByInArray('order_id', $orderIds, $with); return helper::arrayColumn2Key($data, 'order_id'); } /** * 批量更新订单 */ public function onBatchUpdate($orderIds, $data) { return $this->where('order_id', 'in', $orderIds)->save($data); } /** * 批量获取订单列表 */ private function getListByInArray($field, $data, $with = []) { return $this->with($with) ->where($field, 'in', $data) ->where('is_delete', '=', 0) ->select(); } /** * 生成订单号 */ public function orderNo() { return OrderService::createOrderNo(); } /** * 确认核销(自提订单) */ public function verificationOrder($extractClerkId) { if ( $this['pay_status']['value'] != 20 || $this['delivery_type']['value'] != DeliveryTypeEnum::EXTRACT || $this['delivery_status']['value'] == 20 || in_array($this['order_status']['value'], [20, 21]) ) { $this->error = '该订单不满足核销条件'; return false; } return $this->transaction(function () use ($extractClerkId) { // 更新订单状态:已发货、已收货 $status = $this->save([ 'extract_clerk_id' => $extractClerkId, // 核销员 'delivery_status' => 20, 'delivery_time' => time(), 'receipt_status' => 20, 'receipt_time' => time(), 'order_status' => 30 ]); // 新增订单核销记录 StoreOrderModel::add( $this['order_id'], $this['extract_store_id'], $this['extract_clerk_id'], $this['shop_supplier_id'], OrderTypeEnum::MASTER ); // 执行订单完成后的操作 $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER); $OrderCompleteService->complete([$this], static::$app_id); return $status; }); } /** * 获取已付款订单总数 (可指定某天) */ public function getOrderData($startDate = null, $endDate = null, $type, $shop_supplier_id = 0) { $model = $this; !is_null($startDate) && $model = $model->where('pay_time', '>=', strtotime($startDate)); if(is_null($endDate)){ !is_null($startDate) && $model = $model->where('pay_time', '<', strtotime($startDate) + 86400); }else{ $model = $model->where('pay_time', '<', strtotime($endDate) + 86400); } if($shop_supplier_id > 0){ $model = $model->where('shop_supplier_id', '=', $shop_supplier_id); } $model = $model->where('is_delete', '=', 0) ->where('pay_status', '=', 20) ->where('order_status', '<>', 20); if($type == 'order_total'){ // 订单数量 return $model->count(); }else if($type == 'order_total_price'){ // 订单总金额 return $model->sum('pay_price'); }else if($type == 'order_user_total'){ // 支付用户数 return count($model->distinct(true)->column('user_id')); } return 0; } /** * 修改订单价格 */ public function updatePrice($data) { if ($this['pay_status']['value'] != 10) { $this->error = '该订单不合法'; return false; } if ($this['order_source'] != 10) { $this->error = '该订单不合法'; return false; } // 实际付款金额 $payPrice = bcadd($data['update_price'], $data['update_express_price'], 2); if ($payPrice <= 0) { $this->error = '订单实付款价格不能为0.00元'; return false; } return $this->save([ 'order_no' => $this->orderNo(), // 修改订单号, 否则微信支付提示重复 'order_price' => $data['update_price'], 'pay_price' => $payPrice, 'update_price' => helper::bcsub($data['update_price'], helper::bcsub($this['total_price'], $this['coupon_money'])), 'express_price' => $data['update_express_price'] ]) !== false; } }