Course.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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 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;
}
}