<?php

namespace app\ros\controller;

use app\ros\model\Product as ProductModel;
use app\ros\model\Order as OrderModel;
use fastphp\base\Controller;
use extend\wechatpay\Wxpay;
use extend\alipay\Alipay;
use app\user\model\User as UserModel;
use app\user\model\UserScore as UserScore;

class Order extends Controller
{

    /**
     * @description: 获取订单列表 前台用户
     * @param {*}
     * @return {*}
     */    
    public function getList() {
        $order_model = new OrderModel();
        $userid = $this->userinfo['UserId'];
        $list = $order_model->getListPage(['user_id' => $userid,'status' => 1]);

        $info = [
            'code'=>1,
            'data' => $list
        ];
        echo  json_encode($info);
        die;
    }

    /**
     * @description: 获取订单列表 所有用户
     * @param {*}
     * @return {*}
     */    
    public function getAllList() {
        $page = 0;
        if (isset($_GET['page'])) {
            $page = ($_GET['page'] - 1) * 50;
        }
        $order_model = new OrderModel();
        $list = $order_model->getListPage(['status' => 1],'*','id desc',"$page,50");

        $info = [
            'Code'=>10000,
            'Data' => $list,
            'Count' => (int)$order_model->getCount(['status' => 1])
        ];
        echo  json_encode($info);
        die;
    }

    /**
     * @description: 修改是否发货
     * @param {*}
     * @return {*}
     */    
    public function setIsend() {
        $data = json_decode(file_get_contents("php://input"),true);
        $order_model = new OrderModel();
        $order_model->updateOne(['id' => $data['r']['id']],['is_send' => $data['r']['is_send']]);
        $info = [
            'Code'=>10000,
        ];
        echo  json_encode($info);
        die;
    }

    /**
     * @description: 支付宝付款
     * @param {*}
     * @return {*}
     */    
    public function alipay()
    {
        $data = json_decode(file_get_contents("php://input"),true)['data'];

        if (empty($data['username']) || empty($data['phone']) || empty($data['address'])) {
            echo json_encode(['code' => -1]);
            die;
        }

        $product_model = new ProductModel();

        $product_info = $product_model->getOne(['id' => $data['product_id']]);

        $money = abs($product_info['price']);
        
        $order_no = "ruanluyou".time().rand(100,999);

        $order_model = new OrderModel();

        $order_data = [
            'user_id' => $this->userinfo['UserId'],
            'username' => $this->userinfo['LoginName'],
            'product_id' => $product_info['id'],
            'product_name' => $product_info['name'],
            'order_price' => $product_info['price'],
            'real_price' => $money,
            'order_no' =>$order_no,
            'user' =>$data['username'],
            'phone' =>$data['phone'],
            'address' =>$data['address'],
        ];
        $order_model->add($order_data);

        $param = [
            'out_trade_no' => $order_no,
            'total_amount' => $money,
            'subject' => '购买软路由'.$product_info['name'].'-'.$this->userinfo['LoginName'],
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
            'timeout_express' => '30m',
        ];


        $pay_html = Alipay::pay2($param);
        
        $info = [
            'code'=>2,
            'data' => $pay_html
        ];
        echo  json_encode($info);
        die;
    }

    /**
     * @description: 微信支付
     * @param {*}
     * @return {*}
     */    
    public function wxpay() {
        $data = json_decode(file_get_contents("php://input"),true)['data'];

        if (empty($data['username']) || empty($data['phone']) || empty($data['address'])) {
            echo json_encode(['code' => -1]);
            die;
        }

        $product_model = new ProductModel();

        $product_info = $product_model->getOne(['id' => $data['product_id']]);

        $money = abs($product_info['price']);
        
        $order_no = "ruanluyou".time().rand(100,999);;

        $order_model = new OrderModel();

        $order_data = [
            'user_id' => $this->userinfo['UserId'],
            'username' => $this->userinfo['LoginName'],
            'product_id' => $product_info['id'],
            'product_name' => $product_info['name'],
            'order_price' => $product_info['price'],
            'real_price' => $money,
            'order_no' =>$order_no,
            'user' =>$data['username'],
            'phone' =>$data['phone'],
            'address' =>$data['address'],
        ];
        $order_model->add($order_data);

        //微信支付
        $wxpay = new Wxpay();

        $param = [
            'username' => $this->userinfo['LoginName'],
            'money' => $money*100,
            'order_no' => $order_no,
        ];
        $url = $wxpay->pay($param);

        $info = [
            'code'=>1,
            'data' => $url
        ];
        echo  json_encode($info);
        die;
    }

    /**
     * @description: 余额支付
     * @param {*}
     * @return {*}
     */    
    public function balancepay(){
        $data = json_decode(file_get_contents("php://input"),true)['data'];

        if (empty($data['username']) || empty($data['phone']) || empty($data['address'])) {
            echo json_encode(['code' => -1]);
            die;
        }

        $product_model = new ProductModel();

        $product_info = $product_model->getOne(['id' => $data['product_id']]);

        $money = abs($product_info['price']);

        $user_model = new UserModel;

        $user_info = $user_model->getOne(['Id' => $this->userinfo['UserId']]);

        $old_balance = $rest_money = $user_info['RestAmount'];

        if ($money > $rest_money) {
            $info = [
                'code'=>-1,
                'msg' => '余额不足'
            ];
            echo  json_encode($info);
            die;
        } else {
            $rest_money -= $money;
            $rest_data = [
                'RestAmount' => $rest_money
            ];
            if ($user_model->updateOne(['Id'=>$this->userinfo['UserId'],'RestAmount'=>$old_balance],$rest_data)) {
                $score_model = new UserScore;
                $score_data = [
                    'TenantId'=>0,
                    'UserId' => $this->userinfo['UserId'],
                    'ScoreType' => '6',
                    'ScoreTypeName' => '购买软路由',
                    'ScoreValue' => $money,
                    'UserName' => $user_info['Phone'],
                    'OperateUserName' => $user_info['LoginCode'],
                    'RestAmount1' => $old_balance,
                    'RestAmount2' => $rest_money,
                    'DeleteTag'=>0,
                    'CreateTime' => date('Y-m-d H:i:s'),
                    'UpdateTime' => date('Y-m-d H:i:s'),
                ];
                $score_model->add($score_data);
            } else {
                $info = [
                    'code'=>-1,
                    'msg' => '支付失败，请联系客服！'
                ];
                echo  json_encode($info);
                die;
            }

            $order_no = "ruanluyou".time().rand(100,999);

            $order_model = new OrderModel();
            $order_data = [
                'user_id' => $this->userinfo['UserId'],
                'username' => $this->userinfo['LoginName'],
                'product_id' => $product_info['id'],
                'product_name' => $product_info['name'],
                'order_price' => $product_info['price'],
                'real_price' => $money,
                'order_no' =>$order_no,
                'user' =>$data['username'],
                'phone' =>$data['phone'],
                'address' =>$data['address'],
                'status' => 1
            ];
            $order_model->add($order_data);
        }
        
        $info = [
            'code'=>3,
        ];
        echo  json_encode($info);
        die;
        


    }
}