<?php namespace app\mobile\model; use think\Model; class Course extends Model { // 表名 protected $name = 'mobile_course'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = 'int'; // 定义时间戳字段名 protected $createTime = 'createtime'; protected $updateTime = 'updatetime'; // 错误提示 public $error = ''; // 状态码 public $code = 0; // 格式化封面图 public function getCoverAttr($value){ return !empty($value) ? cdnurl($value,true) : ''; } // 格式化老师头像 public function getTeacherAvatarAttr($value){ return !empty($value) ? cdnurl($value,true) : ''; } /** * 下单预览 */ public function payView($user,$data) { if(empty($data['course_id'])){ $this->setError('缺少必要参数'); return false; } $info = $this->get($data['course_id']); if(empty($info)){ $this->setError('课程信息不存在'); return false; } if($user['group_id'] == 1){ $company = Company::get(['user_id'=>$user['id']]); if(empty($data['spec_id'])){ $this->setError('请选择规格',2); return false; } $spec = CourseSpec::where('id',$data['spec_id']) ->where('course_id',$data['course_id']) ->find(); if(empty($spec)){ $this->setError('规格信息不存在'); return false; } // 企业下员工数量(包含管理员) $people_num = CompanyUser::where('company_id',$company['id']) ->where('status','1') ->count(); if($spec['is_top'] == '0' && $spec['people_num'] < $people_num){ $this->setError('该套餐满足不了您公司'); return false; } // 企业已购该套餐 $order = CourseOrder::where('company_id',$company['id']) ->where('course_id',$data['course_id']) ->where('pay_status','1') ->order('people_num desc') ->find(); $pay_price = !empty($order) ? $spec['current_price']-$order['course_price'] : $spec['current_price']; }else{ // 积分 $score_info = ['score'=>$user['score'],'score_price'=>0,'use_score'=>0]; if(!empty($data['score_switch'])){ if($info['current_price'] >= $user['score']){ $score_info['score_price'] = $user['score']; $score_info['use_score'] = $user['score']; }else{ $score_info['score_price'] = $info['current_price']; $score_info['use_score'] = ceil($info['current_price']); } } $pay_price = bcsub($info['current_price'], $score_info['score_price'], 2); } return [ 'course_info' => $info, 'spec_info' => $user['group_id'] == 1 ? $spec : [], 'course_price' => $info['current_price'], 'score_info' => $user['group_id'] == 1 ? [] : $score_info, 'pay_price' => $pay_price, ]; } /** * 设置错误信息 * @param $error */ private function setError($error,$code=0) { empty($this->error) && $this->error = $error; $this->code = $code; } /** * 是否存在错误 * @return bool */ private function hasError() { return !empty($this->error); } /** * 获取错误信息 * @return string */ public function getError() { return $this->error; } /** * 获取状态码 * @return string */ public function getCode() { return $this->code; } }