<?php

namespace liuniu\repositories;

use app\common\model\Lave as LaveModel;
use app\common\model\LaveMonth as LaveMonthModel;
use app\common\model\UserRelation;
use liuniu\MiniProgramService;
use liuniu\WechatService;

class LaveRepository
{
    /**
     * 微信公众号JS支付
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws Exception
     */
    public static function wxpay($cid, $orderId, $field = "order_id")
    {
        if (is_string($orderId))
            $orderInfo = LaveModel::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $openid = UserRelation::userIdToOpenId($orderInfo['user_id']);
        $rs = WechatService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
        return WechatService::jspay($cid, $rs['prepay_id']);

    }

    /**
     * 小程序JS支付
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws Exception
     */
    public static function jsPay($cid, $orderId, $field = 'order_id')
    {
        if (is_string($orderId))
            $orderInfo = LaveModel::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $openid = UserRelation::userIdToOpenId($orderInfo['user_id'], 'routine_openid');
        $bodyContent = $orderInfo['intention'];
        $site_name = sys_config('site_name');
        if (!$bodyContent && !$site_name) exception('支付参数缺少：请前往后台设置->系统设置-> 填写 网站名称');
        return MiniProgramService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['money'], 'lave', $orderInfo['order_name'], '', '', [], $cid);

    }

    /**
     * 微信h5支付
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws Exception
     */
    public static function h5Pay($cid, $orderId, $field = 'order_id')
    {
        if (is_string($orderId))
            $orderInfo = Donate::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $bodyContent = $orderInfo['intention'];
        $site_name = sys_config('site_name');
        if (!$bodyContent && !$site_name) exception('支付参数缺少：请前往后台设置->系统设置-> 填写 网站名称');
        return WechatService::payment(false, $cid)->paymentOrder(null, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'MWEB', [], $cid);
    }
    /**
     * 微信公众号JS支付签约
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws Exception
     */
    public static function wxpaySign($cid, $orderId,$plan_record, $field = "order_id")
    {
        if (is_string($orderId))
            $orderInfo = LaveMonthModel::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $openid = UserRelation::userIdToOpenId($plan_record['uid']);
//                                          ($openid, $out_trade_no,         $total_fee,                  $attach,  $body,                   $contract_code,              $plan_id,              $spbill_create_ip                   ,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
        $rs = WechatService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], $plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'],'', 'JSAPI', [], $cid,$plan_record['contract_display_account']);
        return WechatService::jspay($cid, $rs['prepay_id']);

    }

    /**
     * 小程序JS支付签约
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws Exception
     */
    public static function jsPaySign($cid, $orderId,$plan_record, $field = 'order_id')
    {
        if (is_string($orderId))
            $orderInfo = LaveMonthModel::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $openid = UserRelation::userIdToOpenId($orderInfo['user_id'], 'routine_openid');
        $bodyContent = $orderInfo['intention'];
        $site_name = sys_config('site_name');
        if (!$bodyContent && !$site_name) exception('支付参数缺少：请前往后台设置->系统设置-> 填写 网站名称');
//                                              ($openid, $out_trade_no,         $total_fee,                     $attach, $body,                  $contract_code,             $plan_id,$spbill_create_ip, $detail = '', $trade_type = 'JSAPI', $options = [],        $cid=0,$contract_display_account='',            $contract_notify_url='')
        return MiniProgramService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], 'lave', $orderInfo['order_name'], $plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'],'', '', [], $cid,$plan_record['contract_display_account'],$plan_record['contract_notify_url']);

    }

    /**
     * 微信h5支付签约
     * @param $orderId
     * @param string $field
     * @return array|string
     * @throws Exception
     */
    public static function h5PaySign($cid, $orderId,$plan_record, $field = 'order_id')
    {
        if (is_string($orderId))
            $orderInfo = Donate::where($field, $orderId)->find();
        else
            $orderInfo = $orderId;
        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
        if ($orderInfo['paid']) exception('支付已支付!');
        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
        $bodyContent = $orderInfo['intention'];
        $site_name = sys_config('site_name');
        if (!$bodyContent && !$site_name) exception('支付参数缺少：请前往后台设置->系统设置-> 填写 网站名称');
        //                                                            ($openid, $out_trade_no,         $total_fee,           $attach,  $body,                   $contract_code,              $plan_id,              $spbill_create_ip             ,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
        return WechatService::payment(false, $cid)->paysignedOrder(null, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'],$plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'], '', 'MWEB', [], $cid.$plan_record['contract_display_account']);
    }
}