// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\common\model; use app\common\model\order\RefundHis; use app\common\model\store\shop\Order as ShopOrder; use app\common\service\Order as OrderService; use app\common\service\order\Complete as OrderCompleteService; use app\common\enum\OrderType as OrderTypeEnum; use app\common\enum\order\PayStatus as PayStatusEnum; use app\common\enum\order\OrderStatus as OrderStatusEnum; use app\common\enum\order\DeliveryType as DeliveryTypeEnum; use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum; use app\common\library\helper; use app\console\model\Setting as SettingModel; /** * 订单模型 * Class Order * @package app\common\model */ class Order extends BaseModel { // 定义表名 protected $name = 'order'; // 定义表名(外部引用) public static $tableName = 'order'; // 定义主键 protected $pk = 'order_id'; // 定义别名 protected $alias = 'order'; /** * 追加字段 * @var array */ protected $append = [ 'state_text', // 售后单状态文字描述 'state_value', // 售后状态码 20已取消 30已完成 11待支付 12待收货未发货 13待收货已发货 ]; /** * 订单商品列表 * @return \think\model\relation\HasMany */ public function goods() { $module = self::getCalledModule(); return $this->hasMany("app\\{$module}\\model\\OrderGoods")->withoutField('content'); } /** * 关联订单收货地址表 * @return \think\model\relation\HasOne */ public function address() { $module = self::getCalledModule(); return $this->hasOne("app\\{$module}\\model\\OrderAddress"); } /** * 关联用户表 * @return \think\model\relation\BelongsTo */ public function user() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\User"); } /** * 关联核销人用户 * @return \think\model\relation\BelongsTo */ public function hxUser() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\User", 'hx_user_id')->field('user_id,mobile,nick_name'); } /** * 关联门店表 * @return \think\model\relation\BelongsTo */ public function shops() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\shop\\Shops", 'staff_shop_id'); } /** * 关联推广人 * @return \think\model\relation\BelongsTo */ public function staffUser() { return $this->belongsTo('User','staff_user_id','user_id')->field('user_id,mobile,nick_name'); } /** * 关联物流公司表 * @return \think\model\relation\BelongsTo */ public function express() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\Express"); } /** * 关联物流公司表 * @return \think\model\relation\BelongsTo */ public function userRiceCard() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\card\\userRiceCard",'rice_card_id','id'); } /** * 关联优惠券信息 * @return \think\model\relation\belongsTo */ public function coupon() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\UserCoupon",'coupon_id','user_coupon_id'); } /** * 订单状态文字描述 * @param $value * @param $data * @return string */ public function getStateTextAttr($value, $data) { // 订单状态 // if (in_array($data['order_status'], [OrderStatusEnum::CANCELLED, OrderStatusEnum::COMPLETED])) { // $orderStatus = [OrderStatusEnum::CANCELLED => '已取消', OrderStatusEnum::COMPLETED => '已完成']; // return $orderStatus[$data['order_status']]; // } // 订单状态 if (isset($data['order_status']) && $data['order_status'] != OrderStatusEnum::NORMAL) { return OrderStatusEnum::data()[$data['order_status']]['name']; } // 付款状态 if (isset($data['pay_status']) && $data['pay_status'] == PayStatusEnum::PENDING) { return '待支付'; } // 订单类型:单独购买 if (isset($data['delivery_status']) && $data['delivery_status'] == 10) { return '待收货'; //待收货未发货 // return '已付款待发货'; //待收货未发货 } if (isset($data['receipt_status']) && $data['receipt_status'] == 10) { return '待收货'; //待收货已发货 // return '已付款待收货'; //待收货未发货 } return $value; } /** * 订单状态码 * @param $value * @param $data * @return string */ public function getStateValueAttr($value, $data) { // 订单状态 // if (in_array($data['order_status'], [OrderStatusEnum::CANCELLED, OrderStatusEnum::COMPLETED])) { // $orderStatus = [OrderStatusEnum::CANCELLED => '已取消', OrderStatusEnum::COMPLETED => '已完成']; // return $orderStatus[$data['order_status']]; // } // 订单状态 if (isset($data['order_status']) && $data['order_status'] != OrderStatusEnum::NORMAL) { return OrderStatusEnum::data()[$data['order_status']]['value']; } // 付款状态 if (isset($data['pay_status']) && $data['pay_status'] == PayStatusEnum::PENDING) { return 11;//'待支付'; } // 订单类型:单独购买 if (isset($data['delivery_status']) && $data['delivery_status'] == 10) { return 12;//'待发货'; } if (isset($data['receipt_status']) && $data['receipt_status'] == 10) { return 13;//'待收货'; } return $value; } /** * 获取器:订单金额(含优惠折扣) * @param $value * @param $data * @return string */ public function getOrderPriceAttr($value, $data) { // 兼容旧数据:订单金额 if ($value == 0) { return helper::bcadd(helper::bcsub($data['total_price'], $data['coupon_money']), $data['update_price']); } return $value; } /** * 改价金额(差价) * @param $value * @return array */ public function getUpdatePriceAttr($value) { return [ 'symbol' => $value < 0 ? '-' : '+', 'value' => sprintf('%.2f', abs($value)) ]; } /** * 付款时间 * @param $value * @return false|string */ public function getPayTimeAttr($value) { return format_time($value); } /** * 发货时间 * @param $value * @return false|string */ public function getDeliveryTimeAttr($value) { return format_time($value); } /** * 收货时间 * @param $value * @return false|string */ public function getReceiptTimeAttr($value) { return format_time($value); } /** * 提货时间 */ public function getPickupTimeAttr($value) { return format_time($value); } /** * 提货截止时间 */ public function getPickupDeadlineAttr($value) { return format_time($value); } /** * 生成订单号 * @return string */ public function orderNo(string $prefix='rw') { return OrderService::createOrderNo($prefix); } /** * 生成核销码 * @return string */ public function hxCode() { return OrderService::createHxCode("H"); } /** * 订单详情 * @param $where * @param array $with * @return array|null|static */ public static function detail($where, $with = []) { is_array($where) ? $filter = $where : $filter['order_id'] = (int)$where; return self::get($filter, $with); } /** * 批量获取订单列表 * @param $orderIds * @param array $with * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getListByIds(array $orderIds, array $with = []) { $data = $this->getListByInArray('order_id', $orderIds, $with); return helper::arrayColumn2Key($data, 'order_id'); } /** * 批量获取订单列表 * @param $field * @param $data * @param array $with * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function getListByInArray($field, $data, $with = []) { return $this->with($with) ->where($field, 'in', $data) ->where('is_delete', '=', 0) ->select(); } /** * 根据订单号批量查询 * @param $orderNos * @param array $with * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getListByOrderNos($orderNos, $with = []) { return $this->getListByInArray('order_no', $orderNos, $with); } /** * 批量更新订单 * @param $orderIds * @param $data * @return false|int */ public function onBatchUpdate($orderIds, $data) { return static::updateBase($data, [['order_id', 'in', $orderIds]]); } /** * 提货截止时间:提交订单2天内 单位:分钟 */ public static function getPickupDeadline() { if (env('SERVE_ENV') == 'test') { return 5; } // return 2*24*60; v2.1.39改版 用户下单(门店自提)后超时未核销将自动退款。自动退款时间从48小时延长到7天(168小时) return 7*24*60; } /** * 申请开票截止时间:订单付款后的40天内可以申请开票 单位:分钟 */ public static function getInvoiceDeadline() { if (env('SERVE_ENV') == 'test') { return 5; } return 40*24*60; } /** * 自动评价n天以前的的已确认收货的订单商品 单位:天 */ public static function getCommentDeadline() { if (env('SERVE_ENV') == 'test') { return 1; // 1天 } return 15; } public static function sumSalesVolume($orderIds){ if(!count($orderIds))return 0; $res = self::whereIn('order_id',$orderIds)->field('sum(pay_price + rice_card_money - express_price) as volume')->find(); return $res->volume?:0; } /** * 关联分销数据 * @return \think\model\relation\hasMany */ public function commissionsDetail() { $model = "app\\common\\model\\user\\CommissionsDetail"; return $this->hasMany($model,'order_id','order_id')->order('role','asc'); } /** * 关联分销上级 * @return \think\model\relation\BelongsTo */ public function commissionsUser() { return $this->belongsTo('User','staff_user_id','user_id')->bind(['staff_user_name'=>'nick_name']); } /** * 自然渠道 * @param $where * @return int|mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function sumNatureSaleVolume($where){ $res = self::where($where)->field('sum(pay_price + rice_card_money - express_price) as volume')->find(); $orderMoney = $res->volume?:0; //退款金额 $refMoney = OrderRefund::alias('od') ->where('od.finance_refund',10) ->whereExists('select 1 from yoshop_order where order_id=od.order_id and staff_user_id=0 and staff_shop_id=1 and pay_status=20 and is_delete=0') ->sum('refund_money'); return helper::bcsub($orderMoney,$refMoney,2); } }