<?php
namespace app\models\store;
use app\models\user\User;
use app\models\user\UserBill;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Exception;

class Order extends BaseModel
{
    use ModelTrait;
    private static $appid ="jnz_formal_0001";
    private static $appsecret = "mxtw58mxbexwjk40ghin";
    private static $url = "https://new.juniuzu.com";
    public  function setSpecAttr($value)
    {
        return json_encode($value);
    }
    public  function getSpecAttr($value)
    {
        return json_decode($value,true);
    }
    /**
     * 生成订单唯一id
     * @param $uid 用户uid
     * @return string
     */
    public static function getNewOrderId()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
        $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
        while (self::be(['order_id' => $orderId])) $orderId = 'pk' . $msectime . mt_rand(10000, 99999);
        return $orderId;
    }

    /**
     * 创建订单
     * @param $data
     * @return bool
     */
    public static function order_create($data)
    {

        try {
            $data['order_id'] = self::getNewOrderId();
            $data['pay_time'] = time();
            $data['pay_type'] = 'integral';
            $data['paid'] = 1;
            $rs = self::create($data);
            if ($rs) {
                User::where('uid', $data['uid'])->dec('integral', $data['pay_price'])->update();
                UserBill::expend('积分兑换', $data['uid'], 'integral', 'pay', $data['pay_price'], $rs['id'], User::where('uid', $data['uid'])->value('integral'), "兑换商品，支付" . $data['pay_price'] . "积分!");
            }
            return $rs;
        }catch (Exception $e)
        {
            return  self::setErrorInfo('创建订单出错！'.$e->getMessage());
        }

    }

    /**
     * 订单列表
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function lst($where)
    {
        $model = new self;
        if(isset($where['uid']) && $where['uid'] !=0)  $model = $model->where('uid',$where['uid']);
        if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"pay_time");
        if(isset($where['paid']) && $where['paid'] >-1)  $model = $model->where('paid',$where['paid']);
        if(isset($where['status']) && $where['status'] >-4)  $model = $model->where('status',$where['status']);
        if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
        $model = $model->order('id desc');
        $count = $model->value('count(id)')?:0;
        $data = $model->page($where['page'],$where['limit'])->select()->toArray();
        return compact('count','data');
    }

    /**
     * 获取系统订单列表
     * @param $where
     * @return array
     */
    public static function sys_lst($where)
    {
        $model = new self;
        if(isset($where['data']) && $where['data'] !='') $model = $model->getModelTime($where,$model,"add_time");
        if(isset($where['paid']) && $where['paid'] >-1)  $model = $model->where('paid',$where['paid']);
        if(isset($where['status']) && $where['status'] >-4)  $model = $model->where('status',$where['status']);
        if(isset($where['key']) && $where['key'] !='') $model = $model->where('order_id','like',"%".$where['key']."%");
        $model = $model->order('id desc');
        return self::page($model, function ($v){

        }, $where);
    }
    public static function sign($param)
    {
        $param['appid'] = self::$appid;
        $param['appsecret'] = self::$appsecret;
        ksort($param);
        $string  = self::toUrlParams($param);
        $string = md5( $string );
        //签名步骤四：所有字符转为大写
        $param['sign']  = strtoupper( $string );
        unset($param['appsecret']);
        return $param;
    }

    /**
     * @param $data
     *
     * @return string
     */
    protected static function toUrlParams( $data ) {
        $str = '';
        foreach( $data as $key => $value ) {
            if( $key != "sign" && !is_array( $value ) ) {
                $str .= '&' . $key . '=' . rawurldecode( $value );
            }
        }
        $str = ltrim( $str, '&' );

        return $str;
    }

}