<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/30
 * Time: 15:23
 */

namespace app\index\controller;


use app\index\controller\WechatPay;
use app\common\controller\HomeBase;
use app\index\model\Firmstores;
use app\index\model\Minestore;
use app\index\model\Price;
use app\index\model\Province;
use app\index\model\Viporder;

class Vip extends HomeBase
{
    /**
     * vip购买页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $user_id = $this->getUserId();
        //个人店铺
        $minestoreModel = new Minestore();
        $minestore = $minestoreModel->findData(['user_id'=>$user_id]);
        //企业店铺
        $firmstoresModel = new Firmstores();
        $firmstores = $firmstoresModel->findData(['user_id'=>$user_id]);
        //判断是否提交申请
        if(empty($minestore) && empty($firmstores)){
            $this->redirect('index/enter/index');
        }
        //省份
        $provinceModel = new Province();
        $province = $provinceModel->selectData([]);
        //价格
        $priceModel = new Price();
        $price = $priceModel->findData(['id'=>1]);
        $this->assign('province',$province);
        $this->assign('price',$price);
        $this->assign('title',"入驻缴费");
        return $this->fetch();
    }

    /**
     * 创建订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create_order(){
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $validate = new \think\Validate([
            'pay_type' => 'require',
            'vip_type' => 'require',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        //价格
        $priceModel = new Price();
        $price = $priceModel->findData(['id'=>1]);
        if($param['vip_type'] == 1){
            if(empty($param['province_ids'])){
                $this->error('请至少选择一个城市');
            }
            $number = explode(',',$param['province_ids']);
            $money = $price['vipprice'] * count($number);
        }else{
            $money = $price['svipprice'];
        }
        //个人店铺
        $minestoreModel = new Minestore();
        $minestore = $minestoreModel->findData(['user_id'=>$user_id]);
        //企业店铺
        $firmstoresModel = new Firmstores();
        $firmstores = $firmstoresModel->findData(['user_id'=>$user_id]);
        //判断是否提交申请
        if(empty($minestore) && empty($firmstores)){
            $this->error('请先提交入驻申请','index/enter/index');
        }
        $type = !empty($minestore) ? 1 : (!empty($firmstores) ? 2 : 0);
        $arr['pay_type'] = $param['pay_type'];
        $arr['user_id'] = $user_id;
        $arr['type'] = $type;
        $arr['vip_type'] = $param['vip_type'];
        $arr['province_ids'] = !empty($param['province_ids']) ? $param['province_ids'] : null;
        $arr['num'] = get_order_sn();
        $arr['money'] = $money;
        $arr['status'] = '1';
        $arr['audit'] = '1';
        $arr['createtime'] = time();
        $viporderModel = new Viporder();
        $result = $viporderModel->insertData($arr);
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS','',['viporder_id'=>$result,'url'=>url("index/vip/pay",array('viporder_id'=>$result),false,true)]);
    }

    /**
     * 支付接口
     */
    public function pay(){
        $viporder_id = $this->request->param('viporder_id',0,'intval');
        if(empty($viporder_id)){
            $this->error('缺少必要参数');
        }
        $viporderModel = new Viporder();
        $data = $viporderModel->findData(['id'=>$viporder_id]);
        if(empty($data)){
            $this->error('未查询到该订单');
        }
        if($data['status'] != '1'){
            $this->error('订单状态不合法');
        }
        if($data['pay_type'] == '1'){
            //微信支付
            $pay = new WechatPay();
            $code_url = $pay->index($data);
            if(empty($code_url)){
                $this->error('生成二维码失败','','','');
            }
            \PHPQRCode\QRcode::png($code_url);
        }else if($data['pay_type'] == '2'){
            //支付宝支付
            $params['subject'] = "购买商品";
            $params['out_trade_no'] = $data['num'];
            $params['total_amount'] = 0.01;//$data['money'];
            $result = \alipay\Pagepay::pay($params);
            echo $result;
        }else{
            $this->error('未知的支付类型');
        }
    }

    /**
     * 检测订单支付状态
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_pay(){
        $user_id = $this->getUserId();
        $order_id = $this->request->param('order_id',0,'intval');
        if(empty($order_id)){
            $this->error('缺少必要参数');
        }
        $viporderModel = new Viporder();
        $data = $viporderModel->findData(['id'=>$order_id]);
        if(empty($data)){
            $this->error('查询为空');
        }
        if($data['status'] != '1'){
            $this->success('SUCCESS',url("index/index/index"));
        }
        $this->error('尚未支付');
    }
}