354 lines
14 KiB
PHP
354 lines
14 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Model;
|
||
|
||
|
||
|
||
use Carbon\Carbon;
|
||
use Hyperf\Database\Model\Collection;
|
||
use Hyperf\Database\Model\Relations\HasOne;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Snowflake\Concern\Snowflake;
|
||
|
||
/**
|
||
* @property int $order_id 主键id
|
||
* @property int $user_id 用户id-患者
|
||
* @property int $patient_id 患者id
|
||
* @property int $doctor_id 医生id(存在为null的情况)
|
||
* @property int $order_type 订单类型(1:问诊订单 2:药品订单 3:检测订单 4:随访包订单 5:健康包订单)
|
||
* @property int $is_delete 删除状态(0:否 1:是)
|
||
* @property int $pay_channel 支付渠道(1:小程序支付 2:微信扫码支付 3:模拟支付)
|
||
* @property int $pay_status 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
* @property string $pay_time 支付时间
|
||
* @property int $refund_status 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常 7:部分退款)
|
||
* @property string $order_no 系统订单编号
|
||
* @property string $escrow_trade_no 第三方支付流水号
|
||
* @property string $amount_total 订单金额
|
||
* @property string $coupon_amount_total 优惠卷总金额
|
||
* @property string $payment_amount_total 实际付款金额
|
||
* @property int $cancel_status 取消状态(0:否 1:是)
|
||
* @property string $cancel_time 订单取消时间
|
||
* @property string $cancel_remarks 取消订单备注
|
||
* @property string $order_remarks 订单备注
|
||
* @property int $is_withdrawal 是否提现(0:否 1:是 2:提现中 3:无需提现)
|
||
* @property string $withdrawal_time 提现时间
|
||
* @property \Carbon\Carbon $created_at 创建时间
|
||
* @property \Carbon\Carbon $updated_at 修改时间
|
||
* @property-read OrderInquiry|null $OrderInquiry
|
||
* @property-read OrderServicePackage|null $OrderServicePackage
|
||
*/
|
||
class Order extends Model
|
||
{
|
||
use Snowflake;
|
||
|
||
/**
|
||
* The table associated with the model.
|
||
*/
|
||
protected ?string $table = 'order';
|
||
|
||
/**
|
||
* The attributes that are mass assignable.
|
||
*/
|
||
protected array $fillable = ['order_id', 'user_id', 'patient_id', 'doctor_id', 'order_type', 'is_delete', 'pay_channel', 'pay_status', 'pay_time', 'refund_status', 'order_no', 'escrow_trade_no', 'amount_total', 'coupon_amount_total', 'payment_amount_total', 'cancel_status', 'cancel_time', 'cancel_remarks', 'order_remarks', 'is_withdrawal', 'withdrawal_time', 'created_at', 'updated_at'];
|
||
|
||
protected string $primaryKey = "order_id";
|
||
|
||
/**
|
||
* 关联问诊订单表
|
||
*/
|
||
public function OrderInquiry(): HasOne
|
||
{
|
||
return $this->hasOne(OrderInquiry::class, 'order_id', 'order_id');
|
||
}
|
||
|
||
/**
|
||
* 关联服务包订单表
|
||
*/
|
||
public function OrderServicePackage(): HasOne
|
||
{
|
||
return $this->hasOne(OrderServicePackage::class, 'order_id', 'order_id');
|
||
}
|
||
|
||
/**
|
||
* 获取信息-单条
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getOne(array $params, array $fields = ['*']): object|null
|
||
{
|
||
return self::where($params)->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 多条
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return Collection|array
|
||
*/
|
||
public static function getList(array $params, array $fields = ['*']): Collection|array
|
||
{
|
||
return self::where($params)->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 新增
|
||
* @param array $data
|
||
* @return Order|\Hyperf\Database\Model\Model
|
||
*/
|
||
public static function addOrder(array $data): \Hyperf\Database\Model\Model|Order
|
||
{
|
||
return self::create($data);
|
||
}
|
||
|
||
/**
|
||
* 修改
|
||
* @param array $params
|
||
* @param array $data
|
||
* @return int
|
||
*/
|
||
public static function edit(array $params = [], array $data = []): int
|
||
{
|
||
return self::where($params)->update($data);
|
||
}
|
||
|
||
/**
|
||
* 获取医生某一时间段收益明细分页数据
|
||
* @param string|int $doctor_id
|
||
* @param array $date_params 时间区间
|
||
* @param string|int $is_platform_deep_cooperation
|
||
* @param array $fields
|
||
* @param int|null $page
|
||
* @param int|null $per_page
|
||
* @return array
|
||
*/
|
||
public static function getDoctorCreatedDateOrderInquiryPage(string|int $doctor_id, array $date_params,string|int $is_platform_deep_cooperation,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
|
||
{
|
||
$params = array();
|
||
$params['doctor_id'] = $doctor_id;
|
||
|
||
$query = self::with(['OrderInquiry', 'OrderServicePackage'])
|
||
->whereIn('order_type',[1,4,5])
|
||
->where($params);
|
||
|
||
// 问诊订单
|
||
$query = $query->where(function ($query) use ($date_params,$is_platform_deep_cooperation,$doctor_id){
|
||
$query->whereExists(function ($subQuery) use ($date_params,$is_platform_deep_cooperation,$doctor_id){
|
||
$subQuery->from('order_inquiry');
|
||
if ($is_platform_deep_cooperation == 1){
|
||
$subQuery->whereNotIn('inquiry_type', [2,4]);
|
||
}
|
||
|
||
$subQuery->where('doctor_id', $doctor_id)
|
||
->whereNotIn('inquiry_mode', [7,8,9])
|
||
->where('doctor_id', $doctor_id)
|
||
->whereIn('inquiry_status', [6])
|
||
->whereBetween('reception_time', $date_params)
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_inquiry.order_id');
|
||
})
|
||
->orWhereExists(function ($subQuery) use ($date_params,$doctor_id) {
|
||
$subQuery->from('order_service_package');
|
||
|
||
$subQuery->where('doctor_id', $doctor_id)
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_service_package.order_id')
|
||
->whereIn('order_service_status', [3,4,5])
|
||
->whereBetween('start_time', $date_params);
|
||
});
|
||
});
|
||
|
||
$result = $query->orderBy('created_at')->paginate($per_page, $fields, "page", $page);
|
||
|
||
$data = array();
|
||
$data['current_page'] = $result->currentPage();// 当前页码
|
||
$data['total'] = $result->total();//数据总数
|
||
$data['data'] = $result->items();//数据
|
||
$data['per_page'] = $result->perPage();//每页个数
|
||
$data['last_page'] = $result->lastPage();//最后一页
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取医生当日预计的订单金额
|
||
* @param array $params
|
||
* @param array $date_params 时间区间
|
||
* @param string|int $is_platform_deep_cooperation
|
||
* @return int|null|string
|
||
*/
|
||
public static function getDoctorDayAmountTotal(array $params, array $date_params,string|int $is_platform_deep_cooperation): int|null|string
|
||
{
|
||
$query = self::where($params)
|
||
->whereIn('order_type',[1,4,5]);
|
||
|
||
// 问诊订单
|
||
$query = $query->where(function ($query) use ($date_params,$is_platform_deep_cooperation){
|
||
$query->whereExists(function ($subQuery) use ($date_params,$is_platform_deep_cooperation){
|
||
$subQuery->from('order_inquiry');
|
||
if ($is_platform_deep_cooperation == 1){
|
||
$subQuery->whereNotIn('inquiry_type', [2,4]);
|
||
}
|
||
|
||
$subQuery->whereNotIn('inquiry_mode', [7,8,9])
|
||
->whereIn('inquiry_status', [4,5])
|
||
->whereBetween('reception_time', $date_params)
|
||
->where('inquiry_refund_status', 0)
|
||
->where('inquiry_pay_status', 2)
|
||
->where('is_withdrawal', 0)
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_inquiry.order_id');
|
||
})
|
||
->orWhereExists(function ($subQuery) use ($date_params) {
|
||
$subQuery->from('order_service_package')
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_service_package.order_id')
|
||
->whereIn('order_service_status', [3])
|
||
->whereBetween('start_time', $date_params)
|
||
->where('refund_status', 0)
|
||
->where('pay_status', 2);
|
||
});
|
||
});
|
||
|
||
$result = $query->orderBy('created_at')
|
||
->sum("amount_total");;
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 获取医生当日已完成待入帐的订单金额
|
||
* @param array $params
|
||
* @param array $date_params 时间区间
|
||
* @param string|int $is_platform_deep_cooperation
|
||
* @return int|null|string
|
||
*/
|
||
public static function getDoctorDayCompletedAmountTotal(array $params, array $date_params,string|int $is_platform_deep_cooperation): int|null|string
|
||
{
|
||
$query = self::where($params)
|
||
->whereIn('order_type',[1,4,5]);
|
||
|
||
// 问诊订单
|
||
$query = $query->where(function ($query) use ($date_params,$is_platform_deep_cooperation){
|
||
$query->whereExists(function ($subQuery) use ($date_params,$is_platform_deep_cooperation){
|
||
$subQuery->from('order_inquiry');
|
||
if ($is_platform_deep_cooperation == 1){
|
||
$subQuery->whereNotIn('inquiry_type', [2,4]);
|
||
}
|
||
|
||
$subQuery->whereNotIn('inquiry_mode', [7,8,9])
|
||
->whereIn('inquiry_status', [5])
|
||
->whereBetween('reception_time', $date_params)
|
||
->where('inquiry_refund_status', 0)
|
||
->where('inquiry_pay_status', 2)
|
||
->where('is_withdrawal', 0)
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_inquiry.order_id');
|
||
})
|
||
->orWhereExists(function ($subQuery) use ($date_params) {
|
||
$subQuery->from('order_service_package')
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_service_package.order_id')
|
||
->whereIn('order_service_status', [3])
|
||
->where('finish_time','>', $date_params[0])
|
||
->where('refund_status', 0)
|
||
->where('pay_status', 2);
|
||
});
|
||
});
|
||
|
||
$result = $query->orderBy('created_at')
|
||
->sum("amount_total");;
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 获取可提现订单列表-分页
|
||
* @param array $params
|
||
* @param string|int $is_platform_deep_cooperation
|
||
* @param array $fields
|
||
* @param int|null $page
|
||
* @param int|null $per_page
|
||
* @return array
|
||
*/
|
||
public static function getDoctorWithdrawalOrderPage(array $params, string|int $is_platform_deep_cooperation,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
|
||
{
|
||
$query = self::with(['OrderInquiry', 'OrderServicePackage'])
|
||
->where($params)
|
||
->whereIn('order_type',[1,4,5]);
|
||
|
||
// 问诊订单
|
||
$query = $query->where(function ($query) use ($is_platform_deep_cooperation){
|
||
$query->whereExists(function ($subQuery) use ($is_platform_deep_cooperation){
|
||
$subQuery->from('order_inquiry');
|
||
if ($is_platform_deep_cooperation == 1){
|
||
$subQuery->whereNotIn('inquiry_type', [2,4]);
|
||
}
|
||
|
||
$subQuery->whereNotIn('inquiry_mode', [7,8,9])
|
||
->whereIn('inquiry_status', [6])
|
||
->whereIn('inquiry_refund_status', [0,3])
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_inquiry.order_id');
|
||
})
|
||
->orWhereExists(function ($subQuery) {
|
||
$subQuery->from('order_service_package')
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_service_package.order_id')
|
||
->whereIn('refund_status', [0,3])
|
||
->where('pay_status', 2)
|
||
->whereIn('order_service_status', [4,5])
|
||
->WhereExists(function ($subQuery){
|
||
$subQuery->from("order_service_package_inquiry")
|
||
->whereRaw('gdxz_order_service_package.order_service_id = gdxz_order_service_package_inquiry.order_service_id');
|
||
});
|
||
});
|
||
});
|
||
|
||
$result = $query->orderBy('created_at')
|
||
->paginate($per_page, $fields, "page", $page);
|
||
|
||
$data = array();
|
||
$data['current_page'] = $result->currentPage();// 当前页码
|
||
$data['total'] = $result->total();//数据总数
|
||
$data['data'] = $result->items();//数据
|
||
$data['per_page'] = $result->perPage();//每页个数
|
||
$data['last_page'] = $result->lastPage();//最后一页
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取可提现订单列表
|
||
* @param array $params
|
||
* @param string|int $is_platform_deep_cooperation
|
||
* @param array $fields
|
||
* @return array|Collection|\Hyperf\Collection\Collection
|
||
*/
|
||
public static function getDoctorWithdrawalOrderList(array $params, string|int $is_platform_deep_cooperation,array $fields = ["*"]): array|Collection|\Hyperf\Collection\Collection
|
||
{
|
||
$query = self::with(['OrderInquiry', 'OrderServicePackage'])
|
||
->where($params)
|
||
->whereIn('order_type',[1,4,5]);
|
||
|
||
$query = $query->where(function ($query) use ($is_platform_deep_cooperation){
|
||
$query->whereExists(function ($subQuery) use ($is_platform_deep_cooperation){
|
||
$subQuery->from('order_inquiry');
|
||
if ($is_platform_deep_cooperation == 1){
|
||
$subQuery->whereNotIn('inquiry_type', [2,4]);
|
||
}
|
||
|
||
$subQuery->whereNotIn('inquiry_mode', [7,8,9])
|
||
->whereIn('inquiry_status', [6])
|
||
->whereIn('inquiry_refund_status', [0,3])
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_inquiry.order_id');
|
||
})
|
||
->orWhereExists(function ($subQuery) {
|
||
$subQuery->from('order_service_package')
|
||
->whereRaw('gdxz_order.order_id = gdxz_order_service_package.order_id')
|
||
->whereIn('refund_status', [0,3])
|
||
->where('pay_status', 2)
|
||
->whereIn('order_service_status', [4,5])
|
||
->WhereExists(function ($subQuery){
|
||
$subQuery->from("order_service_package_inquiry")
|
||
->whereRaw('gdxz_order_service_package.order_service_id = gdxz_order_service_package_inquiry.order_service_id');
|
||
});
|
||
});
|
||
});
|
||
|
||
return $query->orderBy('created_at')->get($fields);
|
||
}
|
||
}
|