<?php

namespace app\common\model;

use liuniu\BaseModel;
use think\Exception;
use think\Model;


class OrderLevel extends BaseModel
{





    // 表名
    protected $name = 'order_level';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;


    public static function order_create($data)
    {
        self::startTrans();
        try {
            $rs = self::create($data);
            self::commit();
            return $rs;
        }catch (Exception $e)
        {
            return self::setErrorInfo($e->getMessage(),true);
        }
    }
    public static function lst($where)
    {
        $model = new self;
        if(isset($where['cid']) && $where['cid']>0) $model->where('cid',$where['cid']);
        if(isset($where['user_id']) && $where['user_id']>0) $model->where('user_id',$where['user_id']);
        if(isset($where['level_id']) && $where['level_id']>0) $model->where('level_id',$where['level_id']);
        if(isset($where['paid']) && $where['paid']>-1) $model->where('paid',$where['paid']);
        $data = $model->order('id desc')->page($where['page'],$where['limit'])->select();
        return $data;
    }
    public static function paySuccess($cid,$order_id)
    {
        $info = self::where('order_id', $order_id)->where('cid',$cid)->find();
        $res = self::where('order_id', $order_id)->where('cid',$cid)->update(['paid' => 1, 'paytime' => time()]);//订单改为支付
        User::setendtime($info['user_id'],$info['level_id']);
        return false !== $res;
    }
    public static function getNewOrderId()
    {
        do {
            list($msec, $sec) = explode(' ', microtime());
            $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
            $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
        } while (self::where(['order_id' => $orderId])->find());
        return $orderId;
    }




}
