OrderModel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Controller\Business\DriverTypeBusiness;
  5. use App\Master\Enum\RedisKeyEnum;
  6. use App\Master\Framework\Library\AliCloud\AliSms;
  7. use App\Master\Framework\Library\Tencent\TencentIm;
  8. use App\Master\Framework\Library\Twilio\Sms;
  9. use App\Model\Model;
  10. use App\Utils\Common;
  11. use App\Utils\RedisUtil;
  12. use Hyperf\DbConnection\Db;
  13. use function Hyperf\Config\config;
  14. class OrderModel extends Model
  15. {
  16. /**
  17. * The table associated with the model.
  18. *
  19. * @var ?string
  20. */
  21. protected ?string $table = 'order';
  22. protected ?string $dateFormat = 'U';
  23. public bool $timestamps = false;
  24. protected int $is_status_search = 0;// 默认使用 status = 1 筛选
  25. protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
  26. /**
  27. * 默认查询字段
  28. *
  29. * @var array|string[]
  30. */
  31. public array $select = [
  32. '*'
  33. ];
  34. const status = [
  35. 10 => '平台已接单',
  36. 20 => '司机已结单',
  37. 30 => '正在服务中',
  38. 40 => '已完成',
  39. 50 => '已取消'
  40. ];
  41. public function searchOrderNoAttribute($query, $value, array $params): mixed
  42. {
  43. if (empty($value)) {
  44. return $query;
  45. }
  46. return $query->where('order_no', $value);
  47. }
  48. public function searchUserIdAttribute($query, $value, array $params): mixed
  49. {
  50. if (empty($value)) {
  51. return $query;
  52. }
  53. return $query->where('user_id', $value);
  54. }
  55. public function searchDriverIdAttribute($query, $value, array $params): mixed
  56. {
  57. if (empty($value)) {
  58. return $query;
  59. }
  60. return $query->where('driver_id', $value);
  61. }
  62. public function searchStatusAttribute($query, $value, array $params): mixed
  63. {
  64. if (empty($value)) {
  65. return $query;
  66. }
  67. return $query->where('status', $value);
  68. }
  69. public function searchStatusInAttribute($query, $value, array $params): mixed
  70. {
  71. if (empty($value)) {
  72. return $query;
  73. }
  74. return $query->whereIn('status', $value);
  75. }
  76. public function searchTypeInAttribute($query, $value, array $params): mixed
  77. {
  78. if (empty($value)) {
  79. return $query;
  80. }
  81. return $query->whereIn('type', $value);
  82. }
  83. public function searchDriverStatusAttribute($query, $value, array $params): mixed
  84. {
  85. if (empty($value)) {
  86. return $query;
  87. }
  88. return $query->whereIn('driver_status', $value);
  89. }
  90. public function searchAppointmentTimeNotAttribute($query, $value, array $params): mixed
  91. {
  92. if (empty($value)) {
  93. return $query;
  94. }
  95. return $query->where('appointment_time', '>', 0);
  96. }
  97. public function searchAppointmentTimeZeroAttribute($query, $value, array $params): mixed
  98. {
  99. if (empty($value)) {
  100. return $query;
  101. }
  102. return $query->where('appointment_time', 0);
  103. }
  104. public function searchAppointmentTimeBetweenAttribute($query, $value, array $params): mixed
  105. {
  106. if (empty($value)) {
  107. return $query;
  108. }
  109. return $query->whereBetween('appointment_time', $value);
  110. }
  111. public function searchAppointmentTimeGtAttribute($query, $value, array $params): mixed
  112. {
  113. if (empty($value)) {
  114. return $query;
  115. }
  116. return $query->where('appointment_time','>=', $value);
  117. }
  118. public function searchPushStatusAttribute($query, $value, array $params): mixed
  119. {
  120. if (empty($value)) {
  121. return $query;
  122. }
  123. return $query->where('push_status', $value);
  124. }
  125. public function searchPushStatusInAttribute($query, $value, array $params): mixed
  126. {
  127. if (empty($value)) {
  128. return $query;
  129. }
  130. return $query->whereIn('push_status', $value);
  131. }
  132. public function searchStatDistanceAttribute($query, $value, array $params): mixed
  133. {
  134. if (empty($value)) {
  135. return $query;
  136. }
  137. return $query->havingRaw('(stat_distance < ? and appointment_time = 0) or appointment_time != 0', [$value]);
  138. }
  139. public function searchStatusOrderByAttribute($query, $value, array $params): mixed
  140. {
  141. if (empty($value)) {
  142. return $query;
  143. }
  144. $order = implode(',',$value);
  145. return $query->orderByRaw("FIELD(status,{$order})");
  146. }
  147. public function searchEndDriverDateBetweenAttribute($query, $value, array $params): mixed
  148. {
  149. if (empty($value)) {
  150. return $query;
  151. }
  152. return $query->whereBetween('end_driver_time', $value);
  153. }
  154. // 筛选,如果是快车和接送机 则需要判断司机车型是否大于 订单所需车型
  155. public function searchDriverCarSeatIdAttribute($query, $value, array $params): mixed
  156. {
  157. if (empty($value)) {
  158. return $query;
  159. }
  160. return $query->where(function ($where) use ($value){
  161. $where->whereIn('type',[10,20])->where('car_seat_id','<=', $value);
  162. });
  163. }
  164. public function createOrder(int $user_id, array $params = [], array $point = [])
  165. {
  166. $params['user_id'] = $user_id;
  167. $params['order_no'] = Common::createOrderNo('C');
  168. $params['status'] = 10;
  169. $params['create_time'] = time();
  170. if (!$order_id = self::query()->insertGetId($params)) {
  171. return $this->error('订单创建失败');
  172. }
  173. $pointNum = count($point);
  174. // 机场坐标
  175. $common_airport_coord = array_values(site('common_airport_coord'));
  176. // 接送机范围
  177. $common_airport_range = site('common_airport_range');
  178. // 市区邮编
  179. $common_city_postal_code = array_values(site('common_city_postal_code'));
  180. // 创建途径点
  181. foreach ($point as $key => $item) {
  182. if ($key == 0) {
  183. $type = 1;
  184. } elseif ($key == ($pointNum - 1)) {
  185. $type = 2;
  186. } else {
  187. $type = 3;
  188. }
  189. $is_in_city = 0;// 是否在市区:1=是,0=否
  190. $is_at_airport = 0;// 是否在机场:1=是,0=否
  191. // 判断 起始点 途径点 是否 在市区
  192. if (in_array($item['postal_code'], $common_city_postal_code)) {
  193. $is_in_city = 1;
  194. }
  195. // 循环判断 起始点 途径点 是否 在机场附近
  196. foreach ($common_airport_coord as $coord) {
  197. [$lng, $lat] = explode(',', $coord);
  198. [$cha_km,$unit] = Common::distance((float)$lng, (float)$lat, (float)$item['lng'], (float)$item['lat'], 1);
  199. if ($cha_km <= $common_airport_range) {
  200. $is_at_airport = 1;
  201. }
  202. }
  203. $order_point[] = [
  204. 'user_id' => $user_id,
  205. 'order_id' => $order_id,
  206. 'order_no' => $params['order_no'],
  207. 'type' => $type,
  208. 'name' => $item['name'] ?? '',
  209. 'phone' => $item['phone'] ?? '',
  210. 'lng' => $item['lng'],
  211. 'lat' => $item['lat'],
  212. 'address' => $item['address'],
  213. 'postal_code' => $item['postal_code'],
  214. 'is_in_city' => $is_in_city,
  215. 'is_at_airport' => $is_at_airport,
  216. 'status' => 1,
  217. 'create_time' => time()
  218. ];
  219. }
  220. if (isset($order_point)) {
  221. if (!OrderPointModel::query()->insert($order_point)) {
  222. return $this->error('创建途径点失败');
  223. }
  224. }
  225. return $this->success('提交订单成功', [
  226. 'order_no' => $params['order_no']
  227. ]);
  228. }
  229. /**
  230. * 取消订单
  231. * @param string $order_no
  232. * @return bool
  233. */
  234. public function cancel(string $order_no,int $user_id = 0,int $driver_id = 0,string $reason = '',int $is_system = 0)
  235. {
  236. $params['order_no'] = $order_no;
  237. if (!empty($user_id)){
  238. $params['user_id'] = $user_id;
  239. }
  240. if (!empty($driver_id)){
  241. $params['driver_id'] = $driver_id;
  242. }
  243. $order = $this->getDetail(params: $params, with: ['user']);
  244. if ((!empty($params['user_id']) && !in_array($order['status'],[10,20,30])) || (!empty($params['driver_id']) && $order['status'] != 20)){
  245. $status_name = self::status[$order['status']];
  246. return $this->error("当前订单{$status_name},无法取消");
  247. }
  248. if (!empty($params['user_id']) && $order['status'] == 30 && $order['driver_status'] != 1){
  249. return $this->error("订单只能在司机到达出发点前才可取消");
  250. }
  251. // 如果用户编号和司机编号都没传 则只有司机未结单的状态才可取消
  252. if (empty($params['user_id']) && empty($params['driver_id']) && $is_system === 0 && !in_array($order['status'],[10])){
  253. $status_name = self::status[$order['status']];
  254. return $this->error("当前订单{$status_name},无法取消");
  255. }
  256. // 订单取消原因
  257. if (!empty($driver_id)){
  258. $reason = "司机已取消订单:{$params['order_no']}";
  259. // 发送系统消息
  260. MessageModel::add([
  261. 'user_id' => $order['user_id'],
  262. 'type' => 1,
  263. 'name' => "订单已取消",
  264. 'content' => $reason,
  265. 'value' => $order_no
  266. ]);
  267. if ($order['user']['area_code'] == '+86') {
  268. $sms = new AliSms();
  269. if (!$sms->send($order['user']['mobile'], [],4)) {
  270. return $this->error($sms->getMessage() ?? '发送失败');
  271. }
  272. } else {
  273. // 国外短信
  274. $text = "【好滴用车】您的好滴用车订单已被司机取消,请前往好滴用车用户端重新下单";
  275. $sms = new Sms();
  276. if (!$sms->send("{$order['user']['area_code']}{$order['user']['mobile']}", $text)) {
  277. return $this->error($sms->getMessage() ?? '发送失败');
  278. }
  279. }
  280. }else{
  281. if (!empty($order['driver_id'])){
  282. $reason = "用户已取消订单:{$params['order_no']},{$reason}";
  283. // 发送系统消息
  284. DriverMessageModel::add([
  285. 'driver_id' => $order['driver_id'],
  286. 'type' => 1,
  287. 'name' => "订单已取消",
  288. 'content' => $reason,
  289. 'value' => $order_no
  290. ]);
  291. }elseif ($is_system === 1){
  292. $reason = "系统操作:{$params['order_no']},{$reason}";
  293. // 发送系统消息
  294. MessageModel::add([
  295. 'user_id' => $order['user_id'],
  296. 'type' => 1,
  297. 'name' => "订单已取消",
  298. 'content' => $reason,
  299. 'value' => $order_no
  300. ]);
  301. }else{
  302. $reason = "订单超时暂无司机接单:{$params['order_no']},{$reason}";
  303. // 发送系统消息
  304. MessageModel::add([
  305. 'user_id' => $order['user_id'],
  306. 'type' => 1,
  307. 'name' => "订单已取消",
  308. 'content' => $reason,
  309. 'value' => $order_no
  310. ]);
  311. }
  312. }
  313. $orderUp = OrderModel::query()->where('id', $order['id'])->whereIn('status',[10, 20, 30])->update([
  314. 'status' => 50,
  315. 'cancel_reason' => $reason,
  316. 'update_time' => time()
  317. ]);
  318. if (!$orderUp) {
  319. return $this->error('操作失败');
  320. }
  321. // 在线支付需要退还支付金额
  322. if ($order['pay_type'] == 1){
  323. $type_name = DriverTypeBusiness::type[$order['type']] ?? '';
  324. $wallet = new UserWalletModel();
  325. if (!$wallet->change($order['user_id'],(float)$order['pay_amount'],"{$type_name}订单取消退还支付金额",4)){
  326. return $this->error($wallet->getMessage());
  327. }
  328. }
  329. // 退还优惠券
  330. if (!empty($order['coupon_id'])){
  331. if (!UserCouponModel::query()->where('id',$order['coupon_id'])->update(['is_use'=>0,'update_time'=>time()])){
  332. return $this->error('优惠券退还失败');
  333. }
  334. }
  335. // 删除司机用户聊天框
  336. RedisUtil::getInstance(RedisKeyEnum::USER_DRIVER_CHAT_DEL,"user_{$order['user_id']}")->setex("driver_{$order['driver_id']}",500);
  337. $tencent = new TencentIm();
  338. $tencent->delete_chat("user_{$order['user_id']}","driver_{$order['driver_id']}");
  339. return $this->success('操作成功');
  340. }
  341. /**
  342. * 邀请司机接单
  343. * @param int $order_id
  344. * @return bool
  345. */
  346. public function tailwind_invite(int $order_id, int $driver_order_id)
  347. {
  348. $orderUp = OrderModel::query()->where('id', $order_id)->where('status', 10)->update([
  349. 'driver_order_id' => $driver_order_id,
  350. 'update_time' => time()
  351. ]);
  352. if (!$orderUp) {
  353. return $this->error('操作失败');
  354. }
  355. return $this->success('操作成功');
  356. }
  357. /**
  358. * 加价
  359. * @param int $order_id
  360. * @param float $amount
  361. * @return bool
  362. */
  363. public function addMoney(int $order_id, float $amount)
  364. {
  365. $orderUp = OrderModel::query()->where('id', $order_id)->update([
  366. 'total_amount' => Db::raw('total_amount + ' . $amount),
  367. 'pay_amount' => Db::raw('pay_amount + ' . $amount),
  368. 'raise_amount' => Db::raw('raise_amount + ' . $amount),
  369. 'update_time' => time()
  370. ]);
  371. if (!$orderUp) {
  372. return $this->error('操作失败');
  373. }
  374. return $this->success('操作成功');
  375. }
  376. public function points()
  377. {
  378. return $this->hasMany(OrderPointModel::class, 'order_id', 'id');
  379. }
  380. public function user()
  381. {
  382. return $this->hasOne(UserModel::class, 'id', 'user_id');
  383. }
  384. public function driver()
  385. {
  386. return $this->hasOne(DriverModel::class, 'id', 'driver_id');
  387. }
  388. public function car_seat()
  389. {
  390. return $this->hasOne(CarSeatModel::class, 'id', 'car_seat_id');
  391. }
  392. }