<?php

namespace app\common\model;

use think\Db;
use think\Model;
use addons\epay\library\Service;

/**
 * 问诊模型
 */
class Wenzhenorder extends Model
{

    // 表名
    protected $name = 'wenzhen_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    // 退款
    public function old_refund($order, $refund_price)
    {

        $table = 'wenzhen_order';
        $remark = '问诊退款';

        // 生成退款单
        $refund_data = [
            'order_id' => $order['id'],
            'out_refund_no'=> createUniqueNo('WR',$order['id']),
            'pay_fee'  => $order['total_fee'],
            'refund_price' => $refund_price,
            'pay_type' => $order['pay_type'],
            'status' => 0,
            'createtime' => time(),
            'updatetime' => time(),
            'table' => $table,
            'table_id' => $order['id'],
        ];
        $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
        if(!$refund_log_id){
            return '退款失败';
        }

        if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
            // 微信|支付宝退款

            // 退款数据
            $order_data = [
                'out_trade_no' => $order['pay_out_trade_no']
            ];

            if ($order['pay_type'] == 'wechat') {
                $total_fee = $order['total_fee'] * 100;
                $refund_fee = $refund_price * 100;

                $order_data = array_merge($order_data, [
                    'out_refund_no' => $refund_data['out_refund_no'],
                    'total_fee' => $total_fee,
                    'refund_fee' => $refund_fee,
                    'refund_desc' => $remark,
                ]);
            } else {
                $order_data = array_merge($order_data, [
                    'out_request_no' => $refund_data['out_refund_no'],
                    'refund_amount' => $refund_price,
                ]);
            }

            //
            if ($order['pay_type'] == 'wechat') {
                $wxpay = new \app\common\library\Wxpay;
                $result = $wxpay->WxPayRefund($order_data);
                // 微信通知回调 pay->notifyr
                if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
                    return true;
                } else {
                    return $result['return_msg'];
                }
            } else {

                $result = Service::submitRefund($order['total_fee'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');

                if($result['code'] == '10000'){
                    Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
                    return true;
                }else{
                    return $result['msg'];
                }



                /* return 'alipay wrong way';
                 $alipay = new \app\common\library\AliPay;
                 $result = $alipay->AliPayRefund($order_data);
                 // 支付宝通知回调 pay->notifyx
                 return $result;*/

                /*if ($result['code'] == "10000") {
                    return true;
                } else {
                    throw new \Exception($result['msg']);
                }*/
            }

            // {        // 微信返回结果
            //     "return_code":"SUCCESS",
            //     "return_msg":"OK",
            //     "appid":"wx39cd0799d4567dd0",
            //     "mch_id":"1481069012",
            //     "nonce_str":"huW9eIAb5BDPn0Ma",
            //     "sign":"250316740B263FE53F5DFF50AF5A8FA1",
            //     "result_code":"SUCCESS",
            //     "transaction_id":"4200000497202004072822298902",
            //     "out_trade_no":"202010300857029180027000",
            //     "out_refund_no":"1586241595",
            //     "refund_id":"50300603862020040700031444448",
            //     "refund_channel":[],
            //     "refund_fee":"1",
            //     "coupon_refund_fee":"0",
            //     "total_fee":"1",
            //     "cash_fee":"1",
            //     "coupon_refund_count":"0",
            //     "cash_refund_fee":"1
            // }

            // {        // 支付宝返回结果
            //     "code": "10000",
            //     "msg": "Success",
            //     "buyer_logon_id": "157***@163.com",
            //     "buyer_user_id": "2088902485164146",
            //     "fund_change": "Y",
            //     "gmt_refund_pay": "2020-08-15 16:11:45",
            //     "out_trade_no": "202002460317545607015300",
            //     "refund_fee": "0.01",
            //     "send_back_fee": "0.00",
            //     "trade_no": "2020081522001464141438570535"
            // }
        }

        return true;
    }


}
