<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 分享赚钱
 */
namespace app\allwin\model;
use think\Model;
use app\allwin\model\AllwinUnmarket;

class AllwinUnmarketOrder extends Model{

    protected $pk = 'id';
    
   //和主用户表绑定关系
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    //获得主表
    public function market(){
        return $this->hasOne('AllwinUnmarket','id','share_id');
    }

    //创建订单
    public static function createOrder(array $param){
        $info = AllwinUnmarket::where(['member_miniapp_id' => $param['member_miniapp_id'],'id' => $param['id']])->find();
        if(empty($info)){
            return false;
        }
        $data = [
            'uid'               => $param['uid'],
            'share_uid'         => $param['share_uid'],
            'order_sn'          => $param['order_no'],
            'share_id'          => $info['id'],
            'telname'           => $param['telname'],
            'telphone'          => $param['telphone'],
            'price'             => $info['price'],
            'member_miniapp_id' => $info['member_miniapp_id'],
            'state'             => 0,
            'order_time'        => time(),
        ];
        $rel = self::insert($data);
        return $rel ? $info : false;
    }
}