travel/service/app/model/Orders.php

151 lines
5.4 KiB
PHP
Raw Normal View History

2024-06-24 11:52:30 +08:00
<?php
2024-06-27 17:34:53 +08:00
2024-06-24 11:52:30 +08:00
namespace app\model;
use support\Log;
use think\facade\Db;
use support\Redis;
2024-06-27 17:34:53 +08:00
class Orders extends base
{
2024-06-24 11:52:30 +08:00
//orderStatus 1, 未付款 2 已取消 3 , 待使用 4, 已使用 5, 已退款
2024-06-27 17:34:53 +08:00
const OrderStatus = [1 => '未付款', 2 => '已取消', 3 => '待使用', 4 => '已使用', 5 => '已退款'];
const KuaishouStatus = [1 => '已取消', 2 => '待支付', 3 => '订单确认中', 4 => '待使用', 5 => '已完成', 6 => '已过期', 7 => '待分享'];
2024-06-24 11:52:30 +08:00
2024-06-27 17:34:53 +08:00
const DouyinReservationStatus = [1 => '未预约', 2 => '待接单', 3 => '已预约', 4 => '已取消', 5 => '已完成', 6 => '取消/退款申请'];
const DouyinStatus = [1 => '未核销', 2 => '已核销', 3 => '申请退款中', 4 => '已退款', 5 => '部分核销'];
2024-06-24 11:52:30 +08:00
2024-06-28 17:30:56 +08:00
const AllOssStatus = [1 => '待使用', 2 => '已核销', 3 => '已退款'];
const AllOssStatusSql = [
1 => '((os=1 and order_status=3) or (os=2 and order_status=4) or (os=3 and order_status=1))', //待使用
2 => '((os=1 and order_status=4) or (os=2 and order_status=5) or (os=3 and order_status=2))', //已核销
3 => '((os=1 and order_status=5) or (os=3 and order_status=4))' //已退款(快手暂无已退款状态)
];
2024-06-27 17:34:53 +08:00
const StatusName = ['待跟进', '跟进中', '已核销', '核销失败', '放弃跟单'];
const OSS = [1 => '美团', 2 => '快手', 3 => '抖音', 4 => '全平台'];
2024-06-24 11:52:30 +08:00
const timeType = ['create_time' => '添加记录时间', 'update_time' => '修改记录时间', 'last_follow' => '最后跟进时间', 'next_follow' => '下次跟进时间', 'travel_date' => '出行时间', 'create_at' => '下单时间'];
protected $json = ['personnel'];
2024-06-27 17:34:53 +08:00
public function getOrderStatusNameAttr($val)
{
2024-06-24 11:52:30 +08:00
if ($this->os == 1)
return self::OrderStatus[$this->order_status] ?? '未知';
2024-06-27 17:34:53 +08:00
elseif ($this->os == 3)
2024-06-24 11:52:30 +08:00
return self::DouyinStatus[$this->order_status] ?? '未知';
else
return self::KuaishouStatus[$this->order_status] ?? '未知';
}
2024-06-27 17:34:53 +08:00
public function getStatusNameAttr($val)
{
2024-06-24 11:52:30 +08:00
return self::StatusName[$this->status] ?? '未知';
}
2024-06-27 17:34:53 +08:00
public function getOsNameAttr($val)
{
2024-06-24 11:52:30 +08:00
return self::OSS[$this->os] ?? '未知';
}
2024-06-27 17:34:53 +08:00
public function getAdminNameAttr()
{
if ($this->admin_id > 0) {
2024-06-24 11:52:30 +08:00
return $this->admin->getData('name');
2024-06-27 17:34:53 +08:00
} else {
$aid = Redis::get('Travel:Order:' . $this->id);
if ($aid) {
2024-06-24 11:52:30 +08:00
$a = Admins::where('id', $aid)->find();
2024-06-27 17:34:53 +08:00
return $a->getData('name') . '(电话中...';
2024-06-24 11:52:30 +08:00
}
}
return '';
}
2024-06-27 17:34:53 +08:00
public static function attimes($type, $times)
{
2024-06-24 11:52:30 +08:00
$query = self::where([]);
2024-06-27 17:34:53 +08:00
if (in_array($type, array_keys(self::timeType))) {
if (is_string($times)) {
2024-06-24 11:52:30 +08:00
$times = explode(',', $times);
2024-06-27 17:34:53 +08:00
} else {
if (empty($times[0])) $times[0] = '';
if (empty($times[1])) $times[1] = '';
2024-06-24 11:52:30 +08:00
}
}
switch ($type) {
case 'create_time': //添加记录时间
$query->whereBetween('create_time', [strtotime($times[0]), strtotime($times[1])]);
2024-06-27 17:34:53 +08:00
Log::warning("==Orders====:", ['a' => strtotime($times[0]), 'b' => strtotime($times[1])]);
2024-06-24 11:52:30 +08:00
break;
case 'update_time': //修改记录时间
$query->whereBetween('update_time', [strtotime($times[0]), strtotime($times[1])]);
break;
2024-06-27 17:34:53 +08:00
2024-06-24 11:52:30 +08:00
case 'last_follow': //最后跟进时间
2024-06-27 17:34:53 +08:00
$query->whereBetween('last_follow', [strtotime($times[0]) * 1000, strtotime($times[1]) * 1000 + 999]);
2024-06-24 11:52:30 +08:00
break;
2024-06-27 17:34:53 +08:00
2024-06-24 11:52:30 +08:00
case 'next_follow': //下次跟进时间
2024-06-27 17:34:53 +08:00
$query->whereBetween('next_follow', [strtotime($times[0]) * 1000, strtotime($times[1]) * 1000 + 999]);
2024-06-24 11:52:30 +08:00
break;
case 'travel_date': //出行时间
2024-06-27 17:34:53 +08:00
$query->whereBetween('travel_date', [date('Y-m-d H:i:s', strtotime($times[0])), date('Y-m-d H:i:s', strtotime($times[1]))]);
2024-06-24 11:52:30 +08:00
break;
2024-06-27 17:34:53 +08:00
2024-06-24 11:52:30 +08:00
case 'create_at': //下单时间
2024-06-27 17:34:53 +08:00
$query->whereBetween('create_at', [strtotime($times[0]) * 1000, strtotime($times[1]) * 1000 + 999]);
2024-06-24 11:52:30 +08:00
break;
2024-06-27 17:34:53 +08:00
2024-06-24 11:52:30 +08:00
default:
# code...
break;
}
return $query;
}
2024-06-27 17:34:53 +08:00
public function admin()
{
return $this->belongsTo(Admins::class, 'admin_id')->visible(['name', 'username', 'avatar']);
2024-06-24 11:52:30 +08:00
}
2024-06-27 17:34:53 +08:00
public function anchor()
{
return $this->belongsTo(Admins::class, 'zhubo')->visible(['name', 'username', 'avatar']);
2024-06-24 11:52:30 +08:00
}
2024-06-27 17:34:53 +08:00
public function backs()
{
return $this->hasOne(Backs::class, 'order_id')->where('status', 0)->visible(['status', 'admin_id']);
2024-06-24 20:58:23 +08:00
}
2024-06-27 17:34:53 +08:00
public function follow()
{
return $this->hasMany(Follows::class, 'order_id')->order('id', 'desc');
2024-06-24 11:52:30 +08:00
}
2024-06-27 17:34:53 +08:00
public function finance()
{
return $this->hasMany(Finances::class, 'order_id')->order('id', 'desc');
2024-06-24 11:52:30 +08:00
}
2024-06-27 17:34:53 +08:00
public static function fish($id, $admin_id)
{
return Db::transaction(function () use ($id, $admin_id) {
2024-06-24 11:52:30 +08:00
$order = Orders::where('id', $id)->where('admin_id', 0)->lock(true)->find();
2024-06-27 17:34:53 +08:00
if (empty($order)) return false;
2024-06-24 11:52:30 +08:00
$order->admin_id = $admin_id;
$order->give_time = time();
$order->save();
Logs::todo($id, $admin_id, 6);
return true;
});
}
}