PackageOrder.php
1.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
<?php
namespace app\mobile\model;
use think\Model;
use think\Db;
class PackageOrder extends Model
{
// 表名
protected $name = 'mobile_package_order';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 订单详情
public function package(){
return $this->hasMany('PackageOrderInfo','package_order_id');
}
/**
* 新增订单
*/
public function add($user, $pay_type)
{
$company_id = Company::where('user_id',$user['id'])->value('id');
$cart_list = PackageCart::where('company_id',$company_id)->select();
Db::startTrans();
// 记录订单信息
$this->save([
'user_id' => $user['id'],
'company_id' => $company_id,
'order_sn' => get_order_sn(),
'pay_price' => array_sum(array_column($cart_list, 'pay_price')),
'pay_type' => $pay_type,
]);
// 记录套餐订单详情
$package_data = [];
foreach ($cart_list as $v) {
$package_data[] = [
'company_id' => $company_id,
'user_id' => $user['id'],
'package_id' => $v['package_id'],
'package_spec_id' => $v['package_spec_id'],
'pay_price' => $v['pay_price'],
'current_price' => $v['current_price'],
'original_price' => $v['original_price'],
'people_num' => $v['people_num'],
'is_top' => $v['is_top'],
];
}
$this->package()->saveAll($package_data);
Db::commit();
return true;
}
}