<?php

namespace app\common\controller;

use app\ros\model\Order as OrderModel;
use app\http\model\User as UserModel;
use app\http\model\Recharge as RechargeModel;
use app\jinyou\model\ProductOrder as ProductOrderModel;
use app\jinyou\model\Account as AccountModel;
use app\jinyou\model\Package as PackageModel;
use app\jinqiao\model\Order as JqOrderModel;
use app\jinqiao\model\Key as KeyModel;
use app\agent\model\AgentUser as AgentModel;
use app\agent\model\ProductPriceScheme;
use app\agent\model\AgentScore as AgentScoreModel;


class Wxpay {

     /**
     * @description: 获取支付宝是否支付成功
     * @param {*}
     * @return {*}
     */    
    public function wxpayVerify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new OrderModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['order_no'=>$order_no]);

        if (!empty($order_info['ali_wx_no'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['ali_wx_no'] = $wx_no;
            $update_data['status'] = 1;
            $order_model->updateOne(['order_no'=>$order_no],$update_data);


            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }

    public function httpwxpayVerify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new RechargeModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['order_no'=>$order_no]);

        if (!empty($order_info['ali_wx_no'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['ali_wx_no'] = $wx_no;
            $update_data['pay_status'] = 1;
            $order_model->updateOne(['order_no'=>$order_no],$update_data);
            $user_model = new UserModel();
            $user_info = $user_model->getOne(['Id' => $order_info['user_id']]);
            $user_update = [];
            $user_update['ju_money'] = $user_info['ju_money'] + $order_info['ju_money'];
            $user_model->updateOne(['Id'=>$order_info['user_id']],$user_update);


            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }

    
    public function jinyouwxpayverify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new ProductOrderModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['OrderNo'=>$order_no]);

        if (!empty($order_info['TradeNo'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['TradeNo'] = $wx_no;
            $update_data['OrderState'] = 90;
            $order_model->updateOne(['OrderNo'=>$order_no],$update_data);
            
            $account_info = json_decode($order_info['remark2'],true);
                    
            $user_model = new UserModel();
            $user_info = $user_model->getOne(['Id' => $order_info['UserId']]);
            
            $package_model = new PackageModel;//套餐详情
            $package_info = $package_model->getOne(['Id' => $account_info['package_id']]);

            $package_info['ProductName'] = $order_info['ProductName'];
            $account_model = new AccountModel;
            $account_model->open_account($account_info,$user_info,$package_info);

            //代理计算
            if($order_info['agent_id'] != 0 && $order_info['PaymentAmount'] != 0.1){

                $agent_model = new AgentModel();

                $agent_info = $agent_model->getOne(['id' => $order_info['agent_id']]);

                //根据折扣设置价格
                $score_money = 0.00;

                if($agent_info['discount_id'] != 0){

                    $scheme_model = new ProductPriceScheme();
                   //获取代理折扣
                    $discount  = $scheme_model->getOne(['Id' => $agent_info['discount_id']]);

                    $price = $package_info['LinePrice'] * $discount['discount'] / 100;
                    if ($price < $package_info['MinPrice']) {
                        $price = $package_info['MinPrice'];
                    }
                    $score_money = $order_info['PaymentAmount'] - $order_info['ConnectCount'] * $order_info['AccountCount'] * $price;
                }

                $score_model = new AgentScoreModel();
                
                $score_data = [
                    'agent_id' => $order_info['agent_id'],
                    'order_id' => $order_info['Id'],
                    'score_type' => 1,
                    'score_value' => $score_money,
                    'remark' => "客户购买产品".$order_info['Accounts'],
                    'agent_name' => $agent_info['username'],
                    'op_user' => $order_info['UserName'],
                    'rest_amount1' => $agent_info['account'],
                    'rest_amount2' => $agent_info['account'] + $score_money
                ];

                $agent_data = ['account' => ($agent_info['account'] + $score_money)];

                $score_model->add($score_data);

                $agent_model->updateOne(['id' => $order_info['agent_id']],$agent_data);

            }




            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }

    public function jinqiaowxpayverify() {
        

        //接收数据
        $xmlData = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        $order_model = new JqOrderModel();
        $order_no = $data['out_trade_no'];
        $wx_no = $data['transaction_id'];

        //获取充值信息
        $order_info = $order_model->getOne(['order_no'=>$order_no]);

        if (!empty($order_info['TradeNo'])) {
            die;
        }

        //判断算出的签名和通知信息的签名是否一致
        if($data['result_code'] == 'SUCCESS' && $data['appid'] == 'wx18e5b4f42773c3ec' && $data['mch_id'] == '1571608411'){
            
            $update_data = [];
            
            //更新充值订单
            $update_data['ali_wx_no'] = $wx_no;
            $update_data['status'] = 1;
            $order_model->updateOne(['order_no'=>$order_no],$update_data);
            
            $key_model = new KeyModel();
            $key_model->where(['package_id' => $order_info['package_id'],'isused' =>0])->limit(1)->update(['isused' => 1,'user_id' => $order_info['user_id']]);
            

            //处理完成之后，告诉微信成功结果
            echo '<xml>
                    <return_code><![CDATA[SUCCESS]]></return_code>
                    <return_msg><![CDATA[OK]]></return_msg>
                </xml>';
            exit();
        }
    }
}