Order.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\book;
  13. use app\api\model\School;
  14. use app\api\model\SchoolSpeciality;
  15. use app\api\model\SpecialityBook;
  16. use app\api\model\User;
  17. use app\api\service\User as UserService;
  18. use app\common\library\wechat\WxPay;
  19. use app\common\model\book\Order as OrderModel;
  20. use app\common\model\wxapp\Setting as WxappSettingModel;
  21. use app\common\service\Order as OrderService;
  22. use app\common\enum\book\order\PayStatus as PayStatusEnum;
  23. use app\common\exception\BaseException;
  24. use think\model\relation\HasOne;
  25. /**
  26. * 用户报名订单模型
  27. * Class Order
  28. * @package app\api\model\book
  29. */
  30. class Order extends OrderModel
  31. {
  32. protected $globalScope = [''];
  33. /**
  34. * 隐藏字段
  35. * @var array
  36. */
  37. protected $hidden = [
  38. 'transaction_id',
  39. 'create_time',
  40. 'update_time',
  41. ];
  42. /**
  43. * 关联学校
  44. * @return HasOne
  45. */
  46. public function school(): HasOne
  47. {
  48. return $this->HasOne('app\\common\\model\\School','id','school_id')
  49. ->field('id,school_name as book_school_name,logo as school_logo')
  50. ->bind(['book_school_name','school_logo']);
  51. }
  52. /**
  53. * 代付用户
  54. * @return HasOne
  55. */
  56. public function pilUser(): HasOne
  57. {
  58. return $this->HasOne('app\\common\\model\\User','user_id','pil_user_id')
  59. ->field('user_id,nick_name as pil_user_nickname')
  60. ->bind(['pil_user_nickname']);
  61. }
  62. /**
  63. * 关联学校
  64. * @return HasOne
  65. */
  66. public function speciality(): HasOne
  67. {
  68. return $this->HasOne('app\\common\\model\\SchoolSpeciality','speciality_id','speciality_id')
  69. ->field('speciality_id,speciality_name')
  70. ->bind(['speciality_name']);
  71. }
  72. /**
  73. * 获取订单列表
  74. * @return \think\Paginator
  75. * @throws BaseException
  76. * @throws \think\db\exception\DbException
  77. */
  78. public function getList($params=[], $listRows=15, $field='')
  79. {
  80. $field = $field? $field: 'order_id,order_no,book_user_id,user_id,school_id,speciality_id,pil_user_id,price,total_price,pay_price,pay_type,pay_time,notice_img,receipt_name,receipt_mobile,receipt_address,refund_status,create_time,status';
  81. // 获取列表数据
  82. $list = $this->with(['school','speciality','pilUser'])
  83. ->where('status', '>=', PayStatusEnum::PENDING)
  84. ->where(function ($query) use($params){
  85. // 用户订单
  86. $userId = isset($params['book_user_id'])? $params['book_user_id'] : 0;
  87. if($userId>0){
  88. $query->where('book_user_id','=', $userId);
  89. }
  90. // 关键词
  91. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  92. if($keyword){
  93. $query->where('order_no','like', "%{$keyword}%");
  94. }
  95. // 老师订单
  96. $teacherId = isset($params['user_id'])? $params['user_id'] : 0;
  97. if($teacherId>0){
  98. $query->where('user_id','=', $teacherId);
  99. }
  100. $status = isset($params['status'])? $params['status'] : 0;
  101. if($status>0 || $status == -2){
  102. if($status == 1){
  103. $status = [1,2];
  104. }
  105. $status = !is_array($status)? [$status] : $status;
  106. $query->whereIn('status', $status);
  107. }
  108. })
  109. ->field($field)
  110. ->order(['create_time' => 'desc'])
  111. ->paginate($listRows)
  112. ->each(function($item, $k){
  113. $item['school_logo'] = $item['school_logo']? getPreview($item['school_logo']) : '';
  114. $item['pilUser'] = !is_null($item['pilUser'])? $item['pilUser'] : [];
  115. // $item['create_time_text'] = $item['create_time']? date('Y/m/d H:i:s', $item['create_time']) : '';
  116. });
  117. $list->hidden(['fields','update_time','is_settled']);
  118. return $list;
  119. }
  120. /**
  121. * 获取订单详情(待付款状态)
  122. * @param $orderNo
  123. * @return array|null|static
  124. */
  125. public static function getPayDetail(string $orderNo)
  126. {
  127. return self::detail(['order_no' => $orderNo, 'status' => PayStatusEnum::PENDING]);
  128. }
  129. /**
  130. * 获取用户报名订单详情(含关联数据)
  131. * @param int $orderId 订单ID
  132. * @return Order|array|null
  133. * @throws BaseException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public static function getUserOrderDetail(int $orderId)
  139. {
  140. // 查询订单记录
  141. $order = static::getDetail($orderId);
  142. // 该订单是否允许申请售后
  143. $order['isAllowRefund'] = 1;
  144. return $order;
  145. }
  146. /**
  147. * 获取用户报名订单详情(仅订单记录)
  148. * @param int $orderId
  149. * @param array $with
  150. * @return Order|array|null
  151. * @throws BaseException
  152. */
  153. public static function getDetail(int $orderId, array $with = [])
  154. {
  155. // 查询订单记录
  156. $order = static::detail($orderId, $with);
  157. empty($order) && throwError('订单不存在');
  158. return $order;
  159. }
  160. /**
  161. * 创建报名订单
  162. * @param array|null $params
  163. * @return bool|int
  164. * @throws BaseException
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function createOrder(array $params)
  170. {
  171. $teacherUserId = isset($params['user_id'])? intval($params['user_id']) : 0;
  172. $teacher = User::detail($teacherUserId,['info']);
  173. $teacherInfo = isset($teacher['info'])? $teacher['info'] : [];
  174. if(empty($teacherUserId) || empty($teacher)){
  175. $this->error = '报名咨询老师参数错误';
  176. return false;
  177. }
  178. $schoolId = isset($params['school_id'])? intval($params['school_id']) : 0;
  179. $specialityId = isset($params['speciality_id'])? intval($params['speciality_id']) : 0;
  180. if($schoolId<=0 || $specialityId <= 0){
  181. $this->error = '报名学校或报名专业参数错误';
  182. return false;
  183. }
  184. $school = School::detail($schoolId);
  185. if(empty($school)){
  186. $this->error = '报名学校不存在';
  187. return false;
  188. }
  189. // 表单参数验证
  190. $realName = isset($params['real_name'])? $params['real_name'] : '';
  191. if(empty($realName)){
  192. $this->error = '请填写报名用户真实姓名';
  193. return false;
  194. }
  195. $gender = isset($params['gender'])? intval($params['gender']) : 0;
  196. if($gender<=0){
  197. $this->error = '请选择报名用户性别';
  198. return false;
  199. }
  200. $schoolName = isset($params['school_name'])? $params['school_name'] : '';
  201. if(empty($schoolName)){
  202. $this->error = '请填写报名用户现读学校';
  203. return false;
  204. }
  205. $realName = isset($params['real_name'])? $params['real_name'] : '';
  206. if(empty($realName)){
  207. $this->error = '请填写报名用户真实姓名';
  208. return false;
  209. }
  210. $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
  211. if(empty($receiptName)){
  212. $this->error = '请填写收货人姓名';
  213. return false;
  214. }
  215. $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
  216. if(empty($receiptMobile)){
  217. $this->error = '请填写收货人联系电话';
  218. return false;
  219. }
  220. $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
  221. if(empty($receiptAddress)){
  222. $this->error = '请填写收货人地址';
  223. return false;
  224. }
  225. $trade = \app\store\model\Setting::getItem('trade');
  226. $price = isset($trade['books']['book_cost'])? floatval($trade['books']['book_cost']) : 0;
  227. if($price<=0){
  228. $this->error = '请先后台设置报名费用';
  229. return false;
  230. }
  231. // 专业参数验证
  232. $speciality = SchoolSpeciality::detail($specialityId);
  233. if(empty($speciality)){
  234. $this->error = '报名专业不存在';
  235. return false;
  236. }
  237. if($speciality['school_id'] != $schoolId){
  238. $this->error = '报名专业和学校不匹配';
  239. return false;
  240. }
  241. // 报名人数验证
  242. $bookNum = SpecialityBook::getBooks($specialityId);
  243. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  244. if($remainBookNum <= 0){
  245. $this->error = '该专业报名人数已满';
  246. return false;
  247. }
  248. // 订单数据
  249. $fields = isset($params['fields'])? $params['fields'] : [];
  250. $fields = $fields && is_array($fields)? json_encode($fields) : '';
  251. $order = [
  252. 'book_user_id' => UserService::getCurrentLoginUserId(),
  253. 'user_id' => $teacherUserId,
  254. 'agent_id' => isset($teacherInfo['agent_id'])? $teacherInfo['agent_id'] : 0,
  255. 'order_no' => 'BK' . OrderService::createOrderNo(),
  256. 'real_name' => $realName,
  257. 'gender' => $gender,
  258. 'age' => isset($params['age'])? $params['age'] : 0,
  259. 'school_name' => $schoolName,
  260. 'school_id' => $schoolId,
  261. 'speciality_id' => $specialityId,
  262. 'price' => floatval($price),
  263. 'total_price' => floatval($price),
  264. 'receipt_name' => $receiptName,
  265. 'receipt_mobile' => $receiptMobile,
  266. 'receipt_address' => $receiptAddress,
  267. 'fields' => $fields,
  268. 'create_time' => time(),
  269. 'update_time' => time(),
  270. 'status' => 1,
  271. ];
  272. return $this->save($order);
  273. }
  274. public function modifyOrder($orderId, $params=[])
  275. {
  276. if(empty($orderId)){
  277. $this->error = '订单参数错误';
  278. return false;
  279. }
  280. $modifyType = isset($params['type'])? $params['type'] : 0;
  281. if(!in_array($modifyType, [1,2])){
  282. $this->error = '订单修改类型错误';
  283. return false;
  284. }
  285. $model = self::detail($orderId);
  286. if(empty($model)){
  287. $this->error = '订单不存在';
  288. return false;
  289. }
  290. $data = [];
  291. if($modifyType == 1){
  292. if(!in_array($model['status'],[1,2,3])){
  293. $this->error = '订单状态不可操作';
  294. return false;
  295. }
  296. $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
  297. if(empty($receiptName)){
  298. $this->error = '请填写收货人姓名';
  299. return false;
  300. }
  301. $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
  302. if(empty($receiptMobile)){
  303. $this->error = '请填写收货人联系电话';
  304. return false;
  305. }
  306. $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
  307. if(empty($receiptAddress)){
  308. $this->error = '请填写收货人地址';
  309. return false;
  310. }
  311. $data = [
  312. 'receipt_name'=> $receiptName,
  313. 'receipt_mobile'=> $receiptMobile,
  314. 'receipt_address'=> $receiptAddress,
  315. 'update_time'=> time()
  316. ];
  317. }else if ($modifyType == 2){
  318. if(!in_array($model['status'],[1,2,3])){
  319. $this->error = '订单状态不可操作';
  320. return false;
  321. }
  322. $specialityId = isset($params['speciality_id'])? intval($params['speciality_id']) : 0;
  323. if(empty($specialityId)){
  324. $this->error = '请选择修改的专业';
  325. return false;
  326. }
  327. // 专业参数验证
  328. $speciality = SchoolSpeciality::detail($specialityId);
  329. if(empty($speciality)){
  330. $this->error = '报名专业不存在';
  331. return false;
  332. }
  333. if($speciality['school_id'] != $model['school_id']){
  334. $this->error = '报名专业和学校不匹配';
  335. return false;
  336. }
  337. // 报名人数验证
  338. $bookNum = SpecialityBook::getBooks($specialityId);
  339. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  340. if($remainBookNum <= 0){
  341. $this->error = '该专业报名人数已满';
  342. return false;
  343. }
  344. $data = ['speciality_id'=>$specialityId, 'update_time'=> time()] ;
  345. }
  346. if(empty($data)){
  347. $this->error = '修改类型错误';
  348. return false;
  349. }
  350. return $model->save($data);
  351. }
  352. /**
  353. * 验证支付订单
  354. * @param $order
  355. * @return bool
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. */
  360. public function checkPay($order)
  361. {
  362. $orderId = isset($order['order_id'])? $order['order_id'] : 0;
  363. if($orderId<=0){
  364. $this->error = '订单参数错误';
  365. return false;
  366. }
  367. $orderStatus = isset($order['status'])? $order['status'] : 0;
  368. if($orderStatus == 3){
  369. $this->error = '订单已支付';
  370. return false;
  371. }
  372. if($orderStatus != 2){
  373. $this->error = '订单未确认或状态不可支付';
  374. return false;
  375. }
  376. $specialityId = isset($order['speciality_id'])? intval($order['speciality_id']) : 0;
  377. // 专业参数验证
  378. $speciality = SchoolSpeciality::detail($specialityId);
  379. if(empty($speciality)){
  380. $this->error = '报名专业不存在';
  381. return false;
  382. }
  383. // 报名人数验证
  384. $bookNum = SpecialityBook::getBooks($specialityId);
  385. $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
  386. if($remainBookNum <= 0){
  387. $this->error = '该专业报名人数已满';
  388. return false;
  389. }
  390. // 已经报名次数
  391. $trade = \app\store\model\Setting::getItem('trade');
  392. $bookLimit = isset($trade['books']['book_limit'])? intval($trade['books']['book_limit']) : 0;
  393. $bookLimit = $bookLimit? $bookLimit : 3;
  394. if(SpecialityBook::getUserBooks($orderId) >= $bookLimit){
  395. $this->error = "您已经报名了{$bookLimit}次,不能再报名";
  396. return false;
  397. }
  398. return true;
  399. }
  400. /**
  401. * 确认报名订单
  402. * @param $orderId
  403. * @param $noticeImg
  404. * @return bool
  405. * @throws BaseException
  406. * @throws \think\db\exception\DataNotFoundException
  407. * @throws \think\db\exception\DbException
  408. * @throws \think\db\exception\ModelNotFoundException
  409. */
  410. public function confirmOrder($orderId, $noticeImg)
  411. {
  412. if(empty($orderId)){
  413. $this->error = '订单参数错误';
  414. return false;
  415. }
  416. if(empty($noticeImg)){
  417. $this->error = '请上传录取通知书';
  418. return false;
  419. }
  420. $model = new Order();
  421. $model = $model::getUserOrderDetail($orderId);
  422. if(!$model){
  423. $this->error = '报名订单不存在';
  424. return false;
  425. }
  426. // 订单所属老师才何以设置
  427. $userInfo = UserService::getCurrentLoginUser(true);
  428. if($userInfo['user_id'] != $model['user_id']){
  429. $this->error = '订单无权操作';
  430. return false;
  431. }
  432. if($model['status'] != 1){
  433. $this->error = '订单已确认或状态不可操作';
  434. return false;
  435. }
  436. return $model->save(['status'=> 2,'notice_img'=> $noticeImg]);
  437. }
  438. /**
  439. * 取消订单
  440. * @param $orderId
  441. * @param string $remark
  442. * @return bool
  443. * @throws BaseException
  444. * @throws \cores\exception\BaseException
  445. * @throws \think\db\exception\DataNotFoundException
  446. * @throws \think\db\exception\DbException
  447. * @throws \think\db\exception\ModelNotFoundException
  448. */
  449. public function cancelOrder($orderId, $remark='')
  450. {
  451. if(empty($orderId)){
  452. $this->error = '订单参数错误';
  453. return false;
  454. }
  455. $model = new Order();
  456. $model = $model::getUserOrderDetail($orderId);
  457. if(!$model){
  458. $this->error = '报名订单不存在';
  459. return false;
  460. }
  461. // 订单所属老师才何以设置
  462. $userInfo = UserService::getCurrentLoginUser(true);
  463. if($userInfo['user_id'] != $model['book_user_id']){
  464. $this->error = '订单无权操作';
  465. return false;
  466. }
  467. if(!in_array($model['status'], [1,2])){
  468. $this->error = '订单状态不可操作';
  469. return false;
  470. }
  471. return $model->save(['status'=> -1,'update_time'=> time(),'remark'=> $remark]);
  472. }
  473. /**
  474. * 订单发货设置
  475. * @param int $orderId 订单ID
  476. * @param $params 发货参数:快递公司ID、快递公司名称、快递单号
  477. * @return bool
  478. * @throws BaseException
  479. * @throws \think\db\exception\DataNotFoundException
  480. * @throws \think\db\exception\DbException
  481. * @throws \think\db\exception\ModelNotFoundException
  482. */
  483. public function setDelivery(int $orderId, $params)
  484. {
  485. $data['express_id'] = isset($params['express_id'])? $params['express_id'] : '';
  486. $data['express_company'] = isset($params['express_company'])? $params['express_company'] : '';
  487. $data['express_no'] = isset($params['express_no'])? $params['express_no'] : '';
  488. if(empty($orderId)){
  489. $this->error = '订单参数错误';
  490. return false;
  491. }
  492. if(empty($data['express_id'])){
  493. $this->error = '请选择快递公司';
  494. return false;
  495. }
  496. if(empty($data['express_company'])){
  497. $this->error = '请选择快递公司';
  498. return false;
  499. }
  500. if(empty($data['express_no'])){
  501. $this->error = '请填写快递单号';
  502. return false;
  503. }
  504. $model = new Order();
  505. $model = $model::getUserOrderDetail($orderId);
  506. if(!$model){
  507. $this->error = '报名订单不存在';
  508. return false;
  509. }
  510. // 订单所属老师才何以设置对应发货信息
  511. $userInfo = UserService::getCurrentLoginUser(true);
  512. if($userInfo['user_id'] != $model['user_id']){
  513. $this->error = '订单无权操作';
  514. return false;
  515. }
  516. if(in_array($model['status'], [1,2])){
  517. $this->error = '订单未支付';
  518. return false;
  519. }
  520. if($model['status'] != 3){
  521. $this->error = '订单已发货或状态不可操作';
  522. return false;
  523. }
  524. $data['status'] = 4;
  525. $data['delivery_time'] = time();
  526. return $model->save($data);
  527. }
  528. /**
  529. * 订单确认收货
  530. * @param $orderId
  531. * @return false
  532. * @throws BaseException
  533. * @throws \cores\exception\BaseException
  534. * @throws \think\db\exception\DataNotFoundException
  535. * @throws \think\db\exception\DbException
  536. * @throws \think\db\exception\ModelNotFoundException
  537. */
  538. public function receiptOrder($orderId)
  539. {
  540. if(empty($orderId)){
  541. $this->error = '订单参数错误';
  542. return false;
  543. }
  544. $model = new Order();
  545. $model = $model::getUserOrderDetail($orderId);
  546. if(!$model){
  547. $this->error = '报名订单不存在';
  548. return false;
  549. }
  550. $userInfo = UserService::getCurrentLoginUser(true);
  551. if($userInfo['user_id'] != $model['book_user_id']){
  552. $this->error = '订单无权操作';
  553. return false;
  554. }
  555. if($model['status'] != 4){
  556. $this->error = '订单已收货或状态不可操作';
  557. return false;
  558. }
  559. // 直接收货完成
  560. $data['status'] = 6;
  561. $data['receipt_time'] = time();
  562. return $model->save($data);
  563. }
  564. /**
  565. * 设置订单为可退款
  566. * @param int $orderId
  567. * @return bool
  568. * @throws BaseException
  569. * @throws \think\db\exception\DataNotFoundException
  570. * @throws \think\db\exception\DbException
  571. * @throws \think\db\exception\ModelNotFoundException
  572. */
  573. public function setRefund(int $orderId)
  574. {
  575. if(empty($orderId)){
  576. $this->error = '订单参数错误';
  577. return false;
  578. }
  579. $model = new Order();
  580. $model = $model::getUserOrderDetail($orderId);
  581. if(!$model){
  582. $this->error = '报名订单不存在';
  583. return false;
  584. }
  585. // 订单所属老师才可以操作
  586. $userInfo = UserService::getCurrentLoginUser(true);
  587. if($userInfo['user_id'] != $model['user_id']){
  588. $this->error = '订单无权操作';
  589. return false;
  590. }
  591. if(in_array($model['status'], [1,2])){
  592. $this->error = '订单未支付';
  593. return false;
  594. }
  595. if($model['refund_status'] != 1 || !in_array($model['status'], [3,4])){
  596. $this->error = '订单设置为可退款或状态不可操作';
  597. return false;
  598. }
  599. return $model->save(['refund_status'=> 2]);
  600. }
  601. /**
  602. * 订单申请退款
  603. * @param $orderId
  604. * @param $refundResult 退款原因
  605. * @return bool
  606. * @throws BaseException
  607. * @throws \think\db\exception\DataNotFoundException
  608. * @throws \think\db\exception\DbException
  609. * @throws \think\db\exception\ModelNotFoundException
  610. */
  611. public function refundOrder($orderId, $refundResult)
  612. {
  613. if(empty($orderId)){
  614. $this->error = '订单参数错误';
  615. return false;
  616. }
  617. if(empty($refundResult)){
  618. $this->error = '请填写退款原因';
  619. return false;
  620. }
  621. $model = new Order();
  622. $model = $model::getUserOrderDetail($orderId);
  623. if(!$model){
  624. $this->error = '报名订单不存在';
  625. return false;
  626. }
  627. // 订单所属用户才可以操作
  628. $userInfo = UserService::getCurrentLoginUser(true);
  629. if($userInfo['user_id'] != $model['book_user_id']){
  630. $this->error = '订单无权操作';
  631. return false;
  632. }
  633. if(in_array($model['status'], [1,2])){
  634. $this->error = '订单未支付';
  635. return false;
  636. }
  637. if($model['refund_status'] != 2 || !in_array($model['status'], [3,4])){
  638. $this->error = '订单已申请退款或状态不可操作';
  639. return false;
  640. }
  641. return $model->save(['refund_status'=> 3,'refund_result'=> $refundResult]);
  642. }
  643. /**
  644. * 订单退款确认
  645. * @param $orderId
  646. * @param $refundResult 退款凭证
  647. * @return bool
  648. * @throws BaseException
  649. * @throws \think\db\exception\DataNotFoundException
  650. * @throws \think\db\exception\DbException
  651. * @throws \think\db\exception\ModelNotFoundException
  652. */
  653. public function refundConfirm($orderId, $refundImg)
  654. {
  655. if(empty($orderId)){
  656. $this->error = '订单参数错误';
  657. return false;
  658. }
  659. if(empty($refundResult)){
  660. $this->error = '请填写退款原因';
  661. return false;
  662. }
  663. $model = new Order();
  664. $model = $model::getUserOrderDetail($orderId);
  665. if(!$model){
  666. $this->error = '报名订单不存在';
  667. return false;
  668. }
  669. // 订单所属老师才可以处理
  670. $userInfo = UserService::getCurrentLoginUser(true);
  671. if($userInfo['user_id'] != $model['user_id']){
  672. $this->error = '订单无权操作';
  673. return false;
  674. }
  675. if($model['status'] == -2){
  676. $this->error = '订单已退款';
  677. return false;
  678. }
  679. if(in_array($model['status'], [1,2])){
  680. $this->error = '订单未支付';
  681. return false;
  682. }
  683. if($model['refund_status'] != 3 || !in_array($model['status'], [3,4])){
  684. $this->error = '订单已退款或状态不可操作';
  685. return false;
  686. }
  687. // 调起退款
  688. if(!$this->refund($model)){
  689. $this->error = '退款处理失败';
  690. return false;
  691. }
  692. return $model->save(['refund_status'=> 4,'refund_img'=> $refundImg,'status'=> -2]);
  693. }
  694. public function cancel()
  695. {
  696. }
  697. /**
  698. * 退款调起
  699. * @param $order
  700. * @param int $money
  701. * @return bool
  702. * @throws BaseException
  703. * @throws \cores\exception\BaseException
  704. * @throws \think\db\exception\DataNotFoundException
  705. * @throws \think\db\exception\DbException
  706. * @throws \think\db\exception\ModelNotFoundException
  707. */
  708. private function refund($order, $money=0)
  709. {
  710. $wxConfig = WxappSettingModel::getWxappConfig(getStoreId());
  711. $WxPay = new WxPay($wxConfig, getStoreId());
  712. $money = $money? $money : $order['pay_price'];
  713. return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
  714. }
  715. }