<?php


namespace App\Services\pay;


use Api\PhpServices\Idgen\Idgen;
use App\Exception\custom\PayException;
use App\Models\order\mysql\PayOrder;
use App\Models\order\mysql\PayOrderClearing;

class PayService
{

    public function do_pay($order_id, $user_id) {

        // 判断是否存在有效订单
        $this->exist_valid($order_id, $user_id);

        // 获取订单信息 + 子单信息 + 分润信息
        $data = $this->get_order_data($order_id, $user_id);

        // 创建订单信息
        $pay_order = $this->make_pay_order($data);

        // 发送ping++ 创建支付订单
        $this->send_pingxx($pay_order);
    }

    /**
     * @param $order_id
     * @param $user_id
     * @throws PayException
     * 判断是否有效订单，如果存在则调用三方判断订单状态
     */
    private function exist_valid($order_id, $user_id) {
        $cur_date = date('Y-m-d H:i:s');
        $order = PayOrder::ge(["order_id" => $order_id, 'expire_time[>]'=>$cur_date]);
        if(!empty($order)) {
            if($order['user_id'] != $user_id) {
                throw new PayException(['cus' => 1]);
            }

            if(empty($order['third_order_id'])) {
                throw new PayException(['cus' => 2]);
            }

            $this->get_order_from_pingxx($order['third_order_id']);
        }
    }

    private function get_order_data($order_id, $user_id) {

    }



    public function makePayOrder($data) {

    }

    public function getInfo($orderId) {

        $order = PayOrder::getRecord(["order_id" => $orderId]);


        `expire_time` timestamp NULL DEFAULT NULL COMMENT '支付订单过期时间',
        pay_order_status




    }

    public function iPingCreate() {

    }

    /**
     * 创建店铺，同时更新生活号表的店铺数
     * @param $life_account_id
     * @param $merchant_id
     * @param string $name
     * @param bool $audit 是否需要审核
     * @return \Api\PhpUtils\Mysql\MysqlBase
     * @throws LifeAccountException
     */
    public function create($life_account_id, $merchant_id, $name='', $shop_status=Shop::SHOP_STATUS_ON){


        $life = LifeAccount::getMaster(['life_account_name'],['life_account_id' => $life_account_id]);
        if(empty($life)){
            throw new LifeAccountException(['cus'=>2]);
        }

        // 店铺id，通过idgen生成
        $shop_id = OrderService::get_idgen_id(substr($life_account_id, -2));
        $shop_id = $shop_id[0] ?? '';
        if(empty($shop_id)){
            throw new ShopException(['cus'=>1]);
        }

        // 店铺名，默认同生活号名
        if(empty($name)){
            $name = $life['life_account_name'];
        }

        $shop_res = Shop::insertRecord([
            'shop_id'           => $shop_id,
            'shop_name'         => $name,
            'merchant_id'       => $merchant_id,
            'life_account_id'   => $life_account_id,
            'shop_status'       => $shop_status
        ]);

        if($shop_res===false){
            throw new ShopException(['cus'=>0]);
        }

        // 更新生活号店铺数
        $shop_res2 = LifeAccount::update([
            "shop_totel_count[+]"   => 1,
            "shop_count[+]"         => 1,
        ],[
            "life_account_id"   => $life_account_id
        ]);

        if(!$shop_res2){
            throw new ShopException(['cus'=>2]);
        }

        return $shop_id;

    }

    /**
     * 更新商户id
     */
    public static function changeMid($o_mid, $n_mid){
        $res = Shop::update([
            'merchant_id'   => $n_mid
        ],[
            'merchant_id'   => $o_mid
        ]);
        if($res===false){
            throw new ShopException(['cus'=>3]);
        }
        return true;
    }
}