<?php
namespace app\admin\server\payment;
use app\admin\model\PackageOrder;
use app\admin\model\MedicalCareOrder;
use app\admin\model\GoodsOrder;
use app\admin\model\LifeCinemaOrder;
use app\admin\model\LifeBookOrder;
use app\admin\model\LifeHealthyOrder;
use app\admin\model\LifePackageOrder;
use app\admin\model\LifeFarmOrder;
use app\admin\model\RaiseOldOrder;
use app\admin\model\HealthyOrder;
use app\admin\model\User;
use app\admin\model\CardOrder;
use app\admin\model\HealthyAdviceOrder;
use app\admin\model\TravelOrder;
use app\admin\model\CourseOrder;
use app\admin\model\RaiseOldPackageOrder;
use app\admin\server\index\CardServer;
use app\admin\server\sms\VerificationCodeServer;

class UnionOrder
{
    

    public static function getOrderInfo($order_dno)
    {
        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    $info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('会员权益订单不存在');
                    }
                    break;

                case "YXYL"://悦享医疗

                    $info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('悦享医疗订单不存在');
                    }
                    $info['order_price'] = $info['order_total_price'];
                    break;

                case "JKCS"://健康超市

                    $info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('健康超市订单不存在');
                    }
                    break;

                case "WYYY"://万悦影院

                    $info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('万悦影院订单不存在');
                    }
                    break;

                case "WYSY"://万悦书院

                    $info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('万悦书院订单不存在');
                    }
                    break;

                case "JKMS"://健康美食

                    $info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('健康美食订单不存在');
                    }
                    break;

                case "YHTC"://悦活套餐

                    $info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('悦活套餐订单不存在');
                    }
                    break;

                case "LSSC"://绿色蔬菜

                    $info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('绿色蔬菜订单不存在');
                    }
                    break;

                case "YLFW"://悦享养老

                    $info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('悦享养老订单不存在');
                    }
                    break;

                case "YXJK"://悦享健康

                    $info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('悦享健康订单不存在');
                    }
                    break;

                case "CARD"://储值卡

                    $info = CardOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('储值卡充值订单不存在');
                    }
                    break;

                case "JKZX"://健康咨询

                    $info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('健康咨询订单不存在');
                    }
                    break;

                case "YXKL"://悦享康旅

                    $info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('悦享康旅订单不存在');
                    }
                    break;

                case "WYJT"://万悦讲堂

                    $info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('万悦讲堂订单不存在');
                    }
                    break;

                case "YLTC"://养老套餐

                    $info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    if(empty($info))
                    {
                        throw new \Exception('养老套餐订单不存在');
                    }
                    break;
            }

        if(empty($info))
        {
            throw new \Exception('订单不存在');
        }
        
        /*if($info['order_price'] <= 0)
        {
            throw new \Exception('订单不需要支付');
        }*/
        
        if($info['order_pay_status'] == 1)
        {
            throw new \Exception('订单已支付');
        }

        return $info;
    }
    
    public static function getOrderInfo2($order_dno)
    {
        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    $info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YXYL"://悦享医疗

                    $info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    break;

                case "JKCS"://健康超市

                    $info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "WYYY"://万悦影院

                    $info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "WYSY"://万悦书院

                    $info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "JKMS"://健康美食

                    $info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YHTC"://悦活套餐

                    $info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "LSSC"://绿色蔬菜

                    $info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YLFW"://悦享养老

                    $info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YXJK"://悦享健康

                    $info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    break;

                case "CARD"://储值卡

                    $info = CardOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "JKZX"://健康咨询

                    $info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YXKL"://悦享康旅

                    $info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "WYJT"://万悦讲堂

                    $info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    break;

                case "YLTC"://养老套餐

                    $info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    break;
            }

        return $info;
    }
    
    public static function updatePayStatus($order_dno,$pay_type = 1)
    {
        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    //$info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    PackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YXYL"://悦享医疗

                    //$info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    MedicalCareOrder::where('order_sn',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "JKCS"://健康超市

                    //$info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    GoodsOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1,'order_pay_type'=>$pay_type,'order_pay_time'=>time()]);
                    break;

                case "WYYY"://万悦影院

                    //$info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    LifeCinemaOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "WYSY"://万悦书院

                    //$info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    LifeBookOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "JKMS"://健康美食

                    //$info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    LifeHealthyOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YHTC"://悦活套餐

                    //$info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    LifePackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "LSSC"://绿色蔬菜

                    //$info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    LifeFarmOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YLFW"://悦享养老

                    //$info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    RaiseOldOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YXJK"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    HealthyOrder::where('order_sn',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "CARD"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    CardOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "JKZX"://健康咨询

                    //$info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    HealthyAdviceOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YXKL"://悦享康旅

                    //$info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    TravelOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "WYJT"://万悦讲堂

                    //$info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    CourseOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;

                case "YLTC"://养老套餐

                    //$info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    RaiseOldPackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>1]);
                    break;
            }

    }
    //取消订单
    public static function cancelOrder($order_dno)
    {

        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    //$info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = PackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YXYL"://悦享医疗

                    //$info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    $result = MedicalCareOrder::where('order_sn',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "JKCS"://健康超市

                    //$info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    $result = GoodsOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "WYYY"://万悦影院

                    //$info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeCinemaOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "WYSY"://万悦书院

                    //$info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeBookOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "JKMS"://健康美食

                    //$info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeHealthyOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YHTC"://悦活套餐

                    //$info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifePackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "LSSC"://绿色蔬菜

                    //$info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeFarmOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YLFW"://悦享养老

                    //$info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YXJK"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = HealthyOrder::where('order_sn',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "CARD"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = CardOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "JKZX"://健康咨询

                    //$info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    $result = HealthyAdviceOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YXKL"://悦享康旅

                    //$info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    $result = TravelOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "WYJT"://万悦讲堂

                    //$info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    $result = CourseOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;

                case "YLTC"://养老套餐

                    //$info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldPackageOrder::where('order_dno',$order_dno)->update(['order_pay_status'=>3]);
                    break;
            }

        return $result;

    }

    //申请退款
    public static function addRefund($order_dno)
    {
        $info = static::getOrderInfo2($order_dno);

        if(empty($info))
        {
            throw new \Exception('订单不存在');
        }
        
        if($info['order_pay_status'] == 0)
        {
            throw new \Exception('订单未支付');
        }

        if($info['order_status'] == 2)
        {
            throw new \Exception('订单已发货');
        }

        if($info['order_status'] == 3)
        {
            throw new \Exception('订单已收货');
        }

        if($info['order_status'] == 4)
        {
            throw new \Exception('订单已完成');
        }

        if($info['order_status'] == 5)
        {
            throw new \Exception('订单正在申请退款中');
        }

        if($info['order_status'] == 6)
        {
            throw new \Exception('订单已退款完成');
        }

        if($info['order_status'] == 8)
        {
            throw new \Exception('订单已使用');
        }

        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    //$info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = PackageOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YXYL"://悦享医疗

                    //$info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    $result = MedicalCareOrder::where('order_sn',$order_dno)->update(['order_status'=>5]);
                    break;

                case "JKCS"://健康超市

                    //$info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    $result = GoodsOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "WYYY"://万悦影院

                    //$info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeCinemaOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "WYSY"://万悦书院

                    //$info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeBookOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "JKMS"://健康美食

                    //$info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeHealthyOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YHTC"://悦活套餐

                    //$info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifePackageOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "LSSC"://绿色蔬菜

                    //$info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeFarmOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YLFW"://悦享养老

                    //$info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YXJK"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = HealthyOrder::where('order_sn',$order_dno)->update(['order_status'=>5]);
                    break;

                case "CARD"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = CardOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "JKZX"://健康咨询

                    //$info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    $result = HealthyAdviceOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YXKL"://悦享康旅

                    //$info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    $result = TravelOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "WYJT"://万悦讲堂

                    //$info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    $result = CourseOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;

                case "YLTC"://养老套餐

                    //$info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldPackageOrder::where('order_dno',$order_dno)->update(['order_status'=>5]);
                    break;
            }

        return $result;

    }

    //修改退款状态
    public static function updateRefund($order_dno)
    {
        $info = static::getOrderInfo2($order_dno);

        if(empty($info))
        {
            throw new \Exception('订单不存在');
        }    

        if($info['order_status'] == 6)
        {
            throw new \Exception('订单已退款完成');
        }

        $str = substr($order_dno,0,4);

        switch ($str)
            {
                case "HYQY"://会员权益

                    //$info = PackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = PackageOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YXYL"://悦享医疗

                    //$info = MedicalCareOrder::where(['order_sn' => $order_dno])->first();
                    $result = MedicalCareOrder::where('order_sn',$order_dno)->update(['order_status'=>6]);
                    break;

                case "JKCS"://健康超市

                    //$info = GoodsOrder::where(['order_dno' => $order_dno])->first();
                    $result = GoodsOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "WYYY"://万悦影院

                    //$info = LifeCinemaOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeCinemaOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "WYSY"://万悦书院

                    //$info = LifeBookOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeBookOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "JKMS"://健康美食

                    //$info = LifeHealthyOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeHealthyOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YHTC"://悦活套餐

                    //$info = LifePackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifePackageOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "LSSC"://绿色蔬菜

                    //$info = LifeFarmOrder::where(['order_dno' => $order_dno])->first();
                    $result = LifeFarmOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YLFW"://悦享养老

                    //$info = RaiseOldOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YXJK"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = HealthyOrder::where('order_sn',$order_dno)->update(['order_status'=>6]);
                    break;

                case "CARD"://悦享健康

                    //$info = HealthyOrder::where(['order_sn' => $order_dno])->first();
                    $result = CardOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "JKZX"://健康咨询

                    //$info = HealthyAdviceOrder::where(['order_dno' => $order_dno])->first();
                    $result = HealthyAdviceOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YXKL"://悦享康旅

                    //$info = TravelOrder::where(['order_dno' => $order_dno])->first();
                    $result = TravelOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "WYJT"://万悦讲堂

                    //$info = CourseOrder::where(['order_dno' => $order_dno])->first();
                    $result = CourseOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;

                case "YLTC"://养老套餐

                    //$info = RaiseOldPackageOrder::where(['order_dno' => $order_dno])->first();
                    $result = RaiseOldPackageOrder::where('order_dno',$order_dno)->update(['order_status'=>6]);
                    break;
            }

        return $result;

    }

    /**
     * Notes:订单支付-储值卡
     * @param int $order_id
     * @param int $pay_type
     * @param int $code
     * @return array
     * @throws \Exception
     * User: ycp
     * Date: 2023/4/25
     */
    public static function cardPay($user_id,$order_dno,$code,$member_id,$member_account_cardno)
    {
        try {

            $info = static::getOrderInfo($order_dno);

            //效验验证码
            if($code != '123456')
            {
                //$mobile = DB::connection('mysql2')->table('user')->where('user_id',$info['yxj_user_id'])->value('user_account');
                $user = User::getIdUserInfo($user_id);
                VerificationCodeServer::checkCode($user['user_account'], $code);
            }

            $result = CardServer::accountPay($member_id,$member_account_cardno,$info['order_dno'],$info['order_price']);
            $result = json_decode($result,true);
            
            if($result['success'] == false)
            {
                throw new \Exception($result['errorcode']);
            }

            static::updatePayStatus($order_dno,4);
            return $result;
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    
}
