PackageOrder.php 1.7 KB
<?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;
    }
}