| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model\book;
- use app\api\model\School;
- use app\api\model\SchoolSpeciality;
- use app\api\model\SpecialityBook;
- use app\api\model\User;
- use app\api\service\User as UserService;
- use app\common\library\wechat\WxPay;
- use app\common\model\book\Order as OrderModel;
- use app\common\model\wxapp\Setting as WxappSettingModel;
- use app\common\service\Order as OrderService;
- use app\common\enum\book\order\PayStatus as PayStatusEnum;
- use app\common\exception\BaseException;
- use think\model\relation\HasOne;
- /**
- * 用户报名订单模型
- * Class Order
- * @package app\api\model\book
- */
- class Order extends OrderModel
- {
- protected $globalScope = [''];
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'transaction_id',
- 'create_time',
- 'update_time',
- ];
- /**
- * 关联学校
- * @return HasOne
- */
- public function school(): HasOne
- {
- return $this->HasOne('app\\common\\model\\School','id','school_id')
- ->field('id,school_name as book_school_name,logo as school_logo')
- ->bind(['book_school_name','school_logo']);
- }
- /**
- * 代付用户
- * @return HasOne
- */
- public function pilUser(): HasOne
- {
- return $this->HasOne('app\\common\\model\\User','user_id','pil_user_id')
- ->field('user_id,nick_name as pil_user_nickname')
- ->bind(['pil_user_nickname']);
- }
- /**
- * 关联学校
- * @return HasOne
- */
- public function speciality(): HasOne
- {
- return $this->HasOne('app\\common\\model\\SchoolSpeciality','speciality_id','speciality_id')
- ->field('speciality_id,speciality_name')
- ->bind(['speciality_name']);
- }
- /**
- * 获取订单列表
- * @return \think\Paginator
- * @throws BaseException
- * @throws \think\db\exception\DbException
- */
- public function getList($params=[], $listRows=15, $field='')
- {
- $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';
- // 获取列表数据
- $list = $this->with(['school','speciality','pilUser'])
- ->where('status', '>=', PayStatusEnum::PENDING)
- ->where(function ($query) use($params){
- // 用户订单
- $userId = isset($params['book_user_id'])? $params['book_user_id'] : 0;
- if($userId>0){
- $query->where('book_user_id','=', $userId);
- }
- // 关键词
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('order_no','like', "%{$keyword}%");
- }
- // 老师订单
- $teacherId = isset($params['user_id'])? $params['user_id'] : 0;
- if($teacherId>0){
- $query->where('user_id','=', $teacherId);
- }
- $status = isset($params['status'])? $params['status'] : 0;
- if($status>0 || $status == -2){
- if($status == 1){
- $status = [1,2];
- }
- $status = !is_array($status)? [$status] : $status;
- $query->whereIn('status', $status);
- }
- })
- ->field($field)
- ->order(['create_time' => 'desc'])
- ->paginate($listRows)
- ->each(function($item, $k){
- $item['school_logo'] = $item['school_logo']? getPreview($item['school_logo']) : '';
- $item['pilUser'] = !is_null($item['pilUser'])? $item['pilUser'] : [];
- // $item['create_time_text'] = $item['create_time']? date('Y/m/d H:i:s', $item['create_time']) : '';
- });
- $list->hidden(['fields','update_time','is_settled']);
- return $list;
- }
- /**
- * 获取订单详情(待付款状态)
- * @param $orderNo
- * @return array|null|static
- */
- public static function getPayDetail(string $orderNo)
- {
- return self::detail(['order_no' => $orderNo, 'status' => PayStatusEnum::PENDING]);
- }
- /**
- * 获取用户报名订单详情(含关联数据)
- * @param int $orderId 订单ID
- * @return Order|array|null
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getUserOrderDetail(int $orderId)
- {
- // 查询订单记录
- $order = static::getDetail($orderId);
- // 该订单是否允许申请售后
- $order['isAllowRefund'] = 1;
- return $order;
- }
- /**
- * 获取用户报名订单详情(仅订单记录)
- * @param int $orderId
- * @param array $with
- * @return Order|array|null
- * @throws BaseException
- */
- public static function getDetail(int $orderId, array $with = [])
- {
- // 查询订单记录
- $order = static::detail($orderId, $with);
- empty($order) && throwError('订单不存在');
- return $order;
- }
- /**
- * 创建报名订单
- * @param array|null $params
- * @return bool|int
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function createOrder(array $params)
- {
- $teacherUserId = isset($params['user_id'])? intval($params['user_id']) : 0;
- $teacher = User::detail($teacherUserId,['info']);
- $teacherInfo = isset($teacher['info'])? $teacher['info'] : [];
- if(empty($teacherUserId) || empty($teacher)){
- $this->error = '报名咨询老师参数错误';
- return false;
- }
- $schoolId = isset($params['school_id'])? intval($params['school_id']) : 0;
- $specialityId = isset($params['speciality_id'])? intval($params['speciality_id']) : 0;
- if($schoolId<=0 || $specialityId <= 0){
- $this->error = '报名学校或报名专业参数错误';
- return false;
- }
- $school = School::detail($schoolId);
- if(empty($school)){
- $this->error = '报名学校不存在';
- return false;
- }
- // 表单参数验证
- $realName = isset($params['real_name'])? $params['real_name'] : '';
- if(empty($realName)){
- $this->error = '请填写报名用户真实姓名';
- return false;
- }
- $gender = isset($params['gender'])? intval($params['gender']) : 0;
- if($gender<=0){
- $this->error = '请选择报名用户性别';
- return false;
- }
- $schoolName = isset($params['school_name'])? $params['school_name'] : '';
- if(empty($schoolName)){
- $this->error = '请填写报名用户现读学校';
- return false;
- }
- $realName = isset($params['real_name'])? $params['real_name'] : '';
- if(empty($realName)){
- $this->error = '请填写报名用户真实姓名';
- return false;
- }
- $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
- if(empty($receiptName)){
- $this->error = '请填写收货人姓名';
- return false;
- }
- $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
- if(empty($receiptMobile)){
- $this->error = '请填写收货人联系电话';
- return false;
- }
- $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
- if(empty($receiptAddress)){
- $this->error = '请填写收货人地址';
- return false;
- }
- $trade = \app\store\model\Setting::getItem('trade');
- $price = isset($trade['books']['book_cost'])? floatval($trade['books']['book_cost']) : 0;
- if($price<=0){
- $this->error = '请先后台设置报名费用';
- return false;
- }
- // 专业参数验证
- $speciality = SchoolSpeciality::detail($specialityId);
- if(empty($speciality)){
- $this->error = '报名专业不存在';
- return false;
- }
- if($speciality['school_id'] != $schoolId){
- $this->error = '报名专业和学校不匹配';
- return false;
- }
- // 报名人数验证
- $bookNum = SpecialityBook::getBooks($specialityId);
- $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
- if($remainBookNum <= 0){
- $this->error = '该专业报名人数已满';
- return false;
- }
- // 订单数据
- $fields = isset($params['fields'])? $params['fields'] : [];
- $fields = $fields && is_array($fields)? json_encode($fields) : '';
- $order = [
- 'book_user_id' => UserService::getCurrentLoginUserId(),
- 'user_id' => $teacherUserId,
- 'agent_id' => isset($teacherInfo['agent_id'])? $teacherInfo['agent_id'] : 0,
- 'order_no' => 'BK' . OrderService::createOrderNo(),
- 'real_name' => $realName,
- 'gender' => $gender,
- 'age' => isset($params['age'])? $params['age'] : 0,
- 'school_name' => $schoolName,
- 'school_id' => $schoolId,
- 'speciality_id' => $specialityId,
- 'price' => floatval($price),
- 'total_price' => floatval($price),
- 'receipt_name' => $receiptName,
- 'receipt_mobile' => $receiptMobile,
- 'receipt_address' => $receiptAddress,
- 'fields' => $fields,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- ];
- return $this->save($order);
- }
- public function modifyOrder($orderId, $params=[])
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- $modifyType = isset($params['type'])? $params['type'] : 0;
- if(!in_array($modifyType, [1,2])){
- $this->error = '订单修改类型错误';
- return false;
- }
- $model = self::detail($orderId);
- if(empty($model)){
- $this->error = '订单不存在';
- return false;
- }
- $data = ['update_time'=> time()];
- if($modifyType == 1){
- if(!in_array($model['status'],[1,2,3])){
- $this->error = '订单状态不可操作';
- return false;
- }
- $receiptName = isset($params['receipt_name'])? $params['receipt_name'] : '';
- if(empty($receiptName)){
- $this->error = '请填写收货人姓名';
- return false;
- }
- $receiptMobile = isset($params['receipt_mobile'])? $params['receipt_mobile'] : '';
- if(empty($receiptMobile)){
- $this->error = '请填写收货人联系电话';
- return false;
- }
- $receiptAddress = isset($params['receipt_address'])? $params['receipt_address'] : '';
- if(empty($receiptAddress)){
- $this->error = '请填写收货人地址';
- return false;
- }
- $data = [
- 'receipt_name'=> $receiptName,
- 'receipt_mobile'=> $receiptMobile,
- 'receipt_address'=> $receiptAddress,
- ];
- }else if ($modifyType == 2){
- if(!in_array($model['status'],[1,2,3])){
- $this->error = '订单状态不可操作';
- return false;
- }
- $specialityId = isset($params['speciality_id'])? intval(['speciality_id']) : 0;
- if(empty($specialityId)){
- $this->error = '请选择修改的专业';
- return false;
- }
- // 专业参数验证
- $speciality = SchoolSpeciality::detail($specialityId);
- if(empty($speciality)){
- $this->error = '报名专业不存在';
- return false;
- }
- if($speciality['school_id'] != $model['school_id']){
- $this->error = '报名专业和学校不匹配';
- return false;
- }
- // 报名人数验证
- $bookNum = SpecialityBook::getBooks($specialityId);
- $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
- if($remainBookNum <= 0){
- $this->error = '该专业报名人数已满';
- return false;
- }
- $data = [
- 'speciality_id' => $specialityId
- ];
- }
- return $model->save($data);
- }
- /**
- * 验证支付订单
- * @param $order
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function checkPay($order)
- {
- $orderId = isset($order['order_id'])? $order['order_id'] : 0;
- if($orderId<=0){
- $this->error = '订单参数错误';
- return false;
- }
- $orderStatus = isset($order['status'])? $order['status'] : 0;
- if($orderStatus == 3){
- $this->error = '订单已支付';
- return false;
- }
- if($orderStatus != 2){
- $this->error = '订单未确认或状态不可支付';
- return false;
- }
- $specialityId = isset($order['speciality_id'])? intval($order['speciality_id']) : 0;
- // 专业参数验证
- $speciality = SchoolSpeciality::detail($specialityId);
- if(empty($speciality)){
- $this->error = '报名专业不存在';
- return false;
- }
- // 报名人数验证
- $bookNum = SpecialityBook::getBooks($specialityId);
- $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
- if($remainBookNum <= 0){
- $this->error = '该专业报名人数已满';
- return false;
- }
- // 已经报名次数
- $trade = \app\store\model\Setting::getItem('trade');
- $bookLimit = isset($trade['books']['book_limit'])? intval($trade['books']['book_limit']) : 0;
- $bookLimit = $bookLimit? $bookLimit : 3;
- if(SpecialityBook::getUserBooks($orderId) >= $bookLimit){
- $this->error = "您已经报名了{$bookLimit}次,不能再报名";
- return false;
- }
- return true;
- }
- /**
- * 确认报名订单
- * @param $orderId
- * @param $noticeImg
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function confirmOrder($orderId, $noticeImg)
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- if(empty($noticeImg)){
- $this->error = '请上传录取通知书';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属老师才何以设置
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if($model['status'] != 1){
- $this->error = '订单已确认或状态不可操作';
- return false;
- }
- return $model->save(['status'=> 2,'notice_img'=> $noticeImg]);
- }
- /**
- * 取消订单
- * @param $orderId
- * @param string $remark
- * @return bool
- * @throws BaseException
- * @throws \cores\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cancelOrder($orderId, $remark='')
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属老师才何以设置
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['book_user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if(!in_array($model['status'], [1,2])){
- $this->error = '订单状态不可操作';
- return false;
- }
- return $model->save(['status'=> -1,'update_time'=> time(),'remark'=> $remark]);
- }
- /**
- * 订单发货设置
- * @param int $orderId 订单ID
- * @param $params 发货参数:快递公司ID、快递公司名称、快递单号
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function setDelivery(int $orderId, $params)
- {
- $data['express_id'] = isset($params['express_id'])? $params['express_id'] : '';
- $data['express_company'] = isset($params['express_company'])? $params['express_company'] : '';
- $data['express_no'] = isset($params['express_no'])? $params['express_no'] : '';
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- if(empty($data['express_id'])){
- $this->error = '请选择快递公司';
- return false;
- }
- if(empty($data['express_company'])){
- $this->error = '请选择快递公司';
- return false;
- }
- if(empty($data['express_no'])){
- $this->error = '请填写快递单号';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属老师才何以设置对应发货信息
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if(in_array($model['status'], [1,2])){
- $this->error = '订单未支付';
- return false;
- }
- if($model['status'] != 3){
- $this->error = '订单已发货或状态不可操作';
- return false;
- }
- $data['status'] = 4;
- $data['delivery_time'] = time();
- return $model->save($data);
- }
- /**
- * 订单确认收货
- * @param $orderId
- * @return false
- * @throws BaseException
- * @throws \cores\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function receiptOrder($orderId)
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['book_user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if($model['refund_status'] != 4){
- $this->error = '订单已收货或状态不可操作';
- return false;
- }
- // 直接收货完成
- $data['status'] = 6;
- $data['receipt_time'] = time();
- return $model->save($data);
- }
- /**
- * 设置订单为可退款
- * @param int $orderId
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function setRefund(int $orderId)
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属老师才可以操作
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if(in_array($model['status'], [1,2])){
- $this->error = '订单未支付';
- return false;
- }
- if($model['refund_status'] != 1 || !in_array($model['status'], [3,4])){
- $this->error = '订单设置为可退款或状态不可操作';
- return false;
- }
- return $model->save(['refund_status'=> 2]);
- }
- /**
- * 订单申请退款
- * @param $orderId
- * @param $refundResult 退款原因
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function refundOrder($orderId, $refundResult)
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- if(empty($refundResult)){
- $this->error = '请填写退款原因';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属用户才可以操作
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['book_user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if(in_array($model['status'], [1,2])){
- $this->error = '订单未支付';
- return false;
- }
- if($model['refund_status'] != 2 || !in_array($model['status'], [3,4])){
- $this->error = '订单已申请退款或状态不可操作';
- return false;
- }
- return $model->save(['refund_status'=> 3,'refund_result'=> $refundResult]);
- }
- /**
- * 订单退款确认
- * @param $orderId
- * @param $refundResult 退款凭证
- * @return bool
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function refundConfirm($orderId, $refundImg)
- {
- if(empty($orderId)){
- $this->error = '订单参数错误';
- return false;
- }
- if(empty($refundResult)){
- $this->error = '请填写退款原因';
- return false;
- }
- $model = new Order();
- $model = $model::getUserOrderDetail($orderId);
- if(!$model){
- $this->error = '报名订单不存在';
- return false;
- }
- // 订单所属老师才可以处理
- $userInfo = UserService::getCurrentLoginUser(true);
- if($userInfo['user_id'] != $model['user_id']){
- $this->error = '订单无权操作';
- return false;
- }
- if($model['status'] == -2){
- $this->error = '订单已退款';
- return false;
- }
- if(in_array($model['status'], [1,2])){
- $this->error = '订单未支付';
- return false;
- }
- if($model['refund_status'] != 3 || !in_array($model['status'], [3,4])){
- $this->error = '订单已退款或状态不可操作';
- return false;
- }
- // 调起退款
- if(!$this->refund($model)){
- $this->error = '退款处理失败';
- return false;
- }
- return $model->save(['refund_status'=> 4,'refund_img'=> $refundImg,'status'=> -2]);
- }
- public function cancel()
- {
- }
- /**
- * 退款调起
- * @param $order
- * @param int $money
- * @return bool
- * @throws BaseException
- * @throws \cores\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private function refund($order, $money=0)
- {
- $wxConfig = WxappSettingModel::getWxappConfig(getStoreId());
- $WxPay = new WxPay($wxConfig, getStoreId());
- $money = $money? $money : $order['pay_price'];
- return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
- }
- }
|