SecretOrder.php 1.7 KB
<?php
namespace app\mobile\model;

use think\Model;
use think\Db;

class SecretOrder extends Model
{
	// 表名
    protected $name = 'mobile_secret_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 密卷信息
    public function secret(){
    	return $this->belongsTo('Secret');
    }

    /**
     * 新增订单
     */
    public function add($user, $order, $pay_type)
    {
        Db::startTrans();
        $save_data = [
        	'user_id' => $user['id'],
            'secret_id' => $order['secret_info']['id'],
            'order_sn' => get_order_sn(),
            'pay_price' => $order['pay_price'],
            'pay_type' => $pay_type,
            'secret_price' => $order['secret_price']
        ];
        // 记录订单信息
        if($user['group_id'] == 1){
        	$this->save(array_merge([
                'company_id' => Company::where('user_id',$user['id'])->value('id'),
                'people_num' => $order['spec_info']['people_num'],
                'secret_spec_id' => $order['spec_info']['id'],
                'is_top' => $order['spec_info']['is_top'],
            ],$save_data));
        }else{
        	$this->save(array_merge([
                'score' => $order['score_info']['use_score'],
                'score_price' => $order['score_info']['score_price'],
            ],$save_data));
	        // 用掉积分
	        if($order['score_info']['use_score'] > 0){
	            \app\common\model\User::score(-$order['score_info']['use_score'],$user['id'],'密卷下单-使用积分');
	        }
        }
        Db::commit();
        return true;
    }
}