<?php
declare (strict_types=1);
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | 支付结算
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:23
// +----------------------------------------------------------------------


namespace app\lib;

use app\model\api\PayTrade;
use app\model\api\Order;
use app\model\api\ShowTemplateOrder;
use app\model\api\UserShowTemplate;
use app\model\api\OrderInfo;
use app\model\api\User as UserModel;
use app\model\api\UserDetail as UserDetailModel;
use app\model\api\Sys as SysModel;

use think\facade\Db;

class OrderLib
{

    /**
     * 支付反馈
     * @param $orderId
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderPay($payOn,$payDataInfo="")
    {
        $payTradeData = (new PayTrade)->where('pay_no', $payOn)->find();
        if (empty($payTradeData)) {
            return false;
        }
        //已支付
        if ($payTradeData['status'] == 1) {
            return false;
        }
        $userData = (new UserModel)->where('uid', $payTradeData['uid'])->find();
        if(empty($userData)){
            return false;
        }
        //处理支付成功
        PayTrade::beginTrans();
        try {
            (new PayTrade)->where('id', $payTradeData['id'])->update(['status' => 1, 'pay_time' => time(),'pay_json' => $payDataInfo]);
            //购买模板
            if($payTradeData["type"]=="temp"){
                $tempOrderData = (new ShowTemplateOrder)->where("id",$payTradeData['o_id'])->find();
                if(empty($tempOrderData)){
                    PayTrade::rollbackTrans();
                    return false;
                }
                (new ShowTemplateOrder)->where("id",$payTradeData['o_id'])->update(['status' => 1, 'pay_time' => time()]);
                //验证并绑定当前模板
                (new UserShowTemplate)->userBindTemp($tempOrderData["show_template_id"],$tempOrderData['uid']);
                //添加消费记录
                if(in_array($payTradeData["pay_type"],["alipay","wxpay"])){
//                    (new UserDetailModel)->consumeLog($payTradeData['uid'],$payTradeData["money"],"show_temp_buy","购买名片模板");
                }
                PayTrade::commitTrans();
                return true;
            }
            //商城订单
            if($payTradeData["type"]=="shop"){
                (new Order)->where("id",$payTradeData['o_id'])->update(['status' => 1, 'pay_time' => time()]);
                PayTrade::commitTrans();
                return true;
            }
            PayTrade::commitTrans();
        } catch (DbException $dbException) {
            PayTrade::rollbackTrans();
            return false;
        }
        //后台管理人员支付直接返回
        if($payTradeData["pay_type"]=="system"){
            return true;
        }

        return true;
    }
}
