parent
9c43fe42bd
commit
b89c162045
@ -0,0 +1,47 @@ |
||||
<?php |
||||
|
||||
namespace app\common\enum; |
||||
|
||||
class ServerEnum extends EnumBasics |
||||
{ |
||||
// 待付款 |
||||
const APPLYPAY = 10; |
||||
// 待派单 |
||||
const APPLYDISPATCH = 20; |
||||
// 待服务 |
||||
const APPLYSERVER = 30; |
||||
// 已完成 |
||||
const COMPLETED = 40; |
||||
// 已取消 |
||||
const CANCELLED = 50; |
||||
|
||||
/** |
||||
* 获取枚举数据 |
||||
* @return array |
||||
*/ |
||||
public static function data(): array |
||||
{ |
||||
return [ |
||||
self::APPLYPAY => [ |
||||
'name' => '待付款', |
||||
'value' => self::APPLYPAY, |
||||
], |
||||
self::CANCELLED => [ |
||||
'name' => '已取消', |
||||
'value' => self::CANCELLED, |
||||
], |
||||
self::APPLYDISPATCH => [ |
||||
'name' => '待派单', |
||||
'value' => self::APPLYDISPATCH, |
||||
], |
||||
self::APPLYSERVER => [ |
||||
'name' => '待服务', |
||||
'value' => self::APPLYSERVER, |
||||
], |
||||
self::COMPLETED => [ |
||||
'name' => '已完成', |
||||
'value' => self::COMPLETED, |
||||
] |
||||
]; |
||||
} |
||||
} |
@ -0,0 +1,31 @@ |
||||
<?php |
||||
|
||||
namespace app\common\enum\dealer; |
||||
|
||||
use app\common\enum\EnumBasics; |
||||
|
||||
class DealerUserEnum extends EnumBasics |
||||
{ |
||||
//工程师 |
||||
const ENGINEER = 20; |
||||
//普通 |
||||
const NORMAL = 10; |
||||
|
||||
/** |
||||
* 获取枚举数据 |
||||
* @return array |
||||
*/ |
||||
public static function data(): array |
||||
{ |
||||
return [ |
||||
self::ENGINEER => [ |
||||
'name' => '工程师', |
||||
'value' => self::ENGINEER, |
||||
], |
||||
self::NORMAL => [ |
||||
'name' => '普通', |
||||
'value' => self::NORMAL, |
||||
], |
||||
]; |
||||
} |
||||
} |
@ -0,0 +1,103 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\model\server; |
||||
|
||||
use app\common\enum\ServerEnum; |
||||
use app\common\model\UploadFile; |
||||
use cores\BaseModel; |
||||
use think\model\relation\BelongsTo; |
||||
use think\model\relation\HasOne; |
||||
use think\Paginator; |
||||
|
||||
class Order extends BaseModel |
||||
{ |
||||
// 定义表名 |
||||
protected $name = 'server_order'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'order_id'; |
||||
|
||||
/** |
||||
* 追加字段 |
||||
* @var array |
||||
*/ |
||||
protected $append = [ |
||||
'order_status_text', // 订单状态文字描述 |
||||
]; |
||||
|
||||
|
||||
public function serve(): BelongsTo |
||||
{ |
||||
return $this->belongsTo(Server::class, 'id', 'server_id'); |
||||
} |
||||
|
||||
/** |
||||
* 获取器:订单状态文字描述 |
||||
* @return string |
||||
*/ |
||||
public function getOrderStatusTextAttr($value, $data): string |
||||
{ |
||||
// 订单状态 |
||||
return ServerEnum::data()[$data['order_status']]['name']; |
||||
} |
||||
|
||||
/** |
||||
* 图片 |
||||
* @return HasOne |
||||
*/ |
||||
public function image(): HasOne |
||||
{ |
||||
return $this->hasOne(UploadFile::class, 'file_id', 'server_image_id') |
||||
->bind(['server_image' => 'preview_url']); |
||||
} |
||||
|
||||
/** |
||||
* 关联用户表 |
||||
* @return BelongsTo |
||||
*/ |
||||
public function user(): BelongsTo |
||||
{ |
||||
$module = self::getCalledModule(); |
||||
return $this->belongsTo("app\\$module\\model\\User") |
||||
->bind(['user_name' => 'nick_name', 'user_mobile' => 'mobile']); |
||||
} |
||||
|
||||
public function dealer(): BelongsTo |
||||
{ |
||||
$module = self::getCalledModule(); |
||||
return $this->belongsTo("app\\$module\\model\\User", 'dealer_id', 'user_id') |
||||
->bind(['dealer_name' => 'nick_name', 'dealer_mobile' => 'mobile']); |
||||
} |
||||
|
||||
/** |
||||
* @notes:服务订单列表 |
||||
* @param array $where |
||||
* @param int $listRows |
||||
* @return Paginator |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getList(array $where, int $listRows = 15): Paginator |
||||
{ |
||||
$where = $this->setQueryDefaultValue($where); |
||||
return $this->with(['image', 'dealer']) |
||||
->alias('a') |
||||
->join('user b', 'b.user_id = a.user_id') |
||||
->where($where) |
||||
->order(['create_time']) |
||||
->field('a.*,b.mobile as user_mobile,b.nick_name as user_nick_name') |
||||
->paginate($listRows); |
||||
} |
||||
|
||||
/** |
||||
* @notes:订单详情 |
||||
* @param $where |
||||
* @param array $with |
||||
* @return Order|array|null |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function detail($where, array $with = []) |
||||
{ |
||||
return static::get($where, $with); |
||||
} |
||||
} |
@ -0,0 +1,91 @@ |
||||
<?php |
||||
declare (strict_types=1); |
||||
|
||||
namespace app\common\service\server; |
||||
|
||||
use app\api\model\dealer\User as DealerUserModel; |
||||
use app\common\enum\dealer\DealerUserEnum; |
||||
use app\common\enum\order\PayStatus; |
||||
use app\common\enum\ServerEnum; |
||||
use app\common\model\UserCoupon as UserCouponModel; |
||||
use app\common\service\BaseService; |
||||
use app\common\service\order\Refund as RefundService; |
||||
use cores\exception\BaseException; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
|
||||
class Order extends BaseService |
||||
{ |
||||
/** |
||||
* 生成订单号 |
||||
* @return string |
||||
*/ |
||||
public static function createOrderNo(): string |
||||
{ |
||||
return date('Ymd') . substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13)))), 0, 8); |
||||
} |
||||
|
||||
/** |
||||
* @notes:用户取消订单 |
||||
* @param array $order |
||||
* @return bool |
||||
* @throws BaseException |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function cancelOrder(array $order): bool |
||||
{ |
||||
//未完成 未取消 |
||||
if (!empty($order['order_status']) && $order['order_status'] != ServerEnum::COMPLETED || $order['order_status'] != ServerEnum::CANCELLED) { |
||||
if (!empty($order['coupon_id'])) { |
||||
// 回退用户优惠券 |
||||
$order['coupon_id'] > 0 && UserCouponModel::setIsUse($order['coupon_id'], false); |
||||
} |
||||
//判断是否已支付 |
||||
if ($order['pay_status'] == PayStatus::SUCCESS) { |
||||
if (!(new RefundService)->handle($order)) { |
||||
try { |
||||
throwError('执行订单退款失败'); |
||||
} catch (BaseException $e) { |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
//更新订单状态 |
||||
$model = new \app\common\model\server\Order; |
||||
$model->where(['order_id' => $order['order_id']])->save(['order_status' => ServerEnum::CANCELLED]); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* @notes:派单 |
||||
* @param array $order |
||||
* @param int $dealerId |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function dispatchOrders(array $order, int $dealerId): bool |
||||
{ |
||||
//判断该订单是否可以指派 |
||||
if (!empty($order) && $order['order_status'] == ServerEnum::APPLYDISPATCH) { |
||||
//判断指派人员是否是分销工程师 |
||||
$dealerInfo = DealerUserModel::detail($dealerId, []); |
||||
if (!empty($dealerInfo)) { |
||||
$dealerInfo = $dealerInfo->toArray(); |
||||
if ($dealerInfo['type'] == DealerUserEnum::ENGINEER) { |
||||
$model = new \app\common\model\server\Order; |
||||
$up['dealer_id'] = $dealerId; |
||||
$up['order_status'] = ServerEnum::APPLYSERVER; |
||||
$model->where(['order_id' => $order['order_id']])->save($up); |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
} |
Loading…
Reference in new issue