306 lines
8.2 KiB
PHP
306 lines
8.2 KiB
PHP
<?php
|
||
|
||
namespace app\admin\model;
|
||
|
||
use app\admin\library\Auth;
|
||
use app\common\services\RedisService;
|
||
use think\Cache;
|
||
use think\Model;
|
||
use traits\model\SoftDelete;
|
||
|
||
|
||
class Order extends Model
|
||
{
|
||
|
||
|
||
use SoftDelete;
|
||
|
||
|
||
// 表名
|
||
protected $name = 'order';
|
||
|
||
// 自动写入时间戳字段
|
||
protected $autoWriteTimestamp = 'datetime';
|
||
|
||
protected $dateFormat = 'Y-m-d H:i:s';
|
||
// 定义时间戳字段名
|
||
protected $createTime = 'create_time';
|
||
protected $updateTime = 'update_time';
|
||
protected $deleteTime = 'delete_time';
|
||
|
||
// 追加属性
|
||
protected $append = [
|
||
'status_text',
|
||
'collect_text',
|
||
'dispatch_type_text',
|
||
'offline_amount_type_text'
|
||
];
|
||
|
||
|
||
const TAB_DISPATCH = 'dispatch'; //派单
|
||
|
||
const TAB_PENDING = 'pending'; //跟进中
|
||
|
||
const TAB_SETTING = 'setting'; //跟进中
|
||
|
||
const TAB_AUDIT = 'audit'; //审核
|
||
|
||
const TAB_REVIEW = 'review'; //回访
|
||
|
||
const TAB_VALID = 'valid'; //有效的订单,即除了取消的和草稿
|
||
|
||
const STATUS_DRAFT = 0; //草稿
|
||
const STATUS_DISPATCHING = 10; //待派单
|
||
const STATUS_DISPATCHED = 20; //已派单
|
||
//const STATUS_ING = 30; //进行中
|
||
const STATUS_CHECKING = 40; //待审核
|
||
const STATUS_CHECKONCE = 41; //审核未通过
|
||
const STATUS_AUDITING = 50; //审核中
|
||
const STATUS_FINISHED = 60; //已完成
|
||
const STATUS_AFTERSALE= 70; //已完成
|
||
const STATUS_CANCEL = -10; //取消
|
||
|
||
|
||
|
||
|
||
|
||
public function getStatusList()
|
||
{
|
||
return ['0' => __('Status 0'),'10' => __('Status 10'), '20' => __('Status 20'),
|
||
//'30' => __('Status 30'),
|
||
'40' => __('Status 40'),
|
||
'41' => __('Status 41'),
|
||
'50' => __('Status 50'),
|
||
'60' => __('Status 60'),
|
||
'70' => __('Status 70'),
|
||
'-10' => __('Status -10')];
|
||
}
|
||
|
||
|
||
public function getDispatchTypeList()
|
||
{
|
||
return ['10' => __('Dispatch_type 10'), '11' => __('Dispatch_type 11'), '20' => __('Dispatch_type 20')];
|
||
}
|
||
|
||
|
||
public function getAuditStatusList()
|
||
{
|
||
return [self::STATUS_AUDITING => __('Status 50'), self::STATUS_FINISHED => __('Status 60'), ];
|
||
}
|
||
|
||
|
||
public function getOfflineTotalTypeList()
|
||
{
|
||
return ['0' => __('Offline_amount_type 0'), '1'=> __('Offline_amount_type 1'),'2' => __('Offline_amount_type 2')];
|
||
}
|
||
|
||
|
||
public function getStatusTextAttr($value, $data)
|
||
{
|
||
$value = $value ?: ($data['status'] ?? '');
|
||
$list = $this->getStatusList();
|
||
return $list[$value] ?? '';
|
||
}
|
||
|
||
public function getOfflineAmountTypeTextAttr($value, $data)
|
||
{
|
||
$value = $value ?: ($data['offline_amount_type'] ?? '');
|
||
$list = $this->getOfflineTotalTypeList();
|
||
return $list[$value] ?? '';
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
public function getCollectTextAttr($value, $data)
|
||
{
|
||
$value = $value ?: ($data['collect'] ?? '');
|
||
//$list = $this->getCollectList();
|
||
return $list[$value] ?? '';
|
||
}
|
||
|
||
|
||
public function getDispatchTypeTextAttr($value, $data)
|
||
{
|
||
$value = $value ?: ($data['dispatch_type'] ?? '');
|
||
$list = $this->getDispatchTypeList();
|
||
return $list[$value] ?? '';
|
||
}
|
||
|
||
|
||
protected function scopeTab($query, $tab=null)
|
||
{
|
||
$status = $this->tabStatus($tab);
|
||
if(!empty($status)){
|
||
$query->whereIn('fa_order.status', $status);
|
||
}
|
||
return $query;
|
||
}
|
||
|
||
public function tabStatus($tab){
|
||
$tabStatus = [
|
||
self::TAB_DISPATCH => [
|
||
self::STATUS_DISPATCHING,
|
||
self::STATUS_DISPATCHED,
|
||
self::STATUS_CANCEL
|
||
], //派单管理状态
|
||
self::TAB_PENDING => [
|
||
self::STATUS_DISPATCHED,
|
||
//self::STATUS_ING,
|
||
self::STATUS_CHECKING,
|
||
], //订单跟进状态
|
||
self::TAB_AUDIT => [
|
||
self::STATUS_AUDITING,
|
||
self::STATUS_FINISHED
|
||
], //待结算
|
||
self::TAB_REVIEW => [
|
||
self::STATUS_FINISHED
|
||
],
|
||
self::TAB_SETTING => [
|
||
self::STATUS_CHECKING,
|
||
self::STATUS_CHECKONCE
|
||
],
|
||
self::TAB_VALID => [
|
||
self::STATUS_DISPATCHING,
|
||
self::STATUS_DISPATCHED,
|
||
self::STATUS_CHECKING,
|
||
self::STATUS_CHECKONCE,
|
||
self::STATUS_AUDITING,
|
||
self::STATUS_FINISHED
|
||
],
|
||
];
|
||
return $tabStatus[$tab] ?? [];
|
||
}
|
||
|
||
public const DELETED_TYPE = [
|
||
1 => '无相关师傅',
|
||
2 => '距离客户太远',
|
||
3 => '价格不同意',
|
||
4 => '重复订单',
|
||
5 => '联系客户已晚',
|
||
6 => '询价后没有后续',
|
||
7 => '一直联系不上',
|
||
8 => '客户不能久等',
|
||
];
|
||
|
||
|
||
public function incomeBtnStatus(){
|
||
return array_merge($this->tabStatus(self::TAB_PENDING),[self::STATUS_AUDITING]);
|
||
}
|
||
|
||
public function user(){
|
||
return $this->belongsTo(Admin::class,'admin_id',);
|
||
}
|
||
|
||
public function admin(){
|
||
return $this->belongsTo(Admin::class,'admin_id',joinType: 'left')
|
||
->setEagerlyType(0);
|
||
}
|
||
|
||
public function item(){
|
||
return $this->belongsTo(Item::class,'item_id',);
|
||
}
|
||
public function source(){
|
||
return $this->belongsTo(Source::class,'source');
|
||
}
|
||
|
||
public function area(){
|
||
return $this->belongsTo(Area::class,'area_id','area_code');
|
||
}
|
||
public function coupon(){
|
||
return $this->belongsTo(Coupons::class,'coupon_id');
|
||
}
|
||
public function phone(){
|
||
return $this->belongsTo(Phones::class,'work_tel_id',);
|
||
}
|
||
|
||
public function auditadmin(){
|
||
return $this->belongsTo(Admin::class,'audit_admin_id',);
|
||
}
|
||
|
||
|
||
public function dispatchadmin(){
|
||
return $this->belongsTo(Admin::class,'dispatch_admin_id',);
|
||
}
|
||
|
||
|
||
public function dispatch()
|
||
{
|
||
return $this->hasOne(OrderDispatch::class, 'order_id', 'id', [], 'LEFT')->setEagerlyType(0)->where('fa_order_dispatch.status','>',0);
|
||
}
|
||
|
||
public function dispatch2()
|
||
{
|
||
return $this->hasOne(OrderDispatch::class, 'order_id', 'id', [], 'LEFT')->setEagerlyType(1)->where('fa_order_dispatch.status','>',0);
|
||
}
|
||
|
||
/**
|
||
* 管理员权限
|
||
* @param $query
|
||
* @param Auth $auth
|
||
* @param string $auth_admin_id
|
||
* @return mixed
|
||
*/
|
||
public function scopeAuth($query, Auth $auth, string $admin_id_field='admin_id'){
|
||
|
||
if(!$auth->isSuperAdmin()){
|
||
$query->where('fa_order.'.$admin_id_field,$auth->id);
|
||
}
|
||
return $query;
|
||
}
|
||
|
||
public function scopeDispatcherauth($query, Auth $auth, string $admin_id_field='admin_id'){
|
||
|
||
if($auth->isDispatcher()){ //是派单员
|
||
$query->where('fa_order.'.$admin_id_field,$auth->id);
|
||
}
|
||
return $query;
|
||
}
|
||
|
||
|
||
/**
|
||
* 地域权限
|
||
* @param $query
|
||
* @param Auth $auth
|
||
* @param string $area_id_field
|
||
* @return mixed
|
||
*/
|
||
public function scopeAreaauth($query,Auth $auth,string $area_id_field='area_id'){
|
||
if(!$auth->isSuperAdmin()){
|
||
$areaIds = array_unique(array_filter(explode(',',trim($auth->area_ids??''))));
|
||
if(!in_array('*',$areaIds)){
|
||
$query->whereIn('fa_order.'.$area_id_field,$areaIds);
|
||
}
|
||
}
|
||
return $query;
|
||
}
|
||
|
||
|
||
public function workerman(){
|
||
return $this->belongsTo(Worker::class,'worker_id',);
|
||
}
|
||
|
||
/**
|
||
* 生成唯一订单号:8位纯数字(如25063001)
|
||
* 格式:yyMMdd + 当天自增序号
|
||
*/
|
||
public static function generateOrderNo()
|
||
{
|
||
$date = date('ymd'); // 6 位日期
|
||
$key = 'order_inc:' . date('Ymd');
|
||
|
||
$redis = new RedisService(0);
|
||
$inc = $redis->incrWithExpire($key);
|
||
|
||
if ($inc > 9999) {
|
||
throw new \Exception('订单号已满');
|
||
}
|
||
|
||
$suffix = str_pad($inc, 4, '0', STR_PAD_LEFT); // 可改为 4 位更安全
|
||
return $date . $suffix; // 最终结果:如 2506300001
|
||
}
|
||
|
||
|
||
}
|