diff --git a/application/api/controller/school/HourOrder.php b/application/api/controller/school/HourOrder.php index 41ceb1c..b249f67 100644 --- a/application/api/controller/school/HourOrder.php +++ b/application/api/controller/school/HourOrder.php @@ -5,7 +5,7 @@ namespace app\api\controller\school; use app\common\model\school\classes\hourorder\Order as OrderModel; /** - * 课时预约订单接口 + * 用户端:课时预约订单接口 */ class HourOrder extends Base { @@ -29,6 +29,105 @@ class HourOrder extends Base } + + + + + + + /** + * @ApiTitle( 订单确认/订单计算接口) + * @ApiSummary(订单确认/订单计算接口【有过期时间】) + * @ApiRoute(/api/school/hour_order/confirm) + * @ApiMethod(POST) + * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id") + * @ApiParams(name = "classes_lib_spec_id", type = "int",required=false,description = "课时规格id") + * @ApiParams(name = "order_no", type = "string",required=false,description = "缓存key") + * @ApiParams(name = "is_compute", type = "int",required=false,description = "是否重新计算并更新缓存 默认传1") + * @ApiReturn({ + * + *}) + */ + public function confirm(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $classes_order_id = $this->request->post('classes_order_id/d', 0); //课程id + $classes_lib_spec_id = $this->request->post('classes_lib_spec_id/d', 0); //课程id +// + $param = []; //参数 + $order_no = $this->request->post('order_no/s', ''); //订单号 + $is_compute = $this->request->post('is_compute/d', 1); //是否重新计算并更新缓存 + try{ + //当前申请状态 + $res = $this->model->confirm($user_id, $classes_order_id,$order_no,$classes_lib_spec_id,$param, $is_compute); + }catch (\Exception $e){ +// Log::log($e->getMessage()); + $this->error($e->getMessage(),['errcode'=>$e->getCode()]); + } + $this->success('执行成功,可用缓存key去预约', $res); + } + + + /** + * @ApiTitle( 订单下单接口) + * @ApiSummary(订单下单接口) + * @ApiRoute(/api/school/hour_order/create) + * @ApiMethod(POST) + * @ApiParams(name = "order_no", type = "string",required=true,description = "缓存key") + * @ApiReturn({ + * + *}) + */ + public function create(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $order_no = $this->request->post('order_no/s', ''); //订单号 + $remark = $this->request->post('remark/s', ''); //下单备注 +// repeat_filter("appointment\order\create".$user_id, 2); + try{ + //当前申请状态 + $res = $this->model->cacheCreateOrder($order_no, $user_id,$remark, true); + }catch (\Throwable $e){ +// file_put_contents("test.txt",$e->getMessage().$e->getFile().$e->getLine());//写入文件,一般做正式环境测试 +// Log::log($e->getMessage()); + $this->error($e->getMessage(),['errcode'=>$e->getCode()]);; + } + $this->success('订单创建成功,缓存key被消耗', $res); + } + + + + /** + * @ApiTitle( 预约课时单取消接口) + * @ApiSummary(预约课时单取消接口(已完成的单无法取消)) + * @ApiRoute(/api/school/hour_order/cancel) + * @ApiMethod(POST) + * @ApiParams(name = "order_no", type = "string",required=true,description = "订单号") + * @ApiReturn({ + * + *}) + */ + public function cancel(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $order_no = $this->request->post('order_no/s', ''); //订单号 + try{ + //当前申请状态 + $res = $this->model->cancel($order_no,$user_id,true,'user',0,true); + }catch (\Throwable $e){ + $this->error($e->getMessage()); + } + $this->success('预约课时取消成功', $res); + } + + + + + + /** * @ApiTitle( 课时订单详情) * @ApiSummary(课时订单详情) diff --git a/application/api/controller/school/Order.php b/application/api/controller/school/Order.php index 01596db..b928c21 100644 --- a/application/api/controller/school/Order.php +++ b/application/api/controller/school/Order.php @@ -6,7 +6,7 @@ use app\common\model\school\classes\order\Order as OrderModel; use app\common\model\school\classes\Teacher as Teachermodel; /** - * 课程订单接口 + * 用户端:课程订单接口 */ class Order extends Base { diff --git a/application/api/controller/school/worker/HourOrder.php b/application/api/controller/school/worker/HourOrder.php index 5c53c9f..5d568f8 100644 --- a/application/api/controller/school/worker/HourOrder.php +++ b/application/api/controller/school/worker/HourOrder.php @@ -4,6 +4,7 @@ namespace app\api\controller\school\worker; use app\common\model\school\classes\hourorder\Order as OrderModel; +use app\common\model\school\classes\order\Order as ClassesOrderModel; /** * 员工端:课时订单管理接口 @@ -30,6 +31,112 @@ class HourOrder extends Base } + + + + + /** + * @ApiTitle( 员工代下单:订单确认/订单计算接口) + * @ApiSummary(订单确认/订单计算接口【有过期时间】) + * @ApiRoute(/api/school/worker/hour_order/confirm) + * @ApiMethod(POST) + * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id") + * @ApiParams(name = "classes_lib_spec_id", type = "int",required=false,description = "课时规格id") + * @ApiParams(name = "order_no", type = "string",required=false,description = "缓存key") + * @ApiParams(name = "is_compute", type = "int",required=false,description = "是否重新计算并更新缓存 默认传1") + * @ApiReturn({ + * + *}) + */ + public function confirm(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $classes_order_id = $this->request->post('classes_order_id/d', 0); //课程id + $classes_lib_spec_id = $this->request->post('classes_lib_spec_id/d', 0); //课程id + //记录代下单人信息 + $param = [ + "type" =>'2', + "help_user_id" =>$user_id, + "help_type" => 'user', + ]; //参数 + $order_no = $this->request->post('order_no/s', ''); //订单号 + $is_compute = $this->request->post('is_compute/d', 1); //是否重新计算并更新缓存 + try{ + + //当前申请状态 + $res = $this->model->confirm($user_id, $classes_order_id,$order_no,$classes_lib_spec_id,$param, $is_compute); + }catch (\Exception $e){ +// Log::log($e->getMessage()); + $this->error($e->getMessage(),['errcode'=>$e->getCode()]); + } + $this->success('执行成功,可用缓存key去预约', $res); + } + + + /** + * @ApiTitle( 员工代下单:订单下单接口) + * @ApiSummary(订单下单接口) + * @ApiRoute(/api/school/worker/hour_order/create) + * @ApiMethod(POST) + * @ApiParams(name = "order_no", type = "string",required=true,description = "缓存key") + * @ApiReturn({ + * + *}) + */ + public function create(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $order_no = $this->request->post('order_no/s', ''); //订单号 + $remark = $this->request->post('remark/s', ''); //下单备注 +// repeat_filter("appointment\order\create".$user_id, 2); + try{ + //当前申请状态 + $res = $this->model->cacheCreateOrder($order_no, $user_id,$remark, true); + }catch (\Throwable $e){ +// file_put_contents("test.txt",$e->getMessage().$e->getFile().$e->getLine());//写入文件,一般做正式环境测试 +// Log::log($e->getMessage()); + $this->error($e->getMessage(),['errcode'=>$e->getCode()]);; + } + $this->success('订单创建成功,缓存key被消耗', $res); + } + + + + + + /** + * @ApiTitle( 预约课时单取消接口) + * @ApiSummary(预约课时单取消接口(已完成的单无法取消)) + * @ApiRoute(/api/school/worker/hour_order/cancel) + * @ApiMethod(POST) + * @ApiParams(name = "order_no", type = "string",required=true,description = "订单号") + * @ApiReturn({ + * + *}) + */ + public function cancel(){ + $user_id = 0; + $user = $this->auth->getUser();//登录用户 + if($user)$user_id = $user['id']; + $order_no = $this->request->post('order_no/s', ''); //订单号 + try{ + //当前申请状态 + $res = $this->model->cancel($order_no,0,true,'user',$user_id,true); + }catch (\Throwable $e){ + $this->error($e->getMessage()); + } + $this->success('预约课时取消成功', $res); + } + + + + + + + + /** * @ApiTitle( 课时订单详情) * @ApiSummary(课时订单详情) @@ -125,5 +232,16 @@ class HourOrder extends Base $this->success('查询成功', $res); } + + + + + + + + + + + } diff --git a/application/common/model/school/classes/ClassesLib.php b/application/common/model/school/classes/ClassesLib.php index 9a2f438..db6eae7 100644 --- a/application/common/model/school/classes/ClassesLib.php +++ b/application/common/model/school/classes/ClassesLib.php @@ -4,6 +4,7 @@ namespace app\common\model\school\classes; use app\common\model\BaseModel; use app\common\model\dyqc\ManystoreShop; +use app\common\model\school\classes\lib\Spec; use app\common\model\User; use think\Model; use traits\model\SoftDelete; @@ -236,6 +237,11 @@ class ClassesLib extends BaseModel } + public function specs() + { + return $this->hasMany(ClassesSpec::class,'classes_lib_id'); + } + diff --git a/application/common/model/school/classes/hourorder/Order.php b/application/common/model/school/classes/hourorder/Order.php index 45c003f..ba21dbc 100644 --- a/application/common/model/school/classes/hourorder/Order.php +++ b/application/common/model/school/classes/hourorder/Order.php @@ -2,6 +2,7 @@ namespace app\common\model\school\classes\hourorder; +use app\admin\model\Admin; use app\common\model\school\classes\ClassesLib; use app\common\model\school\classes\ClassesSpec; use app\common\model\User; @@ -38,6 +39,9 @@ class Order extends BaseModel 'finish_time_text', 'cancel_time_text' ]; + + + protected $order_user_id; @@ -392,6 +396,7 @@ class Order extends BaseModel */ public function confirm($user_id, $classes_order_id,$order_no,$classes_lib_spec_id=0,$param=[], $is_compute = false) { + if ($order_no && !$is_compute) { //得到缓存 $data = self::getOrderCache($user_id, $order_no); @@ -418,6 +423,9 @@ class Order extends BaseModel + + + /**订单校验 * @param $user_id 用户id * @param $classes_order_id 课程订单id @@ -428,6 +436,24 @@ class Order extends BaseModel public function orderVaild($user_id,$classes_order_id,$order_no,$classes_lib_spec_id, $param,$check=false){ if(!$user_id||!$classes_order_id)throw new \Exception("缺少必要参数"); + + //代下单检测是否有代下单权限 + if(isset($param["help_user_id"]) && $param["help_user_id"]){ + if(!isset($param["help_type"]) || !$param["help_type"])throw new \Exception("请选择代下单类型!"); + + //课程是否存在并上架 + $classesOrder = \app\common\model\school\classes\order\Order::where("id",$classes_order_id)->find(); + if(!$classesOrder)throw new \Exception("订单不存在!"); + //代下单时将下单人修正成课程下单人,不影响后续判断 + $user_id = $classesOrder["user_id"]; + + //用户操作权限检测 + self::checkOptionAuth($classes_order_id,$param["help_user_id"],$param["help_type"]); + + + } + + //默认校验订单是否已创建 if($check){ //判断订单是否已创建 @@ -436,6 +462,8 @@ class Order extends BaseModel if(!$classes_lib_spec_id)throw new \Exception("请选择您要预约的课时信息!"); } //校验订单参数 + //更新最近次数后进行验证 + \app\common\model\school\classes\order\Order::statisticsAndUpdateClassesNumber($classes_order_id); //课程是否存在并上架 $classesOrder = \app\common\model\school\classes\order\Order::where("id",$classes_order_id) ->where("user_id",$user_id) @@ -446,6 +474,7 @@ class Order extends BaseModel $detail = $classesOrder->detail; if(!$detail)throw new \Exception("课程不存在!"); //是否还有次数 + if($detail['sub_num']<=0) throw new \Exception("该课程已无剩余课时!"); //不是可用状态 if($classesOrder['status']!="3") throw new \Exception("该课程单当前状态不可操作!"); @@ -454,17 +483,15 @@ class Order extends BaseModel //判断课时信息 if($classes_lib_spec_id){ - $classes_lib_spec_info = ClassesSpec::where('id',$classes_lib_spec_id) - ->where('classes_lib_id',$detail["classes_lib_id"]) - ->find(); - if(!$classes_lib_spec_info)throw new \Exception("课时信息不正确!"); - //课时已下架 - if($classes_lib_spec_info['status']!='1') throw new \Exception("该课时已下架,请选择其他课时!"); + self::checkLibSpec($user_id,$detail["classes_lib_id"],$classes_lib_spec_id); } + + + // $classes_lib_info = ClassesLib::where('id',$classesOrder["classes_lib_id"])->find(); // if(!$classes_lib_info || $classes_lib_info['status']!='1') throw new \Exception("该课程不存在或已下架!"); @@ -479,6 +506,17 @@ class Order extends BaseModel //计算订单所需返回数据接口 public static function getCost($user_id,$classes_order_id,$classes_lib_spec_id,$param=[],$other_params=[],$check = false){ + if(isset($param["help_user_id"]) && $param["help_user_id"]){ + + //课程是否存在并上架 + $classesOrder = \app\common\model\school\classes\order\Order::where("id",$classes_order_id)->find(); + if(!$classesOrder)throw new \Exception("订单不存在!"); + //代下单时将下单人修正成课程下单人,不影响后续判断 + $user_id = $classesOrder["user_id"]; + } + + + //校验订单参数 //课程是否存在并上架 $classesOrder = \app\common\model\school\classes\order\Order::where("id",$classes_order_id) @@ -521,7 +559,9 @@ class Order extends BaseModel $order_data["status"] = '-1'; $order_data["verification_user_id"] = 0; $order_data["reservation_time"] = time(); - $order_data["type"] = '1'; + $order_data["type"] = $param["type"] ?? '1'; + $order_data["help_user_id"] = $param["help_user_id"] ?? 0; + $order_data["help_type"] = $param["help_type"] ?? null; $order_data["reason"] = ''; $order_data["auth_status"] = '0'; @@ -551,9 +591,17 @@ class Order extends BaseModel } try { //1订单执行创建 - $this->createOrder($user_id,$orderInfo['classes_order_id'],$orderInfo['classes_lib_spec_id'],$order_no,$orderInfo['param'],$remark); + $order = $this->createOrder($user_id,$orderInfo['classes_order_id'],$orderInfo['classes_lib_spec_id'],$order_no,$orderInfo['param'],$remark); //5删除缓存 self::deleteOrderCache($user_id, $order_no); + //代下单直接执行审核成功的预约中状态 + if($order["type"] == '2'){ + //兼容代下单逻辑,修正下单人信息 +// $user_id =$order['user_id']; + //do something... + $this->examine($order_no,1,"",0,false,$order["help_type"],$order["help_user_id"],false); + + } if ($trans) { self::commitTrans(); } @@ -594,6 +642,24 @@ class Order extends BaseModel } + + /**得到可取消订单 + * @param $order_no + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getHaveCancelOrder($order_no){ +// $where = [self::STATUS_NOPAY,self::STATUS_PAYED]; + $order = self::where('order_no|id',$order_no)->where("status","in",['0','-1'])->find(); + if(!$order)throw new \Exception("只有待审核和已预约的订单可以取消!"); + + return $order; + } + + + //生成或更新课时 public static function buildLibSpec($order,$classes_lib_spec){ if(is_string($order)||is_numeric($order))$order = self::getHaveUpdateOrder($order); @@ -624,7 +690,11 @@ class Order extends BaseModel //组装订单数据 $order_data = $order_info['order_data']; $order_data["order_no"] = $order_no; - //课时数据 + + //兼容代下单逻辑,修正下单人信息 + $user_id =$order_data['user_id']; + + //课时数据 $classes_lib_spec_info = $order_info['classes_lib_spec_info']; @@ -635,10 +705,15 @@ class Order extends BaseModel $order = self::buildLibSpec($res1,$classes_lib_spec_id); //扣减课程数 - + $res1 = \app\common\model\school\classes\order\Order::statisticsAndUpdateClassesNumber($classes_order_id); //记录订单日志 - OrderLog::log($res1['id'],"课时预约下单成功,等待机构老师审核",'user',$user_id); + if($order["type"] == "1"){ + OrderLog::log($res1['id'],"课时预约下单成功,等待机构老师审核",'user',$user_id); + }else{ + OrderLog::log($res1['id'],"课时预约机构老师代下单成功",'user',$order["help_user_id"]); + } + //7事件 $data = ['order' => $res1]; \think\Hook::listen('classeshour_order_create_after', $data); @@ -647,6 +722,283 @@ class Order extends BaseModel + /**更新订单取消状态 + * @param $order + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function updateCancel($order){ + if(is_string($order))$order = self::getHaveCancelOrder($order); + $order->status = "-3";//refund_status + $order->cancel_time = time(); + $order->save(); + return $order; + } + + + + /**订单取消 + * @param $order_no + * @param int $user_id + * @param bool $check + * @param bool $trans + * @return bool + * @throws \Exception + */ + public function cancel($order_no,$user_id=0,$check=false,$oper_type='user',$oper_id=0,$trans=false){ + //得到可取消订单 + $order = self::getHaveCancelOrder($order_no); + if($check){ + //用户操作权限检测 + self::checkOptionAuth($order['classes_order_id'],$user_id ?: $oper_id,$oper_type); + } + + //判断逻辑 + if($trans){ + self::beginTrans(); + } + $res = true; + try{ + //事务逻辑 + //更新订单取消状态 + $order = Order::updateCancel($order); + //插入订单取消日志 + if(!$user_id ||$order["user_id"] !=$user_id ){ + OrderLog::log($order['id'],"[员工代操作]课时预约单取消成功,课时已原路退回",$oper_type ?: 'user', $oper_id ?: $order['user_id']); + }else{ + OrderLog::log($order['id'],"课时预约单取消成功,课时已原路退回",$oper_type ?: 'user', $oper_id ?: $order['user_id']); + } + + //调用订单取消事件 + $data = ['order' => $order]; + \think\Hook::listen('classeshour_order_cancel_after', $data); + //执行课时数更新 + $res1 = \app\common\model\school\classes\order\Order::statisticsAndUpdateClassesNumber($order['classes_order_id']); + + + if($trans){ + self::commitTrans(); + } + }catch (\Exception $e){ + if($trans){ + self::rollbackTrans(); + } + throw new \Exception($e->getMessage()); + } + return $res; + } + + + /** 课时信息选择合法检测 + * @param $user_id 下单用户 + * @param $classes_lib_id 课程id + * @param $classes_lib_spec_id 课时规格id + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function checkLibSpec($user_id,$classes_lib_id,$classes_lib_spec_id){ + $classes_lib_spec_info = ClassesSpec::where('id',$classes_lib_spec_id) + ->where('classes_lib_id',$classes_lib_id) + ->find(); + if(!$classes_lib_spec_info)throw new \Exception("课时信息不正确!"); + //课时已下架 + if($classes_lib_spec_info['status']!='1') throw new \Exception("该课时已下架,请选择其他课时!"); + + //存在同规格正在进行中的课时预约 + $order_info = self::where('classes_lib_spec_id',$classes_lib_spec_id) + ->where('status','in',['-1',"0"]) + ->where('user_id',$user_id) + ->find(); + if($order_info) throw new \Exception("该课时已预约,请勿重复预约!"); + } + + + + + /** 课时订单操作权限检测 + * @param $order 订单 + * @param $oper_id 操作人id + * @param $oper_type 操作人类型:user-用户或员工,admin-管理员 + * @return void + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function checkOptionAuth($classes_order_id,$oper_id,$oper_type){ + + //课程是否存在并上架 + $classesOrder = \app\common\model\school\classes\order\Order::where("id",$classes_order_id)->find(); + if(!$classesOrder)throw new \Exception("订单不存在!"); + + switch ($oper_type) { + case 'user': + //自己或操作员 + if($oper_id != $classesOrder["user_id"]){ + //说明是操作员 + $help_user_info = User::where('id',$oper_id)->find(); + if(!$help_user_info) throw new \Exception("代下单员工不存在!"); + $classes_lib_ids = (new ClassesLib)->getClassesAuthIds($oper_id); + //判断当前订单课程是否在此课程授权范围内 + if(!in_array($classesOrder["classes_lib_id"],$classes_lib_ids)) throw new \Exception("该课程不在您的授权范围内,无法代操作!"); + } + break; + case 'admin': + $admin_info = Admin::where('id',$oper_id)->find(); + if(!$admin_info) throw new \Exception("代下单管理员不存在!"); + break; + default: + throw new \Exception("请选择正确的代下单类型!"); + } + + + } + + + + + + /**得到可取消订单 + * @param $order_no + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getHaveExamineOrder($order_no){ +// $where = [self::STATUS_NOPAY,self::STATUS_PAYED]; + $order = self::where('order_no|id',$order_no)->where("status","in",['-1'])->find(); + if(!$order)throw new \Exception("不是待审核的订单!"); + + return $order; + } + + + + + /**更新订单取消状态 + * @param $order + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function updateExamineSuccess($order,$oper_id = 0,$oper_type='user'){ + if(is_string($order))$order = self::getHaveExamineOrder($order); + $order->status = "0";//refund_status + $order->auth_status = "1";//refund_status + $order->auth_time = time(); + $order->auth_user_id = $oper_id; + $order->auth_type = $oper_type; + $order->save(); + return $order; + } + + + + + /**更新订单取消状态 + * @param $order + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function updateExamineFail($order,$reason,$oper_id = 0,$oper_type='user'){ + if(is_string($order))$order = self::getHaveExamineOrder($order); + $order->auth_status = "2"; + $order->auth_time = time(); + $order->reason = $reason; + $order->auth_user_id = $oper_id; + $order->auth_type = $oper_type; + $order->save(); + return $order; + } + + + + + /**课时订单审核 + * @param $order_no + * @param int $user_id + * @param bool $check + * @param bool $trans + * @return bool + * @throws \Exception + */ + public function examine($order_no,$auth_status,$reason="",$user_id=0,$check=false,$oper_type='user',$oper_id=0,$trans=false){ + //得到可取消订单 + $order = self::getHaveExamineOrder($order_no); + if($check){ + //用户操作权限检测 + self::checkOptionAuth($order['classes_order_id'],$user_id ?: $oper_id,$oper_type); + } + //审核状态字段检测 + $auth_status_arr = [1,2];//0=待审核,1=审核通过,2=审核失败 + if( !in_array($auth_status,$auth_status_arr)) throw new \Exception("审核状态不正确!"); + //审核失败需要传理由 + if($auth_status==2 && empty($reason)) throw new \Exception("审核失败需要填写理由!"); + + + //判断逻辑 + if($trans){ + self::beginTrans(); + } + $res = true; + try{ + //事务逻辑 + + //插入订单取消日志 + if(!$user_id ||$order["user_id"] !=$user_id ){ + $pron = "[员工操作]"; + }else{ + $pron = ""; + } + //审核成功逻辑 + if($auth_status == 1){ + //更新订单状态 + $order = Order::updateExamineSuccess($order,$user_id ?: $oper_id,$oper_type); + + OrderLog::log($order['id'],$pron."课时预约单审核成功,预约成功等待核销!",$oper_type ?: 'user', $oper_id ?: $order['user_id']); + //调用订单事件 + $data = ['order' => $order]; + \think\Hook::listen('classeshour_order_auth_success_after', $data); + + }else{ + + //更新订单状态 + $order = Order::updateExamineFail($order,$reason,$user_id ?: $oper_id,$oper_type); + + //审核失败逻辑 + OrderLog::log($order['id'],$pron."课时预约单审核不通过,原因;{$reason},该课时单将取消以便重新下单",$oper_type ?: 'user', $oper_id ?: $order['user_id']); + //调用订单事件 + $data = ['order' => $order]; + \think\Hook::listen('classeshour_order_auth_fail_after', $data); + + //执行订单取消逻辑 + $this->cancel($order_no,$user_id,$check,$oper_type,$oper_id); + } + + //执行课时数更新 +// $res1 = \app\common\model\school\classes\order\Order::statisticsAndUpdateClassesNumber($order['classes_order_id']); + + if($trans){ + self::commitTrans(); + } + }catch (\Exception $e){ + if($trans){ + self::rollbackTrans(); + } + throw new \Exception($e->getMessage()); + } + return $res; + } + + + } diff --git a/application/common/model/school/classes/hourorder/OrderLog.php b/application/common/model/school/classes/hourorder/OrderLog.php index 3d2d67b..d3755e6 100644 --- a/application/common/model/school/classes/hourorder/OrderLog.php +++ b/application/common/model/school/classes/hourorder/OrderLog.php @@ -79,6 +79,10 @@ class OrderLog extends BaseModel unset($log_data['id']); unset($log_data['createtime']); if($mark)$log_data['log_text'] = $mark; + + + $log_data = array_merge($log_data,$data); + $log = (new self); $log->allowField(true)->save($log_data); if($trans){ diff --git a/application/common/model/school/classes/order/Order.php b/application/common/model/school/classes/order/Order.php index a01a71c..8f11145 100644 --- a/application/common/model/school/classes/order/Order.php +++ b/application/common/model/school/classes/order/Order.php @@ -405,6 +405,11 @@ class Order extends BaseModel //7事件 $data = ['order' => $res1]; \think\Hook::listen('classes_order_create_after', $data); + + //更新订单数据 + self::statisticsAndUpdateClassesNumber($res1); + + return $res1; } @@ -495,6 +500,20 @@ class Order extends BaseModel return $order; } + /**得到订单 + * @param $order_no + * @return array|false|\PDOStatement|string|Model + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\exception\DbException + */ + public static function getOrder($order_no){ + $order = self::where('order_no|id|pay_no|code',$order_no)->find(); + if(!$order)throw new \Exception("订单不存在"); + return $order; + } + + /** * 检测支付开关(待扩展) @@ -649,6 +668,8 @@ class Order extends BaseModel //调用支付成功事件 $data = ['order' => $order]; \think\Hook::listen('classes_order_payed_after', $data); + //更新订单数据 + self::statisticsAndUpdateClassesNumber($order); return true; } @@ -846,6 +867,50 @@ class Order extends BaseModel + //统计并更新课时数等相关统计数据 + public static function statisticsAndUpdateClassesNumber($order){ + if(is_string($order)||is_numeric($order))$order = self::getOrder($order); + $detail = $order->detail; + if(!$detail)throw new \think\Exception("订单信息缺失!"); + //得到当前订单的所有未取消课时订单 + $hourorderOrderCount = \app\common\model\school\classes\hourorder\Order::where("status","<>",'-3') + ->where("classes_order_id","=",$order["id"]) + ->count(); + //更新已用课程数和剩余课程数 + $detail->use_num = $hourorderOrderCount; + $detail->sub_num = $detail->classes_num - $detail->use_num; + //更新已用课程金额和剩余课程金额 + $detail->used_price = bcmul($detail->unit_price , $detail->use_num,2); + $detail->save(); + //更新订单应退金额 + $order->total_refundprice = bcsub($order->totalprice,$detail->used_price,2); + $order->save(); + + //课程下单时已核销人数更新 + $lib = $order->lib; + if($lib){ + $lib->sale = self::where("classes_lib_id",$lib["id"])->where("status","<>","-3")->count(); + $lib->save(); + //遍历课程课时规格,更新课时统计数据 + $specs = $lib->specs; + if($specs){ + foreach ($specs as $spec){ + + // '已核销人数', + $spec->verification_num = \app\common\model\school\classes\hourorder\Order::where("classes_lib_spec_id",$spec["id"])->where("status","=","3")->count(); + //已报名人数 + $spec->sign_num = \app\common\model\school\classes\hourorder\Order::where("classes_lib_spec_id",$spec["id"])->where("status","in",["-1","0"])->count(); + + $spec->save(); + } + + } + } + + + return $order; + } + diff --git a/application/common/model/school/classes/order/OrderLog.php b/application/common/model/school/classes/order/OrderLog.php index 2f20ab8..fed2494 100644 --- a/application/common/model/school/classes/order/OrderLog.php +++ b/application/common/model/school/classes/order/OrderLog.php @@ -124,6 +124,9 @@ class OrderLog extends BaseModel unset($log_data['id']); unset($log_data['createtime']); if($mark)$log_data['log_text'] = $mark; + + $log_data = array_merge($log_data,$data); + $log = (new self); $log->allowField(true)->save($log_data); if($trans){